2 * Hotspot 2.0 OSU client
3 * Copyright (c) 2012-2014, Qualcomm Atheros, Inc.
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
13 #include "private/android_filesystem_config.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"
28 const char *spp_xsd_fname = "spp.xsd";
31 void write_result(struct hs20_osu_client *ctx, const char *fmt, ...)
38 vsnprintf(buf, sizeof(buf), fmt, ap);
40 write_summary(ctx, "%s", buf);
42 if (!ctx->result_file)
45 f = fopen(ctx->result_file, "w");
57 void write_summary(struct hs20_osu_client *ctx, const char *fmt, ...)
62 if (!ctx->summary_file)
65 f = fopen(ctx->summary_file, "a");
77 void debug_dump_node(struct hs20_osu_client *ctx, const char *title,
80 char *str = xml_node_to_str(ctx->xml, node);
81 wpa_printf(MSG_DEBUG, "[hs20] %s: '%s'", title, str);
86 static int valid_fqdn(const char *fqdn)
90 /* TODO: could make this more complete.. */
91 if (strchr(fqdn, '.') == 0 || strlen(fqdn) > 255)
93 for (pos = fqdn; *pos; pos++) {
94 if (*pos >= 'a' && *pos <= 'z')
96 if (*pos >= 'A' && *pos <= 'Z')
98 if (*pos >= '0' && *pos <= '9')
100 if (*pos == '-' || *pos == '.' || *pos == '_')
108 int osu_get_certificate(struct hs20_osu_client *ctx, xml_node_t *getcert)
111 char *url, *user = NULL, *pw = NULL;
115 proto = xml_node_get_attr_value(ctx->xml, getcert,
116 "enrollmentProtocol");
119 wpa_printf(MSG_INFO, "getCertificate - enrollmentProtocol=%s", proto);
120 write_summary(ctx, "getCertificate - enrollmentProtocol=%s", proto);
121 if (os_strcasecmp(proto, "EST") != 0) {
122 wpa_printf(MSG_INFO, "Unsupported enrollmentProtocol");
123 xml_node_get_attr_value_free(ctx->xml, proto);
126 xml_node_get_attr_value_free(ctx->xml, proto);
128 node = get_node(ctx->xml, getcert, "enrollmentServerURI");
130 wpa_printf(MSG_INFO, "Could not find enrollmentServerURI node");
131 xml_node_get_attr_value_free(ctx->xml, proto);
134 url = xml_node_get_text(ctx->xml, node);
136 wpa_printf(MSG_INFO, "Could not get URL text");
139 wpa_printf(MSG_INFO, "enrollmentServerURI: %s", url);
140 write_summary(ctx, "enrollmentServerURI: %s", url);
142 node = get_node(ctx->xml, getcert, "estUserID");
143 if (node == NULL && !ctx->client_cert_present) {
144 wpa_printf(MSG_INFO, "Could not find estUserID node");
148 user = xml_node_get_text(ctx->xml, node);
150 wpa_printf(MSG_INFO, "Could not get estUserID text");
153 wpa_printf(MSG_INFO, "estUserID: %s", user);
154 write_summary(ctx, "estUserID: %s", user);
157 node = get_node(ctx->xml, getcert, "estPassword");
158 if (node == NULL && !ctx->client_cert_present) {
159 wpa_printf(MSG_INFO, "Could not find estPassword node");
163 pw = xml_node_get_base64_text(ctx->xml, node, NULL);
165 wpa_printf(MSG_INFO, "Could not get estPassword text");
168 wpa_printf(MSG_INFO, "estPassword: %s", pw);
171 mkdir("Cert", S_IRWXU);
172 if (est_load_cacerts(ctx, url) < 0 ||
173 est_build_csr(ctx, url) < 0 ||
174 est_simple_enroll(ctx, url, user, pw) < 0)
179 xml_node_get_text_free(ctx->xml, url);
180 xml_node_get_text_free(ctx->xml, user);
181 xml_node_get_text_free(ctx->xml, pw);
187 static int process_est_cert(struct hs20_osu_client *ctx, xml_node_t *cert,
190 u8 digest1[SHA256_MAC_LEN], digest2[SHA256_MAC_LEN];
192 size_t der_len, pem_len;
196 wpa_printf(MSG_INFO, "PPS for certificate credential - fqdn=%s", fqdn);
198 fingerprint = xml_node_get_text(ctx->xml, cert);
199 if (fingerprint == NULL)
201 if (hexstr2bin(fingerprint, digest1, SHA256_MAC_LEN) < 0) {
202 wpa_printf(MSG_INFO, "Invalid SHA256 hash value");
203 write_result(ctx, "Invalid client certificate SHA256 hash value in PPS");
204 xml_node_get_text_free(ctx->xml, fingerprint);
207 xml_node_get_text_free(ctx->xml, fingerprint);
209 der = os_readfile("Cert/est_cert.der", &der_len);
211 wpa_printf(MSG_INFO, "Could not find client certificate from EST");
212 write_result(ctx, "Could not find client certificate from EST");
216 if (sha256_vector(1, (const u8 **) &der, &der_len, digest2) < 0) {
222 if (os_memcmp(digest1, digest2, sizeof(digest1)) != 0) {
223 wpa_printf(MSG_INFO, "Client certificate from EST does not match fingerprint from PPS MO");
224 write_result(ctx, "Client certificate from EST does not match fingerprint from PPS MO");
228 wpa_printf(MSG_INFO, "Client certificate from EST matches PPS MO");
229 unlink("Cert/est_cert.der");
231 os_snprintf(buf, sizeof(buf), "SP/%s/client-ca.pem", fqdn);
232 if (rename("Cert/est-cacerts.pem", buf) < 0) {
233 wpa_printf(MSG_INFO, "Could not move est-cacerts.pem to client-ca.pem: %s",
237 pem = os_readfile(buf, &pem_len);
239 os_snprintf(buf, sizeof(buf), "SP/%s/client-cert.pem", fqdn);
240 if (rename("Cert/est_cert.pem", buf) < 0) {
241 wpa_printf(MSG_INFO, "Could not move est_cert.pem to client-cert.pem: %s",
248 FILE *f = fopen(buf, "a");
250 fwrite(pem, pem_len, 1, f);
256 os_snprintf(buf, sizeof(buf), "SP/%s/client-key.pem", fqdn);
257 if (rename("Cert/privkey-plain.pem", buf) < 0) {
258 wpa_printf(MSG_INFO, "Could not move privkey-plain.pem to client-key.pem: %s",
263 unlink("Cert/est-req.b64");
264 unlink("Cert/est-req.pem");
265 unlink("Cert/est-resp.raw");
272 #define TMP_CERT_DL_FILE "tmp-cert-download"
274 static int download_cert(struct hs20_osu_client *ctx, xml_node_t *params,
277 xml_node_t *url_node, *hash_node;
281 u8 digest1[SHA256_MAC_LEN], digest2[SHA256_MAC_LEN];
286 url_node = get_node(ctx->xml, params, "CertURL");
287 hash_node = get_node(ctx->xml, params, "CertSHA256Fingerprint");
288 if (url_node == NULL || hash_node == NULL)
290 url = xml_node_get_text(ctx->xml, url_node);
291 hash = xml_node_get_text(ctx->xml, hash_node);
292 if (url == NULL || hash == NULL) {
293 xml_node_get_text_free(ctx->xml, url);
294 xml_node_get_text_free(ctx->xml, hash);
298 wpa_printf(MSG_INFO, "CertURL: %s", url);
299 wpa_printf(MSG_INFO, "SHA256 hash: %s", hash);
301 if (hexstr2bin(hash, digest1, SHA256_MAC_LEN) < 0) {
302 wpa_printf(MSG_INFO, "Invalid SHA256 hash value");
303 write_result(ctx, "Invalid SHA256 hash value for downloaded certificate");
304 xml_node_get_text_free(ctx->xml, hash);
307 xml_node_get_text_free(ctx->xml, hash);
309 write_summary(ctx, "Download certificate from %s", url);
310 ctx->no_osu_cert_validation = 1;
311 http_ocsp_set(ctx->http, 1);
312 res = http_download_file(ctx->http, url, TMP_CERT_DL_FILE, NULL);
313 http_ocsp_set(ctx->http,
314 (ctx->workarounds & WORKAROUND_OCSP_OPTIONAL) ? 1 : 2);
315 ctx->no_osu_cert_validation = 0;
316 xml_node_get_text_free(ctx->xml, url);
320 cert = os_readfile(TMP_CERT_DL_FILE, &len);
321 remove(TMP_CERT_DL_FILE);
325 if (sha256_vector(1, (const u8 **) &cert, &len, digest2) < 0) {
330 if (os_memcmp(digest1, digest2, sizeof(digest1)) != 0) {
331 wpa_printf(MSG_INFO, "Downloaded certificate fingerprint did not match");
332 write_result(ctx, "Downloaded certificate fingerprint did not match");
337 b64 = base64_encode((unsigned char *) cert, len, NULL);
342 f = fopen(fname, "wb");
348 fprintf(f, "-----BEGIN CERTIFICATE-----\n"
350 "-----END CERTIFICATE-----\n",
356 wpa_printf(MSG_INFO, "Downloaded certificate into %s and validated fingerprint",
358 write_summary(ctx, "Downloaded certificate into %s and validated fingerprint",
365 static int cmd_dl_osu_ca(struct hs20_osu_client *ctx, const char *pps_fname,
366 const char *ca_fname)
368 xml_node_t *pps, *node;
371 pps = node_from_file(ctx->xml, pps_fname);
373 wpa_printf(MSG_INFO, "Could not read or parse '%s'", pps_fname);
377 node = get_child_node(ctx->xml, pps,
378 "SubscriptionUpdate/TrustRoot");
380 wpa_printf(MSG_INFO, "No SubscriptionUpdate/TrustRoot/CertURL found from PPS");
381 xml_node_free(ctx->xml, pps);
385 ret = download_cert(ctx, node, ca_fname);
386 xml_node_free(ctx->xml, pps);
392 static int cmd_dl_polupd_ca(struct hs20_osu_client *ctx, const char *pps_fname,
393 const char *ca_fname)
395 xml_node_t *pps, *node;
398 pps = node_from_file(ctx->xml, pps_fname);
400 wpa_printf(MSG_INFO, "Could not read or parse '%s'", pps_fname);
404 node = get_child_node(ctx->xml, pps,
405 "Policy/PolicyUpdate/TrustRoot");
407 wpa_printf(MSG_INFO, "No Policy/PolicyUpdate/TrustRoot/CertURL found from PPS");
408 xml_node_free(ctx->xml, pps);
412 ret = download_cert(ctx, node, ca_fname);
413 xml_node_free(ctx->xml, pps);
419 static int cmd_dl_aaa_ca(struct hs20_osu_client *ctx, const char *pps_fname,
420 const char *ca_fname)
422 xml_node_t *pps, *node, *aaa;
425 pps = node_from_file(ctx->xml, pps_fname);
427 wpa_printf(MSG_INFO, "Could not read or parse '%s'", pps_fname);
431 node = get_child_node(ctx->xml, pps,
432 "AAAServerTrustRoot");
434 wpa_printf(MSG_INFO, "No AAAServerTrustRoot/CertURL found from PPS");
435 xml_node_free(ctx->xml, pps);
439 aaa = xml_node_first_child(ctx->xml, node);
441 wpa_printf(MSG_INFO, "No AAAServerTrustRoot/CertURL found from PPS");
442 xml_node_free(ctx->xml, pps);
446 ret = download_cert(ctx, aaa, ca_fname);
447 xml_node_free(ctx->xml, pps);
453 static int download_trust_roots(struct hs20_osu_client *ctx,
454 const char *pps_fname)
460 dir = os_strdup(pps_fname);
463 pos = os_strrchr(dir, '/');
470 snprintf(fname, sizeof(fname), "%s/ca.pem", dir);
471 ret = cmd_dl_osu_ca(ctx, pps_fname, fname);
472 snprintf(fname, sizeof(fname), "%s/polupd-ca.pem", dir);
473 cmd_dl_polupd_ca(ctx, pps_fname, fname);
474 snprintf(fname, sizeof(fname), "%s/aaa-ca.pem", dir);
475 cmd_dl_aaa_ca(ctx, pps_fname, fname);
483 static int server_dnsname_suffix_match(struct hs20_osu_client *ctx,
486 size_t match_len, len, i;
489 match_len = os_strlen(fqdn);
491 for (i = 0; i < ctx->server_dnsname_count; i++) {
493 "Checking suffix match against server dNSName %s",
494 ctx->server_dnsname[i]);
495 val = ctx->server_dnsname[i];
496 len = os_strlen(val);
501 if (os_strncasecmp(val + len - match_len, fqdn, match_len) != 0)
502 continue; /* no match */
504 if (match_len == len)
505 return 1; /* exact match */
507 if (val[len - match_len - 1] == '.')
508 return 1; /* full label match completes suffix match */
510 /* Reject due to incomplete label match */
513 /* None of the dNSName(s) matched */
518 int hs20_add_pps_mo(struct hs20_osu_client *ctx, const char *uri,
519 xml_node_t *add_mo, char *fname, size_t fname_len)
523 xml_node_t *tnds, *mo, *cert;
527 if (strncmp(uri, "./Wi-Fi/", 8) != 0) {
528 wpa_printf(MSG_INFO, "Unsupported location for addMO to add PPS MO: '%s'",
530 write_result(ctx, "Unsupported location for addMO to add PPS MO: '%s'",
535 fqdn = strdup(uri + 8);
538 pos = strchr(fqdn, '/');
540 if (os_strcasecmp(pos, "/PerProviderSubscription") != 0) {
541 wpa_printf(MSG_INFO, "Unsupported location for addMO to add PPS MO (extra directory): '%s'",
543 write_result(ctx, "Unsupported location for addMO to "
544 "add PPS MO (extra directory): '%s'", uri);
547 *pos = '\0'; /* remove trailing slash and PPS node name */
549 wpa_printf(MSG_INFO, "SP FQDN: %s", fqdn);
551 if (!server_dnsname_suffix_match(ctx, fqdn)) {
553 "FQDN '%s' for new PPS MO did not have suffix match with server's dNSName values, count: %d",
554 fqdn, (int) ctx->server_dnsname_count);
555 write_result(ctx, "FQDN '%s' for new PPS MO did not have suffix match with server's dNSName values",
561 if (!valid_fqdn(fqdn)) {
562 wpa_printf(MSG_INFO, "Invalid FQDN '%s'", fqdn);
563 write_result(ctx, "Invalid FQDN '%s'", fqdn);
568 mkdir("SP", S_IRWXU);
569 snprintf(fname, fname_len, "SP/%s", fqdn);
570 if (mkdir(fname, S_IRWXU) < 0) {
571 if (errno != EEXIST) {
573 wpa_printf(MSG_INFO, "mkdir(%s) failed: %s",
574 fname, strerror(err));
581 /* Allow processes running with Group ID as AID_WIFI,
582 * to read files from SP/<fqdn> directory */
583 if (chown(fname, -1, AID_WIFI)) {
584 wpa_printf(MSG_INFO, "CTRL: Could not chown directory: %s",
586 /* Try to continue anyway */
588 if (chmod(fname, S_IRWXU | S_IRGRP | S_IXGRP) < 0) {
589 wpa_printf(MSG_INFO, "CTRL: Could not chmod directory: %s",
591 /* Try to continue anyway */
595 snprintf(fname, fname_len, "SP/%s/pps.xml", fqdn);
597 if (os_file_exists(fname)) {
598 wpa_printf(MSG_INFO, "PPS file '%s' exists - reject addMO",
600 write_result(ctx, "PPS file '%s' exists - reject addMO",
605 wpa_printf(MSG_INFO, "Using PPS file: %s", fname);
607 str = xml_node_get_text(ctx->xml, add_mo);
609 wpa_printf(MSG_INFO, "Could not extract MO text");
613 wpa_printf(MSG_DEBUG, "[hs20] addMO text: '%s'", str);
615 tnds = xml_node_from_buf(ctx->xml, str);
616 xml_node_get_text_free(ctx->xml, str);
618 wpa_printf(MSG_INFO, "[hs20] Could not parse addMO text");
623 mo = tnds_to_mo(ctx->xml, tnds);
625 wpa_printf(MSG_INFO, "[hs20] Could not parse addMO TNDS text");
630 debug_dump_node(ctx, "Parsed TNDS", mo);
632 name = xml_node_get_localname(ctx->xml, mo);
633 if (os_strcasecmp(name, "PerProviderSubscription") != 0) {
634 wpa_printf(MSG_INFO, "[hs20] Unexpected PPS MO root node name '%s'",
640 cert = get_child_node(ctx->xml, mo,
641 "Credential/DigitalCertificate/"
642 "CertSHA256Fingerprint");
643 if (cert && process_est_cert(ctx, cert, fqdn) < 0) {
644 xml_node_free(ctx->xml, mo);
650 if (node_to_file(ctx->xml, fname, mo) < 0) {
651 wpa_printf(MSG_INFO, "Could not write MO to file");
652 xml_node_free(ctx->xml, mo);
655 xml_node_free(ctx->xml, mo);
657 wpa_printf(MSG_INFO, "A new PPS MO added as '%s'", fname);
658 write_summary(ctx, "A new PPS MO added as '%s'", fname);
660 ret = download_trust_roots(ctx, fname);
662 wpa_printf(MSG_INFO, "Remove invalid PPS MO file");
663 write_summary(ctx, "Remove invalid PPS MO file");
671 int update_pps_file(struct hs20_osu_client *ctx, const char *pps_fname,
678 if (ctx->client_cert_present) {
680 cert = get_child_node(ctx->xml, pps,
681 "Credential/DigitalCertificate/"
682 "CertSHA256Fingerprint");
683 if (cert && os_file_exists("Cert/est_cert.der") &&
684 process_est_cert(ctx, cert, ctx->fqdn) < 0) {
685 wpa_printf(MSG_INFO, "EST certificate update processing failed on PPS MO update");
690 wpa_printf(MSG_INFO, "Updating PPS MO %s", pps_fname);
692 str = xml_node_to_str(ctx->xml, pps);
694 wpa_printf(MSG_ERROR, "No node found");
697 wpa_printf(MSG_MSGDUMP, "[hs20] Updated PPS: '%s'", str);
699 snprintf(backup, sizeof(backup), "%s.bak", pps_fname);
700 rename(pps_fname, backup);
701 f = fopen(pps_fname, "w");
703 wpa_printf(MSG_INFO, "Could not write PPS");
704 rename(backup, pps_fname);
708 fprintf(f, "%s\n", str);
717 void get_user_pw(struct hs20_osu_client *ctx, xml_node_t *pps,
718 const char *alt_loc, char **user, char **pw)
722 node = get_child_node(ctx->xml, pps,
723 "Credential/UsernamePassword/Username");
725 *user = xml_node_get_text(ctx->xml, node);
727 node = get_child_node(ctx->xml, pps,
728 "Credential/UsernamePassword/Password");
730 *pw = xml_node_get_base64_text(ctx->xml, node, NULL);
732 node = get_child_node(ctx->xml, pps, alt_loc);
735 a = get_node(ctx->xml, node, "Username");
737 xml_node_get_text_free(ctx->xml, *user);
738 *user = xml_node_get_text(ctx->xml, a);
739 wpa_printf(MSG_INFO, "Use OSU username '%s'", *user);
742 a = get_node(ctx->xml, node, "Password");
745 *pw = xml_node_get_base64_text(ctx->xml, a, NULL);
746 wpa_printf(MSG_INFO, "Use OSU password");
752 /* Remove old credentials based on HomeSP/FQDN */
753 static void remove_sp_creds(struct hs20_osu_client *ctx, const char *fqdn)
756 os_snprintf(cmd, sizeof(cmd), "REMOVE_CRED provisioning_sp=%s", fqdn);
757 if (wpa_command(ctx->ifname, cmd) < 0)
758 wpa_printf(MSG_INFO, "Failed to remove old credential(s)");
762 static void set_pps_cred_policy_spe(struct hs20_osu_client *ctx, int id,
768 ssid = get_node(ctx->xml, spe, "SSID");
771 txt = xml_node_get_text(ctx->xml, ssid);
774 wpa_printf(MSG_DEBUG, "- Policy/SPExclusionList/<X+>/SSID = %s", txt);
775 if (set_cred_quoted(ctx->ifname, id, "excluded_ssid", txt) < 0)
776 wpa_printf(MSG_INFO, "Failed to set cred excluded_ssid");
777 xml_node_get_text_free(ctx->xml, txt);
781 static void set_pps_cred_policy_spel(struct hs20_osu_client *ctx, int id,
786 xml_node_for_each_child(ctx->xml, child, spel) {
787 xml_node_for_each_check(ctx->xml, child);
788 set_pps_cred_policy_spe(ctx, id, child);
793 static void set_pps_cred_policy_prp(struct hs20_osu_client *ctx, int id,
797 char *txt = NULL, *pos;
798 char *prio, *country_buf = NULL;
803 node = get_node(ctx->xml, prp, "Priority");
806 prio = xml_node_get_text(ctx->xml, node);
809 wpa_printf(MSG_INFO, "- Policy/PreferredRoamingPartnerList/<X+>/Priority = %s",
811 priority = atoi(prio);
812 xml_node_get_text_free(ctx->xml, prio);
814 node = get_node(ctx->xml, prp, "Country");
816 country_buf = xml_node_get_text(ctx->xml, node);
817 if (country_buf == NULL)
819 country = country_buf;
820 wpa_printf(MSG_INFO, "- Policy/PreferredRoamingPartnerList/<X+>/Country = %s",
826 node = get_node(ctx->xml, prp, "FQDN_Match");
829 txt = xml_node_get_text(ctx->xml, node);
832 wpa_printf(MSG_INFO, "- Policy/PreferredRoamingPartnerList/<X+>/FQDN_Match = %s",
834 pos = strrchr(txt, ',');
839 snprintf(val, sizeof(val), "%s,%d,%d,%s", txt,
840 strcmp(pos, "includeSubdomains") != 0, priority, country);
841 if (set_cred_quoted(ctx->ifname, id, "roaming_partner", val) < 0)
842 wpa_printf(MSG_INFO, "Failed to set cred roaming_partner");
844 xml_node_get_text_free(ctx->xml, country_buf);
845 xml_node_get_text_free(ctx->xml, txt);
849 static void set_pps_cred_policy_prpl(struct hs20_osu_client *ctx, int id,
854 xml_node_for_each_child(ctx->xml, child, prpl) {
855 xml_node_for_each_check(ctx->xml, child);
856 set_pps_cred_policy_prp(ctx, id, child);
861 static void set_pps_cred_policy_min_backhaul(struct hs20_osu_client *ctx, int id,
862 xml_node_t *min_backhaul)
865 char *type, *dl = NULL, *ul = NULL;
868 node = get_node(ctx->xml, min_backhaul, "NetworkType");
870 wpa_printf(MSG_INFO, "Ignore MinBackhaulThreshold without mandatory NetworkType node");
874 type = xml_node_get_text(ctx->xml, node);
877 wpa_printf(MSG_INFO, "- Policy/MinBackhaulThreshold/<X+>/NetworkType = %s",
879 if (os_strcasecmp(type, "home") == 0)
881 else if (os_strcasecmp(type, "roaming") == 0)
884 wpa_printf(MSG_INFO, "Ignore MinBackhaulThreshold with invalid NetworkType");
885 xml_node_get_text_free(ctx->xml, type);
888 xml_node_get_text_free(ctx->xml, type);
890 node = get_node(ctx->xml, min_backhaul, "DLBandwidth");
892 dl = xml_node_get_text(ctx->xml, node);
894 node = get_node(ctx->xml, min_backhaul, "ULBandwidth");
896 ul = xml_node_get_text(ctx->xml, node);
898 if (dl == NULL && ul == NULL) {
899 wpa_printf(MSG_INFO, "Ignore MinBackhaulThreshold without either DLBandwidth or ULBandwidth nodes");
904 wpa_printf(MSG_INFO, "- Policy/MinBackhaulThreshold/<X+>/DLBandwidth = %s",
907 wpa_printf(MSG_INFO, "- Policy/MinBackhaulThreshold/<X+>/ULBandwidth = %s",
912 set_cred(ctx->ifname, id, "min_dl_bandwidth_home", dl) < 0)
913 wpa_printf(MSG_INFO, "Failed to set cred bandwidth limit");
915 set_cred(ctx->ifname, id, "min_ul_bandwidth_home", ul) < 0)
916 wpa_printf(MSG_INFO, "Failed to set cred bandwidth limit");
919 set_cred(ctx->ifname, id, "min_dl_bandwidth_roaming", dl) <
921 wpa_printf(MSG_INFO, "Failed to set cred bandwidth limit");
923 set_cred(ctx->ifname, id, "min_ul_bandwidth_roaming", ul) <
925 wpa_printf(MSG_INFO, "Failed to set cred bandwidth limit");
928 xml_node_get_text_free(ctx->xml, dl);
929 xml_node_get_text_free(ctx->xml, ul);
933 static void set_pps_cred_policy_min_backhaul_list(struct hs20_osu_client *ctx,
934 int id, xml_node_t *node)
938 wpa_printf(MSG_INFO, "- Policy/MinBackhaulThreshold");
940 xml_node_for_each_child(ctx->xml, child, node) {
941 xml_node_for_each_check(ctx->xml, child);
942 set_pps_cred_policy_min_backhaul(ctx, id, child);
947 static void set_pps_cred_policy_update(struct hs20_osu_client *ctx, int id,
950 wpa_printf(MSG_INFO, "- Policy/PolicyUpdate");
951 /* Not used in wpa_supplicant */
955 static void set_pps_cred_policy_required_proto_port(struct hs20_osu_client *ctx,
956 int id, xml_node_t *tuple)
963 node = get_node(ctx->xml, tuple, "IPProtocol");
965 wpa_printf(MSG_INFO, "Ignore RequiredProtoPortTuple without mandatory IPProtocol node");
969 proto = xml_node_get_text(ctx->xml, node);
973 wpa_printf(MSG_INFO, "- Policy/RequiredProtoPortTuple/<X+>/IPProtocol = %s",
976 node = get_node(ctx->xml, tuple, "PortNumber");
977 port = node ? xml_node_get_text(ctx->xml, node) : NULL;
979 wpa_printf(MSG_INFO, "- Policy/RequiredProtoPortTuple/<X+>/PortNumber = %s",
981 buflen = os_strlen(proto) + os_strlen(port) + 10;
982 buf = os_malloc(buflen);
984 os_snprintf(buf, buflen, "%s:%s", proto, port);
985 xml_node_get_text_free(ctx->xml, port);
987 buflen = os_strlen(proto) + 10;
988 buf = os_malloc(buflen);
990 os_snprintf(buf, buflen, "%s", proto);
993 xml_node_get_text_free(ctx->xml, proto);
998 if (set_cred(ctx->ifname, id, "req_conn_capab", buf) < 0)
999 wpa_printf(MSG_INFO, "Could not set req_conn_capab");
1005 static void set_pps_cred_policy_required_proto_ports(struct hs20_osu_client *ctx,
1006 int id, xml_node_t *node)
1010 wpa_printf(MSG_INFO, "- Policy/RequiredProtoPortTuple");
1012 xml_node_for_each_child(ctx->xml, child, node) {
1013 xml_node_for_each_check(ctx->xml, child);
1014 set_pps_cred_policy_required_proto_port(ctx, id, child);
1019 static void set_pps_cred_policy_max_bss_load(struct hs20_osu_client *ctx, int id,
1022 char *str = xml_node_get_text(ctx->xml, node);
1025 wpa_printf(MSG_INFO, "- Policy/MaximumBSSLoadValue - %s", str);
1026 if (set_cred(ctx->ifname, id, "max_bss_load", str) < 0)
1027 wpa_printf(MSG_INFO, "Failed to set cred max_bss_load limit");
1028 xml_node_get_text_free(ctx->xml, str);
1032 static void set_pps_cred_policy(struct hs20_osu_client *ctx, int id,
1038 wpa_printf(MSG_INFO, "- Policy");
1040 xml_node_for_each_child(ctx->xml, child, node) {
1041 xml_node_for_each_check(ctx->xml, child);
1042 name = xml_node_get_localname(ctx->xml, child);
1043 if (os_strcasecmp(name, "PreferredRoamingPartnerList") == 0)
1044 set_pps_cred_policy_prpl(ctx, id, child);
1045 else if (os_strcasecmp(name, "MinBackhaulThreshold") == 0)
1046 set_pps_cred_policy_min_backhaul_list(ctx, id, child);
1047 else if (os_strcasecmp(name, "PolicyUpdate") == 0)
1048 set_pps_cred_policy_update(ctx, id, child);
1049 else if (os_strcasecmp(name, "SPExclusionList") == 0)
1050 set_pps_cred_policy_spel(ctx, id, child);
1051 else if (os_strcasecmp(name, "RequiredProtoPortTuple") == 0)
1052 set_pps_cred_policy_required_proto_ports(ctx, id, child);
1053 else if (os_strcasecmp(name, "MaximumBSSLoadValue") == 0)
1054 set_pps_cred_policy_max_bss_load(ctx, id, child);
1056 wpa_printf(MSG_INFO, "Unknown Policy node '%s'", name);
1061 static void set_pps_cred_priority(struct hs20_osu_client *ctx, int id,
1064 char *str = xml_node_get_text(ctx->xml, node);
1067 wpa_printf(MSG_INFO, "- CredentialPriority = %s", str);
1068 if (set_cred(ctx->ifname, id, "sp_priority", str) < 0)
1069 wpa_printf(MSG_INFO, "Failed to set cred sp_priority");
1070 xml_node_get_text_free(ctx->xml, str);
1074 static void set_pps_cred_aaa_server_trust_root(struct hs20_osu_client *ctx,
1075 int id, xml_node_t *node)
1077 wpa_printf(MSG_INFO, "- AAAServerTrustRoot - TODO");
1081 static void set_pps_cred_sub_update(struct hs20_osu_client *ctx, int id,
1084 wpa_printf(MSG_INFO, "- SubscriptionUpdate");
1085 /* not used within wpa_supplicant */
1089 static void set_pps_cred_home_sp_network_id(struct hs20_osu_client *ctx,
1090 int id, xml_node_t *node)
1092 xml_node_t *ssid_node, *hessid_node;
1093 char *ssid, *hessid;
1095 ssid_node = get_node(ctx->xml, node, "SSID");
1096 if (ssid_node == NULL) {
1097 wpa_printf(MSG_INFO, "Ignore HomeSP/NetworkID without mandatory SSID node");
1101 hessid_node = get_node(ctx->xml, node, "HESSID");
1103 ssid = xml_node_get_text(ctx->xml, ssid_node);
1106 hessid = hessid_node ? xml_node_get_text(ctx->xml, hessid_node) : NULL;
1108 wpa_printf(MSG_INFO, "- HomeSP/NetworkID/<X+>/SSID = %s", ssid);
1110 wpa_printf(MSG_INFO, "- HomeSP/NetworkID/<X+>/HESSID = %s",
1113 /* TODO: Configure to wpa_supplicant */
1115 xml_node_get_text_free(ctx->xml, ssid);
1116 xml_node_get_text_free(ctx->xml, hessid);
1120 static void set_pps_cred_home_sp_network_ids(struct hs20_osu_client *ctx,
1121 int id, xml_node_t *node)
1125 wpa_printf(MSG_INFO, "- HomeSP/NetworkID");
1127 xml_node_for_each_child(ctx->xml, child, node) {
1128 xml_node_for_each_check(ctx->xml, child);
1129 set_pps_cred_home_sp_network_id(ctx, id, child);
1134 static void set_pps_cred_home_sp_friendly_name(struct hs20_osu_client *ctx,
1135 int id, xml_node_t *node)
1137 char *str = xml_node_get_text(ctx->xml, node);
1140 wpa_printf(MSG_INFO, "- HomeSP/FriendlyName = %s", str);
1141 /* not used within wpa_supplicant(?) */
1142 xml_node_get_text_free(ctx->xml, str);
1146 static void set_pps_cred_home_sp_icon_url(struct hs20_osu_client *ctx,
1147 int id, xml_node_t *node)
1149 char *str = xml_node_get_text(ctx->xml, node);
1152 wpa_printf(MSG_INFO, "- HomeSP/IconURL = %s", str);
1153 /* not used within wpa_supplicant */
1154 xml_node_get_text_free(ctx->xml, str);
1158 static void set_pps_cred_home_sp_fqdn(struct hs20_osu_client *ctx, int id,
1161 char *str = xml_node_get_text(ctx->xml, node);
1164 wpa_printf(MSG_INFO, "- HomeSP/FQDN = %s", str);
1165 if (set_cred_quoted(ctx->ifname, id, "domain", str) < 0)
1166 wpa_printf(MSG_INFO, "Failed to set cred domain");
1167 if (set_cred_quoted(ctx->ifname, id, "domain_suffix_match", str) < 0)
1168 wpa_printf(MSG_INFO, "Failed to set cred domain_suffix_match");
1169 xml_node_get_text_free(ctx->xml, str);
1173 static void set_pps_cred_home_sp_oi(struct hs20_osu_client *ctx, int id,
1178 char *homeoi = NULL;
1182 xml_node_for_each_child(ctx->xml, child, node) {
1183 xml_node_for_each_check(ctx->xml, child);
1184 name = xml_node_get_localname(ctx->xml, child);
1185 if (strcasecmp(name, "HomeOI") == 0 && !homeoi) {
1186 homeoi = xml_node_get_text(ctx->xml, child);
1187 wpa_printf(MSG_INFO, "- HomeSP/HomeOIList/<X+>/HomeOI = %s",
1189 } else if (strcasecmp(name, "HomeOIRequired") == 0) {
1190 str = xml_node_get_text(ctx->xml, child);
1191 wpa_printf(MSG_INFO, "- HomeSP/HomeOIList/<X+>/HomeOIRequired = '%s'",
1195 required = strcasecmp(str, "true") == 0;
1196 xml_node_get_text_free(ctx->xml, str);
1198 wpa_printf(MSG_INFO, "Unknown HomeOIList node '%s'",
1202 if (homeoi == NULL) {
1203 wpa_printf(MSG_INFO, "- HomeSP/HomeOIList/<X+> without HomeOI ignored");
1207 wpa_printf(MSG_INFO, "- HomeSP/HomeOIList/<X+> '%s' required=%d",
1211 if (set_cred(ctx->ifname, id, "required_roaming_consortium",
1213 wpa_printf(MSG_INFO, "Failed to set cred required_roaming_consortium");
1215 if (set_cred_quoted(ctx->ifname, id, "roaming_consortium",
1217 wpa_printf(MSG_INFO, "Failed to set cred roaming_consortium");
1220 xml_node_get_text_free(ctx->xml, homeoi);
1224 static void set_pps_cred_home_sp_oi_list(struct hs20_osu_client *ctx, int id,
1229 wpa_printf(MSG_INFO, "- HomeSP/HomeOIList");
1231 xml_node_for_each_child(ctx->xml, child, node) {
1232 xml_node_for_each_check(ctx->xml, child);
1233 set_pps_cred_home_sp_oi(ctx, id, child);
1238 static void set_pps_cred_home_sp_other_partner(struct hs20_osu_client *ctx,
1239 int id, xml_node_t *node)
1245 xml_node_for_each_child(ctx->xml, child, node) {
1246 xml_node_for_each_check(ctx->xml, child);
1247 name = xml_node_get_localname(ctx->xml, child);
1248 if (os_strcasecmp(name, "FQDN") == 0 && !fqdn) {
1249 fqdn = xml_node_get_text(ctx->xml, child);
1250 wpa_printf(MSG_INFO, "- HomeSP/OtherHomePartners/<X+>/FQDN = %s",
1253 wpa_printf(MSG_INFO, "Unknown OtherHomePartners node '%s'",
1258 wpa_printf(MSG_INFO, "- HomeSP/OtherHomePartners/<X+> without FQDN ignored");
1262 if (set_cred_quoted(ctx->ifname, id, "domain", fqdn) < 0)
1263 wpa_printf(MSG_INFO, "Failed to set cred domain for OtherHomePartners node");
1265 xml_node_get_text_free(ctx->xml, fqdn);
1269 static void set_pps_cred_home_sp_other_partners(struct hs20_osu_client *ctx,
1275 wpa_printf(MSG_INFO, "- HomeSP/OtherHomePartners");
1277 xml_node_for_each_child(ctx->xml, child, node) {
1278 xml_node_for_each_check(ctx->xml, child);
1279 set_pps_cred_home_sp_other_partner(ctx, id, child);
1284 static void set_pps_cred_home_sp_roaming_consortium_oi(
1285 struct hs20_osu_client *ctx, int id, xml_node_t *node)
1287 char *str = xml_node_get_text(ctx->xml, node);
1290 wpa_printf(MSG_INFO, "- HomeSP/RoamingConsortiumOI = %s", str);
1291 /* TODO: Set to wpa_supplicant */
1292 xml_node_get_text_free(ctx->xml, str);
1296 static void set_pps_cred_home_sp(struct hs20_osu_client *ctx, int id,
1302 wpa_printf(MSG_INFO, "- HomeSP");
1304 xml_node_for_each_child(ctx->xml, child, node) {
1305 xml_node_for_each_check(ctx->xml, child);
1306 name = xml_node_get_localname(ctx->xml, child);
1307 if (os_strcasecmp(name, "NetworkID") == 0)
1308 set_pps_cred_home_sp_network_ids(ctx, id, child);
1309 else if (os_strcasecmp(name, "FriendlyName") == 0)
1310 set_pps_cred_home_sp_friendly_name(ctx, id, child);
1311 else if (os_strcasecmp(name, "IconURL") == 0)
1312 set_pps_cred_home_sp_icon_url(ctx, id, child);
1313 else if (os_strcasecmp(name, "FQDN") == 0)
1314 set_pps_cred_home_sp_fqdn(ctx, id, child);
1315 else if (os_strcasecmp(name, "HomeOIList") == 0)
1316 set_pps_cred_home_sp_oi_list(ctx, id, child);
1317 else if (os_strcasecmp(name, "OtherHomePartners") == 0)
1318 set_pps_cred_home_sp_other_partners(ctx, id, child);
1319 else if (os_strcasecmp(name, "RoamingConsortiumOI") == 0)
1320 set_pps_cred_home_sp_roaming_consortium_oi(ctx, id,
1323 wpa_printf(MSG_INFO, "Unknown HomeSP node '%s'", name);
1328 static void set_pps_cred_sub_params(struct hs20_osu_client *ctx, int id,
1331 wpa_printf(MSG_INFO, "- SubscriptionParameters");
1332 /* not used within wpa_supplicant */
1336 static void set_pps_cred_creation_date(struct hs20_osu_client *ctx, int id,
1339 char *str = xml_node_get_text(ctx->xml, node);
1342 wpa_printf(MSG_INFO, "- Credential/CreationDate = %s", str);
1343 /* not used within wpa_supplicant */
1344 xml_node_get_text_free(ctx->xml, str);
1348 static void set_pps_cred_expiration_date(struct hs20_osu_client *ctx, int id,
1351 char *str = xml_node_get_text(ctx->xml, node);
1354 wpa_printf(MSG_INFO, "- Credential/ExpirationDate = %s", str);
1355 /* not used within wpa_supplicant */
1356 xml_node_get_text_free(ctx->xml, str);
1360 static void set_pps_cred_username(struct hs20_osu_client *ctx, int id,
1363 char *str = xml_node_get_text(ctx->xml, node);
1366 wpa_printf(MSG_INFO, "- Credential/UsernamePassword/Username = %s",
1368 if (set_cred_quoted(ctx->ifname, id, "username", str) < 0)
1369 wpa_printf(MSG_INFO, "Failed to set cred username");
1370 xml_node_get_text_free(ctx->xml, str);
1374 static void set_pps_cred_password(struct hs20_osu_client *ctx, int id,
1378 char *pw, *hex, *pos, *end;
1380 pw = xml_node_get_base64_text(ctx->xml, node, &len);
1384 wpa_printf(MSG_INFO, "- Credential/UsernamePassword/Password = %s", pw);
1386 hex = malloc(len * 2 + 1);
1391 end = hex + len * 2 + 1;
1393 for (i = 0; i < len; i++) {
1394 snprintf(pos, end - pos, "%02x", pw[i]);
1399 if (set_cred(ctx->ifname, id, "password", hex) < 0)
1400 wpa_printf(MSG_INFO, "Failed to set cred password");
1405 static void set_pps_cred_machine_managed(struct hs20_osu_client *ctx, int id,
1408 char *str = xml_node_get_text(ctx->xml, node);
1411 wpa_printf(MSG_INFO, "- Credential/UsernamePassword/MachineManaged = %s",
1413 /* not used within wpa_supplicant */
1414 xml_node_get_text_free(ctx->xml, str);
1418 static void set_pps_cred_soft_token_app(struct hs20_osu_client *ctx, int id,
1421 char *str = xml_node_get_text(ctx->xml, node);
1424 wpa_printf(MSG_INFO, "- Credential/UsernamePassword/SoftTokenApp = %s",
1426 /* not used within wpa_supplicant */
1427 xml_node_get_text_free(ctx->xml, str);
1431 static void set_pps_cred_able_to_share(struct hs20_osu_client *ctx, int id,
1434 char *str = xml_node_get_text(ctx->xml, node);
1437 wpa_printf(MSG_INFO, "- Credential/UsernamePassword/AbleToShare = %s",
1439 /* not used within wpa_supplicant */
1440 xml_node_get_text_free(ctx->xml, str);
1444 static void set_pps_cred_eap_method(struct hs20_osu_client *ctx, int id,
1447 wpa_printf(MSG_INFO, "- Credential/UsernamePassword/EAPMethod - TODO");
1451 static void set_pps_cred_username_password(struct hs20_osu_client *ctx, int id,
1457 wpa_printf(MSG_INFO, "- Credential/UsernamePassword");
1459 xml_node_for_each_child(ctx->xml, child, node) {
1460 xml_node_for_each_check(ctx->xml, child);
1461 name = xml_node_get_localname(ctx->xml, child);
1462 if (os_strcasecmp(name, "Username") == 0)
1463 set_pps_cred_username(ctx, id, child);
1464 else if (os_strcasecmp(name, "Password") == 0)
1465 set_pps_cred_password(ctx, id, child);
1466 else if (os_strcasecmp(name, "MachineManaged") == 0)
1467 set_pps_cred_machine_managed(ctx, id, child);
1468 else if (os_strcasecmp(name, "SoftTokenApp") == 0)
1469 set_pps_cred_soft_token_app(ctx, id, child);
1470 else if (os_strcasecmp(name, "AbleToShare") == 0)
1471 set_pps_cred_able_to_share(ctx, id, child);
1472 else if (os_strcasecmp(name, "EAPMethod") == 0)
1473 set_pps_cred_eap_method(ctx, id, child);
1475 wpa_printf(MSG_INFO, "Unknown Credential/UsernamePassword node '%s'",
1481 static void set_pps_cred_digital_cert(struct hs20_osu_client *ctx, int id,
1482 xml_node_t *node, const char *fqdn)
1484 char buf[200], dir[200];
1486 wpa_printf(MSG_INFO, "- Credential/DigitalCertificate");
1488 if (getcwd(dir, sizeof(dir)) == NULL)
1491 /* TODO: could build username from Subject of Subject AltName */
1492 if (set_cred_quoted(ctx->ifname, id, "username", "cert") < 0) {
1493 wpa_printf(MSG_INFO, "Failed to set username");
1496 snprintf(buf, sizeof(buf), "%s/SP/%s/client-cert.pem", dir, fqdn);
1497 if (os_file_exists(buf)) {
1498 if (set_cred_quoted(ctx->ifname, id, "client_cert", buf) < 0) {
1499 wpa_printf(MSG_INFO, "Failed to set client_cert");
1503 snprintf(buf, sizeof(buf), "%s/SP/%s/client-key.pem", dir, fqdn);
1504 if (os_file_exists(buf)) {
1505 if (set_cred_quoted(ctx->ifname, id, "private_key", buf) < 0) {
1506 wpa_printf(MSG_INFO, "Failed to set private_key");
1512 static void set_pps_cred_realm(struct hs20_osu_client *ctx, int id,
1513 xml_node_t *node, const char *fqdn, int sim)
1515 char *str = xml_node_get_text(ctx->xml, node);
1516 char buf[200], dir[200];
1521 wpa_printf(MSG_INFO, "- Credential/Realm = %s", str);
1522 if (set_cred_quoted(ctx->ifname, id, "realm", str) < 0)
1523 wpa_printf(MSG_INFO, "Failed to set cred realm");
1524 xml_node_get_text_free(ctx->xml, str);
1529 if (getcwd(dir, sizeof(dir)) == NULL)
1531 snprintf(buf, sizeof(buf), "%s/SP/%s/aaa-ca.pem", dir, fqdn);
1532 if (os_file_exists(buf)) {
1533 if (set_cred_quoted(ctx->ifname, id, "ca_cert", buf) < 0) {
1534 wpa_printf(MSG_INFO, "Failed to set CA cert");
1540 static void set_pps_cred_check_aaa_cert_status(struct hs20_osu_client *ctx,
1541 int id, xml_node_t *node)
1543 char *str = xml_node_get_text(ctx->xml, node);
1548 wpa_printf(MSG_INFO, "- Credential/CheckAAAServerCertStatus = %s", str);
1549 if (os_strcasecmp(str, "true") == 0 &&
1550 set_cred(ctx->ifname, id, "ocsp", "2") < 0)
1551 wpa_printf(MSG_INFO, "Failed to set cred ocsp");
1552 xml_node_get_text_free(ctx->xml, str);
1556 static void set_pps_cred_sim(struct hs20_osu_client *ctx, int id,
1557 xml_node_t *sim, xml_node_t *realm)
1560 char *imsi, *eaptype, *str, buf[20];
1565 node = get_node(ctx->xml, sim, "EAPType");
1567 wpa_printf(MSG_INFO, "No SIM/EAPType node in credential");
1570 eaptype = xml_node_get_text(ctx->xml, node);
1571 if (eaptype == NULL) {
1572 wpa_printf(MSG_INFO, "Could not extract SIM/EAPType");
1575 wpa_printf(MSG_INFO, " - Credential/SIM/EAPType = %s", eaptype);
1576 type = atoi(eaptype);
1577 xml_node_get_text_free(ctx->xml, eaptype);
1581 if (set_cred(ctx->ifname, id, "eap", "SIM") < 0)
1582 wpa_printf(MSG_INFO, "Could not set eap=SIM");
1585 if (set_cred(ctx->ifname, id, "eap", "AKA") < 0)
1586 wpa_printf(MSG_INFO, "Could not set eap=SIM");
1588 case EAP_TYPE_AKA_PRIME:
1589 if (set_cred(ctx->ifname, id, "eap", "AKA'") < 0)
1590 wpa_printf(MSG_INFO, "Could not set eap=SIM");
1593 wpa_printf(MSG_INFO, "Unsupported SIM/EAPType %d", type);
1597 node = get_node(ctx->xml, sim, "IMSI");
1599 wpa_printf(MSG_INFO, "No SIM/IMSI node in credential");
1602 imsi = xml_node_get_text(ctx->xml, node);
1604 wpa_printf(MSG_INFO, "Could not extract SIM/IMSI");
1607 wpa_printf(MSG_INFO, " - Credential/SIM/IMSI = %s", imsi);
1608 imsi_len = os_strlen(imsi);
1609 if (imsi_len < 7 || imsi_len + 2 > sizeof(buf)) {
1610 wpa_printf(MSG_INFO, "Invalid IMSI length");
1611 xml_node_get_text_free(ctx->xml, imsi);
1615 str = xml_node_get_text(ctx->xml, node);
1618 pos = os_strstr(str, "mnc");
1619 if (pos && os_strlen(pos) >= 6) {
1620 if (os_strncmp(imsi + 3, pos + 3, 3) == 0)
1622 else if (os_strncmp(imsi + 3, pos + 4, 2) == 0)
1625 xml_node_get_text_free(ctx->xml, str);
1628 os_memcpy(buf, imsi, 3 + mnc_len);
1629 buf[3 + mnc_len] = '-';
1630 os_strlcpy(buf + 3 + mnc_len + 1, imsi + 3 + mnc_len,
1631 sizeof(buf) - 3 - mnc_len - 1);
1633 xml_node_get_text_free(ctx->xml, imsi);
1635 if (set_cred_quoted(ctx->ifname, id, "imsi", buf) < 0)
1636 wpa_printf(MSG_INFO, "Could not set IMSI");
1638 if (set_cred_quoted(ctx->ifname, id, "milenage",
1639 "90dca4eda45b53cf0f12d7c9c3bc6a89:"
1640 "cb9cccc4b9258e6dca4760379fb82581:000000000123") <
1642 wpa_printf(MSG_INFO, "Could not set Milenage parameters");
1646 static void set_pps_cred_credential(struct hs20_osu_client *ctx, int id,
1647 xml_node_t *node, const char *fqdn)
1649 xml_node_t *child, *sim, *realm;
1652 wpa_printf(MSG_INFO, "- Credential");
1654 sim = get_node(ctx->xml, node, "SIM");
1655 realm = get_node(ctx->xml, node, "Realm");
1657 xml_node_for_each_child(ctx->xml, child, node) {
1658 xml_node_for_each_check(ctx->xml, child);
1659 name = xml_node_get_localname(ctx->xml, child);
1660 if (os_strcasecmp(name, "CreationDate") == 0)
1661 set_pps_cred_creation_date(ctx, id, child);
1662 else if (os_strcasecmp(name, "ExpirationDate") == 0)
1663 set_pps_cred_expiration_date(ctx, id, child);
1664 else if (os_strcasecmp(name, "UsernamePassword") == 0)
1665 set_pps_cred_username_password(ctx, id, child);
1666 else if (os_strcasecmp(name, "DigitalCertificate") == 0)
1667 set_pps_cred_digital_cert(ctx, id, child, fqdn);
1668 else if (os_strcasecmp(name, "Realm") == 0)
1669 set_pps_cred_realm(ctx, id, child, fqdn, sim != NULL);
1670 else if (os_strcasecmp(name, "CheckAAAServerCertStatus") == 0)
1671 set_pps_cred_check_aaa_cert_status(ctx, id, child);
1672 else if (os_strcasecmp(name, "SIM") == 0)
1673 set_pps_cred_sim(ctx, id, child, realm);
1675 wpa_printf(MSG_INFO, "Unknown Credential node '%s'",
1681 static void set_pps_credential(struct hs20_osu_client *ctx, int id,
1682 xml_node_t *cred, const char *fqdn)
1687 xml_node_for_each_child(ctx->xml, child, cred) {
1688 xml_node_for_each_check(ctx->xml, child);
1689 name = xml_node_get_localname(ctx->xml, child);
1690 if (os_strcasecmp(name, "Policy") == 0)
1691 set_pps_cred_policy(ctx, id, child);
1692 else if (os_strcasecmp(name, "CredentialPriority") == 0)
1693 set_pps_cred_priority(ctx, id, child);
1694 else if (os_strcasecmp(name, "AAAServerTrustRoot") == 0)
1695 set_pps_cred_aaa_server_trust_root(ctx, id, child);
1696 else if (os_strcasecmp(name, "SubscriptionUpdate") == 0)
1697 set_pps_cred_sub_update(ctx, id, child);
1698 else if (os_strcasecmp(name, "HomeSP") == 0)
1699 set_pps_cred_home_sp(ctx, id, child);
1700 else if (os_strcasecmp(name, "SubscriptionParameters") == 0)
1701 set_pps_cred_sub_params(ctx, id, child);
1702 else if (os_strcasecmp(name, "Credential") == 0)
1703 set_pps_cred_credential(ctx, id, child, fqdn);
1705 wpa_printf(MSG_INFO, "Unknown credential node '%s'",
1711 static void set_pps(struct hs20_osu_client *ctx, xml_node_t *pps,
1717 char *update_identifier = NULL;
1720 * TODO: Could consider more complex mechanism that would remove
1721 * credentials only if there are changes in the information sent to
1724 remove_sp_creds(ctx, fqdn);
1726 xml_node_for_each_child(ctx->xml, child, pps) {
1727 xml_node_for_each_check(ctx->xml, child);
1728 name = xml_node_get_localname(ctx->xml, child);
1729 if (os_strcasecmp(name, "UpdateIdentifier") == 0) {
1730 update_identifier = xml_node_get_text(ctx->xml, child);
1731 if (update_identifier) {
1732 wpa_printf(MSG_INFO, "- UpdateIdentifier = %s",
1739 xml_node_for_each_child(ctx->xml, child, pps) {
1740 xml_node_for_each_check(ctx->xml, child);
1741 name = xml_node_get_localname(ctx->xml, child);
1742 if (os_strcasecmp(name, "UpdateIdentifier") == 0)
1744 id = add_cred(ctx->ifname);
1746 wpa_printf(MSG_INFO, "Failed to add credential to wpa_supplicant");
1747 write_summary(ctx, "Failed to add credential to wpa_supplicant");
1750 write_summary(ctx, "Add a credential to wpa_supplicant");
1751 if (update_identifier &&
1752 set_cred(ctx->ifname, id, "update_identifier",
1753 update_identifier) < 0)
1754 wpa_printf(MSG_INFO, "Failed to set update_identifier");
1755 if (set_cred_quoted(ctx->ifname, id, "provisioning_sp", fqdn) <
1757 wpa_printf(MSG_INFO, "Failed to set provisioning_sp");
1758 wpa_printf(MSG_INFO, "credential localname: '%s'", name);
1759 set_pps_credential(ctx, id, child, fqdn);
1760 ctx->pps_cred_set = 1;
1763 xml_node_get_text_free(ctx->xml, update_identifier);
1767 void cmd_set_pps(struct hs20_osu_client *ctx, const char *pps_fname)
1771 char *fqdn_buf = NULL, *pos;
1773 pps = node_from_file(ctx->xml, pps_fname);
1775 wpa_printf(MSG_INFO, "Could not read or parse '%s'", pps_fname);
1779 fqdn = os_strstr(pps_fname, "SP/");
1781 fqdn_buf = os_strdup(fqdn + 3);
1782 if (fqdn_buf == NULL)
1784 pos = os_strchr(fqdn_buf, '/');
1791 wpa_printf(MSG_INFO, "Set PPS MO info to wpa_supplicant - SP FQDN %s",
1793 set_pps(ctx, pps, fqdn);
1796 xml_node_free(ctx->xml, pps);
1800 static int cmd_get_fqdn(struct hs20_osu_client *ctx, const char *pps_fname)
1802 xml_node_t *pps, *node;
1805 pps = node_from_file(ctx->xml, pps_fname);
1807 wpa_printf(MSG_INFO, "Could not read or parse '%s'", pps_fname);
1811 node = get_child_node(ctx->xml, pps, "HomeSP/FQDN");
1813 fqdn = xml_node_get_text(ctx->xml, node);
1815 xml_node_free(ctx->xml, pps);
1818 FILE *f = fopen("pps-fqdn", "w");
1820 fprintf(f, "%s", fqdn);
1823 xml_node_get_text_free(ctx->xml, fqdn);
1827 xml_node_get_text_free(ctx->xml, fqdn);
1832 static void cmd_to_tnds(struct hs20_osu_client *ctx, const char *in_fname,
1833 const char *out_fname, const char *urn, int use_path)
1835 xml_node_t *mo, *node;
1837 mo = node_from_file(ctx->xml, in_fname);
1839 wpa_printf(MSG_INFO, "Could not read or parse '%s'", in_fname);
1843 node = mo_to_tnds(ctx->xml, mo, use_path, urn, NULL);
1845 node_to_file(ctx->xml, out_fname, node);
1846 xml_node_free(ctx->xml, node);
1849 xml_node_free(ctx->xml, mo);
1853 static void cmd_from_tnds(struct hs20_osu_client *ctx, const char *in_fname,
1854 const char *out_fname)
1856 xml_node_t *tnds, *mo;
1858 tnds = node_from_file(ctx->xml, in_fname);
1860 wpa_printf(MSG_INFO, "Could not read or parse '%s'", in_fname);
1864 mo = tnds_to_mo(ctx->xml, tnds);
1866 node_to_file(ctx->xml, out_fname, mo);
1867 xml_node_free(ctx->xml, mo);
1870 xml_node_free(ctx->xml, tnds);
1877 char mime_type[256];
1884 unsigned int methods;
1887 struct osu_lang_text friendly_name[MAX_OSU_VALS];
1888 size_t friendly_name_count;
1889 struct osu_lang_text serv_desc[MAX_OSU_VALS];
1890 size_t serv_desc_count;
1891 struct osu_icon icon[MAX_OSU_VALS];
1896 static struct osu_data * parse_osu_providers(const char *fname, size_t *count)
1900 struct osu_data *osu = NULL, *last = NULL;
1901 size_t osu_count = 0;
1904 f = fopen(fname, "r");
1906 wpa_printf(MSG_ERROR, "Could not open %s", fname);
1910 while (fgets(buf, sizeof(buf), f)) {
1911 pos = strchr(buf, '\n');
1915 if (strncmp(buf, "OSU-PROVIDER ", 13) == 0) {
1916 last = realloc(osu, (osu_count + 1) * sizeof(*osu));
1920 last = &osu[osu_count++];
1921 memset(last, 0, sizeof(*last));
1922 snprintf(last->bssid, sizeof(last->bssid), "%s",
1929 if (strncmp(buf, "uri=", 4) == 0) {
1930 snprintf(last->url, sizeof(last->url), "%s", buf + 4);
1934 if (strncmp(buf, "methods=", 8) == 0) {
1935 last->methods = strtol(buf + 8, NULL, 16);
1939 if (strncmp(buf, "osu_ssid=", 9) == 0) {
1940 snprintf(last->osu_ssid, sizeof(last->osu_ssid),
1945 if (os_strncmp(buf, "osu_nai=", 8) == 0) {
1946 os_snprintf(last->osu_nai, sizeof(last->osu_nai),
1951 if (strncmp(buf, "friendly_name=", 14) == 0) {
1952 struct osu_lang_text *txt;
1953 if (last->friendly_name_count == MAX_OSU_VALS)
1955 pos = strchr(buf + 14, ':');
1959 txt = &last->friendly_name[last->friendly_name_count++];
1960 snprintf(txt->lang, sizeof(txt->lang), "%s", buf + 14);
1961 snprintf(txt->text, sizeof(txt->text), "%s", pos);
1964 if (strncmp(buf, "desc=", 5) == 0) {
1965 struct osu_lang_text *txt;
1966 if (last->serv_desc_count == MAX_OSU_VALS)
1968 pos = strchr(buf + 5, ':');
1972 txt = &last->serv_desc[last->serv_desc_count++];
1973 snprintf(txt->lang, sizeof(txt->lang), "%s", buf + 5);
1974 snprintf(txt->text, sizeof(txt->text), "%s", pos);
1977 if (strncmp(buf, "icon=", 5) == 0) {
1978 struct osu_icon *icon;
1979 if (last->icon_count == MAX_OSU_VALS)
1981 icon = &last->icon[last->icon_count++];
1982 icon->id = atoi(buf + 5);
1983 pos = strchr(buf, ':');
1986 pos = strchr(pos + 1, ':');
1989 pos = strchr(pos + 1, ':');
1993 end = strchr(pos, ':');
1997 snprintf(icon->lang, sizeof(icon->lang), "%s", pos);
2000 end = strchr(pos, ':');
2003 snprintf(icon->mime_type, sizeof(icon->mime_type),
2009 end = strchr(pos, ':');
2012 snprintf(icon->filename, sizeof(icon->filename),
2025 static int osu_connect(struct hs20_osu_client *ctx, const char *bssid,
2026 const char *ssid, const char *url,
2027 unsigned int methods, int no_prod_assoc,
2028 const char *osu_nai)
2031 const char *ifname = ctx->ifname;
2033 struct wpa_ctrl *mon;
2036 id = add_network(ifname);
2039 if (set_network_quoted(ifname, id, "ssid", ssid) < 0)
2041 if (osu_nai && os_strlen(osu_nai) > 0) {
2042 char dir[255], fname[300];
2043 if (getcwd(dir, sizeof(dir)) == NULL)
2045 os_snprintf(fname, sizeof(fname), "%s/osu-ca.pem", dir);
2047 if (set_network(ifname, id, "proto", "OSEN") < 0 ||
2048 set_network(ifname, id, "key_mgmt", "OSEN") < 0 ||
2049 set_network(ifname, id, "pairwise", "CCMP") < 0 ||
2050 set_network(ifname, id, "group", "GTK_NOT_USED") < 0 ||
2051 set_network(ifname, id, "eap", "WFA-UNAUTH-TLS") < 0 ||
2052 set_network(ifname, id, "ocsp", "2") < 0 ||
2053 set_network_quoted(ifname, id, "identity", osu_nai) < 0 ||
2054 set_network_quoted(ifname, id, "ca_cert", fname) < 0)
2057 if (set_network(ifname, id, "key_mgmt", "NONE") < 0)
2061 mon = open_wpa_mon(ifname);
2065 wpa_printf(MSG_INFO, "Associate with OSU SSID");
2066 write_summary(ctx, "Associate with OSU SSID");
2067 snprintf(buf, sizeof(buf), "SELECT_NETWORK %d", id);
2068 if (wpa_command(ifname, buf) < 0)
2071 res = get_wpa_cli_event(mon, "CTRL-EVENT-CONNECTED",
2074 wpa_ctrl_detach(mon);
2075 wpa_ctrl_close(mon);
2078 wpa_printf(MSG_INFO, "Could not connect");
2079 write_summary(ctx, "Could not connect to OSU network");
2080 wpa_printf(MSG_INFO, "Remove OSU network connection");
2081 snprintf(buf, sizeof(buf), "REMOVE_NETWORK %d", id);
2082 wpa_command(ifname, buf);
2086 write_summary(ctx, "Waiting for IP address for subscription registration");
2087 if (wait_ip_addr(ifname, 15) < 0) {
2088 wpa_printf(MSG_INFO, "Could not get IP address for WLAN - try connection anyway");
2091 if (no_prod_assoc) {
2094 wpa_printf(MSG_INFO, "No production connection used for testing purposes");
2095 write_summary(ctx, "No production connection used for testing purposes");
2099 ctx->no_reconnect = 1;
2100 if (methods & 0x02) {
2101 wpa_printf(MSG_DEBUG, "Calling cmd_prov from osu_connect");
2102 res = cmd_prov(ctx, url);
2103 } else if (methods & 0x01) {
2104 wpa_printf(MSG_DEBUG,
2105 "Calling cmd_oma_dm_prov from osu_connect");
2106 res = cmd_oma_dm_prov(ctx, url);
2109 wpa_printf(MSG_INFO, "Remove OSU network connection");
2110 write_summary(ctx, "Remove OSU network connection");
2111 snprintf(buf, sizeof(buf), "REMOVE_NETWORK %d", id);
2112 wpa_command(ifname, buf);
2117 wpa_printf(MSG_INFO, "Requesting reconnection with updated configuration");
2118 write_summary(ctx, "Requesting reconnection with updated configuration");
2119 if (wpa_command(ctx->ifname, "INTERWORKING_SELECT auto") < 0) {
2120 wpa_printf(MSG_INFO, "Failed to request wpa_supplicant to reconnect");
2121 write_summary(ctx, "Failed to request wpa_supplicant to reconnect");
2129 static int cmd_osu_select(struct hs20_osu_client *ctx, const char *dir,
2130 int connect, int no_prod_assoc,
2131 const char *friendly_name)
2135 struct osu_data *osu = NULL, *last = NULL;
2136 size_t osu_count, i, j;
2139 write_summary(ctx, "OSU provider selection");
2142 wpa_printf(MSG_INFO, "Missing dir parameter to osu_select");
2146 snprintf(fname, sizeof(fname), "%s/osu-providers.txt", dir);
2147 osu = parse_osu_providers(fname, &osu_count);
2149 wpa_printf(MSG_INFO, "Could not find any OSU providers from %s",
2151 write_result(ctx, "No OSU providers available");
2155 if (friendly_name) {
2156 for (i = 0; i < osu_count; i++) {
2158 for (j = 0; j < last->friendly_name_count; j++) {
2159 if (os_strcmp(last->friendly_name[j].text,
2160 friendly_name) == 0)
2163 if (j < last->friendly_name_count)
2166 if (i == osu_count) {
2167 wpa_printf(MSG_INFO, "Requested operator friendly name '%s' not found in the list of available providers",
2169 write_summary(ctx, "Requested operator friendly name '%s' not found in the list of available providers",
2175 wpa_printf(MSG_INFO, "OSU Provider selected based on requested operator friendly name '%s'",
2177 write_summary(ctx, "OSU Provider selected based on requested operator friendly name '%s'",
2183 snprintf(fname, sizeof(fname), "%s/osu-providers.html", dir);
2184 f = fopen(fname, "w");
2186 wpa_printf(MSG_INFO, "Could not open %s", fname);
2191 fprintf(f, "<html><head>"
2192 "<meta http-equiv=\"Content-type\" content=\"text/html; "
2193 "charset=utf-8\"<title>Select service operator</title>"
2194 "</head><body><h1>Select service operator</h1>\n");
2197 fprintf(f, "No online signup available\n");
2199 for (i = 0; i < osu_count; i++) {
2203 "<a href=\"http://localhost:12345/osu/%d\">"
2204 "<table><tr><td>", (int) i + 1);
2207 "<a href=\"osu://%d\">"
2208 "<table><tr><td>", (int) i + 1);
2209 #endif /* ANDROID */
2210 for (j = 0; j < last->icon_count; j++) {
2211 fprintf(f, "<img src=\"osu-icon-%d.%s\">\n",
2213 strcasecmp(last->icon[j].mime_type,
2214 "image/png") == 0 ? "png" : "icon");
2217 for (j = 0; j < last->friendly_name_count; j++) {
2218 fprintf(f, "<small>[%s]</small> %s<br>\n",
2219 last->friendly_name[j].lang,
2220 last->friendly_name[j].text);
2222 fprintf(f, "<tr><td colspan=2>");
2223 for (j = 0; j < last->serv_desc_count; j++) {
2224 fprintf(f, "<small>[%s]</small> %s<br>\n",
2225 last->serv_desc[j].lang,
2226 last->serv_desc[j].text);
2228 fprintf(f, "</table></a><br><small>BSSID: %s<br>\n"
2230 last->bssid, last->osu_ssid);
2232 fprintf(f, "NAI: %s<br>\n", last->osu_nai);
2233 fprintf(f, "URL: %s<br>\n"
2234 "methods:%s%s<br>\n"
2237 last->methods & 0x01 ? " OMA-DM" : "",
2238 last->methods & 0x02 ? " SOAP-XML-SPP" : "");
2241 fprintf(f, "</body></html>\n");
2245 snprintf(fname, sizeof(fname), "file://%s/osu-providers.html", dir);
2246 write_summary(ctx, "Start web browser with OSU provider selection page");
2247 ret = hs20_web_browser(fname);
2250 if (ret > 0 && (size_t) ret <= osu_count) {
2254 wpa_printf(MSG_INFO, "Selected OSU id=%d", ret);
2255 last = &osu[ret - 1];
2257 wpa_printf(MSG_INFO, "BSSID: %s", last->bssid);
2258 wpa_printf(MSG_INFO, "SSID: %s", last->osu_ssid);
2259 wpa_printf(MSG_INFO, "URL: %s", last->url);
2260 write_summary(ctx, "Selected OSU provider id=%d BSSID=%s SSID=%s URL=%s",
2261 ret, last->bssid, last->osu_ssid, last->url);
2263 ctx->friendly_name_count = last->friendly_name_count;
2264 for (j = 0; j < last->friendly_name_count; j++) {
2265 wpa_printf(MSG_INFO, "FRIENDLY_NAME: [%s]%s",
2266 last->friendly_name[j].lang,
2267 last->friendly_name[j].text);
2268 os_strlcpy(ctx->friendly_name[j].lang,
2269 last->friendly_name[j].lang,
2270 sizeof(ctx->friendly_name[j].lang));
2271 os_strlcpy(ctx->friendly_name[j].text,
2272 last->friendly_name[j].text,
2273 sizeof(ctx->friendly_name[j].text));
2276 ctx->icon_count = last->icon_count;
2277 for (j = 0; j < last->icon_count; j++) {
2280 os_snprintf(fname, sizeof(fname), "%s/osu-icon-%d.%s",
2281 dir, last->icon[j].id,
2282 strcasecmp(last->icon[j].mime_type,
2285 wpa_printf(MSG_INFO, "ICON: %s (%s)",
2286 fname, last->icon[j].filename);
2287 os_strlcpy(ctx->icon_filename[j],
2288 last->icon[j].filename,
2289 sizeof(ctx->icon_filename[j]));
2291 data = os_readfile(fname, &data_len);
2293 sha256_vector(1, (const u8 **) &data, &data_len,
2300 if (last->methods & 0x02) {
2301 wpa_printf(MSG_DEBUG,
2302 "Calling cmd_prov from cmd_osu_select");
2303 ret = cmd_prov(ctx, last->url);
2304 } else if (last->methods & 0x01) {
2305 wpa_printf(MSG_DEBUG,
2306 "Calling cmd_oma_dm_prov from cmd_osu_select");
2307 ret = cmd_oma_dm_prov(ctx, last->url);
2309 wpa_printf(MSG_DEBUG,
2310 "No supported OSU provisioning method");
2314 ret = osu_connect(ctx, last->bssid, last->osu_ssid,
2315 last->url, last->methods,
2316 no_prod_assoc, last->osu_nai);
2326 static int cmd_signup(struct hs20_osu_client *ctx, int no_prod_assoc,
2327 const char *friendly_name)
2330 char fname[300], buf[400];
2331 struct wpa_ctrl *mon;
2335 ifname = ctx->ifname;
2337 if (getcwd(dir, sizeof(dir)) == NULL)
2340 snprintf(fname, sizeof(fname), "%s/osu-info", dir);
2341 if (mkdir(fname, S_IRWXU | S_IRWXG) < 0 && errno != EEXIST) {
2342 wpa_printf(MSG_INFO, "mkdir(%s) failed: %s",
2343 fname, strerror(errno));
2347 snprintf(buf, sizeof(buf), "SET osu_dir %s", fname);
2348 if (wpa_command(ifname, buf) < 0) {
2349 wpa_printf(MSG_INFO, "Failed to configure osu_dir to wpa_supplicant");
2353 mon = open_wpa_mon(ifname);
2357 wpa_printf(MSG_INFO, "Starting OSU fetch");
2358 write_summary(ctx, "Starting OSU provider information fetch");
2359 if (wpa_command(ifname, "FETCH_OSU") < 0) {
2360 wpa_printf(MSG_INFO, "Could not start OSU fetch");
2361 wpa_ctrl_detach(mon);
2362 wpa_ctrl_close(mon);
2365 res = get_wpa_cli_event(mon, "OSU provider fetch completed",
2368 wpa_ctrl_detach(mon);
2369 wpa_ctrl_close(mon);
2372 wpa_printf(MSG_INFO, "OSU fetch did not complete");
2373 write_summary(ctx, "OSU fetch did not complete");
2376 wpa_printf(MSG_INFO, "OSU provider fetch completed");
2378 return cmd_osu_select(ctx, fname, 1, no_prod_assoc, friendly_name);
2382 static int cmd_sub_rem(struct hs20_osu_client *ctx, const char *address,
2383 const char *pps_fname, const char *ca_fname)
2385 xml_node_t *pps, *node;
2386 char pps_fname_buf[300];
2387 char ca_fname_buf[200];
2388 char *cred_username = NULL;
2389 char *cred_password = NULL;
2390 char *sub_rem_uri = NULL;
2391 char client_cert_buf[200];
2392 char *client_cert = NULL;
2393 char client_key_buf[200];
2394 char *client_key = NULL;
2397 wpa_printf(MSG_INFO, "Subscription remediation requested with Server URL: %s",
2402 wpa_printf(MSG_INFO, "Determining PPS file based on Home SP information");
2403 if (os_strncmp(address, "fqdn=", 5) == 0) {
2404 wpa_printf(MSG_INFO, "Use requested FQDN from command line");
2405 os_snprintf(buf, sizeof(buf), "%s", address + 5);
2407 } else if (get_wpa_status(ctx->ifname, "provisioning_sp", buf,
2409 wpa_printf(MSG_INFO, "Could not get provisioning Home SP FQDN from wpa_supplicant");
2413 ctx->fqdn = os_strdup(buf);
2414 if (ctx->fqdn == NULL)
2416 wpa_printf(MSG_INFO, "Home SP FQDN for current credential: %s",
2418 os_snprintf(pps_fname_buf, sizeof(pps_fname_buf),
2419 "SP/%s/pps.xml", ctx->fqdn);
2420 pps_fname = pps_fname_buf;
2422 os_snprintf(ca_fname_buf, sizeof(ca_fname_buf), "SP/%s/ca.pem",
2424 ca_fname = ca_fname_buf;
2427 if (!os_file_exists(pps_fname)) {
2428 wpa_printf(MSG_INFO, "PPS file '%s' does not exist or is not accessible",
2432 wpa_printf(MSG_INFO, "Using PPS file: %s", pps_fname);
2434 if (ca_fname && !os_file_exists(ca_fname)) {
2435 wpa_printf(MSG_INFO, "CA file '%s' does not exist or is not accessible",
2439 wpa_printf(MSG_INFO, "Using server trust root: %s", ca_fname);
2440 ctx->ca_fname = ca_fname;
2442 pps = node_from_file(ctx->xml, pps_fname);
2444 wpa_printf(MSG_INFO, "Could not read PPS MO");
2450 node = get_child_node(ctx->xml, pps, "HomeSP/FQDN");
2452 wpa_printf(MSG_INFO, "No HomeSP/FQDN found from PPS");
2455 tmp = xml_node_get_text(ctx->xml, node);
2457 wpa_printf(MSG_INFO, "No HomeSP/FQDN text found from PPS");
2460 ctx->fqdn = os_strdup(tmp);
2461 xml_node_get_text_free(ctx->xml, tmp);
2463 wpa_printf(MSG_INFO, "No FQDN known");
2468 node = get_child_node(ctx->xml, pps,
2469 "SubscriptionUpdate/UpdateMethod");
2472 tmp = xml_node_get_text(ctx->xml, node);
2473 if (tmp && os_strcasecmp(tmp, "OMA-DM-ClientInitiated") == 0)
2478 wpa_printf(MSG_INFO, "No UpdateMethod specified - assume SPP");
2482 get_user_pw(ctx, pps, "SubscriptionUpdate/UsernamePassword",
2483 &cred_username, &cred_password);
2485 wpa_printf(MSG_INFO, "Using username: %s", cred_username);
2487 wpa_printf(MSG_DEBUG, "Using password: %s", cred_password);
2489 if (cred_username == NULL && cred_password == NULL &&
2490 get_child_node(ctx->xml, pps, "Credential/DigitalCertificate")) {
2491 wpa_printf(MSG_INFO, "Using client certificate");
2492 os_snprintf(client_cert_buf, sizeof(client_cert_buf),
2493 "SP/%s/client-cert.pem", ctx->fqdn);
2494 client_cert = client_cert_buf;
2495 os_snprintf(client_key_buf, sizeof(client_key_buf),
2496 "SP/%s/client-key.pem", ctx->fqdn);
2497 client_key = client_key_buf;
2498 ctx->client_cert_present = 1;
2501 node = get_child_node(ctx->xml, pps, "SubscriptionUpdate/URI");
2503 sub_rem_uri = xml_node_get_text(ctx->xml, node);
2505 (!address || os_strcmp(address, sub_rem_uri) != 0)) {
2506 wpa_printf(MSG_INFO, "Override sub rem URI based on PPS: %s",
2508 address = sub_rem_uri;
2512 wpa_printf(MSG_INFO, "Server URL not known");
2516 write_summary(ctx, "Wait for IP address for subscriptiom remediation");
2517 wpa_printf(MSG_INFO, "Wait for IP address before starting subscription remediation");
2519 if (wait_ip_addr(ctx->ifname, 15) < 0) {
2520 wpa_printf(MSG_INFO, "Could not get IP address for WLAN - try connection anyway");
2524 spp_sub_rem(ctx, address, pps_fname,
2525 client_cert, client_key,
2526 cred_username, cred_password, pps);
2528 oma_dm_sub_rem(ctx, address, pps_fname,
2529 client_cert, client_key,
2530 cred_username, cred_password, pps);
2532 xml_node_get_text_free(ctx->xml, sub_rem_uri);
2533 xml_node_get_text_free(ctx->xml, cred_username);
2534 str_clear_free(cred_password);
2535 xml_node_free(ctx->xml, pps);
2540 static int cmd_pol_upd(struct hs20_osu_client *ctx, const char *address,
2541 const char *pps_fname, const char *ca_fname)
2545 char pps_fname_buf[300];
2546 char ca_fname_buf[200];
2548 char *cred_username = NULL;
2549 char *cred_password = NULL;
2550 char client_cert_buf[200];
2551 char *client_cert = NULL;
2552 char client_key_buf[200];
2553 char *client_key = NULL;
2556 wpa_printf(MSG_INFO, "Policy update requested");
2560 wpa_printf(MSG_INFO, "Determining PPS file based on Home SP information");
2561 if (os_strncmp(address, "fqdn=", 5) == 0) {
2562 wpa_printf(MSG_INFO, "Use requested FQDN from command line");
2563 os_snprintf(buf, sizeof(buf), "%s", address + 5);
2565 } else if (get_wpa_status(ctx->ifname, "provisioning_sp", buf,
2567 wpa_printf(MSG_INFO, "Could not get provisioning Home SP FQDN from wpa_supplicant");
2571 ctx->fqdn = os_strdup(buf);
2572 if (ctx->fqdn == NULL)
2574 wpa_printf(MSG_INFO, "Home SP FQDN for current credential: %s",
2576 os_snprintf(pps_fname_buf, sizeof(pps_fname_buf),
2577 "SP/%s/pps.xml", ctx->fqdn);
2578 pps_fname = pps_fname_buf;
2580 os_snprintf(ca_fname_buf, sizeof(ca_fname_buf), "SP/%s/ca.pem",
2582 ca_fname = ca_fname_buf;
2585 if (!os_file_exists(pps_fname)) {
2586 wpa_printf(MSG_INFO, "PPS file '%s' does not exist or is not accessible",
2590 wpa_printf(MSG_INFO, "Using PPS file: %s", pps_fname);
2592 if (ca_fname && !os_file_exists(ca_fname)) {
2593 wpa_printf(MSG_INFO, "CA file '%s' does not exist or is not accessible",
2597 wpa_printf(MSG_INFO, "Using server trust root: %s", ca_fname);
2598 ctx->ca_fname = ca_fname;
2600 pps = node_from_file(ctx->xml, pps_fname);
2602 wpa_printf(MSG_INFO, "Could not read PPS MO");
2608 node = get_child_node(ctx->xml, pps, "HomeSP/FQDN");
2610 wpa_printf(MSG_INFO, "No HomeSP/FQDN found from PPS");
2613 tmp = xml_node_get_text(ctx->xml, node);
2615 wpa_printf(MSG_INFO, "No HomeSP/FQDN text found from PPS");
2618 ctx->fqdn = os_strdup(tmp);
2619 xml_node_get_text_free(ctx->xml, tmp);
2621 wpa_printf(MSG_INFO, "No FQDN known");
2626 node = get_child_node(ctx->xml, pps,
2627 "Policy/PolicyUpdate/UpdateMethod");
2630 tmp = xml_node_get_text(ctx->xml, node);
2631 if (tmp && os_strcasecmp(tmp, "OMA-DM-ClientInitiated") == 0)
2636 wpa_printf(MSG_INFO, "No UpdateMethod specified - assume SPP");
2640 get_user_pw(ctx, pps, "Policy/PolicyUpdate/UsernamePassword",
2641 &cred_username, &cred_password);
2643 wpa_printf(MSG_INFO, "Using username: %s", cred_username);
2645 wpa_printf(MSG_DEBUG, "Using password: %s", cred_password);
2647 if (cred_username == NULL && cred_password == NULL &&
2648 get_child_node(ctx->xml, pps, "Credential/DigitalCertificate")) {
2649 wpa_printf(MSG_INFO, "Using client certificate");
2650 os_snprintf(client_cert_buf, sizeof(client_cert_buf),
2651 "SP/%s/client-cert.pem", ctx->fqdn);
2652 client_cert = client_cert_buf;
2653 os_snprintf(client_key_buf, sizeof(client_key_buf),
2654 "SP/%s/client-key.pem", ctx->fqdn);
2655 client_key = client_key_buf;
2659 node = get_child_node(ctx->xml, pps, "Policy/PolicyUpdate/URI");
2661 uri = xml_node_get_text(ctx->xml, node);
2662 wpa_printf(MSG_INFO, "URI based on PPS: %s", uri);
2667 wpa_printf(MSG_INFO, "Server URL not known");
2672 spp_pol_upd(ctx, address, pps_fname,
2673 client_cert, client_key,
2674 cred_username, cred_password, pps);
2676 oma_dm_pol_upd(ctx, address, pps_fname,
2677 client_cert, client_key,
2678 cred_username, cred_password, pps);
2680 xml_node_get_text_free(ctx->xml, uri);
2681 xml_node_get_text_free(ctx->xml, cred_username);
2682 str_clear_free(cred_password);
2683 xml_node_free(ctx->xml, pps);
2689 static char * get_hostname(const char *url)
2691 const char *pos, *end, *end2;
2697 pos = os_strchr(url, '/');
2705 end = os_strchr(pos, '/');
2706 end2 = os_strchr(pos, ':');
2707 if ((end && end2 && end2 < end) || (!end && end2))
2719 ret = os_malloc(end - pos + 2);
2723 os_memcpy(ret, pos, end - pos + 1);
2724 ret[end - pos + 1] = '\0';
2730 static int osu_cert_cb(void *_ctx, struct http_cert *cert)
2732 struct hs20_osu_client *ctx = _ctx;
2737 wpa_printf(MSG_INFO, "osu_cert_cb(osu_cert_validation=%d, url=%s)",
2738 !ctx->no_osu_cert_validation, ctx->server_url);
2740 host = get_hostname(ctx->server_url);
2742 for (i = 0; i < ctx->server_dnsname_count; i++)
2743 os_free(ctx->server_dnsname[i]);
2744 os_free(ctx->server_dnsname);
2745 ctx->server_dnsname = os_calloc(cert->num_dnsname, sizeof(char *));
2746 ctx->server_dnsname_count = 0;
2749 for (i = 0; i < cert->num_dnsname; i++) {
2750 if (ctx->server_dnsname) {
2751 ctx->server_dnsname[ctx->server_dnsname_count] =
2752 os_strdup(cert->dnsname[i]);
2753 if (ctx->server_dnsname[ctx->server_dnsname_count])
2754 ctx->server_dnsname_count++;
2756 if (host && os_strcasecmp(host, cert->dnsname[i]) == 0)
2758 wpa_printf(MSG_INFO, "dNSName '%s'", cert->dnsname[i]);
2761 if (host && !found) {
2762 wpa_printf(MSG_INFO, "Server name from URL (%s) did not match any dNSName - abort connection",
2764 write_result(ctx, "Server name from URL (%s) did not match any dNSName - abort connection",
2772 for (i = 0; i < cert->num_othername; i++) {
2773 if (os_strcmp(cert->othername[i].oid,
2774 "1.3.6.1.4.1.40808.1.1.1") == 0) {
2775 wpa_hexdump_ascii(MSG_INFO,
2776 "id-wfa-hotspot-friendlyName",
2777 cert->othername[i].data,
2778 cert->othername[i].len);
2782 for (j = 0; !ctx->no_osu_cert_validation &&
2783 j < ctx->friendly_name_count; j++) {
2785 for (i = 0; i < cert->num_othername; i++) {
2786 if (os_strcmp(cert->othername[i].oid,
2787 "1.3.6.1.4.1.40808.1.1.1") != 0)
2789 if (cert->othername[i].len < 3)
2791 if (os_strncasecmp((char *) cert->othername[i].data,
2792 ctx->friendly_name[j].lang, 3) != 0)
2794 if (os_strncmp((char *) cert->othername[i].data + 3,
2795 ctx->friendly_name[j].text,
2796 cert->othername[i].len - 3) == 0) {
2803 wpa_printf(MSG_INFO, "No friendly name match found for '[%s]%s'",
2804 ctx->friendly_name[j].lang,
2805 ctx->friendly_name[j].text);
2806 write_result(ctx, "No friendly name match found for '[%s]%s'",
2807 ctx->friendly_name[j].lang,
2808 ctx->friendly_name[j].text);
2813 for (i = 0; i < cert->num_logo; i++) {
2814 struct http_logo *logo = &cert->logo[i];
2816 wpa_printf(MSG_INFO, "logo hash alg %s uri '%s'",
2817 logo->alg_oid, logo->uri);
2818 wpa_hexdump_ascii(MSG_INFO, "hashValue",
2819 logo->hash, logo->hash_len);
2822 for (j = 0; !ctx->no_osu_cert_validation && j < ctx->icon_count; j++) {
2824 char *name = ctx->icon_filename[j];
2825 size_t name_len = os_strlen(name);
2827 wpa_printf(MSG_INFO,
2828 "[%i] Looking for icon file name '%s' match",
2830 for (i = 0; i < cert->num_logo; i++) {
2831 struct http_logo *logo = &cert->logo[i];
2832 size_t uri_len = os_strlen(logo->uri);
2835 wpa_printf(MSG_INFO,
2836 "[%i] Comparing to '%s' uri_len=%d name_len=%d",
2837 i, logo->uri, (int) uri_len, (int) name_len);
2838 if (uri_len < 1 + name_len) {
2839 wpa_printf(MSG_INFO, "URI Length is too short");
2842 pos = &logo->uri[uri_len - name_len - 1];
2846 if (os_strcmp(pos, name) == 0) {
2853 wpa_printf(MSG_INFO, "No icon filename match found for '%s'",
2856 "No icon filename match found for '%s'",
2862 for (j = 0; !ctx->no_osu_cert_validation && j < ctx->icon_count; j++) {
2865 for (i = 0; i < cert->num_logo; i++) {
2866 struct http_logo *logo = &cert->logo[i];
2868 if (logo->hash_len != 32) {
2869 wpa_printf(MSG_INFO,
2870 "[%i][%i] Icon hash length invalid (should be 32): %d",
2871 j, i, (int) logo->hash_len);
2874 if (os_memcmp(logo->hash, ctx->icon_hash[j], 32) == 0) {
2879 wpa_printf(MSG_DEBUG,
2880 "[%u][%u] Icon hash did not match", j, i);
2881 wpa_hexdump_ascii(MSG_DEBUG, "logo->hash",
2883 wpa_hexdump_ascii(MSG_DEBUG, "ctx->icon_hash[j]",
2884 ctx->icon_hash[j], 32);
2888 wpa_printf(MSG_INFO,
2889 "No icon hash match (by hash) found");
2891 "No icon hash match (by hash) found");
2900 static int init_ctx(struct hs20_osu_client *ctx)
2902 xml_node_t *devinfo, *devid;
2904 os_memset(ctx, 0, sizeof(*ctx));
2905 ctx->ifname = "wlan0";
2906 ctx->xml = xml_node_init_ctx(ctx, NULL);
2907 if (ctx->xml == NULL)
2910 devinfo = node_from_file(ctx->xml, "devinfo.xml");
2912 wpa_printf(MSG_ERROR, "devinfo.xml not found");
2916 devid = get_node(ctx->xml, devinfo, "DevId");
2918 char *tmp = xml_node_get_text(ctx->xml, devid);
2920 ctx->devid = os_strdup(tmp);
2921 xml_node_get_text_free(ctx->xml, tmp);
2924 xml_node_free(ctx->xml, devinfo);
2926 if (ctx->devid == NULL) {
2927 wpa_printf(MSG_ERROR, "Could not fetch DevId from devinfo.xml");
2931 ctx->http = http_init_ctx(ctx, ctx->xml);
2932 if (ctx->http == NULL) {
2933 xml_node_deinit_ctx(ctx->xml);
2936 http_ocsp_set(ctx->http, 2);
2937 http_set_cert_cb(ctx->http, osu_cert_cb, ctx);
2943 static void deinit_ctx(struct hs20_osu_client *ctx)
2947 http_deinit_ctx(ctx->http);
2948 xml_node_deinit_ctx(ctx->xml);
2950 os_free(ctx->server_url);
2951 os_free(ctx->devid);
2953 for (i = 0; i < ctx->server_dnsname_count; i++)
2954 os_free(ctx->server_dnsname[i]);
2955 os_free(ctx->server_dnsname);
2959 static void check_workarounds(struct hs20_osu_client *ctx)
2963 unsigned long int val = 0;
2965 f = fopen("hs20-osu-client.workarounds", "r");
2969 if (fgets(buf, sizeof(buf), f))
2970 val = strtoul(buf, NULL, 16);
2975 wpa_printf(MSG_INFO, "Workarounds enabled: 0x%lx", val);
2976 ctx->workarounds = val;
2977 if (ctx->workarounds & WORKAROUND_OCSP_OPTIONAL)
2978 http_ocsp_set(ctx->http, 1);
2983 static void usage(void)
2985 printf("usage: hs20-osu-client [-dddqqKt] [-S<station ifname>] \\\n"
2986 " [-w<wpa_supplicant ctrl_iface dir>] "
2987 "[-r<result file>] [-f<debug file>] \\\n"
2988 " [-s<summary file>] \\\n"
2989 " [-x<spp.xsd file name>] \\\n"
2990 " <command> [arguments..]\n"
2992 "- to_tnds <XML MO> <XML MO in TNDS format> [URN]\n"
2993 "- to_tnds2 <XML MO> <XML MO in TNDS format (Path) "
2995 "- from_tnds <XML MO in TNDS format> <XML MO>\n"
2996 "- set_pps <PerProviderSubscription XML file name>\n"
2997 "- get_fqdn <PerProviderSubscription XML file name>\n"
2998 "- pol_upd [Server URL] [PPS] [CA cert]\n"
2999 "- sub_rem <Server URL> [PPS] [CA cert]\n"
3000 "- prov <Server URL> [CA cert]\n"
3001 "- oma_dm_prov <Server URL> [CA cert]\n"
3002 "- sim_prov <Server URL> [CA cert]\n"
3003 "- oma_dm_sim_prov <Server URL> [CA cert]\n"
3004 "- signup [CA cert]\n"
3005 "- dl_osu_ca <PPS> <CA file>\n"
3006 "- dl_polupd_ca <PPS> <CA file>\n"
3007 "- dl_aaa_ca <PPS> <CA file>\n"
3009 "- parse_cert <X.509 certificate (DER)>\n"
3010 "- osu_select <OSU info directory> [CA cert]\n");
3014 int main(int argc, char *argv[])
3016 struct hs20_osu_client ctx;
3019 int no_prod_assoc = 0;
3020 const char *friendly_name = NULL;
3021 const char *wpa_debug_file_path = NULL;
3022 extern char *wpas_ctrl_path;
3023 extern int wpa_debug_level;
3024 extern int wpa_debug_show_keys;
3025 extern int wpa_debug_timestamp;
3027 if (init_ctx(&ctx) < 0)
3031 c = getopt(argc, argv, "df:hKNO:qr:s:S:tw:x:");
3036 if (wpa_debug_level > 0)
3040 wpa_debug_file_path = optarg;
3043 wpa_debug_show_keys++;
3049 friendly_name = optarg;
3055 ctx.result_file = optarg;
3058 ctx.summary_file = optarg;
3061 ctx.ifname = optarg;
3064 wpa_debug_timestamp++;
3067 wpas_ctrl_path = optarg;
3070 spp_xsd_fname = optarg;
3080 if (argc - optind < 1) {
3085 wpa_debug_open_file(wpa_debug_file_path);
3089 #endif /* __linux__ */
3091 if (ctx.result_file)
3092 unlink(ctx.result_file);
3093 wpa_printf(MSG_DEBUG, "===[hs20-osu-client START - command: %s ]======"
3094 "================", argv[optind]);
3095 check_workarounds(&ctx);
3097 if (strcmp(argv[optind], "to_tnds") == 0) {
3098 if (argc - optind < 2) {
3102 cmd_to_tnds(&ctx, argv[optind + 1], argv[optind + 2],
3103 argc > optind + 3 ? argv[optind + 3] : NULL,
3105 } else if (strcmp(argv[optind], "to_tnds2") == 0) {
3106 if (argc - optind < 2) {
3110 cmd_to_tnds(&ctx, argv[optind + 1], argv[optind + 2],
3111 argc > optind + 3 ? argv[optind + 3] : NULL,
3113 } else if (strcmp(argv[optind], "from_tnds") == 0) {
3114 if (argc - optind < 2) {
3118 cmd_from_tnds(&ctx, argv[optind + 1], argv[optind + 2]);
3119 } else if (strcmp(argv[optind], "sub_rem") == 0) {
3120 if (argc - optind < 2) {
3124 if (argc - optind < 2)
3125 wpa_printf(MSG_ERROR, "Server URL missing from command line");
3127 ret = cmd_sub_rem(&ctx, argv[optind + 1],
3129 argv[optind + 2] : NULL,
3131 argv[optind + 3] : NULL);
3132 } else if (strcmp(argv[optind], "pol_upd") == 0) {
3133 if (argc - optind < 2) {
3137 ret = cmd_pol_upd(&ctx, argc > 2 ? argv[optind + 1] : NULL,
3138 argc > optind + 2 ? argv[optind + 2] : NULL,
3139 argc > optind + 3 ? argv[optind + 3] : NULL);
3140 } else if (strcmp(argv[optind], "prov") == 0) {
3141 if (argc - optind < 2) {
3145 ctx.ca_fname = argv[optind + 2];
3146 wpa_printf(MSG_DEBUG, "Calling cmd_prov from main");
3147 cmd_prov(&ctx, argv[optind + 1]);
3148 } else if (strcmp(argv[optind], "sim_prov") == 0) {
3149 if (argc - optind < 2) {
3153 ctx.ca_fname = argv[optind + 2];
3154 cmd_sim_prov(&ctx, argv[optind + 1]);
3155 } else if (strcmp(argv[optind], "dl_osu_ca") == 0) {
3156 if (argc - optind < 2) {
3160 cmd_dl_osu_ca(&ctx, argv[optind + 1], argv[optind + 2]);
3161 } else if (strcmp(argv[optind], "dl_polupd_ca") == 0) {
3162 if (argc - optind < 2) {
3166 cmd_dl_polupd_ca(&ctx, argv[optind + 1], argv[optind + 2]);
3167 } else if (strcmp(argv[optind], "dl_aaa_ca") == 0) {
3168 if (argc - optind < 2) {
3172 cmd_dl_aaa_ca(&ctx, argv[optind + 1], argv[optind + 2]);
3173 } else if (strcmp(argv[optind], "osu_select") == 0) {
3174 if (argc - optind < 2) {
3178 ctx.ca_fname = argc > optind + 2 ? argv[optind + 2] : NULL;
3179 cmd_osu_select(&ctx, argv[optind + 1], 2, 1, NULL);
3180 } else if (strcmp(argv[optind], "signup") == 0) {
3181 ctx.ca_fname = argc > optind + 1 ? argv[optind + 1] : NULL;
3182 ret = cmd_signup(&ctx, no_prod_assoc, friendly_name);
3183 } else if (strcmp(argv[optind], "set_pps") == 0) {
3184 if (argc - optind < 2) {
3188 cmd_set_pps(&ctx, argv[optind + 1]);
3189 } else if (strcmp(argv[optind], "get_fqdn") == 0) {
3190 if (argc - optind < 1) {
3194 ret = cmd_get_fqdn(&ctx, argv[optind + 1]);
3195 } else if (strcmp(argv[optind], "oma_dm_prov") == 0) {
3196 if (argc - optind < 2) {
3200 ctx.ca_fname = argv[optind + 2];
3201 cmd_oma_dm_prov(&ctx, argv[optind + 1]);
3202 } else if (strcmp(argv[optind], "oma_dm_sim_prov") == 0) {
3203 if (argc - optind < 2) {
3207 ctx.ca_fname = argv[optind + 2];
3208 if (cmd_oma_dm_sim_prov(&ctx, argv[optind + 1]) < 0) {
3209 write_summary(&ctx, "Failed to complete OMA DM SIM provisioning");
3212 } else if (strcmp(argv[optind], "oma_dm_add") == 0) {
3213 if (argc - optind < 2) {
3217 cmd_oma_dm_add(&ctx, argv[optind + 1], argv[optind + 2]);
3218 } else if (strcmp(argv[optind], "oma_dm_replace") == 0) {
3219 if (argc - optind < 2) {
3223 cmd_oma_dm_replace(&ctx, argv[optind + 1], argv[optind + 2]);
3224 } else if (strcmp(argv[optind], "est_csr") == 0) {
3225 if (argc - optind < 2) {
3229 mkdir("Cert", S_IRWXU);
3230 est_build_csr(&ctx, argv[optind + 1]);
3231 } else if (strcmp(argv[optind], "browser") == 0) {
3234 if (argc - optind < 2) {
3239 wpa_printf(MSG_INFO, "Launch web browser to URL %s",
3241 ret = hs20_web_browser(argv[optind + 1]);
3242 wpa_printf(MSG_INFO, "Web browser result: %d", ret);
3243 } else if (strcmp(argv[optind], "parse_cert") == 0) {
3244 if (argc - optind < 2) {
3249 wpa_debug_level = MSG_MSGDUMP;
3250 http_parse_x509_certificate(ctx.http, argv[optind + 1]);
3251 wpa_debug_level = MSG_INFO;
3253 wpa_printf(MSG_INFO, "Unknown command '%s'", argv[optind]);
3257 wpa_printf(MSG_DEBUG,
3258 "===[hs20-osu-client END ]======================");
3260 wpa_debug_close_file();