blob: 01e7b7553ff418dc6edd4db686232e9dc6db2520 [file] [log] [blame]
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07001/*
2 * Hotspot 2.0 OSU client
Dmitry Shmidt6cb1f652014-03-21 10:54:03 -07003 * Copyright (c) 2012-2014, Qualcomm Atheros, Inc.
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07004 *
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
7 */
8
9#include "includes.h"
10#include <time.h>
11#include <sys/stat.h>
Dmitry Shmidt7f656022015-02-25 14:36:37 -080012#ifdef ANDROID
13#include "private/android_filesystem_config.h"
14#endif /* ANDROID */
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -070015
16#include "common.h"
17#include "utils/browser.h"
18#include "utils/base64.h"
19#include "utils/xml-utils.h"
20#include "utils/http-utils.h"
21#include "common/wpa_ctrl.h"
22#include "common/wpa_helpers.h"
23#include "eap_common/eap_defs.h"
24#include "crypto/crypto.h"
25#include "crypto/sha256.h"
26#include "osu_client.h"
27
Dmitry Shmidtaf9da312015-04-03 10:03:11 -070028const char *spp_xsd_fname = "spp.xsd";
29
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -070030
31void write_result(struct hs20_osu_client *ctx, const char *fmt, ...)
32{
33 va_list ap;
34 FILE *f;
35 char buf[500];
36
37 va_start(ap, fmt);
38 vsnprintf(buf, sizeof(buf), fmt, ap);
39 va_end(ap);
40 write_summary(ctx, "%s", buf);
41
42 if (!ctx->result_file)
43 return;
44
45 f = fopen(ctx->result_file, "w");
46 if (f == NULL)
47 return;
48
49 va_start(ap, fmt);
50 vfprintf(f, fmt, ap);
51 va_end(ap);
52 fprintf(f, "\n");
53 fclose(f);
54}
55
56
57void write_summary(struct hs20_osu_client *ctx, const char *fmt, ...)
58{
59 va_list ap;
60 FILE *f;
61
62 if (!ctx->summary_file)
63 return;
64
65 f = fopen(ctx->summary_file, "a");
66 if (f == NULL)
67 return;
68
69 va_start(ap, fmt);
70 vfprintf(f, fmt, ap);
71 va_end(ap);
72 fprintf(f, "\n");
73 fclose(f);
74}
75
76
77void debug_dump_node(struct hs20_osu_client *ctx, const char *title,
78 xml_node_t *node)
79{
80 char *str = xml_node_to_str(ctx->xml, node);
81 wpa_printf(MSG_DEBUG, "[hs20] %s: '%s'", title, str);
82 free(str);
83}
84
85
86static int valid_fqdn(const char *fqdn)
87{
88 const char *pos;
89
90 /* TODO: could make this more complete.. */
91 if (strchr(fqdn, '.') == 0 || strlen(fqdn) > 255)
92 return 0;
93 for (pos = fqdn; *pos; pos++) {
94 if (*pos >= 'a' && *pos <= 'z')
95 continue;
96 if (*pos >= 'A' && *pos <= 'Z')
97 continue;
98 if (*pos >= '0' && *pos <= '9')
99 continue;
100 if (*pos == '-' || *pos == '.' || *pos == '_')
101 continue;
102 return 0;
103 }
104 return 1;
105}
106
107
Roshan Pius3a1667e2018-07-03 15:17:14 -0700108static int android_update_permission(const char *path, mode_t mode)
109{
110#ifdef ANDROID
111 /* we need to change file/folder permission for Android */
112
113 if (!path) {
114 wpa_printf(MSG_ERROR, "file path null");
115 return -1;
116 }
117
118 /* Allow processes running with Group ID as AID_WIFI,
119 * to read files from SP, SP/<fqdn>, Cert and osu-info directories */
Hai Shalom74f70d42019-02-11 14:42:39 -0800120 if (lchown(path, -1, AID_WIFI)) {
121 wpa_printf(MSG_INFO, "CTRL: Could not lchown directory: %s",
Roshan Pius3a1667e2018-07-03 15:17:14 -0700122 strerror(errno));
123 return -1;
124 }
125
126 if (chmod(path, mode) < 0) {
127 wpa_printf(MSG_INFO, "CTRL: Could not chmod directory: %s",
128 strerror(errno));
129 return -1;
130 }
131#endif /* ANDROID */
132
133 return 0;
134}
135
136
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -0700137int osu_get_certificate(struct hs20_osu_client *ctx, xml_node_t *getcert)
138{
139 xml_node_t *node;
140 char *url, *user = NULL, *pw = NULL;
141 char *proto;
142 int ret = -1;
143
144 proto = xml_node_get_attr_value(ctx->xml, getcert,
145 "enrollmentProtocol");
146 if (!proto)
147 return -1;
148 wpa_printf(MSG_INFO, "getCertificate - enrollmentProtocol=%s", proto);
149 write_summary(ctx, "getCertificate - enrollmentProtocol=%s", proto);
150 if (os_strcasecmp(proto, "EST") != 0) {
151 wpa_printf(MSG_INFO, "Unsupported enrollmentProtocol");
152 xml_node_get_attr_value_free(ctx->xml, proto);
153 return -1;
154 }
155 xml_node_get_attr_value_free(ctx->xml, proto);
156
157 node = get_node(ctx->xml, getcert, "enrollmentServerURI");
158 if (node == NULL) {
159 wpa_printf(MSG_INFO, "Could not find enrollmentServerURI node");
160 xml_node_get_attr_value_free(ctx->xml, proto);
161 return -1;
162 }
163 url = xml_node_get_text(ctx->xml, node);
164 if (url == NULL) {
165 wpa_printf(MSG_INFO, "Could not get URL text");
166 return -1;
167 }
168 wpa_printf(MSG_INFO, "enrollmentServerURI: %s", url);
169 write_summary(ctx, "enrollmentServerURI: %s", url);
170
171 node = get_node(ctx->xml, getcert, "estUserID");
172 if (node == NULL && !ctx->client_cert_present) {
173 wpa_printf(MSG_INFO, "Could not find estUserID node");
174 goto fail;
175 }
176 if (node) {
177 user = xml_node_get_text(ctx->xml, node);
178 if (user == NULL) {
179 wpa_printf(MSG_INFO, "Could not get estUserID text");
180 goto fail;
181 }
182 wpa_printf(MSG_INFO, "estUserID: %s", user);
183 write_summary(ctx, "estUserID: %s", user);
184 }
185
186 node = get_node(ctx->xml, getcert, "estPassword");
187 if (node == NULL && !ctx->client_cert_present) {
188 wpa_printf(MSG_INFO, "Could not find estPassword node");
189 goto fail;
190 }
191 if (node) {
192 pw = xml_node_get_base64_text(ctx->xml, node, NULL);
193 if (pw == NULL) {
194 wpa_printf(MSG_INFO, "Could not get estPassword text");
195 goto fail;
196 }
197 wpa_printf(MSG_INFO, "estPassword: %s", pw);
198 }
199
200 mkdir("Cert", S_IRWXU);
Roshan Pius3a1667e2018-07-03 15:17:14 -0700201 android_update_permission("Cert", S_IRWXU | S_IRWXG);
202
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -0700203 if (est_load_cacerts(ctx, url) < 0 ||
204 est_build_csr(ctx, url) < 0 ||
205 est_simple_enroll(ctx, url, user, pw) < 0)
206 goto fail;
207
208 ret = 0;
209fail:
210 xml_node_get_text_free(ctx->xml, url);
211 xml_node_get_text_free(ctx->xml, user);
212 xml_node_get_text_free(ctx->xml, pw);
213
214 return ret;
215}
216
217
218static int process_est_cert(struct hs20_osu_client *ctx, xml_node_t *cert,
219 const char *fqdn)
220{
221 u8 digest1[SHA256_MAC_LEN], digest2[SHA256_MAC_LEN];
222 char *der, *pem;
223 size_t der_len, pem_len;
224 char *fingerprint;
225 char buf[200];
226
227 wpa_printf(MSG_INFO, "PPS for certificate credential - fqdn=%s", fqdn);
228
229 fingerprint = xml_node_get_text(ctx->xml, cert);
230 if (fingerprint == NULL)
231 return -1;
232 if (hexstr2bin(fingerprint, digest1, SHA256_MAC_LEN) < 0) {
233 wpa_printf(MSG_INFO, "Invalid SHA256 hash value");
234 write_result(ctx, "Invalid client certificate SHA256 hash value in PPS");
235 xml_node_get_text_free(ctx->xml, fingerprint);
236 return -1;
237 }
238 xml_node_get_text_free(ctx->xml, fingerprint);
239
240 der = os_readfile("Cert/est_cert.der", &der_len);
241 if (der == NULL) {
242 wpa_printf(MSG_INFO, "Could not find client certificate from EST");
243 write_result(ctx, "Could not find client certificate from EST");
244 return -1;
245 }
246
247 if (sha256_vector(1, (const u8 **) &der, &der_len, digest2) < 0) {
248 os_free(der);
249 return -1;
250 }
251 os_free(der);
252
253 if (os_memcmp(digest1, digest2, sizeof(digest1)) != 0) {
254 wpa_printf(MSG_INFO, "Client certificate from EST does not match fingerprint from PPS MO");
255 write_result(ctx, "Client certificate from EST does not match fingerprint from PPS MO");
256 return -1;
257 }
258
259 wpa_printf(MSG_INFO, "Client certificate from EST matches PPS MO");
260 unlink("Cert/est_cert.der");
261
262 os_snprintf(buf, sizeof(buf), "SP/%s/client-ca.pem", fqdn);
263 if (rename("Cert/est-cacerts.pem", buf) < 0) {
264 wpa_printf(MSG_INFO, "Could not move est-cacerts.pem to client-ca.pem: %s",
265 strerror(errno));
266 return -1;
267 }
268 pem = os_readfile(buf, &pem_len);
269
270 os_snprintf(buf, sizeof(buf), "SP/%s/client-cert.pem", fqdn);
271 if (rename("Cert/est_cert.pem", buf) < 0) {
272 wpa_printf(MSG_INFO, "Could not move est_cert.pem to client-cert.pem: %s",
273 strerror(errno));
274 os_free(pem);
275 return -1;
276 }
277
278 if (pem) {
279 FILE *f = fopen(buf, "a");
280 if (f) {
281 fwrite(pem, pem_len, 1, f);
282 fclose(f);
283 }
284 os_free(pem);
285 }
286
287 os_snprintf(buf, sizeof(buf), "SP/%s/client-key.pem", fqdn);
288 if (rename("Cert/privkey-plain.pem", buf) < 0) {
289 wpa_printf(MSG_INFO, "Could not move privkey-plain.pem to client-key.pem: %s",
290 strerror(errno));
291 return -1;
292 }
293
294 unlink("Cert/est-req.b64");
295 unlink("Cert/est-req.pem");
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -0700296 rmdir("Cert");
297
298 return 0;
299}
300
301
302#define TMP_CERT_DL_FILE "tmp-cert-download"
303
304static int download_cert(struct hs20_osu_client *ctx, xml_node_t *params,
305 const char *fname)
306{
307 xml_node_t *url_node, *hash_node;
308 char *url, *hash;
309 char *cert;
310 size_t len;
311 u8 digest1[SHA256_MAC_LEN], digest2[SHA256_MAC_LEN];
312 int res;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800313 char *b64;
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -0700314 FILE *f;
315
316 url_node = get_node(ctx->xml, params, "CertURL");
317 hash_node = get_node(ctx->xml, params, "CertSHA256Fingerprint");
318 if (url_node == NULL || hash_node == NULL)
319 return -1;
320 url = xml_node_get_text(ctx->xml, url_node);
321 hash = xml_node_get_text(ctx->xml, hash_node);
322 if (url == NULL || hash == NULL) {
323 xml_node_get_text_free(ctx->xml, url);
324 xml_node_get_text_free(ctx->xml, hash);
325 return -1;
326 }
327
328 wpa_printf(MSG_INFO, "CertURL: %s", url);
329 wpa_printf(MSG_INFO, "SHA256 hash: %s", hash);
330
331 if (hexstr2bin(hash, digest1, SHA256_MAC_LEN) < 0) {
332 wpa_printf(MSG_INFO, "Invalid SHA256 hash value");
333 write_result(ctx, "Invalid SHA256 hash value for downloaded certificate");
334 xml_node_get_text_free(ctx->xml, hash);
335 return -1;
336 }
337 xml_node_get_text_free(ctx->xml, hash);
338
339 write_summary(ctx, "Download certificate from %s", url);
Dmitry Shmidt6cb1f652014-03-21 10:54:03 -0700340 ctx->no_osu_cert_validation = 1;
341 http_ocsp_set(ctx->http, 1);
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -0700342 res = http_download_file(ctx->http, url, TMP_CERT_DL_FILE, NULL);
Dmitry Shmidt6cb1f652014-03-21 10:54:03 -0700343 http_ocsp_set(ctx->http,
344 (ctx->workarounds & WORKAROUND_OCSP_OPTIONAL) ? 1 : 2);
345 ctx->no_osu_cert_validation = 0;
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -0700346 xml_node_get_text_free(ctx->xml, url);
347 if (res < 0)
348 return -1;
349
350 cert = os_readfile(TMP_CERT_DL_FILE, &len);
351 remove(TMP_CERT_DL_FILE);
352 if (cert == NULL)
353 return -1;
354
355 if (sha256_vector(1, (const u8 **) &cert, &len, digest2) < 0) {
356 os_free(cert);
357 return -1;
358 }
359
360 if (os_memcmp(digest1, digest2, sizeof(digest1)) != 0) {
361 wpa_printf(MSG_INFO, "Downloaded certificate fingerprint did not match");
362 write_result(ctx, "Downloaded certificate fingerprint did not match");
363 os_free(cert);
364 return -1;
365 }
366
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800367 b64 = base64_encode(cert, len, NULL);
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -0700368 os_free(cert);
369 if (b64 == NULL)
370 return -1;
371
372 f = fopen(fname, "wb");
373 if (f == NULL) {
374 os_free(b64);
375 return -1;
376 }
377
378 fprintf(f, "-----BEGIN CERTIFICATE-----\n"
379 "%s"
380 "-----END CERTIFICATE-----\n",
381 b64);
382
383 os_free(b64);
384 fclose(f);
385
386 wpa_printf(MSG_INFO, "Downloaded certificate into %s and validated fingerprint",
387 fname);
388 write_summary(ctx, "Downloaded certificate into %s and validated fingerprint",
389 fname);
390
391 return 0;
392}
393
394
395static int cmd_dl_osu_ca(struct hs20_osu_client *ctx, const char *pps_fname,
396 const char *ca_fname)
397{
398 xml_node_t *pps, *node;
399 int ret;
400
401 pps = node_from_file(ctx->xml, pps_fname);
402 if (pps == NULL) {
403 wpa_printf(MSG_INFO, "Could not read or parse '%s'", pps_fname);
404 return -1;
405 }
406
407 node = get_child_node(ctx->xml, pps,
408 "SubscriptionUpdate/TrustRoot");
409 if (node == NULL) {
410 wpa_printf(MSG_INFO, "No SubscriptionUpdate/TrustRoot/CertURL found from PPS");
411 xml_node_free(ctx->xml, pps);
412 return -1;
413 }
414
415 ret = download_cert(ctx, node, ca_fname);
416 xml_node_free(ctx->xml, pps);
417
418 return ret;
419}
420
421
422static int cmd_dl_polupd_ca(struct hs20_osu_client *ctx, const char *pps_fname,
423 const char *ca_fname)
424{
425 xml_node_t *pps, *node;
426 int ret;
427
428 pps = node_from_file(ctx->xml, pps_fname);
429 if (pps == NULL) {
430 wpa_printf(MSG_INFO, "Could not read or parse '%s'", pps_fname);
431 return -1;
432 }
433
434 node = get_child_node(ctx->xml, pps,
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800435 "Policy/PolicyUpdate/TrustRoot");
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -0700436 if (node == NULL) {
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800437 wpa_printf(MSG_INFO, "No Policy/PolicyUpdate/TrustRoot/CertURL found from PPS");
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -0700438 xml_node_free(ctx->xml, pps);
Hai Shalom39ba6fc2019-01-22 12:40:38 -0800439 return -2;
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -0700440 }
441
442 ret = download_cert(ctx, node, ca_fname);
443 xml_node_free(ctx->xml, pps);
444
445 return ret;
446}
447
448
449static int cmd_dl_aaa_ca(struct hs20_osu_client *ctx, const char *pps_fname,
450 const char *ca_fname)
451{
452 xml_node_t *pps, *node, *aaa;
453 int ret;
454
455 pps = node_from_file(ctx->xml, pps_fname);
456 if (pps == NULL) {
457 wpa_printf(MSG_INFO, "Could not read or parse '%s'", pps_fname);
458 return -1;
459 }
460
461 node = get_child_node(ctx->xml, pps,
462 "AAAServerTrustRoot");
463 if (node == NULL) {
464 wpa_printf(MSG_INFO, "No AAAServerTrustRoot/CertURL found from PPS");
465 xml_node_free(ctx->xml, pps);
Hai Shalom39ba6fc2019-01-22 12:40:38 -0800466 return -2;
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -0700467 }
468
469 aaa = xml_node_first_child(ctx->xml, node);
470 if (aaa == NULL) {
471 wpa_printf(MSG_INFO, "No AAAServerTrustRoot/CertURL found from PPS");
472 xml_node_free(ctx->xml, pps);
473 return -1;
474 }
475
476 ret = download_cert(ctx, aaa, ca_fname);
477 xml_node_free(ctx->xml, pps);
478
479 return ret;
480}
481
482
483static int download_trust_roots(struct hs20_osu_client *ctx,
484 const char *pps_fname)
485{
486 char *dir, *pos;
487 char fname[300];
Hai Shalom39ba6fc2019-01-22 12:40:38 -0800488 int ret, ret1;
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -0700489
490 dir = os_strdup(pps_fname);
491 if (dir == NULL)
492 return -1;
493 pos = os_strrchr(dir, '/');
494 if (pos == NULL) {
495 os_free(dir);
496 return -1;
497 }
498 *pos = '\0';
499
500 snprintf(fname, sizeof(fname), "%s/ca.pem", dir);
501 ret = cmd_dl_osu_ca(ctx, pps_fname, fname);
502 snprintf(fname, sizeof(fname), "%s/polupd-ca.pem", dir);
Hai Shalom39ba6fc2019-01-22 12:40:38 -0800503 ret1 = cmd_dl_polupd_ca(ctx, pps_fname, fname);
504 if (ret == 0 && ret1 == -1)
505 ret = -1;
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -0700506 snprintf(fname, sizeof(fname), "%s/aaa-ca.pem", dir);
Hai Shalom39ba6fc2019-01-22 12:40:38 -0800507 ret1 = cmd_dl_aaa_ca(ctx, pps_fname, fname);
508 if (ret == 0 && ret1 == -1)
509 ret = -1;
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -0700510
511 os_free(dir);
512
513 return ret;
514}
515
516
517static int server_dnsname_suffix_match(struct hs20_osu_client *ctx,
518 const char *fqdn)
519{
520 size_t match_len, len, i;
521 const char *val;
522
523 match_len = os_strlen(fqdn);
524
525 for (i = 0; i < ctx->server_dnsname_count; i++) {
526 wpa_printf(MSG_INFO,
527 "Checking suffix match against server dNSName %s",
528 ctx->server_dnsname[i]);
529 val = ctx->server_dnsname[i];
530 len = os_strlen(val);
531
532 if (match_len > len)
533 continue;
534
535 if (os_strncasecmp(val + len - match_len, fqdn, match_len) != 0)
536 continue; /* no match */
537
538 if (match_len == len)
539 return 1; /* exact match */
540
541 if (val[len - match_len - 1] == '.')
542 return 1; /* full label match completes suffix match */
543
544 /* Reject due to incomplete label match */
545 }
546
547 /* None of the dNSName(s) matched */
548 return 0;
549}
550
551
552int hs20_add_pps_mo(struct hs20_osu_client *ctx, const char *uri,
553 xml_node_t *add_mo, char *fname, size_t fname_len)
554{
555 char *str;
556 char *fqdn, *pos;
557 xml_node_t *tnds, *mo, *cert;
558 const char *name;
559 int ret;
560
561 if (strncmp(uri, "./Wi-Fi/", 8) != 0) {
562 wpa_printf(MSG_INFO, "Unsupported location for addMO to add PPS MO: '%s'",
563 uri);
564 write_result(ctx, "Unsupported location for addMO to add PPS MO: '%s'",
565 uri);
566 return -1;
567 }
568
569 fqdn = strdup(uri + 8);
570 if (fqdn == NULL)
571 return -1;
572 pos = strchr(fqdn, '/');
573 if (pos) {
574 if (os_strcasecmp(pos, "/PerProviderSubscription") != 0) {
575 wpa_printf(MSG_INFO, "Unsupported location for addMO to add PPS MO (extra directory): '%s'",
576 uri);
577 write_result(ctx, "Unsupported location for addMO to "
578 "add PPS MO (extra directory): '%s'", uri);
Dmitry Shmidt41712582015-06-29 11:02:15 -0700579 free(fqdn);
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -0700580 return -1;
581 }
582 *pos = '\0'; /* remove trailing slash and PPS node name */
583 }
584 wpa_printf(MSG_INFO, "SP FQDN: %s", fqdn);
585
586 if (!server_dnsname_suffix_match(ctx, fqdn)) {
Dmitry Shmidtaf9da312015-04-03 10:03:11 -0700587 wpa_printf(MSG_INFO,
588 "FQDN '%s' for new PPS MO did not have suffix match with server's dNSName values, count: %d",
589 fqdn, (int) ctx->server_dnsname_count);
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -0700590 write_result(ctx, "FQDN '%s' for new PPS MO did not have suffix match with server's dNSName values",
591 fqdn);
592 free(fqdn);
593 return -1;
594 }
595
596 if (!valid_fqdn(fqdn)) {
597 wpa_printf(MSG_INFO, "Invalid FQDN '%s'", fqdn);
598 write_result(ctx, "Invalid FQDN '%s'", fqdn);
599 free(fqdn);
600 return -1;
601 }
602
603 mkdir("SP", S_IRWXU);
604 snprintf(fname, fname_len, "SP/%s", fqdn);
605 if (mkdir(fname, S_IRWXU) < 0) {
606 if (errno != EEXIST) {
607 int err = errno;
608 wpa_printf(MSG_INFO, "mkdir(%s) failed: %s",
609 fname, strerror(err));
610 free(fqdn);
611 return -1;
612 }
613 }
614
Hai Shalom021b0b52019-04-10 11:17:58 -0700615 android_update_permission("SP", S_IRWXU | S_IRWXG);
616 android_update_permission(fname, S_IRWXU | S_IRWXG);
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800617
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -0700618 snprintf(fname, fname_len, "SP/%s/pps.xml", fqdn);
619
620 if (os_file_exists(fname)) {
621 wpa_printf(MSG_INFO, "PPS file '%s' exists - reject addMO",
622 fname);
623 write_result(ctx, "PPS file '%s' exists - reject addMO",
624 fname);
625 free(fqdn);
626 return -2;
627 }
628 wpa_printf(MSG_INFO, "Using PPS file: %s", fname);
629
630 str = xml_node_get_text(ctx->xml, add_mo);
631 if (str == NULL) {
632 wpa_printf(MSG_INFO, "Could not extract MO text");
633 free(fqdn);
634 return -1;
635 }
636 wpa_printf(MSG_DEBUG, "[hs20] addMO text: '%s'", str);
637
638 tnds = xml_node_from_buf(ctx->xml, str);
639 xml_node_get_text_free(ctx->xml, str);
640 if (tnds == NULL) {
641 wpa_printf(MSG_INFO, "[hs20] Could not parse addMO text");
642 free(fqdn);
643 return -1;
644 }
645
646 mo = tnds_to_mo(ctx->xml, tnds);
647 if (mo == NULL) {
648 wpa_printf(MSG_INFO, "[hs20] Could not parse addMO TNDS text");
649 free(fqdn);
650 return -1;
651 }
652
653 debug_dump_node(ctx, "Parsed TNDS", mo);
654
655 name = xml_node_get_localname(ctx->xml, mo);
656 if (os_strcasecmp(name, "PerProviderSubscription") != 0) {
657 wpa_printf(MSG_INFO, "[hs20] Unexpected PPS MO root node name '%s'",
658 name);
659 free(fqdn);
660 return -1;
661 }
662
663 cert = get_child_node(ctx->xml, mo,
664 "Credential/DigitalCertificate/"
665 "CertSHA256Fingerprint");
666 if (cert && process_est_cert(ctx, cert, fqdn) < 0) {
667 xml_node_free(ctx->xml, mo);
668 free(fqdn);
669 return -1;
670 }
671 free(fqdn);
672
673 if (node_to_file(ctx->xml, fname, mo) < 0) {
674 wpa_printf(MSG_INFO, "Could not write MO to file");
675 xml_node_free(ctx->xml, mo);
676 return -1;
677 }
678 xml_node_free(ctx->xml, mo);
679
680 wpa_printf(MSG_INFO, "A new PPS MO added as '%s'", fname);
681 write_summary(ctx, "A new PPS MO added as '%s'", fname);
682
683 ret = download_trust_roots(ctx, fname);
684 if (ret < 0) {
685 wpa_printf(MSG_INFO, "Remove invalid PPS MO file");
686 write_summary(ctx, "Remove invalid PPS MO file");
687 unlink(fname);
688 }
689
690 return ret;
691}
692
693
694int update_pps_file(struct hs20_osu_client *ctx, const char *pps_fname,
695 xml_node_t *pps)
696{
697 char *str;
698 FILE *f;
699 char backup[300];
700
701 if (ctx->client_cert_present) {
702 xml_node_t *cert;
703 cert = get_child_node(ctx->xml, pps,
704 "Credential/DigitalCertificate/"
705 "CertSHA256Fingerprint");
706 if (cert && os_file_exists("Cert/est_cert.der") &&
707 process_est_cert(ctx, cert, ctx->fqdn) < 0) {
708 wpa_printf(MSG_INFO, "EST certificate update processing failed on PPS MO update");
709 return -1;
710 }
711 }
712
713 wpa_printf(MSG_INFO, "Updating PPS MO %s", pps_fname);
714
715 str = xml_node_to_str(ctx->xml, pps);
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800716 if (str == NULL) {
717 wpa_printf(MSG_ERROR, "No node found");
718 return -1;
719 }
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -0700720 wpa_printf(MSG_MSGDUMP, "[hs20] Updated PPS: '%s'", str);
721
722 snprintf(backup, sizeof(backup), "%s.bak", pps_fname);
723 rename(pps_fname, backup);
724 f = fopen(pps_fname, "w");
725 if (f == NULL) {
726 wpa_printf(MSG_INFO, "Could not write PPS");
727 rename(backup, pps_fname);
728 free(str);
729 return -1;
730 }
731 fprintf(f, "%s\n", str);
732 fclose(f);
733
734 free(str);
735
736 return 0;
737}
738
739
740void get_user_pw(struct hs20_osu_client *ctx, xml_node_t *pps,
741 const char *alt_loc, char **user, char **pw)
742{
743 xml_node_t *node;
744
745 node = get_child_node(ctx->xml, pps,
746 "Credential/UsernamePassword/Username");
747 if (node)
748 *user = xml_node_get_text(ctx->xml, node);
749
750 node = get_child_node(ctx->xml, pps,
751 "Credential/UsernamePassword/Password");
752 if (node)
753 *pw = xml_node_get_base64_text(ctx->xml, node, NULL);
754
755 node = get_child_node(ctx->xml, pps, alt_loc);
756 if (node) {
757 xml_node_t *a;
758 a = get_node(ctx->xml, node, "Username");
759 if (a) {
760 xml_node_get_text_free(ctx->xml, *user);
761 *user = xml_node_get_text(ctx->xml, a);
762 wpa_printf(MSG_INFO, "Use OSU username '%s'", *user);
763 }
764
765 a = get_node(ctx->xml, node, "Password");
766 if (a) {
767 free(*pw);
768 *pw = xml_node_get_base64_text(ctx->xml, a, NULL);
769 wpa_printf(MSG_INFO, "Use OSU password");
770 }
771 }
772}
773
774
775/* Remove old credentials based on HomeSP/FQDN */
776static void remove_sp_creds(struct hs20_osu_client *ctx, const char *fqdn)
777{
778 char cmd[300];
779 os_snprintf(cmd, sizeof(cmd), "REMOVE_CRED provisioning_sp=%s", fqdn);
780 if (wpa_command(ctx->ifname, cmd) < 0)
781 wpa_printf(MSG_INFO, "Failed to remove old credential(s)");
782}
783
784
785static void set_pps_cred_policy_spe(struct hs20_osu_client *ctx, int id,
786 xml_node_t *spe)
787{
788 xml_node_t *ssid;
789 char *txt;
790
791 ssid = get_node(ctx->xml, spe, "SSID");
792 if (ssid == NULL)
793 return;
794 txt = xml_node_get_text(ctx->xml, ssid);
795 if (txt == NULL)
796 return;
797 wpa_printf(MSG_DEBUG, "- Policy/SPExclusionList/<X+>/SSID = %s", txt);
798 if (set_cred_quoted(ctx->ifname, id, "excluded_ssid", txt) < 0)
799 wpa_printf(MSG_INFO, "Failed to set cred excluded_ssid");
800 xml_node_get_text_free(ctx->xml, txt);
801}
802
803
804static void set_pps_cred_policy_spel(struct hs20_osu_client *ctx, int id,
805 xml_node_t *spel)
806{
807 xml_node_t *child;
808
809 xml_node_for_each_child(ctx->xml, child, spel) {
810 xml_node_for_each_check(ctx->xml, child);
811 set_pps_cred_policy_spe(ctx, id, child);
812 }
813}
814
815
816static void set_pps_cred_policy_prp(struct hs20_osu_client *ctx, int id,
817 xml_node_t *prp)
818{
819 xml_node_t *node;
820 char *txt = NULL, *pos;
821 char *prio, *country_buf = NULL;
822 const char *country;
823 char val[200];
824 int priority;
825
826 node = get_node(ctx->xml, prp, "Priority");
827 if (node == NULL)
828 return;
829 prio = xml_node_get_text(ctx->xml, node);
830 if (prio == NULL)
831 return;
832 wpa_printf(MSG_INFO, "- Policy/PreferredRoamingPartnerList/<X+>/Priority = %s",
833 prio);
834 priority = atoi(prio);
835 xml_node_get_text_free(ctx->xml, prio);
836
837 node = get_node(ctx->xml, prp, "Country");
838 if (node) {
839 country_buf = xml_node_get_text(ctx->xml, node);
840 if (country_buf == NULL)
841 return;
842 country = country_buf;
843 wpa_printf(MSG_INFO, "- Policy/PreferredRoamingPartnerList/<X+>/Country = %s",
844 country);
845 } else {
846 country = "*";
847 }
848
849 node = get_node(ctx->xml, prp, "FQDN_Match");
850 if (node == NULL)
851 goto out;
852 txt = xml_node_get_text(ctx->xml, node);
853 if (txt == NULL)
854 goto out;
855 wpa_printf(MSG_INFO, "- Policy/PreferredRoamingPartnerList/<X+>/FQDN_Match = %s",
856 txt);
857 pos = strrchr(txt, ',');
858 if (pos == NULL)
859 goto out;
860 *pos++ = '\0';
861
862 snprintf(val, sizeof(val), "%s,%d,%d,%s", txt,
863 strcmp(pos, "includeSubdomains") != 0, priority, country);
864 if (set_cred_quoted(ctx->ifname, id, "roaming_partner", val) < 0)
865 wpa_printf(MSG_INFO, "Failed to set cred roaming_partner");
866out:
867 xml_node_get_text_free(ctx->xml, country_buf);
868 xml_node_get_text_free(ctx->xml, txt);
869}
870
871
872static void set_pps_cred_policy_prpl(struct hs20_osu_client *ctx, int id,
873 xml_node_t *prpl)
874{
875 xml_node_t *child;
876
877 xml_node_for_each_child(ctx->xml, child, prpl) {
878 xml_node_for_each_check(ctx->xml, child);
879 set_pps_cred_policy_prp(ctx, id, child);
880 }
881}
882
883
884static void set_pps_cred_policy_min_backhaul(struct hs20_osu_client *ctx, int id,
885 xml_node_t *min_backhaul)
886{
887 xml_node_t *node;
888 char *type, *dl = NULL, *ul = NULL;
889 int home;
890
891 node = get_node(ctx->xml, min_backhaul, "NetworkType");
892 if (node == NULL) {
893 wpa_printf(MSG_INFO, "Ignore MinBackhaulThreshold without mandatory NetworkType node");
894 return;
895 }
896
897 type = xml_node_get_text(ctx->xml, node);
898 if (type == NULL)
899 return;
900 wpa_printf(MSG_INFO, "- Policy/MinBackhaulThreshold/<X+>/NetworkType = %s",
901 type);
902 if (os_strcasecmp(type, "home") == 0)
903 home = 1;
904 else if (os_strcasecmp(type, "roaming") == 0)
905 home = 0;
906 else {
907 wpa_printf(MSG_INFO, "Ignore MinBackhaulThreshold with invalid NetworkType");
908 xml_node_get_text_free(ctx->xml, type);
909 return;
910 }
911 xml_node_get_text_free(ctx->xml, type);
912
913 node = get_node(ctx->xml, min_backhaul, "DLBandwidth");
914 if (node)
915 dl = xml_node_get_text(ctx->xml, node);
916
917 node = get_node(ctx->xml, min_backhaul, "ULBandwidth");
918 if (node)
919 ul = xml_node_get_text(ctx->xml, node);
920
921 if (dl == NULL && ul == NULL) {
922 wpa_printf(MSG_INFO, "Ignore MinBackhaulThreshold without either DLBandwidth or ULBandwidth nodes");
923 return;
924 }
925
926 if (dl)
927 wpa_printf(MSG_INFO, "- Policy/MinBackhaulThreshold/<X+>/DLBandwidth = %s",
928 dl);
929 if (ul)
930 wpa_printf(MSG_INFO, "- Policy/MinBackhaulThreshold/<X+>/ULBandwidth = %s",
931 ul);
932
933 if (home) {
934 if (dl &&
935 set_cred(ctx->ifname, id, "min_dl_bandwidth_home", dl) < 0)
936 wpa_printf(MSG_INFO, "Failed to set cred bandwidth limit");
937 if (ul &&
938 set_cred(ctx->ifname, id, "min_ul_bandwidth_home", ul) < 0)
939 wpa_printf(MSG_INFO, "Failed to set cred bandwidth limit");
940 } else {
941 if (dl &&
942 set_cred(ctx->ifname, id, "min_dl_bandwidth_roaming", dl) <
943 0)
944 wpa_printf(MSG_INFO, "Failed to set cred bandwidth limit");
945 if (ul &&
946 set_cred(ctx->ifname, id, "min_ul_bandwidth_roaming", ul) <
947 0)
948 wpa_printf(MSG_INFO, "Failed to set cred bandwidth limit");
949 }
950
951 xml_node_get_text_free(ctx->xml, dl);
952 xml_node_get_text_free(ctx->xml, ul);
953}
954
955
956static void set_pps_cred_policy_min_backhaul_list(struct hs20_osu_client *ctx,
957 int id, xml_node_t *node)
958{
959 xml_node_t *child;
960
961 wpa_printf(MSG_INFO, "- Policy/MinBackhaulThreshold");
962
963 xml_node_for_each_child(ctx->xml, child, node) {
964 xml_node_for_each_check(ctx->xml, child);
965 set_pps_cred_policy_min_backhaul(ctx, id, child);
966 }
967}
968
969
970static void set_pps_cred_policy_update(struct hs20_osu_client *ctx, int id,
971 xml_node_t *node)
972{
973 wpa_printf(MSG_INFO, "- Policy/PolicyUpdate");
974 /* Not used in wpa_supplicant */
975}
976
977
978static void set_pps_cred_policy_required_proto_port(struct hs20_osu_client *ctx,
979 int id, xml_node_t *tuple)
980{
981 xml_node_t *node;
982 char *proto, *port;
983 char *buf;
984 size_t buflen;
985
986 node = get_node(ctx->xml, tuple, "IPProtocol");
987 if (node == NULL) {
988 wpa_printf(MSG_INFO, "Ignore RequiredProtoPortTuple without mandatory IPProtocol node");
989 return;
990 }
991
992 proto = xml_node_get_text(ctx->xml, node);
993 if (proto == NULL)
994 return;
995
996 wpa_printf(MSG_INFO, "- Policy/RequiredProtoPortTuple/<X+>/IPProtocol = %s",
997 proto);
998
999 node = get_node(ctx->xml, tuple, "PortNumber");
1000 port = node ? xml_node_get_text(ctx->xml, node) : NULL;
1001 if (port) {
1002 wpa_printf(MSG_INFO, "- Policy/RequiredProtoPortTuple/<X+>/PortNumber = %s",
1003 port);
1004 buflen = os_strlen(proto) + os_strlen(port) + 10;
1005 buf = os_malloc(buflen);
1006 if (buf)
1007 os_snprintf(buf, buflen, "%s:%s", proto, port);
1008 xml_node_get_text_free(ctx->xml, port);
1009 } else {
1010 buflen = os_strlen(proto) + 10;
1011 buf = os_malloc(buflen);
1012 if (buf)
1013 os_snprintf(buf, buflen, "%s", proto);
1014 }
1015
1016 xml_node_get_text_free(ctx->xml, proto);
1017
1018 if (buf == NULL)
1019 return;
1020
1021 if (set_cred(ctx->ifname, id, "req_conn_capab", buf) < 0)
1022 wpa_printf(MSG_INFO, "Could not set req_conn_capab");
1023
1024 os_free(buf);
1025}
1026
1027
1028static void set_pps_cred_policy_required_proto_ports(struct hs20_osu_client *ctx,
1029 int id, xml_node_t *node)
1030{
1031 xml_node_t *child;
1032
1033 wpa_printf(MSG_INFO, "- Policy/RequiredProtoPortTuple");
1034
1035 xml_node_for_each_child(ctx->xml, child, node) {
1036 xml_node_for_each_check(ctx->xml, child);
1037 set_pps_cred_policy_required_proto_port(ctx, id, child);
1038 }
1039}
1040
1041
1042static void set_pps_cred_policy_max_bss_load(struct hs20_osu_client *ctx, int id,
1043 xml_node_t *node)
1044{
1045 char *str = xml_node_get_text(ctx->xml, node);
1046 if (str == NULL)
1047 return;
1048 wpa_printf(MSG_INFO, "- Policy/MaximumBSSLoadValue - %s", str);
1049 if (set_cred(ctx->ifname, id, "max_bss_load", str) < 0)
1050 wpa_printf(MSG_INFO, "Failed to set cred max_bss_load limit");
1051 xml_node_get_text_free(ctx->xml, str);
1052}
1053
1054
1055static void set_pps_cred_policy(struct hs20_osu_client *ctx, int id,
1056 xml_node_t *node)
1057{
1058 xml_node_t *child;
1059 const char *name;
1060
1061 wpa_printf(MSG_INFO, "- Policy");
1062
1063 xml_node_for_each_child(ctx->xml, child, node) {
1064 xml_node_for_each_check(ctx->xml, child);
1065 name = xml_node_get_localname(ctx->xml, child);
1066 if (os_strcasecmp(name, "PreferredRoamingPartnerList") == 0)
1067 set_pps_cred_policy_prpl(ctx, id, child);
1068 else if (os_strcasecmp(name, "MinBackhaulThreshold") == 0)
1069 set_pps_cred_policy_min_backhaul_list(ctx, id, child);
1070 else if (os_strcasecmp(name, "PolicyUpdate") == 0)
1071 set_pps_cred_policy_update(ctx, id, child);
1072 else if (os_strcasecmp(name, "SPExclusionList") == 0)
1073 set_pps_cred_policy_spel(ctx, id, child);
1074 else if (os_strcasecmp(name, "RequiredProtoPortTuple") == 0)
1075 set_pps_cred_policy_required_proto_ports(ctx, id, child);
1076 else if (os_strcasecmp(name, "MaximumBSSLoadValue") == 0)
1077 set_pps_cred_policy_max_bss_load(ctx, id, child);
1078 else
1079 wpa_printf(MSG_INFO, "Unknown Policy node '%s'", name);
1080 }
1081}
1082
1083
1084static void set_pps_cred_priority(struct hs20_osu_client *ctx, int id,
1085 xml_node_t *node)
1086{
1087 char *str = xml_node_get_text(ctx->xml, node);
1088 if (str == NULL)
1089 return;
1090 wpa_printf(MSG_INFO, "- CredentialPriority = %s", str);
1091 if (set_cred(ctx->ifname, id, "sp_priority", str) < 0)
1092 wpa_printf(MSG_INFO, "Failed to set cred sp_priority");
1093 xml_node_get_text_free(ctx->xml, str);
1094}
1095
1096
1097static void set_pps_cred_aaa_server_trust_root(struct hs20_osu_client *ctx,
1098 int id, xml_node_t *node)
1099{
1100 wpa_printf(MSG_INFO, "- AAAServerTrustRoot - TODO");
1101}
1102
1103
1104static void set_pps_cred_sub_update(struct hs20_osu_client *ctx, int id,
1105 xml_node_t *node)
1106{
1107 wpa_printf(MSG_INFO, "- SubscriptionUpdate");
1108 /* not used within wpa_supplicant */
1109}
1110
1111
1112static void set_pps_cred_home_sp_network_id(struct hs20_osu_client *ctx,
1113 int id, xml_node_t *node)
1114{
1115 xml_node_t *ssid_node, *hessid_node;
1116 char *ssid, *hessid;
1117
1118 ssid_node = get_node(ctx->xml, node, "SSID");
1119 if (ssid_node == NULL) {
1120 wpa_printf(MSG_INFO, "Ignore HomeSP/NetworkID without mandatory SSID node");
1121 return;
1122 }
1123
1124 hessid_node = get_node(ctx->xml, node, "HESSID");
1125
1126 ssid = xml_node_get_text(ctx->xml, ssid_node);
1127 if (ssid == NULL)
1128 return;
1129 hessid = hessid_node ? xml_node_get_text(ctx->xml, hessid_node) : NULL;
1130
1131 wpa_printf(MSG_INFO, "- HomeSP/NetworkID/<X+>/SSID = %s", ssid);
1132 if (hessid)
1133 wpa_printf(MSG_INFO, "- HomeSP/NetworkID/<X+>/HESSID = %s",
1134 hessid);
1135
1136 /* TODO: Configure to wpa_supplicant */
1137
1138 xml_node_get_text_free(ctx->xml, ssid);
1139 xml_node_get_text_free(ctx->xml, hessid);
1140}
1141
1142
1143static void set_pps_cred_home_sp_network_ids(struct hs20_osu_client *ctx,
1144 int id, xml_node_t *node)
1145{
1146 xml_node_t *child;
1147
1148 wpa_printf(MSG_INFO, "- HomeSP/NetworkID");
1149
1150 xml_node_for_each_child(ctx->xml, child, node) {
1151 xml_node_for_each_check(ctx->xml, child);
1152 set_pps_cred_home_sp_network_id(ctx, id, child);
1153 }
1154}
1155
1156
1157static void set_pps_cred_home_sp_friendly_name(struct hs20_osu_client *ctx,
1158 int id, xml_node_t *node)
1159{
1160 char *str = xml_node_get_text(ctx->xml, node);
1161 if (str == NULL)
1162 return;
1163 wpa_printf(MSG_INFO, "- HomeSP/FriendlyName = %s", str);
1164 /* not used within wpa_supplicant(?) */
1165 xml_node_get_text_free(ctx->xml, str);
1166}
1167
1168
1169static void set_pps_cred_home_sp_icon_url(struct hs20_osu_client *ctx,
1170 int id, xml_node_t *node)
1171{
1172 char *str = xml_node_get_text(ctx->xml, node);
1173 if (str == NULL)
1174 return;
1175 wpa_printf(MSG_INFO, "- HomeSP/IconURL = %s", str);
1176 /* not used within wpa_supplicant */
1177 xml_node_get_text_free(ctx->xml, str);
1178}
1179
1180
1181static void set_pps_cred_home_sp_fqdn(struct hs20_osu_client *ctx, int id,
1182 xml_node_t *node)
1183{
1184 char *str = xml_node_get_text(ctx->xml, node);
1185 if (str == NULL)
1186 return;
1187 wpa_printf(MSG_INFO, "- HomeSP/FQDN = %s", str);
1188 if (set_cred_quoted(ctx->ifname, id, "domain", str) < 0)
1189 wpa_printf(MSG_INFO, "Failed to set cred domain");
1190 if (set_cred_quoted(ctx->ifname, id, "domain_suffix_match", str) < 0)
1191 wpa_printf(MSG_INFO, "Failed to set cred domain_suffix_match");
1192 xml_node_get_text_free(ctx->xml, str);
1193}
1194
1195
1196static void set_pps_cred_home_sp_oi(struct hs20_osu_client *ctx, int id,
1197 xml_node_t *node)
1198{
1199 xml_node_t *child;
1200 const char *name;
1201 char *homeoi = NULL;
1202 int required = 0;
1203 char *str;
1204
1205 xml_node_for_each_child(ctx->xml, child, node) {
1206 xml_node_for_each_check(ctx->xml, child);
1207 name = xml_node_get_localname(ctx->xml, child);
1208 if (strcasecmp(name, "HomeOI") == 0 && !homeoi) {
1209 homeoi = xml_node_get_text(ctx->xml, child);
1210 wpa_printf(MSG_INFO, "- HomeSP/HomeOIList/<X+>/HomeOI = %s",
1211 homeoi);
1212 } else if (strcasecmp(name, "HomeOIRequired") == 0) {
1213 str = xml_node_get_text(ctx->xml, child);
1214 wpa_printf(MSG_INFO, "- HomeSP/HomeOIList/<X+>/HomeOIRequired = '%s'",
1215 str);
1216 if (str == NULL)
1217 continue;
1218 required = strcasecmp(str, "true") == 0;
1219 xml_node_get_text_free(ctx->xml, str);
1220 } else
1221 wpa_printf(MSG_INFO, "Unknown HomeOIList node '%s'",
1222 name);
1223 }
1224
1225 if (homeoi == NULL) {
1226 wpa_printf(MSG_INFO, "- HomeSP/HomeOIList/<X+> without HomeOI ignored");
1227 return;
1228 }
1229
1230 wpa_printf(MSG_INFO, "- HomeSP/HomeOIList/<X+> '%s' required=%d",
1231 homeoi, required);
1232
1233 if (required) {
1234 if (set_cred(ctx->ifname, id, "required_roaming_consortium",
1235 homeoi) < 0)
1236 wpa_printf(MSG_INFO, "Failed to set cred required_roaming_consortium");
1237 } else {
Hai Shalomce48b4a2018-09-05 11:41:35 -07001238 if (set_cred(ctx->ifname, id, "roaming_consortium", homeoi) < 0)
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07001239 wpa_printf(MSG_INFO, "Failed to set cred roaming_consortium");
1240 }
1241
1242 xml_node_get_text_free(ctx->xml, homeoi);
1243}
1244
1245
1246static void set_pps_cred_home_sp_oi_list(struct hs20_osu_client *ctx, int id,
1247 xml_node_t *node)
1248{
1249 xml_node_t *child;
1250
1251 wpa_printf(MSG_INFO, "- HomeSP/HomeOIList");
1252
1253 xml_node_for_each_child(ctx->xml, child, node) {
1254 xml_node_for_each_check(ctx->xml, child);
1255 set_pps_cred_home_sp_oi(ctx, id, child);
1256 }
1257}
1258
1259
1260static void set_pps_cred_home_sp_other_partner(struct hs20_osu_client *ctx,
1261 int id, xml_node_t *node)
1262{
1263 xml_node_t *child;
1264 const char *name;
1265 char *fqdn = NULL;
1266
1267 xml_node_for_each_child(ctx->xml, child, node) {
1268 xml_node_for_each_check(ctx->xml, child);
1269 name = xml_node_get_localname(ctx->xml, child);
1270 if (os_strcasecmp(name, "FQDN") == 0 && !fqdn) {
1271 fqdn = xml_node_get_text(ctx->xml, child);
1272 wpa_printf(MSG_INFO, "- HomeSP/OtherHomePartners/<X+>/FQDN = %s",
1273 fqdn);
1274 } else
1275 wpa_printf(MSG_INFO, "Unknown OtherHomePartners node '%s'",
1276 name);
1277 }
1278
1279 if (fqdn == NULL) {
1280 wpa_printf(MSG_INFO, "- HomeSP/OtherHomePartners/<X+> without FQDN ignored");
1281 return;
1282 }
1283
1284 if (set_cred_quoted(ctx->ifname, id, "domain", fqdn) < 0)
1285 wpa_printf(MSG_INFO, "Failed to set cred domain for OtherHomePartners node");
1286
1287 xml_node_get_text_free(ctx->xml, fqdn);
1288}
1289
1290
1291static void set_pps_cred_home_sp_other_partners(struct hs20_osu_client *ctx,
1292 int id,
1293 xml_node_t *node)
1294{
1295 xml_node_t *child;
1296
1297 wpa_printf(MSG_INFO, "- HomeSP/OtherHomePartners");
1298
1299 xml_node_for_each_child(ctx->xml, child, node) {
1300 xml_node_for_each_check(ctx->xml, child);
1301 set_pps_cred_home_sp_other_partner(ctx, id, child);
1302 }
1303}
1304
1305
1306static void set_pps_cred_home_sp_roaming_consortium_oi(
1307 struct hs20_osu_client *ctx, int id, xml_node_t *node)
1308{
1309 char *str = xml_node_get_text(ctx->xml, node);
1310 if (str == NULL)
1311 return;
1312 wpa_printf(MSG_INFO, "- HomeSP/RoamingConsortiumOI = %s", str);
Roshan Pius3a1667e2018-07-03 15:17:14 -07001313 if (set_cred_quoted(ctx->ifname, id, "roaming_consortiums",
1314 str) < 0)
1315 wpa_printf(MSG_INFO, "Failed to set cred roaming_consortiums");
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07001316 xml_node_get_text_free(ctx->xml, str);
1317}
1318
1319
1320static void set_pps_cred_home_sp(struct hs20_osu_client *ctx, int id,
1321 xml_node_t *node)
1322{
1323 xml_node_t *child;
1324 const char *name;
1325
1326 wpa_printf(MSG_INFO, "- HomeSP");
1327
1328 xml_node_for_each_child(ctx->xml, child, node) {
1329 xml_node_for_each_check(ctx->xml, child);
1330 name = xml_node_get_localname(ctx->xml, child);
1331 if (os_strcasecmp(name, "NetworkID") == 0)
1332 set_pps_cred_home_sp_network_ids(ctx, id, child);
1333 else if (os_strcasecmp(name, "FriendlyName") == 0)
1334 set_pps_cred_home_sp_friendly_name(ctx, id, child);
1335 else if (os_strcasecmp(name, "IconURL") == 0)
1336 set_pps_cred_home_sp_icon_url(ctx, id, child);
1337 else if (os_strcasecmp(name, "FQDN") == 0)
1338 set_pps_cred_home_sp_fqdn(ctx, id, child);
1339 else if (os_strcasecmp(name, "HomeOIList") == 0)
1340 set_pps_cred_home_sp_oi_list(ctx, id, child);
1341 else if (os_strcasecmp(name, "OtherHomePartners") == 0)
1342 set_pps_cred_home_sp_other_partners(ctx, id, child);
1343 else if (os_strcasecmp(name, "RoamingConsortiumOI") == 0)
1344 set_pps_cred_home_sp_roaming_consortium_oi(ctx, id,
1345 child);
1346 else
1347 wpa_printf(MSG_INFO, "Unknown HomeSP node '%s'", name);
1348 }
1349}
1350
1351
1352static void set_pps_cred_sub_params(struct hs20_osu_client *ctx, int id,
1353 xml_node_t *node)
1354{
1355 wpa_printf(MSG_INFO, "- SubscriptionParameters");
1356 /* not used within wpa_supplicant */
1357}
1358
1359
1360static void set_pps_cred_creation_date(struct hs20_osu_client *ctx, int id,
1361 xml_node_t *node)
1362{
1363 char *str = xml_node_get_text(ctx->xml, node);
1364 if (str == NULL)
1365 return;
1366 wpa_printf(MSG_INFO, "- Credential/CreationDate = %s", str);
1367 /* not used within wpa_supplicant */
1368 xml_node_get_text_free(ctx->xml, str);
1369}
1370
1371
1372static void set_pps_cred_expiration_date(struct hs20_osu_client *ctx, int id,
1373 xml_node_t *node)
1374{
1375 char *str = xml_node_get_text(ctx->xml, node);
1376 if (str == NULL)
1377 return;
1378 wpa_printf(MSG_INFO, "- Credential/ExpirationDate = %s", str);
1379 /* not used within wpa_supplicant */
1380 xml_node_get_text_free(ctx->xml, str);
1381}
1382
1383
1384static void set_pps_cred_username(struct hs20_osu_client *ctx, int id,
1385 xml_node_t *node)
1386{
1387 char *str = xml_node_get_text(ctx->xml, node);
1388 if (str == NULL)
1389 return;
1390 wpa_printf(MSG_INFO, "- Credential/UsernamePassword/Username = %s",
1391 str);
1392 if (set_cred_quoted(ctx->ifname, id, "username", str) < 0)
1393 wpa_printf(MSG_INFO, "Failed to set cred username");
1394 xml_node_get_text_free(ctx->xml, str);
1395}
1396
1397
1398static void set_pps_cred_password(struct hs20_osu_client *ctx, int id,
1399 xml_node_t *node)
1400{
1401 int len, i;
1402 char *pw, *hex, *pos, *end;
1403
1404 pw = xml_node_get_base64_text(ctx->xml, node, &len);
1405 if (pw == NULL)
1406 return;
1407
1408 wpa_printf(MSG_INFO, "- Credential/UsernamePassword/Password = %s", pw);
1409
1410 hex = malloc(len * 2 + 1);
1411 if (hex == NULL) {
1412 free(pw);
1413 return;
1414 }
1415 end = hex + len * 2 + 1;
1416 pos = hex;
1417 for (i = 0; i < len; i++) {
1418 snprintf(pos, end - pos, "%02x", pw[i]);
1419 pos += 2;
1420 }
1421 free(pw);
1422
1423 if (set_cred(ctx->ifname, id, "password", hex) < 0)
1424 wpa_printf(MSG_INFO, "Failed to set cred password");
1425 free(hex);
1426}
1427
1428
1429static void set_pps_cred_machine_managed(struct hs20_osu_client *ctx, int id,
1430 xml_node_t *node)
1431{
1432 char *str = xml_node_get_text(ctx->xml, node);
1433 if (str == NULL)
1434 return;
1435 wpa_printf(MSG_INFO, "- Credential/UsernamePassword/MachineManaged = %s",
1436 str);
1437 /* not used within wpa_supplicant */
1438 xml_node_get_text_free(ctx->xml, str);
1439}
1440
1441
1442static void set_pps_cred_soft_token_app(struct hs20_osu_client *ctx, int id,
1443 xml_node_t *node)
1444{
1445 char *str = xml_node_get_text(ctx->xml, node);
1446 if (str == NULL)
1447 return;
1448 wpa_printf(MSG_INFO, "- Credential/UsernamePassword/SoftTokenApp = %s",
1449 str);
1450 /* not used within wpa_supplicant */
1451 xml_node_get_text_free(ctx->xml, str);
1452}
1453
1454
1455static void set_pps_cred_able_to_share(struct hs20_osu_client *ctx, int id,
1456 xml_node_t *node)
1457{
1458 char *str = xml_node_get_text(ctx->xml, node);
1459 if (str == NULL)
1460 return;
1461 wpa_printf(MSG_INFO, "- Credential/UsernamePassword/AbleToShare = %s",
1462 str);
1463 /* not used within wpa_supplicant */
1464 xml_node_get_text_free(ctx->xml, str);
1465}
1466
1467
Roshan Pius3a1667e2018-07-03 15:17:14 -07001468static void set_pps_cred_eap_method_eap_type(struct hs20_osu_client *ctx,
1469 int id, xml_node_t *node)
1470{
1471 char *str = xml_node_get_text(ctx->xml, node);
1472 int type;
1473 const char *eap_method = NULL;
1474
1475 if (!str)
1476 return;
1477 wpa_printf(MSG_INFO,
1478 "- Credential/UsernamePassword/EAPMethod/EAPType = %s", str);
1479 type = atoi(str);
1480 switch (type) {
1481 case EAP_TYPE_TLS:
1482 eap_method = "TLS";
1483 break;
1484 case EAP_TYPE_TTLS:
1485 eap_method = "TTLS";
1486 break;
1487 case EAP_TYPE_PEAP:
1488 eap_method = "PEAP";
1489 break;
1490 case EAP_TYPE_PWD:
1491 eap_method = "PWD";
1492 break;
1493 }
1494 xml_node_get_text_free(ctx->xml, str);
1495 if (!eap_method) {
1496 wpa_printf(MSG_INFO, "Unknown EAPType value");
1497 return;
1498 }
1499
1500 if (set_cred(ctx->ifname, id, "eap", eap_method) < 0)
1501 wpa_printf(MSG_INFO, "Failed to set cred eap");
1502}
1503
1504
1505static void set_pps_cred_eap_method_inner_method(struct hs20_osu_client *ctx,
1506 int id, xml_node_t *node)
1507{
1508 char *str = xml_node_get_text(ctx->xml, node);
1509 const char *phase2 = NULL;
1510
1511 if (!str)
1512 return;
1513 wpa_printf(MSG_INFO,
1514 "- Credential/UsernamePassword/EAPMethod/InnerMethod = %s",
1515 str);
1516 if (os_strcmp(str, "PAP") == 0)
1517 phase2 = "auth=PAP";
1518 else if (os_strcmp(str, "CHAP") == 0)
1519 phase2 = "auth=CHAP";
1520 else if (os_strcmp(str, "MS-CHAP") == 0)
1521 phase2 = "auth=MSCHAP";
1522 else if (os_strcmp(str, "MS-CHAP-V2") == 0)
1523 phase2 = "auth=MSCHAPV2";
1524 xml_node_get_text_free(ctx->xml, str);
1525 if (!phase2) {
1526 wpa_printf(MSG_INFO, "Unknown InnerMethod value");
1527 return;
1528 }
1529
1530 if (set_cred_quoted(ctx->ifname, id, "phase2", phase2) < 0)
1531 wpa_printf(MSG_INFO, "Failed to set cred phase2");
1532}
1533
1534
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07001535static void set_pps_cred_eap_method(struct hs20_osu_client *ctx, int id,
1536 xml_node_t *node)
1537{
Roshan Pius3a1667e2018-07-03 15:17:14 -07001538 xml_node_t *child;
1539 const char *name;
1540
1541 wpa_printf(MSG_INFO, "- Credential/UsernamePassword/EAPMethod");
1542
1543 xml_node_for_each_child(ctx->xml, child, node) {
1544 xml_node_for_each_check(ctx->xml, child);
1545 name = xml_node_get_localname(ctx->xml, child);
1546 if (os_strcasecmp(name, "EAPType") == 0)
1547 set_pps_cred_eap_method_eap_type(ctx, id, child);
1548 else if (os_strcasecmp(name, "InnerMethod") == 0)
1549 set_pps_cred_eap_method_inner_method(ctx, id, child);
1550 else
1551 wpa_printf(MSG_INFO, "Unknown Credential/UsernamePassword/EAPMethod node '%s'",
1552 name);
1553 }
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07001554}
1555
1556
1557static void set_pps_cred_username_password(struct hs20_osu_client *ctx, int id,
1558 xml_node_t *node)
1559{
1560 xml_node_t *child;
1561 const char *name;
1562
1563 wpa_printf(MSG_INFO, "- Credential/UsernamePassword");
1564
1565 xml_node_for_each_child(ctx->xml, child, node) {
1566 xml_node_for_each_check(ctx->xml, child);
1567 name = xml_node_get_localname(ctx->xml, child);
1568 if (os_strcasecmp(name, "Username") == 0)
1569 set_pps_cred_username(ctx, id, child);
1570 else if (os_strcasecmp(name, "Password") == 0)
1571 set_pps_cred_password(ctx, id, child);
1572 else if (os_strcasecmp(name, "MachineManaged") == 0)
1573 set_pps_cred_machine_managed(ctx, id, child);
1574 else if (os_strcasecmp(name, "SoftTokenApp") == 0)
1575 set_pps_cred_soft_token_app(ctx, id, child);
1576 else if (os_strcasecmp(name, "AbleToShare") == 0)
1577 set_pps_cred_able_to_share(ctx, id, child);
1578 else if (os_strcasecmp(name, "EAPMethod") == 0)
1579 set_pps_cred_eap_method(ctx, id, child);
1580 else
1581 wpa_printf(MSG_INFO, "Unknown Credential/UsernamePassword node '%s'",
1582 name);
1583 }
1584}
1585
1586
1587static void set_pps_cred_digital_cert(struct hs20_osu_client *ctx, int id,
1588 xml_node_t *node, const char *fqdn)
1589{
1590 char buf[200], dir[200];
Hai Shalom81f62d82019-07-22 12:10:00 -07001591 int res;
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07001592
1593 wpa_printf(MSG_INFO, "- Credential/DigitalCertificate");
1594
1595 if (getcwd(dir, sizeof(dir)) == NULL)
1596 return;
1597
1598 /* TODO: could build username from Subject of Subject AltName */
1599 if (set_cred_quoted(ctx->ifname, id, "username", "cert") < 0) {
1600 wpa_printf(MSG_INFO, "Failed to set username");
1601 }
1602
Hai Shalom81f62d82019-07-22 12:10:00 -07001603 res = os_snprintf(buf, sizeof(buf), "%s/SP/%s/client-cert.pem", dir,
1604 fqdn);
1605 if (os_snprintf_error(sizeof(buf), res))
1606 return;
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07001607 if (os_file_exists(buf)) {
1608 if (set_cred_quoted(ctx->ifname, id, "client_cert", buf) < 0) {
1609 wpa_printf(MSG_INFO, "Failed to set client_cert");
1610 }
1611 }
1612
Hai Shalom81f62d82019-07-22 12:10:00 -07001613 res = os_snprintf(buf, sizeof(buf), "%s/SP/%s/client-key.pem", dir,
1614 fqdn);
1615 if (os_snprintf_error(sizeof(buf), res))
1616 return;
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07001617 if (os_file_exists(buf)) {
1618 if (set_cred_quoted(ctx->ifname, id, "private_key", buf) < 0) {
1619 wpa_printf(MSG_INFO, "Failed to set private_key");
1620 }
1621 }
1622}
1623
1624
1625static void set_pps_cred_realm(struct hs20_osu_client *ctx, int id,
1626 xml_node_t *node, const char *fqdn, int sim)
1627{
1628 char *str = xml_node_get_text(ctx->xml, node);
1629 char buf[200], dir[200];
Hai Shalom81f62d82019-07-22 12:10:00 -07001630 int res;
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07001631
1632 if (str == NULL)
1633 return;
1634
1635 wpa_printf(MSG_INFO, "- Credential/Realm = %s", str);
1636 if (set_cred_quoted(ctx->ifname, id, "realm", str) < 0)
1637 wpa_printf(MSG_INFO, "Failed to set cred realm");
1638 xml_node_get_text_free(ctx->xml, str);
1639
1640 if (sim)
1641 return;
1642
1643 if (getcwd(dir, sizeof(dir)) == NULL)
1644 return;
Hai Shalom81f62d82019-07-22 12:10:00 -07001645 res = os_snprintf(buf, sizeof(buf), "%s/SP/%s/aaa-ca.pem", dir, fqdn);
1646 if (os_snprintf_error(sizeof(buf), res))
1647 return;
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07001648 if (os_file_exists(buf)) {
1649 if (set_cred_quoted(ctx->ifname, id, "ca_cert", buf) < 0) {
1650 wpa_printf(MSG_INFO, "Failed to set CA cert");
1651 }
1652 }
1653}
1654
1655
1656static void set_pps_cred_check_aaa_cert_status(struct hs20_osu_client *ctx,
1657 int id, xml_node_t *node)
1658{
1659 char *str = xml_node_get_text(ctx->xml, node);
1660
1661 if (str == NULL)
1662 return;
1663
1664 wpa_printf(MSG_INFO, "- Credential/CheckAAAServerCertStatus = %s", str);
1665 if (os_strcasecmp(str, "true") == 0 &&
1666 set_cred(ctx->ifname, id, "ocsp", "2") < 0)
1667 wpa_printf(MSG_INFO, "Failed to set cred ocsp");
1668 xml_node_get_text_free(ctx->xml, str);
1669}
1670
1671
1672static void set_pps_cred_sim(struct hs20_osu_client *ctx, int id,
1673 xml_node_t *sim, xml_node_t *realm)
1674{
1675 xml_node_t *node;
1676 char *imsi, *eaptype, *str, buf[20];
1677 int type;
1678 int mnc_len = 3;
1679 size_t imsi_len;
1680
1681 node = get_node(ctx->xml, sim, "EAPType");
1682 if (node == NULL) {
1683 wpa_printf(MSG_INFO, "No SIM/EAPType node in credential");
1684 return;
1685 }
1686 eaptype = xml_node_get_text(ctx->xml, node);
1687 if (eaptype == NULL) {
1688 wpa_printf(MSG_INFO, "Could not extract SIM/EAPType");
1689 return;
1690 }
1691 wpa_printf(MSG_INFO, " - Credential/SIM/EAPType = %s", eaptype);
1692 type = atoi(eaptype);
1693 xml_node_get_text_free(ctx->xml, eaptype);
1694
1695 switch (type) {
1696 case EAP_TYPE_SIM:
1697 if (set_cred(ctx->ifname, id, "eap", "SIM") < 0)
1698 wpa_printf(MSG_INFO, "Could not set eap=SIM");
1699 break;
1700 case EAP_TYPE_AKA:
1701 if (set_cred(ctx->ifname, id, "eap", "AKA") < 0)
1702 wpa_printf(MSG_INFO, "Could not set eap=SIM");
1703 break;
1704 case EAP_TYPE_AKA_PRIME:
1705 if (set_cred(ctx->ifname, id, "eap", "AKA'") < 0)
1706 wpa_printf(MSG_INFO, "Could not set eap=SIM");
1707 break;
1708 default:
1709 wpa_printf(MSG_INFO, "Unsupported SIM/EAPType %d", type);
1710 return;
1711 }
1712
1713 node = get_node(ctx->xml, sim, "IMSI");
1714 if (node == NULL) {
1715 wpa_printf(MSG_INFO, "No SIM/IMSI node in credential");
1716 return;
1717 }
1718 imsi = xml_node_get_text(ctx->xml, node);
1719 if (imsi == NULL) {
1720 wpa_printf(MSG_INFO, "Could not extract SIM/IMSI");
1721 return;
1722 }
1723 wpa_printf(MSG_INFO, " - Credential/SIM/IMSI = %s", imsi);
1724 imsi_len = os_strlen(imsi);
1725 if (imsi_len < 7 || imsi_len + 2 > sizeof(buf)) {
1726 wpa_printf(MSG_INFO, "Invalid IMSI length");
1727 xml_node_get_text_free(ctx->xml, imsi);
1728 return;
1729 }
1730
1731 str = xml_node_get_text(ctx->xml, node);
1732 if (str) {
1733 char *pos;
1734 pos = os_strstr(str, "mnc");
1735 if (pos && os_strlen(pos) >= 6) {
1736 if (os_strncmp(imsi + 3, pos + 3, 3) == 0)
1737 mnc_len = 3;
1738 else if (os_strncmp(imsi + 3, pos + 4, 2) == 0)
1739 mnc_len = 2;
1740 }
1741 xml_node_get_text_free(ctx->xml, str);
1742 }
1743
1744 os_memcpy(buf, imsi, 3 + mnc_len);
1745 buf[3 + mnc_len] = '-';
1746 os_strlcpy(buf + 3 + mnc_len + 1, imsi + 3 + mnc_len,
1747 sizeof(buf) - 3 - mnc_len - 1);
1748
1749 xml_node_get_text_free(ctx->xml, imsi);
1750
1751 if (set_cred_quoted(ctx->ifname, id, "imsi", buf) < 0)
1752 wpa_printf(MSG_INFO, "Could not set IMSI");
1753
1754 if (set_cred_quoted(ctx->ifname, id, "milenage",
1755 "90dca4eda45b53cf0f12d7c9c3bc6a89:"
1756 "cb9cccc4b9258e6dca4760379fb82581:000000000123") <
1757 0)
1758 wpa_printf(MSG_INFO, "Could not set Milenage parameters");
1759}
1760
1761
1762static void set_pps_cred_credential(struct hs20_osu_client *ctx, int id,
1763 xml_node_t *node, const char *fqdn)
1764{
1765 xml_node_t *child, *sim, *realm;
1766 const char *name;
1767
1768 wpa_printf(MSG_INFO, "- Credential");
1769
1770 sim = get_node(ctx->xml, node, "SIM");
1771 realm = get_node(ctx->xml, node, "Realm");
1772
1773 xml_node_for_each_child(ctx->xml, child, node) {
1774 xml_node_for_each_check(ctx->xml, child);
1775 name = xml_node_get_localname(ctx->xml, child);
1776 if (os_strcasecmp(name, "CreationDate") == 0)
1777 set_pps_cred_creation_date(ctx, id, child);
1778 else if (os_strcasecmp(name, "ExpirationDate") == 0)
1779 set_pps_cred_expiration_date(ctx, id, child);
1780 else if (os_strcasecmp(name, "UsernamePassword") == 0)
1781 set_pps_cred_username_password(ctx, id, child);
1782 else if (os_strcasecmp(name, "DigitalCertificate") == 0)
1783 set_pps_cred_digital_cert(ctx, id, child, fqdn);
1784 else if (os_strcasecmp(name, "Realm") == 0)
1785 set_pps_cred_realm(ctx, id, child, fqdn, sim != NULL);
1786 else if (os_strcasecmp(name, "CheckAAAServerCertStatus") == 0)
1787 set_pps_cred_check_aaa_cert_status(ctx, id, child);
1788 else if (os_strcasecmp(name, "SIM") == 0)
1789 set_pps_cred_sim(ctx, id, child, realm);
1790 else
1791 wpa_printf(MSG_INFO, "Unknown Credential node '%s'",
1792 name);
1793 }
1794}
1795
1796
1797static void set_pps_credential(struct hs20_osu_client *ctx, int id,
1798 xml_node_t *cred, const char *fqdn)
1799{
1800 xml_node_t *child;
1801 const char *name;
1802
1803 xml_node_for_each_child(ctx->xml, child, cred) {
1804 xml_node_for_each_check(ctx->xml, child);
1805 name = xml_node_get_localname(ctx->xml, child);
1806 if (os_strcasecmp(name, "Policy") == 0)
1807 set_pps_cred_policy(ctx, id, child);
1808 else if (os_strcasecmp(name, "CredentialPriority") == 0)
1809 set_pps_cred_priority(ctx, id, child);
1810 else if (os_strcasecmp(name, "AAAServerTrustRoot") == 0)
1811 set_pps_cred_aaa_server_trust_root(ctx, id, child);
1812 else if (os_strcasecmp(name, "SubscriptionUpdate") == 0)
1813 set_pps_cred_sub_update(ctx, id, child);
1814 else if (os_strcasecmp(name, "HomeSP") == 0)
1815 set_pps_cred_home_sp(ctx, id, child);
1816 else if (os_strcasecmp(name, "SubscriptionParameters") == 0)
1817 set_pps_cred_sub_params(ctx, id, child);
1818 else if (os_strcasecmp(name, "Credential") == 0)
1819 set_pps_cred_credential(ctx, id, child, fqdn);
1820 else
1821 wpa_printf(MSG_INFO, "Unknown credential node '%s'",
1822 name);
1823 }
1824}
1825
1826
1827static void set_pps(struct hs20_osu_client *ctx, xml_node_t *pps,
1828 const char *fqdn)
1829{
1830 xml_node_t *child;
1831 const char *name;
1832 int id;
1833 char *update_identifier = NULL;
1834
1835 /*
1836 * TODO: Could consider more complex mechanism that would remove
1837 * credentials only if there are changes in the information sent to
1838 * wpa_supplicant.
1839 */
1840 remove_sp_creds(ctx, fqdn);
1841
1842 xml_node_for_each_child(ctx->xml, child, pps) {
1843 xml_node_for_each_check(ctx->xml, child);
1844 name = xml_node_get_localname(ctx->xml, child);
1845 if (os_strcasecmp(name, "UpdateIdentifier") == 0) {
1846 update_identifier = xml_node_get_text(ctx->xml, child);
1847 if (update_identifier) {
1848 wpa_printf(MSG_INFO, "- UpdateIdentifier = %s",
1849 update_identifier);
1850 break;
1851 }
1852 }
1853 }
1854
1855 xml_node_for_each_child(ctx->xml, child, pps) {
1856 xml_node_for_each_check(ctx->xml, child);
1857 name = xml_node_get_localname(ctx->xml, child);
1858 if (os_strcasecmp(name, "UpdateIdentifier") == 0)
1859 continue;
1860 id = add_cred(ctx->ifname);
1861 if (id < 0) {
1862 wpa_printf(MSG_INFO, "Failed to add credential to wpa_supplicant");
1863 write_summary(ctx, "Failed to add credential to wpa_supplicant");
1864 break;
1865 }
1866 write_summary(ctx, "Add a credential to wpa_supplicant");
1867 if (update_identifier &&
1868 set_cred(ctx->ifname, id, "update_identifier",
1869 update_identifier) < 0)
1870 wpa_printf(MSG_INFO, "Failed to set update_identifier");
1871 if (set_cred_quoted(ctx->ifname, id, "provisioning_sp", fqdn) <
1872 0)
1873 wpa_printf(MSG_INFO, "Failed to set provisioning_sp");
1874 wpa_printf(MSG_INFO, "credential localname: '%s'", name);
1875 set_pps_credential(ctx, id, child, fqdn);
1876 ctx->pps_cred_set = 1;
1877 }
1878
1879 xml_node_get_text_free(ctx->xml, update_identifier);
1880}
1881
1882
1883void cmd_set_pps(struct hs20_osu_client *ctx, const char *pps_fname)
1884{
1885 xml_node_t *pps;
1886 const char *fqdn;
1887 char *fqdn_buf = NULL, *pos;
1888
1889 pps = node_from_file(ctx->xml, pps_fname);
1890 if (pps == NULL) {
1891 wpa_printf(MSG_INFO, "Could not read or parse '%s'", pps_fname);
1892 return;
1893 }
1894
1895 fqdn = os_strstr(pps_fname, "SP/");
1896 if (fqdn) {
1897 fqdn_buf = os_strdup(fqdn + 3);
1898 if (fqdn_buf == NULL)
1899 return;
1900 pos = os_strchr(fqdn_buf, '/');
1901 if (pos)
1902 *pos = '\0';
1903 fqdn = fqdn_buf;
1904 } else
1905 fqdn = "wi-fi.org";
1906
1907 wpa_printf(MSG_INFO, "Set PPS MO info to wpa_supplicant - SP FQDN %s",
1908 fqdn);
1909 set_pps(ctx, pps, fqdn);
1910
1911 os_free(fqdn_buf);
1912 xml_node_free(ctx->xml, pps);
1913}
1914
1915
1916static int cmd_get_fqdn(struct hs20_osu_client *ctx, const char *pps_fname)
1917{
1918 xml_node_t *pps, *node;
1919 char *fqdn = NULL;
1920
1921 pps = node_from_file(ctx->xml, pps_fname);
1922 if (pps == NULL) {
1923 wpa_printf(MSG_INFO, "Could not read or parse '%s'", pps_fname);
1924 return -1;
1925 }
1926
1927 node = get_child_node(ctx->xml, pps, "HomeSP/FQDN");
1928 if (node)
1929 fqdn = xml_node_get_text(ctx->xml, node);
1930
1931 xml_node_free(ctx->xml, pps);
1932
1933 if (fqdn) {
1934 FILE *f = fopen("pps-fqdn", "w");
1935 if (f) {
1936 fprintf(f, "%s", fqdn);
1937 fclose(f);
1938 }
1939 xml_node_get_text_free(ctx->xml, fqdn);
1940 return 0;
1941 }
1942
1943 xml_node_get_text_free(ctx->xml, fqdn);
1944 return -1;
1945}
1946
1947
1948static void cmd_to_tnds(struct hs20_osu_client *ctx, const char *in_fname,
1949 const char *out_fname, const char *urn, int use_path)
1950{
1951 xml_node_t *mo, *node;
1952
1953 mo = node_from_file(ctx->xml, in_fname);
1954 if (mo == NULL) {
1955 wpa_printf(MSG_INFO, "Could not read or parse '%s'", in_fname);
1956 return;
1957 }
1958
1959 node = mo_to_tnds(ctx->xml, mo, use_path, urn, NULL);
1960 if (node) {
1961 node_to_file(ctx->xml, out_fname, node);
1962 xml_node_free(ctx->xml, node);
1963 }
1964
1965 xml_node_free(ctx->xml, mo);
1966}
1967
1968
1969static void cmd_from_tnds(struct hs20_osu_client *ctx, const char *in_fname,
1970 const char *out_fname)
1971{
1972 xml_node_t *tnds, *mo;
1973
1974 tnds = node_from_file(ctx->xml, in_fname);
1975 if (tnds == NULL) {
1976 wpa_printf(MSG_INFO, "Could not read or parse '%s'", in_fname);
1977 return;
1978 }
1979
1980 mo = tnds_to_mo(ctx->xml, tnds);
1981 if (mo) {
1982 node_to_file(ctx->xml, out_fname, mo);
1983 xml_node_free(ctx->xml, mo);
1984 }
1985
1986 xml_node_free(ctx->xml, tnds);
1987}
1988
1989
1990struct osu_icon {
1991 int id;
1992 char lang[4];
1993 char mime_type[256];
1994 char filename[256];
1995};
1996
1997struct osu_data {
1998 char bssid[20];
1999 char url[256];
2000 unsigned int methods;
2001 char osu_ssid[33];
Hai Shalom39ba6fc2019-01-22 12:40:38 -08002002 char osu_ssid2[33];
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07002003 char osu_nai[256];
Hai Shalom39ba6fc2019-01-22 12:40:38 -08002004 char osu_nai2[256];
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07002005 struct osu_lang_text friendly_name[MAX_OSU_VALS];
2006 size_t friendly_name_count;
2007 struct osu_lang_text serv_desc[MAX_OSU_VALS];
2008 size_t serv_desc_count;
2009 struct osu_icon icon[MAX_OSU_VALS];
2010 size_t icon_count;
2011};
2012
2013
2014static struct osu_data * parse_osu_providers(const char *fname, size_t *count)
2015{
2016 FILE *f;
2017 char buf[1000];
2018 struct osu_data *osu = NULL, *last = NULL;
2019 size_t osu_count = 0;
2020 char *pos, *end;
Sunil8cd6f4d2022-06-28 18:40:46 +00002021 int res;
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07002022
2023 f = fopen(fname, "r");
2024 if (f == NULL) {
2025 wpa_printf(MSG_ERROR, "Could not open %s", fname);
2026 return NULL;
2027 }
2028
2029 while (fgets(buf, sizeof(buf), f)) {
2030 pos = strchr(buf, '\n');
2031 if (pos)
2032 *pos = '\0';
2033
2034 if (strncmp(buf, "OSU-PROVIDER ", 13) == 0) {
2035 last = realloc(osu, (osu_count + 1) * sizeof(*osu));
2036 if (last == NULL)
2037 break;
2038 osu = last;
2039 last = &osu[osu_count++];
2040 memset(last, 0, sizeof(*last));
Sunil8cd6f4d2022-06-28 18:40:46 +00002041 res = os_snprintf(last->bssid, sizeof(last->bssid),
2042 "%s", buf + 13);
2043 if (os_snprintf_error(sizeof(last->bssid), res))
2044 break;
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07002045 continue;
2046 }
2047 if (!last)
2048 continue;
2049
2050 if (strncmp(buf, "uri=", 4) == 0) {
Sunil8cd6f4d2022-06-28 18:40:46 +00002051 res = os_snprintf(last->url, sizeof(last->url),
2052 "%s", buf + 4);
2053 if (os_snprintf_error(sizeof(last->url), res))
2054 break;
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07002055 continue;
2056 }
2057
2058 if (strncmp(buf, "methods=", 8) == 0) {
2059 last->methods = strtol(buf + 8, NULL, 16);
2060 continue;
2061 }
2062
2063 if (strncmp(buf, "osu_ssid=", 9) == 0) {
Sunil8cd6f4d2022-06-28 18:40:46 +00002064 res = os_snprintf(last->osu_ssid,
2065 sizeof(last->osu_ssid),
2066 "%s", buf + 9);
2067 if (os_snprintf_error(sizeof(last->osu_ssid), res))
2068 break;
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07002069 continue;
2070 }
2071
Hai Shalom39ba6fc2019-01-22 12:40:38 -08002072 if (strncmp(buf, "osu_ssid2=", 10) == 0) {
Sunil8cd6f4d2022-06-28 18:40:46 +00002073 res = os_snprintf(last->osu_ssid2,
2074 sizeof(last->osu_ssid2),
2075 "%s", buf + 10);
2076 if (os_snprintf_error(sizeof(last->osu_ssid2), res))
2077 break;
Hai Shalom39ba6fc2019-01-22 12:40:38 -08002078 continue;
2079 }
2080
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07002081 if (os_strncmp(buf, "osu_nai=", 8) == 0) {
Sunil8cd6f4d2022-06-28 18:40:46 +00002082 res = os_snprintf(last->osu_nai, sizeof(last->osu_nai),
2083 "%s", buf + 8);
2084 if (os_snprintf_error(sizeof(last->osu_nai), res))
2085 break;
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07002086 continue;
2087 }
2088
Hai Shalom39ba6fc2019-01-22 12:40:38 -08002089 if (os_strncmp(buf, "osu_nai2=", 9) == 0) {
Sunil8cd6f4d2022-06-28 18:40:46 +00002090 res = os_snprintf(last->osu_nai2,
2091 sizeof(last->osu_nai2),
2092 "%s", buf + 9);
2093 if (os_snprintf_error(sizeof(last->osu_nai2), res))
2094 break;
Hai Shalom39ba6fc2019-01-22 12:40:38 -08002095 continue;
2096 }
2097
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07002098 if (strncmp(buf, "friendly_name=", 14) == 0) {
2099 struct osu_lang_text *txt;
2100 if (last->friendly_name_count == MAX_OSU_VALS)
2101 continue;
2102 pos = strchr(buf + 14, ':');
2103 if (pos == NULL)
2104 continue;
2105 *pos++ = '\0';
2106 txt = &last->friendly_name[last->friendly_name_count++];
Sunil8cd6f4d2022-06-28 18:40:46 +00002107 res = os_snprintf(txt->lang, sizeof(txt->lang),
2108 "%s", buf + 14);
2109 if (os_snprintf_error(sizeof(txt->lang), res))
2110 break;
2111 res = os_snprintf(txt->text, sizeof(txt->text),
2112 "%s", pos);
2113 if (os_snprintf_error(sizeof(txt->text), res))
2114 break;
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07002115 }
2116
2117 if (strncmp(buf, "desc=", 5) == 0) {
2118 struct osu_lang_text *txt;
2119 if (last->serv_desc_count == MAX_OSU_VALS)
2120 continue;
2121 pos = strchr(buf + 5, ':');
2122 if (pos == NULL)
2123 continue;
2124 *pos++ = '\0';
2125 txt = &last->serv_desc[last->serv_desc_count++];
Sunil8cd6f4d2022-06-28 18:40:46 +00002126 res = os_snprintf(txt->lang, sizeof(txt->lang),
2127 "%s", buf + 5);
2128 if (os_snprintf_error(sizeof(txt->lang), res))
2129 break;
2130 res = os_snprintf(txt->text, sizeof(txt->text),
2131 "%s", pos);
2132 if (os_snprintf_error(sizeof(txt->text), res))
2133 break;
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07002134 }
2135
2136 if (strncmp(buf, "icon=", 5) == 0) {
2137 struct osu_icon *icon;
2138 if (last->icon_count == MAX_OSU_VALS)
2139 continue;
2140 icon = &last->icon[last->icon_count++];
2141 icon->id = atoi(buf + 5);
2142 pos = strchr(buf, ':');
2143 if (pos == NULL)
2144 continue;
2145 pos = strchr(pos + 1, ':');
2146 if (pos == NULL)
2147 continue;
2148 pos = strchr(pos + 1, ':');
2149 if (pos == NULL)
2150 continue;
2151 pos++;
2152 end = strchr(pos, ':');
2153 if (!end)
2154 continue;
2155 *end = '\0';
Sunil8cd6f4d2022-06-28 18:40:46 +00002156 res = os_snprintf(icon->lang, sizeof(icon->lang),
2157 "%s", pos);
2158 if (os_snprintf_error(sizeof(icon->lang), res))
2159 break;
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07002160 pos = end + 1;
2161
2162 end = strchr(pos, ':');
2163 if (end)
2164 *end = '\0';
Sunil8cd6f4d2022-06-28 18:40:46 +00002165 res = os_snprintf(icon->mime_type,
2166 sizeof(icon->mime_type), "%s", pos);
2167 if (os_snprintf_error(sizeof(icon->mime_type), res))
2168 break;
2169 if (!end)
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07002170 continue;
2171 pos = end + 1;
2172
2173 end = strchr(pos, ':');
2174 if (end)
2175 *end = '\0';
Sunil8cd6f4d2022-06-28 18:40:46 +00002176 res = os_snprintf(icon->filename,
2177 sizeof(icon->filename), "%s", pos);
2178 if (os_snprintf_error(sizeof(icon->filename), res))
2179 break;
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07002180 continue;
2181 }
2182 }
2183
2184 fclose(f);
2185
2186 *count = osu_count;
2187 return osu;
2188}
2189
2190
2191static int osu_connect(struct hs20_osu_client *ctx, const char *bssid,
Hai Shalom39ba6fc2019-01-22 12:40:38 -08002192 const char *ssid, const char *ssid2, const char *url,
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07002193 unsigned int methods, int no_prod_assoc,
Hai Shalom39ba6fc2019-01-22 12:40:38 -08002194 const char *osu_nai, const char *osu_nai2)
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07002195{
2196 int id;
2197 const char *ifname = ctx->ifname;
2198 char buf[200];
2199 struct wpa_ctrl *mon;
2200 int res;
2201
Hai Shalom39ba6fc2019-01-22 12:40:38 -08002202 if (ssid2 && ssid2[0] == '\0')
2203 ssid2 = NULL;
2204
2205 if (ctx->osu_ssid) {
2206 if (os_strcmp(ssid, ctx->osu_ssid) == 0) {
2207 wpa_printf(MSG_DEBUG,
2208 "Enforced OSU SSID matches ANQP info");
2209 ssid2 = NULL;
2210 } else if (ssid2 && os_strcmp(ssid2, ctx->osu_ssid) == 0) {
2211 wpa_printf(MSG_DEBUG,
2212 "Enforced OSU SSID matches RSN[OSEN] info");
2213 ssid = ssid2;
2214 } else {
2215 wpa_printf(MSG_INFO, "Enforced OSU SSID did not match");
2216 write_summary(ctx, "Enforced OSU SSID did not match");
2217 return -1;
2218 }
2219 }
2220
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07002221 id = add_network(ifname);
2222 if (id < 0)
2223 return -1;
2224 if (set_network_quoted(ifname, id, "ssid", ssid) < 0)
2225 return -1;
Hai Shalom39ba6fc2019-01-22 12:40:38 -08002226 if (ssid2)
2227 osu_nai = osu_nai2;
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07002228 if (osu_nai && os_strlen(osu_nai) > 0) {
2229 char dir[255], fname[300];
2230 if (getcwd(dir, sizeof(dir)) == NULL)
2231 return -1;
2232 os_snprintf(fname, sizeof(fname), "%s/osu-ca.pem", dir);
2233
Hai Shalom39ba6fc2019-01-22 12:40:38 -08002234 if (ssid2 && set_network_quoted(ifname, id, "ssid", ssid2) < 0)
2235 return -1;
2236
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07002237 if (set_network(ifname, id, "proto", "OSEN") < 0 ||
2238 set_network(ifname, id, "key_mgmt", "OSEN") < 0 ||
2239 set_network(ifname, id, "pairwise", "CCMP") < 0 ||
Hai Shalomc9e41a12018-07-31 14:41:42 -07002240 set_network(ifname, id, "group", "GTK_NOT_USED CCMP") < 0 ||
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07002241 set_network(ifname, id, "eap", "WFA-UNAUTH-TLS") < 0 ||
2242 set_network(ifname, id, "ocsp", "2") < 0 ||
2243 set_network_quoted(ifname, id, "identity", osu_nai) < 0 ||
2244 set_network_quoted(ifname, id, "ca_cert", fname) < 0)
2245 return -1;
Hai Shalom39ba6fc2019-01-22 12:40:38 -08002246 } else if (ssid2) {
2247 wpa_printf(MSG_INFO, "No OSU_NAI set for RSN[OSEN]");
2248 write_summary(ctx, "No OSU_NAI set for RSN[OSEN]");
2249 return -1;
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07002250 } else {
2251 if (set_network(ifname, id, "key_mgmt", "NONE") < 0)
2252 return -1;
2253 }
2254
2255 mon = open_wpa_mon(ifname);
2256 if (mon == NULL)
2257 return -1;
2258
2259 wpa_printf(MSG_INFO, "Associate with OSU SSID");
2260 write_summary(ctx, "Associate with OSU SSID");
2261 snprintf(buf, sizeof(buf), "SELECT_NETWORK %d", id);
2262 if (wpa_command(ifname, buf) < 0)
2263 return -1;
2264
2265 res = get_wpa_cli_event(mon, "CTRL-EVENT-CONNECTED",
2266 buf, sizeof(buf));
2267
2268 wpa_ctrl_detach(mon);
2269 wpa_ctrl_close(mon);
2270
2271 if (res < 0) {
Hai Shalomfdcde762020-04-02 11:19:20 -07002272 wpa_printf(MSG_INFO, "Could not connect to OSU network");
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07002273 write_summary(ctx, "Could not connect to OSU network");
2274 wpa_printf(MSG_INFO, "Remove OSU network connection");
2275 snprintf(buf, sizeof(buf), "REMOVE_NETWORK %d", id);
2276 wpa_command(ifname, buf);
2277 return -1;
2278 }
2279
2280 write_summary(ctx, "Waiting for IP address for subscription registration");
2281 if (wait_ip_addr(ifname, 15) < 0) {
2282 wpa_printf(MSG_INFO, "Could not get IP address for WLAN - try connection anyway");
2283 }
2284
2285 if (no_prod_assoc) {
2286 if (res < 0)
2287 return -1;
2288 wpa_printf(MSG_INFO, "No production connection used for testing purposes");
2289 write_summary(ctx, "No production connection used for testing purposes");
2290 return 0;
2291 }
2292
2293 ctx->no_reconnect = 1;
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07002294 if (methods & 0x02) {
2295 wpa_printf(MSG_DEBUG, "Calling cmd_prov from osu_connect");
Dmitry Shmidt6cb1f652014-03-21 10:54:03 -07002296 res = cmd_prov(ctx, url);
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07002297 } else if (methods & 0x01) {
2298 wpa_printf(MSG_DEBUG,
2299 "Calling cmd_oma_dm_prov from osu_connect");
Dmitry Shmidt6cb1f652014-03-21 10:54:03 -07002300 res = cmd_oma_dm_prov(ctx, url);
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07002301 }
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07002302
2303 wpa_printf(MSG_INFO, "Remove OSU network connection");
2304 write_summary(ctx, "Remove OSU network connection");
2305 snprintf(buf, sizeof(buf), "REMOVE_NETWORK %d", id);
2306 wpa_command(ifname, buf);
2307
2308 if (res < 0)
2309 return -1;
2310
2311 wpa_printf(MSG_INFO, "Requesting reconnection with updated configuration");
2312 write_summary(ctx, "Requesting reconnection with updated configuration");
2313 if (wpa_command(ctx->ifname, "INTERWORKING_SELECT auto") < 0) {
2314 wpa_printf(MSG_INFO, "Failed to request wpa_supplicant to reconnect");
2315 write_summary(ctx, "Failed to request wpa_supplicant to reconnect");
2316 return -1;
2317 }
2318
2319 return 0;
2320}
2321
2322
2323static int cmd_osu_select(struct hs20_osu_client *ctx, const char *dir,
Dmitry Shmidt6cb1f652014-03-21 10:54:03 -07002324 int connect, int no_prod_assoc,
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07002325 const char *friendly_name)
2326{
2327 char fname[255];
2328 FILE *f;
2329 struct osu_data *osu = NULL, *last = NULL;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08002330 size_t osu_count = 0, i, j;
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07002331 int ret;
2332
2333 write_summary(ctx, "OSU provider selection");
2334
2335 if (dir == NULL) {
2336 wpa_printf(MSG_INFO, "Missing dir parameter to osu_select");
2337 return -1;
2338 }
2339
2340 snprintf(fname, sizeof(fname), "%s/osu-providers.txt", dir);
2341 osu = parse_osu_providers(fname, &osu_count);
2342 if (osu == NULL) {
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07002343 wpa_printf(MSG_INFO, "Could not find any OSU providers from %s",
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07002344 fname);
2345 write_result(ctx, "No OSU providers available");
2346 return -1;
2347 }
2348
2349 if (friendly_name) {
2350 for (i = 0; i < osu_count; i++) {
2351 last = &osu[i];
2352 for (j = 0; j < last->friendly_name_count; j++) {
2353 if (os_strcmp(last->friendly_name[j].text,
2354 friendly_name) == 0)
2355 break;
2356 }
2357 if (j < last->friendly_name_count)
2358 break;
2359 }
2360 if (i == osu_count) {
2361 wpa_printf(MSG_INFO, "Requested operator friendly name '%s' not found in the list of available providers",
2362 friendly_name);
2363 write_summary(ctx, "Requested operator friendly name '%s' not found in the list of available providers",
2364 friendly_name);
2365 free(osu);
2366 return -1;
2367 }
2368
2369 wpa_printf(MSG_INFO, "OSU Provider selected based on requested operator friendly name '%s'",
2370 friendly_name);
2371 write_summary(ctx, "OSU Provider selected based on requested operator friendly name '%s'",
2372 friendly_name);
2373 ret = i + 1;
2374 goto selected;
2375 }
2376
2377 snprintf(fname, sizeof(fname), "%s/osu-providers.html", dir);
2378 f = fopen(fname, "w");
2379 if (f == NULL) {
2380 wpa_printf(MSG_INFO, "Could not open %s", fname);
2381 free(osu);
2382 return -1;
2383 }
2384
2385 fprintf(f, "<html><head>"
2386 "<meta http-equiv=\"Content-type\" content=\"text/html; "
2387 "charset=utf-8\"<title>Select service operator</title>"
2388 "</head><body><h1>Select service operator</h1>\n");
2389
2390 if (osu_count == 0)
2391 fprintf(f, "No online signup available\n");
2392
2393 for (i = 0; i < osu_count; i++) {
2394 last = &osu[i];
2395#ifdef ANDROID
2396 fprintf(f, "<p>\n"
2397 "<a href=\"http://localhost:12345/osu/%d\">"
2398 "<table><tr><td>", (int) i + 1);
2399#else /* ANDROID */
2400 fprintf(f, "<p>\n"
2401 "<a href=\"osu://%d\">"
2402 "<table><tr><td>", (int) i + 1);
2403#endif /* ANDROID */
2404 for (j = 0; j < last->icon_count; j++) {
2405 fprintf(f, "<img src=\"osu-icon-%d.%s\">\n",
2406 last->icon[j].id,
2407 strcasecmp(last->icon[j].mime_type,
2408 "image/png") == 0 ? "png" : "icon");
2409 }
2410 fprintf(f, "<td>");
2411 for (j = 0; j < last->friendly_name_count; j++) {
2412 fprintf(f, "<small>[%s]</small> %s<br>\n",
2413 last->friendly_name[j].lang,
2414 last->friendly_name[j].text);
2415 }
2416 fprintf(f, "<tr><td colspan=2>");
2417 for (j = 0; j < last->serv_desc_count; j++) {
2418 fprintf(f, "<small>[%s]</small> %s<br>\n",
2419 last->serv_desc[j].lang,
2420 last->serv_desc[j].text);
2421 }
2422 fprintf(f, "</table></a><br><small>BSSID: %s<br>\n"
2423 "SSID: %s<br>\n",
2424 last->bssid, last->osu_ssid);
Hai Shalom39ba6fc2019-01-22 12:40:38 -08002425 if (last->osu_ssid2[0])
2426 fprintf(f, "SSID2: %s<br>\n", last->osu_ssid2);
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08002427 if (last->osu_nai[0])
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07002428 fprintf(f, "NAI: %s<br>\n", last->osu_nai);
Hai Shalom39ba6fc2019-01-22 12:40:38 -08002429 if (last->osu_nai2[0])
2430 fprintf(f, "NAI2: %s<br>\n", last->osu_nai2);
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07002431 fprintf(f, "URL: %s<br>\n"
2432 "methods:%s%s<br>\n"
2433 "</small></p>\n",
2434 last->url,
2435 last->methods & 0x01 ? " OMA-DM" : "",
2436 last->methods & 0x02 ? " SOAP-XML-SPP" : "");
2437 }
2438
2439 fprintf(f, "</body></html>\n");
2440
2441 fclose(f);
2442
2443 snprintf(fname, sizeof(fname), "file://%s/osu-providers.html", dir);
2444 write_summary(ctx, "Start web browser with OSU provider selection page");
Hai Shalomfdcde762020-04-02 11:19:20 -07002445 ret = hs20_web_browser(fname, 0);
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07002446
2447selected:
2448 if (ret > 0 && (size_t) ret <= osu_count) {
2449 char *data;
2450 size_t data_len;
2451
2452 wpa_printf(MSG_INFO, "Selected OSU id=%d", ret);
2453 last = &osu[ret - 1];
2454 ret = 0;
2455 wpa_printf(MSG_INFO, "BSSID: %s", last->bssid);
2456 wpa_printf(MSG_INFO, "SSID: %s", last->osu_ssid);
Hai Shalom39ba6fc2019-01-22 12:40:38 -08002457 if (last->osu_ssid2[0])
2458 wpa_printf(MSG_INFO, "SSID2: %s", last->osu_ssid2);
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07002459 wpa_printf(MSG_INFO, "URL: %s", last->url);
2460 write_summary(ctx, "Selected OSU provider id=%d BSSID=%s SSID=%s URL=%s",
2461 ret, last->bssid, last->osu_ssid, last->url);
2462
2463 ctx->friendly_name_count = last->friendly_name_count;
2464 for (j = 0; j < last->friendly_name_count; j++) {
2465 wpa_printf(MSG_INFO, "FRIENDLY_NAME: [%s]%s",
2466 last->friendly_name[j].lang,
2467 last->friendly_name[j].text);
2468 os_strlcpy(ctx->friendly_name[j].lang,
2469 last->friendly_name[j].lang,
2470 sizeof(ctx->friendly_name[j].lang));
2471 os_strlcpy(ctx->friendly_name[j].text,
2472 last->friendly_name[j].text,
2473 sizeof(ctx->friendly_name[j].text));
2474 }
2475
2476 ctx->icon_count = last->icon_count;
2477 for (j = 0; j < last->icon_count; j++) {
2478 char fname[256];
2479
2480 os_snprintf(fname, sizeof(fname), "%s/osu-icon-%d.%s",
2481 dir, last->icon[j].id,
2482 strcasecmp(last->icon[j].mime_type,
2483 "image/png") == 0 ?
2484 "png" : "icon");
2485 wpa_printf(MSG_INFO, "ICON: %s (%s)",
2486 fname, last->icon[j].filename);
2487 os_strlcpy(ctx->icon_filename[j],
2488 last->icon[j].filename,
2489 sizeof(ctx->icon_filename[j]));
2490
2491 data = os_readfile(fname, &data_len);
2492 if (data) {
2493 sha256_vector(1, (const u8 **) &data, &data_len,
2494 ctx->icon_hash[j]);
2495 os_free(data);
2496 }
2497 }
2498
2499 if (connect == 2) {
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07002500 if (last->methods & 0x02) {
2501 wpa_printf(MSG_DEBUG,
2502 "Calling cmd_prov from cmd_osu_select");
Dmitry Shmidt6cb1f652014-03-21 10:54:03 -07002503 ret = cmd_prov(ctx, last->url);
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07002504 } else if (last->methods & 0x01) {
2505 wpa_printf(MSG_DEBUG,
2506 "Calling cmd_oma_dm_prov from cmd_osu_select");
Dmitry Shmidt6cb1f652014-03-21 10:54:03 -07002507 ret = cmd_oma_dm_prov(ctx, last->url);
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07002508 } else {
2509 wpa_printf(MSG_DEBUG,
2510 "No supported OSU provisioning method");
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07002511 ret = -1;
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07002512 }
Hai Shalom39ba6fc2019-01-22 12:40:38 -08002513 } else if (connect) {
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07002514 ret = osu_connect(ctx, last->bssid, last->osu_ssid,
Hai Shalom39ba6fc2019-01-22 12:40:38 -08002515 last->osu_ssid2,
Dmitry Shmidt6cb1f652014-03-21 10:54:03 -07002516 last->url, last->methods,
Hai Shalom39ba6fc2019-01-22 12:40:38 -08002517 no_prod_assoc, last->osu_nai,
2518 last->osu_nai2);
2519 }
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07002520 } else
2521 ret = -1;
2522
2523 free(osu);
2524
2525 return ret;
2526}
2527
2528
Dmitry Shmidt6cb1f652014-03-21 10:54:03 -07002529static int cmd_signup(struct hs20_osu_client *ctx, int no_prod_assoc,
2530 const char *friendly_name)
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07002531{
2532 char dir[255];
2533 char fname[300], buf[400];
2534 struct wpa_ctrl *mon;
2535 const char *ifname;
2536 int res;
2537
2538 ifname = ctx->ifname;
2539
2540 if (getcwd(dir, sizeof(dir)) == NULL)
2541 return -1;
2542
2543 snprintf(fname, sizeof(fname), "%s/osu-info", dir);
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002544 if (mkdir(fname, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) < 0 &&
2545 errno != EEXIST) {
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07002546 wpa_printf(MSG_INFO, "mkdir(%s) failed: %s",
2547 fname, strerror(errno));
2548 return -1;
2549 }
2550
Roshan Pius3a1667e2018-07-03 15:17:14 -07002551 android_update_permission(fname, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002552
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07002553 snprintf(buf, sizeof(buf), "SET osu_dir %s", fname);
2554 if (wpa_command(ifname, buf) < 0) {
2555 wpa_printf(MSG_INFO, "Failed to configure osu_dir to wpa_supplicant");
2556 return -1;
2557 }
2558
2559 mon = open_wpa_mon(ifname);
2560 if (mon == NULL)
2561 return -1;
2562
2563 wpa_printf(MSG_INFO, "Starting OSU fetch");
2564 write_summary(ctx, "Starting OSU provider information fetch");
2565 if (wpa_command(ifname, "FETCH_OSU") < 0) {
2566 wpa_printf(MSG_INFO, "Could not start OSU fetch");
2567 wpa_ctrl_detach(mon);
2568 wpa_ctrl_close(mon);
2569 return -1;
2570 }
2571 res = get_wpa_cli_event(mon, "OSU provider fetch completed",
2572 buf, sizeof(buf));
2573
2574 wpa_ctrl_detach(mon);
2575 wpa_ctrl_close(mon);
2576
2577 if (res < 0) {
2578 wpa_printf(MSG_INFO, "OSU fetch did not complete");
2579 write_summary(ctx, "OSU fetch did not complete");
2580 return -1;
2581 }
2582 wpa_printf(MSG_INFO, "OSU provider fetch completed");
2583
Dmitry Shmidt6cb1f652014-03-21 10:54:03 -07002584 return cmd_osu_select(ctx, fname, 1, no_prod_assoc, friendly_name);
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07002585}
2586
2587
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002588static int cmd_sub_rem(struct hs20_osu_client *ctx, const char *address,
2589 const char *pps_fname, const char *ca_fname)
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07002590{
2591 xml_node_t *pps, *node;
2592 char pps_fname_buf[300];
2593 char ca_fname_buf[200];
2594 char *cred_username = NULL;
2595 char *cred_password = NULL;
2596 char *sub_rem_uri = NULL;
2597 char client_cert_buf[200];
2598 char *client_cert = NULL;
2599 char client_key_buf[200];
2600 char *client_key = NULL;
2601 int spp;
2602
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07002603 wpa_printf(MSG_INFO, "Subscription remediation requested with Server URL: %s",
2604 address);
2605
2606 if (!pps_fname) {
2607 char buf[256];
2608 wpa_printf(MSG_INFO, "Determining PPS file based on Home SP information");
2609 if (os_strncmp(address, "fqdn=", 5) == 0) {
2610 wpa_printf(MSG_INFO, "Use requested FQDN from command line");
2611 os_snprintf(buf, sizeof(buf), "%s", address + 5);
2612 address = NULL;
2613 } else if (get_wpa_status(ctx->ifname, "provisioning_sp", buf,
2614 sizeof(buf)) < 0) {
2615 wpa_printf(MSG_INFO, "Could not get provisioning Home SP FQDN from wpa_supplicant");
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002616 return -1;
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07002617 }
2618 os_free(ctx->fqdn);
2619 ctx->fqdn = os_strdup(buf);
2620 if (ctx->fqdn == NULL)
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002621 return -1;
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07002622 wpa_printf(MSG_INFO, "Home SP FQDN for current credential: %s",
2623 buf);
2624 os_snprintf(pps_fname_buf, sizeof(pps_fname_buf),
2625 "SP/%s/pps.xml", ctx->fqdn);
2626 pps_fname = pps_fname_buf;
2627
2628 os_snprintf(ca_fname_buf, sizeof(ca_fname_buf), "SP/%s/ca.pem",
2629 ctx->fqdn);
2630 ca_fname = ca_fname_buf;
2631 }
2632
2633 if (!os_file_exists(pps_fname)) {
2634 wpa_printf(MSG_INFO, "PPS file '%s' does not exist or is not accessible",
2635 pps_fname);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002636 return -1;
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07002637 }
2638 wpa_printf(MSG_INFO, "Using PPS file: %s", pps_fname);
2639
2640 if (ca_fname && !os_file_exists(ca_fname)) {
2641 wpa_printf(MSG_INFO, "CA file '%s' does not exist or is not accessible",
2642 ca_fname);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002643 return -1;
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07002644 }
2645 wpa_printf(MSG_INFO, "Using server trust root: %s", ca_fname);
Dmitry Shmidt6cb1f652014-03-21 10:54:03 -07002646 ctx->ca_fname = ca_fname;
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07002647
2648 pps = node_from_file(ctx->xml, pps_fname);
2649 if (pps == NULL) {
2650 wpa_printf(MSG_INFO, "Could not read PPS MO");
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002651 return -1;
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07002652 }
2653
2654 if (!ctx->fqdn) {
2655 char *tmp;
2656 node = get_child_node(ctx->xml, pps, "HomeSP/FQDN");
2657 if (node == NULL) {
2658 wpa_printf(MSG_INFO, "No HomeSP/FQDN found from PPS");
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002659 return -1;
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07002660 }
2661 tmp = xml_node_get_text(ctx->xml, node);
2662 if (tmp == NULL) {
2663 wpa_printf(MSG_INFO, "No HomeSP/FQDN text found from PPS");
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002664 return -1;
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07002665 }
2666 ctx->fqdn = os_strdup(tmp);
2667 xml_node_get_text_free(ctx->xml, tmp);
2668 if (!ctx->fqdn) {
2669 wpa_printf(MSG_INFO, "No FQDN known");
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002670 return -1;
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07002671 }
2672 }
2673
2674 node = get_child_node(ctx->xml, pps,
2675 "SubscriptionUpdate/UpdateMethod");
2676 if (node) {
2677 char *tmp;
2678 tmp = xml_node_get_text(ctx->xml, node);
2679 if (tmp && os_strcasecmp(tmp, "OMA-DM-ClientInitiated") == 0)
2680 spp = 0;
2681 else
2682 spp = 1;
2683 } else {
2684 wpa_printf(MSG_INFO, "No UpdateMethod specified - assume SPP");
2685 spp = 1;
2686 }
2687
2688 get_user_pw(ctx, pps, "SubscriptionUpdate/UsernamePassword",
2689 &cred_username, &cred_password);
2690 if (cred_username)
2691 wpa_printf(MSG_INFO, "Using username: %s", cred_username);
2692 if (cred_password)
2693 wpa_printf(MSG_DEBUG, "Using password: %s", cred_password);
2694
2695 if (cred_username == NULL && cred_password == NULL &&
2696 get_child_node(ctx->xml, pps, "Credential/DigitalCertificate")) {
2697 wpa_printf(MSG_INFO, "Using client certificate");
2698 os_snprintf(client_cert_buf, sizeof(client_cert_buf),
2699 "SP/%s/client-cert.pem", ctx->fqdn);
2700 client_cert = client_cert_buf;
2701 os_snprintf(client_key_buf, sizeof(client_key_buf),
2702 "SP/%s/client-key.pem", ctx->fqdn);
2703 client_key = client_key_buf;
2704 ctx->client_cert_present = 1;
2705 }
2706
2707 node = get_child_node(ctx->xml, pps, "SubscriptionUpdate/URI");
2708 if (node) {
2709 sub_rem_uri = xml_node_get_text(ctx->xml, node);
2710 if (sub_rem_uri &&
2711 (!address || os_strcmp(address, sub_rem_uri) != 0)) {
2712 wpa_printf(MSG_INFO, "Override sub rem URI based on PPS: %s",
2713 sub_rem_uri);
2714 address = sub_rem_uri;
2715 }
2716 }
2717 if (!address) {
2718 wpa_printf(MSG_INFO, "Server URL not known");
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002719 return -1;
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07002720 }
2721
2722 write_summary(ctx, "Wait for IP address for subscriptiom remediation");
2723 wpa_printf(MSG_INFO, "Wait for IP address before starting subscription remediation");
2724
2725 if (wait_ip_addr(ctx->ifname, 15) < 0) {
2726 wpa_printf(MSG_INFO, "Could not get IP address for WLAN - try connection anyway");
2727 }
2728
2729 if (spp)
Dmitry Shmidt6cb1f652014-03-21 10:54:03 -07002730 spp_sub_rem(ctx, address, pps_fname,
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07002731 client_cert, client_key,
2732 cred_username, cred_password, pps);
2733 else
Dmitry Shmidt6cb1f652014-03-21 10:54:03 -07002734 oma_dm_sub_rem(ctx, address, pps_fname,
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07002735 client_cert, client_key,
2736 cred_username, cred_password, pps);
2737
2738 xml_node_get_text_free(ctx->xml, sub_rem_uri);
2739 xml_node_get_text_free(ctx->xml, cred_username);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002740 str_clear_free(cred_password);
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07002741 xml_node_free(ctx->xml, pps);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002742 return 0;
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07002743}
2744
2745
2746static int cmd_pol_upd(struct hs20_osu_client *ctx, const char *address,
2747 const char *pps_fname, const char *ca_fname)
2748{
2749 xml_node_t *pps;
2750 xml_node_t *node;
2751 char pps_fname_buf[300];
2752 char ca_fname_buf[200];
2753 char *uri = NULL;
2754 char *cred_username = NULL;
2755 char *cred_password = NULL;
2756 char client_cert_buf[200];
2757 char *client_cert = NULL;
2758 char client_key_buf[200];
2759 char *client_key = NULL;
2760 int spp;
2761
2762 wpa_printf(MSG_INFO, "Policy update requested");
2763
2764 if (!pps_fname) {
2765 char buf[256];
Hai Shalom81f62d82019-07-22 12:10:00 -07002766 int res;
2767
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07002768 wpa_printf(MSG_INFO, "Determining PPS file based on Home SP information");
Dmitry Shmidte4663042016-04-04 10:07:49 -07002769 if (address && os_strncmp(address, "fqdn=", 5) == 0) {
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07002770 wpa_printf(MSG_INFO, "Use requested FQDN from command line");
2771 os_snprintf(buf, sizeof(buf), "%s", address + 5);
2772 address = NULL;
2773 } else if (get_wpa_status(ctx->ifname, "provisioning_sp", buf,
2774 sizeof(buf)) < 0) {
2775 wpa_printf(MSG_INFO, "Could not get provisioning Home SP FQDN from wpa_supplicant");
2776 return -1;
2777 }
2778 os_free(ctx->fqdn);
2779 ctx->fqdn = os_strdup(buf);
2780 if (ctx->fqdn == NULL)
2781 return -1;
2782 wpa_printf(MSG_INFO, "Home SP FQDN for current credential: %s",
2783 buf);
2784 os_snprintf(pps_fname_buf, sizeof(pps_fname_buf),
2785 "SP/%s/pps.xml", ctx->fqdn);
2786 pps_fname = pps_fname_buf;
2787
Hai Shalom81f62d82019-07-22 12:10:00 -07002788 res = os_snprintf(ca_fname_buf, sizeof(ca_fname_buf),
2789 "SP/%s/ca.pem", buf);
2790 if (os_snprintf_error(sizeof(ca_fname_buf), res)) {
2791 os_free(ctx->fqdn);
2792 ctx->fqdn = NULL;
2793 return -1;
2794 }
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07002795 ca_fname = ca_fname_buf;
2796 }
2797
2798 if (!os_file_exists(pps_fname)) {
2799 wpa_printf(MSG_INFO, "PPS file '%s' does not exist or is not accessible",
2800 pps_fname);
2801 return -1;
2802 }
2803 wpa_printf(MSG_INFO, "Using PPS file: %s", pps_fname);
2804
2805 if (ca_fname && !os_file_exists(ca_fname)) {
2806 wpa_printf(MSG_INFO, "CA file '%s' does not exist or is not accessible",
2807 ca_fname);
2808 return -1;
2809 }
2810 wpa_printf(MSG_INFO, "Using server trust root: %s", ca_fname);
Dmitry Shmidt6cb1f652014-03-21 10:54:03 -07002811 ctx->ca_fname = ca_fname;
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07002812
2813 pps = node_from_file(ctx->xml, pps_fname);
2814 if (pps == NULL) {
2815 wpa_printf(MSG_INFO, "Could not read PPS MO");
2816 return -1;
2817 }
2818
2819 if (!ctx->fqdn) {
2820 char *tmp;
2821 node = get_child_node(ctx->xml, pps, "HomeSP/FQDN");
2822 if (node == NULL) {
2823 wpa_printf(MSG_INFO, "No HomeSP/FQDN found from PPS");
2824 return -1;
2825 }
2826 tmp = xml_node_get_text(ctx->xml, node);
2827 if (tmp == NULL) {
2828 wpa_printf(MSG_INFO, "No HomeSP/FQDN text found from PPS");
2829 return -1;
2830 }
2831 ctx->fqdn = os_strdup(tmp);
2832 xml_node_get_text_free(ctx->xml, tmp);
2833 if (!ctx->fqdn) {
2834 wpa_printf(MSG_INFO, "No FQDN known");
2835 return -1;
2836 }
2837 }
2838
2839 node = get_child_node(ctx->xml, pps,
2840 "Policy/PolicyUpdate/UpdateMethod");
2841 if (node) {
2842 char *tmp;
2843 tmp = xml_node_get_text(ctx->xml, node);
2844 if (tmp && os_strcasecmp(tmp, "OMA-DM-ClientInitiated") == 0)
2845 spp = 0;
2846 else
2847 spp = 1;
2848 } else {
2849 wpa_printf(MSG_INFO, "No UpdateMethod specified - assume SPP");
2850 spp = 1;
2851 }
2852
2853 get_user_pw(ctx, pps, "Policy/PolicyUpdate/UsernamePassword",
2854 &cred_username, &cred_password);
2855 if (cred_username)
2856 wpa_printf(MSG_INFO, "Using username: %s", cred_username);
2857 if (cred_password)
2858 wpa_printf(MSG_DEBUG, "Using password: %s", cred_password);
2859
2860 if (cred_username == NULL && cred_password == NULL &&
2861 get_child_node(ctx->xml, pps, "Credential/DigitalCertificate")) {
2862 wpa_printf(MSG_INFO, "Using client certificate");
2863 os_snprintf(client_cert_buf, sizeof(client_cert_buf),
2864 "SP/%s/client-cert.pem", ctx->fqdn);
2865 client_cert = client_cert_buf;
2866 os_snprintf(client_key_buf, sizeof(client_key_buf),
2867 "SP/%s/client-key.pem", ctx->fqdn);
2868 client_key = client_key_buf;
2869 }
2870
2871 if (!address) {
2872 node = get_child_node(ctx->xml, pps, "Policy/PolicyUpdate/URI");
2873 if (node) {
2874 uri = xml_node_get_text(ctx->xml, node);
2875 wpa_printf(MSG_INFO, "URI based on PPS: %s", uri);
2876 address = uri;
2877 }
2878 }
2879 if (!address) {
2880 wpa_printf(MSG_INFO, "Server URL not known");
2881 return -1;
2882 }
2883
2884 if (spp)
Dmitry Shmidt6cb1f652014-03-21 10:54:03 -07002885 spp_pol_upd(ctx, address, pps_fname,
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07002886 client_cert, client_key,
2887 cred_username, cred_password, pps);
2888 else
Dmitry Shmidt6cb1f652014-03-21 10:54:03 -07002889 oma_dm_pol_upd(ctx, address, pps_fname,
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07002890 client_cert, client_key,
2891 cred_username, cred_password, pps);
2892
2893 xml_node_get_text_free(ctx->xml, uri);
2894 xml_node_get_text_free(ctx->xml, cred_username);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002895 str_clear_free(cred_password);
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07002896 xml_node_free(ctx->xml, pps);
2897
2898 return 0;
2899}
2900
2901
2902static char * get_hostname(const char *url)
2903{
2904 const char *pos, *end, *end2;
2905 char *ret;
2906
2907 if (url == NULL)
2908 return NULL;
2909
2910 pos = os_strchr(url, '/');
2911 if (pos == NULL)
2912 return NULL;
2913 pos++;
2914 if (*pos != '/')
2915 return NULL;
2916 pos++;
2917
2918 end = os_strchr(pos, '/');
2919 end2 = os_strchr(pos, ':');
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07002920 if ((end && end2 && end2 < end) || (!end && end2))
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07002921 end = end2;
2922 if (end)
2923 end--;
2924 else {
2925 end = pos;
2926 while (*end)
2927 end++;
2928 if (end > pos)
2929 end--;
2930 }
2931
2932 ret = os_malloc(end - pos + 2);
2933 if (ret == NULL)
2934 return NULL;
2935
2936 os_memcpy(ret, pos, end - pos + 1);
2937 ret[end - pos + 1] = '\0';
2938
2939 return ret;
2940}
2941
2942
2943static int osu_cert_cb(void *_ctx, struct http_cert *cert)
2944{
2945 struct hs20_osu_client *ctx = _ctx;
Hai Shalomfdcde762020-04-02 11:19:20 -07002946 size_t i, j;
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07002947 int found;
2948 char *host = NULL;
2949
Sunil Ravia04bd252022-05-02 22:54:18 -07002950 wpa_printf(MSG_INFO, "osu_cert_cb(osu_cert_validation=%d, url=%s server_url=%s)",
2951 !ctx->no_osu_cert_validation, cert->url ? cert->url : "N/A",
2952 ctx->server_url);
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07002953
Sunil Ravia04bd252022-05-02 22:54:18 -07002954 if (ctx->no_osu_cert_validation && cert->url)
2955 host = get_hostname(cert->url);
2956 else
2957 host = get_hostname(ctx->server_url);
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07002958
Sunil Ravia04bd252022-05-02 22:54:18 -07002959 if (!ctx->no_osu_cert_validation) {
2960 for (i = 0; i < ctx->server_dnsname_count; i++)
2961 os_free(ctx->server_dnsname[i]);
2962 os_free(ctx->server_dnsname);
2963 ctx->server_dnsname = os_calloc(cert->num_dnsname,
2964 sizeof(char *));
2965 ctx->server_dnsname_count = 0;
2966 }
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07002967
2968 found = 0;
2969 for (i = 0; i < cert->num_dnsname; i++) {
Sunil Ravia04bd252022-05-02 22:54:18 -07002970 if (!ctx->no_osu_cert_validation && ctx->server_dnsname) {
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07002971 ctx->server_dnsname[ctx->server_dnsname_count] =
2972 os_strdup(cert->dnsname[i]);
2973 if (ctx->server_dnsname[ctx->server_dnsname_count])
2974 ctx->server_dnsname_count++;
2975 }
2976 if (host && os_strcasecmp(host, cert->dnsname[i]) == 0)
2977 found = 1;
2978 wpa_printf(MSG_INFO, "dNSName '%s'", cert->dnsname[i]);
2979 }
2980
2981 if (host && !found) {
2982 wpa_printf(MSG_INFO, "Server name from URL (%s) did not match any dNSName - abort connection",
2983 host);
2984 write_result(ctx, "Server name from URL (%s) did not match any dNSName - abort connection",
2985 host);
2986 os_free(host);
2987 return -1;
2988 }
2989
2990 os_free(host);
2991
2992 for (i = 0; i < cert->num_othername; i++) {
2993 if (os_strcmp(cert->othername[i].oid,
2994 "1.3.6.1.4.1.40808.1.1.1") == 0) {
2995 wpa_hexdump_ascii(MSG_INFO,
2996 "id-wfa-hotspot-friendlyName",
2997 cert->othername[i].data,
2998 cert->othername[i].len);
2999 }
3000 }
3001
Dmitry Shmidt6cb1f652014-03-21 10:54:03 -07003002 for (j = 0; !ctx->no_osu_cert_validation &&
3003 j < ctx->friendly_name_count; j++) {
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07003004 int found = 0;
3005 for (i = 0; i < cert->num_othername; i++) {
3006 if (os_strcmp(cert->othername[i].oid,
3007 "1.3.6.1.4.1.40808.1.1.1") != 0)
3008 continue;
3009 if (cert->othername[i].len < 3)
3010 continue;
3011 if (os_strncasecmp((char *) cert->othername[i].data,
3012 ctx->friendly_name[j].lang, 3) != 0)
3013 continue;
3014 if (os_strncmp((char *) cert->othername[i].data + 3,
3015 ctx->friendly_name[j].text,
3016 cert->othername[i].len - 3) == 0) {
3017 found = 1;
3018 break;
3019 }
3020 }
3021
3022 if (!found) {
3023 wpa_printf(MSG_INFO, "No friendly name match found for '[%s]%s'",
3024 ctx->friendly_name[j].lang,
3025 ctx->friendly_name[j].text);
3026 write_result(ctx, "No friendly name match found for '[%s]%s'",
3027 ctx->friendly_name[j].lang,
3028 ctx->friendly_name[j].text);
3029 return -1;
3030 }
3031 }
3032
3033 for (i = 0; i < cert->num_logo; i++) {
3034 struct http_logo *logo = &cert->logo[i];
3035
3036 wpa_printf(MSG_INFO, "logo hash alg %s uri '%s'",
3037 logo->alg_oid, logo->uri);
3038 wpa_hexdump_ascii(MSG_INFO, "hashValue",
3039 logo->hash, logo->hash_len);
3040 }
3041
Dmitry Shmidt6cb1f652014-03-21 10:54:03 -07003042 for (j = 0; !ctx->no_osu_cert_validation && j < ctx->icon_count; j++) {
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07003043 int found = 0;
3044 char *name = ctx->icon_filename[j];
3045 size_t name_len = os_strlen(name);
3046
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07003047 wpa_printf(MSG_INFO,
Hai Shalomfdcde762020-04-02 11:19:20 -07003048 "[%zu] Looking for icon file name '%s' match",
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07003049 j, name);
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07003050 for (i = 0; i < cert->num_logo; i++) {
3051 struct http_logo *logo = &cert->logo[i];
3052 size_t uri_len = os_strlen(logo->uri);
3053 char *pos;
3054
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07003055 wpa_printf(MSG_INFO,
Hai Shalomfdcde762020-04-02 11:19:20 -07003056 "[%zu] Comparing to '%s' uri_len=%d name_len=%d",
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07003057 i, logo->uri, (int) uri_len, (int) name_len);
3058 if (uri_len < 1 + name_len) {
3059 wpa_printf(MSG_INFO, "URI Length is too short");
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07003060 continue;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07003061 }
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07003062 pos = &logo->uri[uri_len - name_len - 1];
3063 if (*pos != '/')
3064 continue;
3065 pos++;
3066 if (os_strcmp(pos, name) == 0) {
3067 found = 1;
3068 break;
3069 }
3070 }
3071
3072 if (!found) {
3073 wpa_printf(MSG_INFO, "No icon filename match found for '%s'",
3074 name);
3075 write_result(ctx,
3076 "No icon filename match found for '%s'",
3077 name);
3078 return -1;
3079 }
3080 }
3081
Dmitry Shmidt6cb1f652014-03-21 10:54:03 -07003082 for (j = 0; !ctx->no_osu_cert_validation && j < ctx->icon_count; j++) {
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07003083 int found = 0;
3084
3085 for (i = 0; i < cert->num_logo; i++) {
3086 struct http_logo *logo = &cert->logo[i];
3087
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07003088 if (logo->hash_len != 32) {
3089 wpa_printf(MSG_INFO,
Hai Shalomfdcde762020-04-02 11:19:20 -07003090 "[%zu][%zu] Icon hash length invalid (should be 32): %d",
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07003091 j, i, (int) logo->hash_len);
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07003092 continue;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07003093 }
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07003094 if (os_memcmp(logo->hash, ctx->icon_hash[j], 32) == 0) {
3095 found = 1;
3096 break;
3097 }
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07003098
3099 wpa_printf(MSG_DEBUG,
Hai Shalomfdcde762020-04-02 11:19:20 -07003100 "[%zu][%zu] Icon hash did not match", j, i);
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07003101 wpa_hexdump_ascii(MSG_DEBUG, "logo->hash",
3102 logo->hash, 32);
3103 wpa_hexdump_ascii(MSG_DEBUG, "ctx->icon_hash[j]",
3104 ctx->icon_hash[j], 32);
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07003105 }
3106
3107 if (!found) {
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07003108 wpa_printf(MSG_INFO,
3109 "No icon hash match (by hash) found");
3110 write_result(ctx,
3111 "No icon hash match (by hash) found");
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07003112 return -1;
3113 }
3114 }
3115
3116 return 0;
3117}
3118
3119
3120static int init_ctx(struct hs20_osu_client *ctx)
3121{
3122 xml_node_t *devinfo, *devid;
3123
3124 os_memset(ctx, 0, sizeof(*ctx));
3125 ctx->ifname = "wlan0";
3126 ctx->xml = xml_node_init_ctx(ctx, NULL);
3127 if (ctx->xml == NULL)
3128 return -1;
3129
3130 devinfo = node_from_file(ctx->xml, "devinfo.xml");
Hai Shalom74f70d42019-02-11 14:42:39 -08003131 if (devinfo) {
3132 devid = get_node(ctx->xml, devinfo, "DevId");
3133 if (devid) {
3134 char *tmp = xml_node_get_text(ctx->xml, devid);
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07003135
Hai Shalom74f70d42019-02-11 14:42:39 -08003136 if (tmp) {
3137 ctx->devid = os_strdup(tmp);
3138 xml_node_get_text_free(ctx->xml, tmp);
3139 }
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07003140 }
Hai Shalom74f70d42019-02-11 14:42:39 -08003141 xml_node_free(ctx->xml, devinfo);
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07003142 }
3143
3144 ctx->http = http_init_ctx(ctx, ctx->xml);
3145 if (ctx->http == NULL) {
3146 xml_node_deinit_ctx(ctx->xml);
3147 return -1;
3148 }
3149 http_ocsp_set(ctx->http, 2);
3150 http_set_cert_cb(ctx->http, osu_cert_cb, ctx);
3151
3152 return 0;
3153}
3154
3155
3156static void deinit_ctx(struct hs20_osu_client *ctx)
3157{
3158 size_t i;
3159
3160 http_deinit_ctx(ctx->http);
3161 xml_node_deinit_ctx(ctx->xml);
3162 os_free(ctx->fqdn);
3163 os_free(ctx->server_url);
3164 os_free(ctx->devid);
3165
3166 for (i = 0; i < ctx->server_dnsname_count; i++)
3167 os_free(ctx->server_dnsname[i]);
3168 os_free(ctx->server_dnsname);
3169}
3170
3171
3172static void check_workarounds(struct hs20_osu_client *ctx)
3173{
3174 FILE *f;
3175 char buf[100];
3176 unsigned long int val = 0;
3177
3178 f = fopen("hs20-osu-client.workarounds", "r");
3179 if (f == NULL)
3180 return;
3181
3182 if (fgets(buf, sizeof(buf), f))
3183 val = strtoul(buf, NULL, 16);
3184
3185 fclose(f);
3186
3187 if (val) {
3188 wpa_printf(MSG_INFO, "Workarounds enabled: 0x%lx", val);
3189 ctx->workarounds = val;
3190 if (ctx->workarounds & WORKAROUND_OCSP_OPTIONAL)
3191 http_ocsp_set(ctx->http, 1);
3192 }
3193}
3194
3195
3196static void usage(void)
3197{
Hai Shalomfdcde762020-04-02 11:19:20 -07003198 printf("usage: hs20-osu-client [-dddqqKtT] [-S<station ifname>] \\\n"
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07003199 " [-w<wpa_supplicant ctrl_iface dir>] "
3200 "[-r<result file>] [-f<debug file>] \\\n"
3201 " [-s<summary file>] \\\n"
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07003202 " [-x<spp.xsd file name>] \\\n"
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07003203 " <command> [arguments..]\n"
3204 "commands:\n"
3205 "- to_tnds <XML MO> <XML MO in TNDS format> [URN]\n"
3206 "- to_tnds2 <XML MO> <XML MO in TNDS format (Path) "
3207 "[URN]>\n"
3208 "- from_tnds <XML MO in TNDS format> <XML MO>\n"
3209 "- set_pps <PerProviderSubscription XML file name>\n"
3210 "- get_fqdn <PerProviderSubscription XML file name>\n"
3211 "- pol_upd [Server URL] [PPS] [CA cert]\n"
3212 "- sub_rem <Server URL> [PPS] [CA cert]\n"
3213 "- prov <Server URL> [CA cert]\n"
3214 "- oma_dm_prov <Server URL> [CA cert]\n"
3215 "- sim_prov <Server URL> [CA cert]\n"
3216 "- oma_dm_sim_prov <Server URL> [CA cert]\n"
3217 "- signup [CA cert]\n"
3218 "- dl_osu_ca <PPS> <CA file>\n"
3219 "- dl_polupd_ca <PPS> <CA file>\n"
3220 "- dl_aaa_ca <PPS> <CA file>\n"
3221 "- browser <URL>\n"
3222 "- parse_cert <X.509 certificate (DER)>\n"
3223 "- osu_select <OSU info directory> [CA cert]\n");
3224}
3225
3226
3227int main(int argc, char *argv[])
3228{
3229 struct hs20_osu_client ctx;
3230 int c;
3231 int ret = 0;
3232 int no_prod_assoc = 0;
3233 const char *friendly_name = NULL;
3234 const char *wpa_debug_file_path = NULL;
3235 extern char *wpas_ctrl_path;
3236 extern int wpa_debug_level;
3237 extern int wpa_debug_show_keys;
3238 extern int wpa_debug_timestamp;
3239
3240 if (init_ctx(&ctx) < 0)
3241 return -1;
3242
3243 for (;;) {
Hai Shalomfdcde762020-04-02 11:19:20 -07003244 c = getopt(argc, argv, "df:hKNo:O:qr:s:S:tTw:x:");
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07003245 if (c < 0)
3246 break;
3247 switch (c) {
3248 case 'd':
3249 if (wpa_debug_level > 0)
3250 wpa_debug_level--;
3251 break;
3252 case 'f':
3253 wpa_debug_file_path = optarg;
3254 break;
3255 case 'K':
3256 wpa_debug_show_keys++;
3257 break;
3258 case 'N':
3259 no_prod_assoc = 1;
3260 break;
Hai Shalom39ba6fc2019-01-22 12:40:38 -08003261 case 'o':
3262 ctx.osu_ssid = optarg;
3263 break;
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07003264 case 'O':
3265 friendly_name = optarg;
3266 break;
3267 case 'q':
3268 wpa_debug_level++;
3269 break;
3270 case 'r':
3271 ctx.result_file = optarg;
3272 break;
3273 case 's':
3274 ctx.summary_file = optarg;
3275 break;
3276 case 'S':
3277 ctx.ifname = optarg;
3278 break;
3279 case 't':
3280 wpa_debug_timestamp++;
3281 break;
Hai Shalomfdcde762020-04-02 11:19:20 -07003282 case 'T':
3283 ctx.ignore_tls = 1;
3284 break;
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07003285 case 'w':
3286 wpas_ctrl_path = optarg;
3287 break;
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07003288 case 'x':
3289 spp_xsd_fname = optarg;
3290 break;
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07003291 case 'h':
3292 default:
3293 usage();
3294 exit(0);
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07003295 }
3296 }
3297
3298 if (argc - optind < 1) {
3299 usage();
3300 exit(0);
3301 }
3302
3303 wpa_debug_open_file(wpa_debug_file_path);
3304
3305#ifdef __linux__
3306 setlinebuf(stdout);
3307#endif /* __linux__ */
3308
3309 if (ctx.result_file)
3310 unlink(ctx.result_file);
3311 wpa_printf(MSG_DEBUG, "===[hs20-osu-client START - command: %s ]======"
3312 "================", argv[optind]);
3313 check_workarounds(&ctx);
3314
3315 if (strcmp(argv[optind], "to_tnds") == 0) {
3316 if (argc - optind < 2) {
3317 usage();
3318 exit(0);
3319 }
3320 cmd_to_tnds(&ctx, argv[optind + 1], argv[optind + 2],
3321 argc > optind + 3 ? argv[optind + 3] : NULL,
3322 0);
3323 } else if (strcmp(argv[optind], "to_tnds2") == 0) {
3324 if (argc - optind < 2) {
3325 usage();
3326 exit(0);
3327 }
3328 cmd_to_tnds(&ctx, argv[optind + 1], argv[optind + 2],
3329 argc > optind + 3 ? argv[optind + 3] : NULL,
3330 1);
3331 } else if (strcmp(argv[optind], "from_tnds") == 0) {
3332 if (argc - optind < 2) {
3333 usage();
3334 exit(0);
3335 }
3336 cmd_from_tnds(&ctx, argv[optind + 1], argv[optind + 2]);
3337 } else if (strcmp(argv[optind], "sub_rem") == 0) {
3338 if (argc - optind < 2) {
3339 usage();
3340 exit(0);
3341 }
Dmitry Shmidte4663042016-04-04 10:07:49 -07003342 ret = cmd_sub_rem(&ctx, argv[optind + 1],
3343 argc > optind + 2 ? argv[optind + 2] : NULL,
3344 argc > optind + 3 ? argv[optind + 3] : NULL);
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07003345 } else if (strcmp(argv[optind], "pol_upd") == 0) {
Dmitry Shmidte4663042016-04-04 10:07:49 -07003346 ret = cmd_pol_upd(&ctx,
3347 argc > optind + 1 ? argv[optind + 1] : NULL,
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07003348 argc > optind + 2 ? argv[optind + 2] : NULL,
3349 argc > optind + 3 ? argv[optind + 3] : NULL);
3350 } else if (strcmp(argv[optind], "prov") == 0) {
3351 if (argc - optind < 2) {
3352 usage();
3353 exit(0);
3354 }
Dmitry Shmidt6cb1f652014-03-21 10:54:03 -07003355 ctx.ca_fname = argv[optind + 2];
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07003356 wpa_printf(MSG_DEBUG, "Calling cmd_prov from main");
Dmitry Shmidt6cb1f652014-03-21 10:54:03 -07003357 cmd_prov(&ctx, argv[optind + 1]);
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07003358 } else if (strcmp(argv[optind], "sim_prov") == 0) {
3359 if (argc - optind < 2) {
3360 usage();
3361 exit(0);
3362 }
Dmitry Shmidt6cb1f652014-03-21 10:54:03 -07003363 ctx.ca_fname = argv[optind + 2];
3364 cmd_sim_prov(&ctx, argv[optind + 1]);
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07003365 } else if (strcmp(argv[optind], "dl_osu_ca") == 0) {
3366 if (argc - optind < 2) {
3367 usage();
3368 exit(0);
3369 }
3370 cmd_dl_osu_ca(&ctx, argv[optind + 1], argv[optind + 2]);
3371 } else if (strcmp(argv[optind], "dl_polupd_ca") == 0) {
3372 if (argc - optind < 2) {
3373 usage();
3374 exit(0);
3375 }
3376 cmd_dl_polupd_ca(&ctx, argv[optind + 1], argv[optind + 2]);
3377 } else if (strcmp(argv[optind], "dl_aaa_ca") == 0) {
3378 if (argc - optind < 2) {
3379 usage();
3380 exit(0);
3381 }
3382 cmd_dl_aaa_ca(&ctx, argv[optind + 1], argv[optind + 2]);
3383 } else if (strcmp(argv[optind], "osu_select") == 0) {
3384 if (argc - optind < 2) {
3385 usage();
3386 exit(0);
3387 }
Dmitry Shmidt6cb1f652014-03-21 10:54:03 -07003388 ctx.ca_fname = argc > optind + 2 ? argv[optind + 2] : NULL;
3389 cmd_osu_select(&ctx, argv[optind + 1], 2, 1, NULL);
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07003390 } else if (strcmp(argv[optind], "signup") == 0) {
Dmitry Shmidt6cb1f652014-03-21 10:54:03 -07003391 ctx.ca_fname = argc > optind + 1 ? argv[optind + 1] : NULL;
3392 ret = cmd_signup(&ctx, no_prod_assoc, friendly_name);
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07003393 } else if (strcmp(argv[optind], "set_pps") == 0) {
3394 if (argc - optind < 2) {
3395 usage();
3396 exit(0);
3397 }
3398 cmd_set_pps(&ctx, argv[optind + 1]);
3399 } else if (strcmp(argv[optind], "get_fqdn") == 0) {
3400 if (argc - optind < 1) {
3401 usage();
3402 exit(0);
3403 }
3404 ret = cmd_get_fqdn(&ctx, argv[optind + 1]);
3405 } else if (strcmp(argv[optind], "oma_dm_prov") == 0) {
3406 if (argc - optind < 2) {
3407 usage();
3408 exit(0);
3409 }
Dmitry Shmidt6cb1f652014-03-21 10:54:03 -07003410 ctx.ca_fname = argv[optind + 2];
3411 cmd_oma_dm_prov(&ctx, argv[optind + 1]);
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07003412 } else if (strcmp(argv[optind], "oma_dm_sim_prov") == 0) {
3413 if (argc - optind < 2) {
3414 usage();
3415 exit(0);
3416 }
Dmitry Shmidt6cb1f652014-03-21 10:54:03 -07003417 ctx.ca_fname = argv[optind + 2];
3418 if (cmd_oma_dm_sim_prov(&ctx, argv[optind + 1]) < 0) {
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07003419 write_summary(&ctx, "Failed to complete OMA DM SIM provisioning");
3420 return -1;
3421 }
3422 } else if (strcmp(argv[optind], "oma_dm_add") == 0) {
3423 if (argc - optind < 2) {
3424 usage();
3425 exit(0);
3426 }
3427 cmd_oma_dm_add(&ctx, argv[optind + 1], argv[optind + 2]);
3428 } else if (strcmp(argv[optind], "oma_dm_replace") == 0) {
3429 if (argc - optind < 2) {
3430 usage();
3431 exit(0);
3432 }
3433 cmd_oma_dm_replace(&ctx, argv[optind + 1], argv[optind + 2]);
3434 } else if (strcmp(argv[optind], "est_csr") == 0) {
3435 if (argc - optind < 2) {
3436 usage();
3437 exit(0);
3438 }
3439 mkdir("Cert", S_IRWXU);
3440 est_build_csr(&ctx, argv[optind + 1]);
3441 } else if (strcmp(argv[optind], "browser") == 0) {
3442 int ret;
3443
3444 if (argc - optind < 2) {
3445 usage();
3446 exit(0);
3447 }
3448
3449 wpa_printf(MSG_INFO, "Launch web browser to URL %s",
3450 argv[optind + 1]);
Hai Shalomfdcde762020-04-02 11:19:20 -07003451 ret = hs20_web_browser(argv[optind + 1], ctx.ignore_tls);
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07003452 wpa_printf(MSG_INFO, "Web browser result: %d", ret);
3453 } else if (strcmp(argv[optind], "parse_cert") == 0) {
3454 if (argc - optind < 2) {
3455 usage();
3456 exit(0);
3457 }
3458
3459 wpa_debug_level = MSG_MSGDUMP;
3460 http_parse_x509_certificate(ctx.http, argv[optind + 1]);
3461 wpa_debug_level = MSG_INFO;
3462 } else {
3463 wpa_printf(MSG_INFO, "Unknown command '%s'", argv[optind]);
3464 }
3465
Dmitry Shmidt6cb1f652014-03-21 10:54:03 -07003466 deinit_ctx(&ctx);
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07003467 wpa_printf(MSG_DEBUG,
3468 "===[hs20-osu-client END ]======================");
3469
3470 wpa_debug_close_file();
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07003471
3472 return ret;
3473}