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.
14 #include "utils/browser.h"
15 #include "utils/base64.h"
16 #include "utils/xml-utils.h"
17 #include "utils/http-utils.h"
18 #include "common/wpa_ctrl.h"
19 #include "common/wpa_helpers.h"
20 #include "eap_common/eap_defs.h"
21 #include "crypto/crypto.h"
22 #include "crypto/sha256.h"
23 #include "osu_client.h"
26 void write_result(struct hs20_osu_client *ctx, const char *fmt, ...)
33 vsnprintf(buf, sizeof(buf), fmt, ap);
35 write_summary(ctx, "%s", buf);
37 if (!ctx->result_file)
40 f = fopen(ctx->result_file, "w");
52 void write_summary(struct hs20_osu_client *ctx, const char *fmt, ...)
57 if (!ctx->summary_file)
60 f = fopen(ctx->summary_file, "a");
72 void debug_dump_node(struct hs20_osu_client *ctx, const char *title,
75 char *str = xml_node_to_str(ctx->xml, node);
76 wpa_printf(MSG_DEBUG, "[hs20] %s: '%s'", title, str);
81 static int valid_fqdn(const char *fqdn)
85 /* TODO: could make this more complete.. */
86 if (strchr(fqdn, '.') == 0 || strlen(fqdn) > 255)
88 for (pos = fqdn; *pos; pos++) {
89 if (*pos >= 'a' && *pos <= 'z')
91 if (*pos >= 'A' && *pos <= 'Z')
93 if (*pos >= '0' && *pos <= '9')
95 if (*pos == '-' || *pos == '.' || *pos == '_')
103 int osu_get_certificate(struct hs20_osu_client *ctx, xml_node_t *getcert)
106 char *url, *user = NULL, *pw = NULL;
110 proto = xml_node_get_attr_value(ctx->xml, getcert,
111 "enrollmentProtocol");
114 wpa_printf(MSG_INFO, "getCertificate - enrollmentProtocol=%s", proto);
115 write_summary(ctx, "getCertificate - enrollmentProtocol=%s", proto);
116 if (os_strcasecmp(proto, "EST") != 0) {
117 wpa_printf(MSG_INFO, "Unsupported enrollmentProtocol");
118 xml_node_get_attr_value_free(ctx->xml, proto);
121 xml_node_get_attr_value_free(ctx->xml, proto);
123 node = get_node(ctx->xml, getcert, "enrollmentServerURI");
125 wpa_printf(MSG_INFO, "Could not find enrollmentServerURI node");
126 xml_node_get_attr_value_free(ctx->xml, proto);
129 url = xml_node_get_text(ctx->xml, node);
131 wpa_printf(MSG_INFO, "Could not get URL text");
134 wpa_printf(MSG_INFO, "enrollmentServerURI: %s", url);
135 write_summary(ctx, "enrollmentServerURI: %s", url);
137 node = get_node(ctx->xml, getcert, "estUserID");
138 if (node == NULL && !ctx->client_cert_present) {
139 wpa_printf(MSG_INFO, "Could not find estUserID node");
143 user = xml_node_get_text(ctx->xml, node);
145 wpa_printf(MSG_INFO, "Could not get estUserID text");
148 wpa_printf(MSG_INFO, "estUserID: %s", user);
149 write_summary(ctx, "estUserID: %s", user);
152 node = get_node(ctx->xml, getcert, "estPassword");
153 if (node == NULL && !ctx->client_cert_present) {
154 wpa_printf(MSG_INFO, "Could not find estPassword node");
158 pw = xml_node_get_base64_text(ctx->xml, node, NULL);
160 wpa_printf(MSG_INFO, "Could not get estPassword text");
163 wpa_printf(MSG_INFO, "estPassword: %s", pw);
166 mkdir("Cert", S_IRWXU);
167 if (est_load_cacerts(ctx, url) < 0 ||
168 est_build_csr(ctx, url) < 0 ||
169 est_simple_enroll(ctx, url, user, pw) < 0)
174 xml_node_get_text_free(ctx->xml, url);
175 xml_node_get_text_free(ctx->xml, user);
176 xml_node_get_text_free(ctx->xml, pw);
182 static int process_est_cert(struct hs20_osu_client *ctx, xml_node_t *cert,
185 u8 digest1[SHA256_MAC_LEN], digest2[SHA256_MAC_LEN];
187 size_t der_len, pem_len;
191 wpa_printf(MSG_INFO, "PPS for certificate credential - fqdn=%s", fqdn);
193 fingerprint = xml_node_get_text(ctx->xml, cert);
194 if (fingerprint == NULL)
196 if (hexstr2bin(fingerprint, digest1, SHA256_MAC_LEN) < 0) {
197 wpa_printf(MSG_INFO, "Invalid SHA256 hash value");
198 write_result(ctx, "Invalid client certificate SHA256 hash value in PPS");
199 xml_node_get_text_free(ctx->xml, fingerprint);
202 xml_node_get_text_free(ctx->xml, fingerprint);
204 der = os_readfile("Cert/est_cert.der", &der_len);
206 wpa_printf(MSG_INFO, "Could not find client certificate from EST");
207 write_result(ctx, "Could not find client certificate from EST");
211 if (sha256_vector(1, (const u8 **) &der, &der_len, digest2) < 0) {
217 if (os_memcmp(digest1, digest2, sizeof(digest1)) != 0) {
218 wpa_printf(MSG_INFO, "Client certificate from EST does not match fingerprint from PPS MO");
219 write_result(ctx, "Client certificate from EST does not match fingerprint from PPS MO");
223 wpa_printf(MSG_INFO, "Client certificate from EST matches PPS MO");
224 unlink("Cert/est_cert.der");
226 os_snprintf(buf, sizeof(buf), "SP/%s/client-ca.pem", fqdn);
227 if (rename("Cert/est-cacerts.pem", buf) < 0) {
228 wpa_printf(MSG_INFO, "Could not move est-cacerts.pem to client-ca.pem: %s",
232 pem = os_readfile(buf, &pem_len);
234 os_snprintf(buf, sizeof(buf), "SP/%s/client-cert.pem", fqdn);
235 if (rename("Cert/est_cert.pem", buf) < 0) {
236 wpa_printf(MSG_INFO, "Could not move est_cert.pem to client-cert.pem: %s",
243 FILE *f = fopen(buf, "a");
245 fwrite(pem, pem_len, 1, f);
251 os_snprintf(buf, sizeof(buf), "SP/%s/client-key.pem", fqdn);
252 if (rename("Cert/privkey-plain.pem", buf) < 0) {
253 wpa_printf(MSG_INFO, "Could not move privkey-plain.pem to client-key.pem: %s",
258 unlink("Cert/est-req.b64");
259 unlink("Cert/est-req.pem");
260 unlink("Cert/est-resp.raw");
267 #define TMP_CERT_DL_FILE "tmp-cert-download"
269 static int download_cert(struct hs20_osu_client *ctx, xml_node_t *params,
272 xml_node_t *url_node, *hash_node;
276 u8 digest1[SHA256_MAC_LEN], digest2[SHA256_MAC_LEN];
281 url_node = get_node(ctx->xml, params, "CertURL");
282 hash_node = get_node(ctx->xml, params, "CertSHA256Fingerprint");
283 if (url_node == NULL || hash_node == NULL)
285 url = xml_node_get_text(ctx->xml, url_node);
286 hash = xml_node_get_text(ctx->xml, hash_node);
287 if (url == NULL || hash == NULL) {
288 xml_node_get_text_free(ctx->xml, url);
289 xml_node_get_text_free(ctx->xml, hash);
293 wpa_printf(MSG_INFO, "CertURL: %s", url);
294 wpa_printf(MSG_INFO, "SHA256 hash: %s", hash);
296 if (hexstr2bin(hash, digest1, SHA256_MAC_LEN) < 0) {
297 wpa_printf(MSG_INFO, "Invalid SHA256 hash value");
298 write_result(ctx, "Invalid SHA256 hash value for downloaded certificate");
299 xml_node_get_text_free(ctx->xml, hash);
302 xml_node_get_text_free(ctx->xml, hash);
304 write_summary(ctx, "Download certificate from %s", url);
305 ctx->no_osu_cert_validation = 1;
306 http_ocsp_set(ctx->http, 1);
307 res = http_download_file(ctx->http, url, TMP_CERT_DL_FILE, NULL);
308 http_ocsp_set(ctx->http,
309 (ctx->workarounds & WORKAROUND_OCSP_OPTIONAL) ? 1 : 2);
310 ctx->no_osu_cert_validation = 0;
311 xml_node_get_text_free(ctx->xml, url);
315 cert = os_readfile(TMP_CERT_DL_FILE, &len);
316 remove(TMP_CERT_DL_FILE);
320 if (sha256_vector(1, (const u8 **) &cert, &len, digest2) < 0) {
325 if (os_memcmp(digest1, digest2, sizeof(digest1)) != 0) {
326 wpa_printf(MSG_INFO, "Downloaded certificate fingerprint did not match");
327 write_result(ctx, "Downloaded certificate fingerprint did not match");
332 b64 = base64_encode((unsigned char *) cert, len, NULL);
337 f = fopen(fname, "wb");
343 fprintf(f, "-----BEGIN CERTIFICATE-----\n"
345 "-----END CERTIFICATE-----\n",
351 wpa_printf(MSG_INFO, "Downloaded certificate into %s and validated fingerprint",
353 write_summary(ctx, "Downloaded certificate into %s and validated fingerprint",
360 static int cmd_dl_osu_ca(struct hs20_osu_client *ctx, const char *pps_fname,
361 const char *ca_fname)
363 xml_node_t *pps, *node;
366 pps = node_from_file(ctx->xml, pps_fname);
368 wpa_printf(MSG_INFO, "Could not read or parse '%s'", pps_fname);
372 node = get_child_node(ctx->xml, pps,
373 "SubscriptionUpdate/TrustRoot");
375 wpa_printf(MSG_INFO, "No SubscriptionUpdate/TrustRoot/CertURL found from PPS");
376 xml_node_free(ctx->xml, pps);
380 ret = download_cert(ctx, node, ca_fname);
381 xml_node_free(ctx->xml, pps);
387 static int cmd_dl_polupd_ca(struct hs20_osu_client *ctx, const char *pps_fname,
388 const char *ca_fname)
390 xml_node_t *pps, *node;
393 pps = node_from_file(ctx->xml, pps_fname);
395 wpa_printf(MSG_INFO, "Could not read or parse '%s'", pps_fname);
399 node = get_child_node(ctx->xml, pps,
400 "PolicyUpdate/TrustRoot");
402 wpa_printf(MSG_INFO, "No PolicyUpdate/TrustRoot/CertURL found from PPS");
403 xml_node_free(ctx->xml, pps);
407 ret = download_cert(ctx, node, ca_fname);
408 xml_node_free(ctx->xml, pps);
414 static int cmd_dl_aaa_ca(struct hs20_osu_client *ctx, const char *pps_fname,
415 const char *ca_fname)
417 xml_node_t *pps, *node, *aaa;
420 pps = node_from_file(ctx->xml, pps_fname);
422 wpa_printf(MSG_INFO, "Could not read or parse '%s'", pps_fname);
426 node = get_child_node(ctx->xml, pps,
427 "AAAServerTrustRoot");
429 wpa_printf(MSG_INFO, "No AAAServerTrustRoot/CertURL found from PPS");
430 xml_node_free(ctx->xml, pps);
434 aaa = xml_node_first_child(ctx->xml, node);
436 wpa_printf(MSG_INFO, "No AAAServerTrustRoot/CertURL found from PPS");
437 xml_node_free(ctx->xml, pps);
441 ret = download_cert(ctx, aaa, ca_fname);
442 xml_node_free(ctx->xml, pps);
448 static int download_trust_roots(struct hs20_osu_client *ctx,
449 const char *pps_fname)
455 dir = os_strdup(pps_fname);
458 pos = os_strrchr(dir, '/');
465 snprintf(fname, sizeof(fname), "%s/ca.pem", dir);
466 ret = cmd_dl_osu_ca(ctx, pps_fname, fname);
467 snprintf(fname, sizeof(fname), "%s/polupd-ca.pem", dir);
468 cmd_dl_polupd_ca(ctx, pps_fname, fname);
469 snprintf(fname, sizeof(fname), "%s/aaa-ca.pem", dir);
470 cmd_dl_aaa_ca(ctx, pps_fname, fname);
478 static int server_dnsname_suffix_match(struct hs20_osu_client *ctx,
481 size_t match_len, len, i;
484 match_len = os_strlen(fqdn);
486 for (i = 0; i < ctx->server_dnsname_count; i++) {
488 "Checking suffix match against server dNSName %s",
489 ctx->server_dnsname[i]);
490 val = ctx->server_dnsname[i];
491 len = os_strlen(val);
496 if (os_strncasecmp(val + len - match_len, fqdn, match_len) != 0)
497 continue; /* no match */
499 if (match_len == len)
500 return 1; /* exact match */
502 if (val[len - match_len - 1] == '.')
503 return 1; /* full label match completes suffix match */
505 /* Reject due to incomplete label match */
508 /* None of the dNSName(s) matched */
513 int hs20_add_pps_mo(struct hs20_osu_client *ctx, const char *uri,
514 xml_node_t *add_mo, char *fname, size_t fname_len)
518 xml_node_t *tnds, *mo, *cert;
522 if (strncmp(uri, "./Wi-Fi/", 8) != 0) {
523 wpa_printf(MSG_INFO, "Unsupported location for addMO to add PPS MO: '%s'",
525 write_result(ctx, "Unsupported location for addMO to add PPS MO: '%s'",
530 fqdn = strdup(uri + 8);
533 pos = strchr(fqdn, '/');
535 if (os_strcasecmp(pos, "/PerProviderSubscription") != 0) {
536 wpa_printf(MSG_INFO, "Unsupported location for addMO to add PPS MO (extra directory): '%s'",
538 write_result(ctx, "Unsupported location for addMO to "
539 "add PPS MO (extra directory): '%s'", uri);
542 *pos = '\0'; /* remove trailing slash and PPS node name */
544 wpa_printf(MSG_INFO, "SP FQDN: %s", fqdn);
546 if (!server_dnsname_suffix_match(ctx, fqdn)) {
547 wpa_printf(MSG_INFO, "FQDN '%s' for new PPS MO did not have suffix match with server's dNSName values",
549 write_result(ctx, "FQDN '%s' for new PPS MO did not have suffix match with server's dNSName values",
555 if (!valid_fqdn(fqdn)) {
556 wpa_printf(MSG_INFO, "Invalid FQDN '%s'", fqdn);
557 write_result(ctx, "Invalid FQDN '%s'", fqdn);
562 mkdir("SP", S_IRWXU);
563 snprintf(fname, fname_len, "SP/%s", fqdn);
564 if (mkdir(fname, S_IRWXU) < 0) {
565 if (errno != EEXIST) {
567 wpa_printf(MSG_INFO, "mkdir(%s) failed: %s",
568 fname, strerror(err));
574 snprintf(fname, fname_len, "SP/%s/pps.xml", fqdn);
576 if (os_file_exists(fname)) {
577 wpa_printf(MSG_INFO, "PPS file '%s' exists - reject addMO",
579 write_result(ctx, "PPS file '%s' exists - reject addMO",
584 wpa_printf(MSG_INFO, "Using PPS file: %s", fname);
586 str = xml_node_get_text(ctx->xml, add_mo);
588 wpa_printf(MSG_INFO, "Could not extract MO text");
592 wpa_printf(MSG_DEBUG, "[hs20] addMO text: '%s'", str);
594 tnds = xml_node_from_buf(ctx->xml, str);
595 xml_node_get_text_free(ctx->xml, str);
597 wpa_printf(MSG_INFO, "[hs20] Could not parse addMO text");
602 mo = tnds_to_mo(ctx->xml, tnds);
604 wpa_printf(MSG_INFO, "[hs20] Could not parse addMO TNDS text");
609 debug_dump_node(ctx, "Parsed TNDS", mo);
611 name = xml_node_get_localname(ctx->xml, mo);
612 if (os_strcasecmp(name, "PerProviderSubscription") != 0) {
613 wpa_printf(MSG_INFO, "[hs20] Unexpected PPS MO root node name '%s'",
619 cert = get_child_node(ctx->xml, mo,
620 "Credential/DigitalCertificate/"
621 "CertSHA256Fingerprint");
622 if (cert && process_est_cert(ctx, cert, fqdn) < 0) {
623 xml_node_free(ctx->xml, mo);
629 if (node_to_file(ctx->xml, fname, mo) < 0) {
630 wpa_printf(MSG_INFO, "Could not write MO to file");
631 xml_node_free(ctx->xml, mo);
634 xml_node_free(ctx->xml, mo);
636 wpa_printf(MSG_INFO, "A new PPS MO added as '%s'", fname);
637 write_summary(ctx, "A new PPS MO added as '%s'", fname);
639 ret = download_trust_roots(ctx, fname);
641 wpa_printf(MSG_INFO, "Remove invalid PPS MO file");
642 write_summary(ctx, "Remove invalid PPS MO file");
650 int update_pps_file(struct hs20_osu_client *ctx, const char *pps_fname,
657 if (ctx->client_cert_present) {
659 cert = get_child_node(ctx->xml, pps,
660 "Credential/DigitalCertificate/"
661 "CertSHA256Fingerprint");
662 if (cert && os_file_exists("Cert/est_cert.der") &&
663 process_est_cert(ctx, cert, ctx->fqdn) < 0) {
664 wpa_printf(MSG_INFO, "EST certificate update processing failed on PPS MO update");
669 wpa_printf(MSG_INFO, "Updating PPS MO %s", pps_fname);
671 str = xml_node_to_str(ctx->xml, pps);
672 wpa_printf(MSG_MSGDUMP, "[hs20] Updated PPS: '%s'", str);
674 snprintf(backup, sizeof(backup), "%s.bak", pps_fname);
675 rename(pps_fname, backup);
676 f = fopen(pps_fname, "w");
678 wpa_printf(MSG_INFO, "Could not write PPS");
679 rename(backup, pps_fname);
683 fprintf(f, "%s\n", str);
692 void get_user_pw(struct hs20_osu_client *ctx, xml_node_t *pps,
693 const char *alt_loc, char **user, char **pw)
697 node = get_child_node(ctx->xml, pps,
698 "Credential/UsernamePassword/Username");
700 *user = xml_node_get_text(ctx->xml, node);
702 node = get_child_node(ctx->xml, pps,
703 "Credential/UsernamePassword/Password");
705 *pw = xml_node_get_base64_text(ctx->xml, node, NULL);
707 node = get_child_node(ctx->xml, pps, alt_loc);
710 a = get_node(ctx->xml, node, "Username");
712 xml_node_get_text_free(ctx->xml, *user);
713 *user = xml_node_get_text(ctx->xml, a);
714 wpa_printf(MSG_INFO, "Use OSU username '%s'", *user);
717 a = get_node(ctx->xml, node, "Password");
720 *pw = xml_node_get_base64_text(ctx->xml, a, NULL);
721 wpa_printf(MSG_INFO, "Use OSU password");
727 /* Remove old credentials based on HomeSP/FQDN */
728 static void remove_sp_creds(struct hs20_osu_client *ctx, const char *fqdn)
731 os_snprintf(cmd, sizeof(cmd), "REMOVE_CRED provisioning_sp=%s", fqdn);
732 if (wpa_command(ctx->ifname, cmd) < 0)
733 wpa_printf(MSG_INFO, "Failed to remove old credential(s)");
737 static void set_pps_cred_policy_spe(struct hs20_osu_client *ctx, int id,
743 ssid = get_node(ctx->xml, spe, "SSID");
746 txt = xml_node_get_text(ctx->xml, ssid);
749 wpa_printf(MSG_DEBUG, "- Policy/SPExclusionList/<X+>/SSID = %s", txt);
750 if (set_cred_quoted(ctx->ifname, id, "excluded_ssid", txt) < 0)
751 wpa_printf(MSG_INFO, "Failed to set cred excluded_ssid");
752 xml_node_get_text_free(ctx->xml, txt);
756 static void set_pps_cred_policy_spel(struct hs20_osu_client *ctx, int id,
761 xml_node_for_each_child(ctx->xml, child, spel) {
762 xml_node_for_each_check(ctx->xml, child);
763 set_pps_cred_policy_spe(ctx, id, child);
768 static void set_pps_cred_policy_prp(struct hs20_osu_client *ctx, int id,
772 char *txt = NULL, *pos;
773 char *prio, *country_buf = NULL;
778 node = get_node(ctx->xml, prp, "Priority");
781 prio = xml_node_get_text(ctx->xml, node);
784 wpa_printf(MSG_INFO, "- Policy/PreferredRoamingPartnerList/<X+>/Priority = %s",
786 priority = atoi(prio);
787 xml_node_get_text_free(ctx->xml, prio);
789 node = get_node(ctx->xml, prp, "Country");
791 country_buf = xml_node_get_text(ctx->xml, node);
792 if (country_buf == NULL)
794 country = country_buf;
795 wpa_printf(MSG_INFO, "- Policy/PreferredRoamingPartnerList/<X+>/Country = %s",
801 node = get_node(ctx->xml, prp, "FQDN_Match");
804 txt = xml_node_get_text(ctx->xml, node);
807 wpa_printf(MSG_INFO, "- Policy/PreferredRoamingPartnerList/<X+>/FQDN_Match = %s",
809 pos = strrchr(txt, ',');
814 snprintf(val, sizeof(val), "%s,%d,%d,%s", txt,
815 strcmp(pos, "includeSubdomains") != 0, priority, country);
816 if (set_cred_quoted(ctx->ifname, id, "roaming_partner", val) < 0)
817 wpa_printf(MSG_INFO, "Failed to set cred roaming_partner");
819 xml_node_get_text_free(ctx->xml, country_buf);
820 xml_node_get_text_free(ctx->xml, txt);
824 static void set_pps_cred_policy_prpl(struct hs20_osu_client *ctx, int id,
829 xml_node_for_each_child(ctx->xml, child, prpl) {
830 xml_node_for_each_check(ctx->xml, child);
831 set_pps_cred_policy_prp(ctx, id, child);
836 static void set_pps_cred_policy_min_backhaul(struct hs20_osu_client *ctx, int id,
837 xml_node_t *min_backhaul)
840 char *type, *dl = NULL, *ul = NULL;
843 node = get_node(ctx->xml, min_backhaul, "NetworkType");
845 wpa_printf(MSG_INFO, "Ignore MinBackhaulThreshold without mandatory NetworkType node");
849 type = xml_node_get_text(ctx->xml, node);
852 wpa_printf(MSG_INFO, "- Policy/MinBackhaulThreshold/<X+>/NetworkType = %s",
854 if (os_strcasecmp(type, "home") == 0)
856 else if (os_strcasecmp(type, "roaming") == 0)
859 wpa_printf(MSG_INFO, "Ignore MinBackhaulThreshold with invalid NetworkType");
860 xml_node_get_text_free(ctx->xml, type);
863 xml_node_get_text_free(ctx->xml, type);
865 node = get_node(ctx->xml, min_backhaul, "DLBandwidth");
867 dl = xml_node_get_text(ctx->xml, node);
869 node = get_node(ctx->xml, min_backhaul, "ULBandwidth");
871 ul = xml_node_get_text(ctx->xml, node);
873 if (dl == NULL && ul == NULL) {
874 wpa_printf(MSG_INFO, "Ignore MinBackhaulThreshold without either DLBandwidth or ULBandwidth nodes");
879 wpa_printf(MSG_INFO, "- Policy/MinBackhaulThreshold/<X+>/DLBandwidth = %s",
882 wpa_printf(MSG_INFO, "- Policy/MinBackhaulThreshold/<X+>/ULBandwidth = %s",
887 set_cred(ctx->ifname, id, "min_dl_bandwidth_home", dl) < 0)
888 wpa_printf(MSG_INFO, "Failed to set cred bandwidth limit");
890 set_cred(ctx->ifname, id, "min_ul_bandwidth_home", ul) < 0)
891 wpa_printf(MSG_INFO, "Failed to set cred bandwidth limit");
894 set_cred(ctx->ifname, id, "min_dl_bandwidth_roaming", dl) <
896 wpa_printf(MSG_INFO, "Failed to set cred bandwidth limit");
898 set_cred(ctx->ifname, id, "min_ul_bandwidth_roaming", ul) <
900 wpa_printf(MSG_INFO, "Failed to set cred bandwidth limit");
903 xml_node_get_text_free(ctx->xml, dl);
904 xml_node_get_text_free(ctx->xml, ul);
908 static void set_pps_cred_policy_min_backhaul_list(struct hs20_osu_client *ctx,
909 int id, xml_node_t *node)
913 wpa_printf(MSG_INFO, "- Policy/MinBackhaulThreshold");
915 xml_node_for_each_child(ctx->xml, child, node) {
916 xml_node_for_each_check(ctx->xml, child);
917 set_pps_cred_policy_min_backhaul(ctx, id, child);
922 static void set_pps_cred_policy_update(struct hs20_osu_client *ctx, int id,
925 wpa_printf(MSG_INFO, "- Policy/PolicyUpdate");
926 /* Not used in wpa_supplicant */
930 static void set_pps_cred_policy_required_proto_port(struct hs20_osu_client *ctx,
931 int id, xml_node_t *tuple)
938 node = get_node(ctx->xml, tuple, "IPProtocol");
940 wpa_printf(MSG_INFO, "Ignore RequiredProtoPortTuple without mandatory IPProtocol node");
944 proto = xml_node_get_text(ctx->xml, node);
948 wpa_printf(MSG_INFO, "- Policy/RequiredProtoPortTuple/<X+>/IPProtocol = %s",
951 node = get_node(ctx->xml, tuple, "PortNumber");
952 port = node ? xml_node_get_text(ctx->xml, node) : NULL;
954 wpa_printf(MSG_INFO, "- Policy/RequiredProtoPortTuple/<X+>/PortNumber = %s",
956 buflen = os_strlen(proto) + os_strlen(port) + 10;
957 buf = os_malloc(buflen);
959 os_snprintf(buf, buflen, "%s:%s", proto, port);
960 xml_node_get_text_free(ctx->xml, port);
962 buflen = os_strlen(proto) + 10;
963 buf = os_malloc(buflen);
965 os_snprintf(buf, buflen, "%s", proto);
968 xml_node_get_text_free(ctx->xml, proto);
973 if (set_cred(ctx->ifname, id, "req_conn_capab", buf) < 0)
974 wpa_printf(MSG_INFO, "Could not set req_conn_capab");
980 static void set_pps_cred_policy_required_proto_ports(struct hs20_osu_client *ctx,
981 int id, xml_node_t *node)
985 wpa_printf(MSG_INFO, "- Policy/RequiredProtoPortTuple");
987 xml_node_for_each_child(ctx->xml, child, node) {
988 xml_node_for_each_check(ctx->xml, child);
989 set_pps_cred_policy_required_proto_port(ctx, id, child);
994 static void set_pps_cred_policy_max_bss_load(struct hs20_osu_client *ctx, int id,
997 char *str = xml_node_get_text(ctx->xml, node);
1000 wpa_printf(MSG_INFO, "- Policy/MaximumBSSLoadValue - %s", str);
1001 if (set_cred(ctx->ifname, id, "max_bss_load", str) < 0)
1002 wpa_printf(MSG_INFO, "Failed to set cred max_bss_load limit");
1003 xml_node_get_text_free(ctx->xml, str);
1007 static void set_pps_cred_policy(struct hs20_osu_client *ctx, int id,
1013 wpa_printf(MSG_INFO, "- Policy");
1015 xml_node_for_each_child(ctx->xml, child, node) {
1016 xml_node_for_each_check(ctx->xml, child);
1017 name = xml_node_get_localname(ctx->xml, child);
1018 if (os_strcasecmp(name, "PreferredRoamingPartnerList") == 0)
1019 set_pps_cred_policy_prpl(ctx, id, child);
1020 else if (os_strcasecmp(name, "MinBackhaulThreshold") == 0)
1021 set_pps_cred_policy_min_backhaul_list(ctx, id, child);
1022 else if (os_strcasecmp(name, "PolicyUpdate") == 0)
1023 set_pps_cred_policy_update(ctx, id, child);
1024 else if (os_strcasecmp(name, "SPExclusionList") == 0)
1025 set_pps_cred_policy_spel(ctx, id, child);
1026 else if (os_strcasecmp(name, "RequiredProtoPortTuple") == 0)
1027 set_pps_cred_policy_required_proto_ports(ctx, id, child);
1028 else if (os_strcasecmp(name, "MaximumBSSLoadValue") == 0)
1029 set_pps_cred_policy_max_bss_load(ctx, id, child);
1031 wpa_printf(MSG_INFO, "Unknown Policy node '%s'", name);
1036 static void set_pps_cred_priority(struct hs20_osu_client *ctx, int id,
1039 char *str = xml_node_get_text(ctx->xml, node);
1042 wpa_printf(MSG_INFO, "- CredentialPriority = %s", str);
1043 if (set_cred(ctx->ifname, id, "sp_priority", str) < 0)
1044 wpa_printf(MSG_INFO, "Failed to set cred sp_priority");
1045 xml_node_get_text_free(ctx->xml, str);
1049 static void set_pps_cred_aaa_server_trust_root(struct hs20_osu_client *ctx,
1050 int id, xml_node_t *node)
1052 wpa_printf(MSG_INFO, "- AAAServerTrustRoot - TODO");
1056 static void set_pps_cred_sub_update(struct hs20_osu_client *ctx, int id,
1059 wpa_printf(MSG_INFO, "- SubscriptionUpdate");
1060 /* not used within wpa_supplicant */
1064 static void set_pps_cred_home_sp_network_id(struct hs20_osu_client *ctx,
1065 int id, xml_node_t *node)
1067 xml_node_t *ssid_node, *hessid_node;
1068 char *ssid, *hessid;
1070 ssid_node = get_node(ctx->xml, node, "SSID");
1071 if (ssid_node == NULL) {
1072 wpa_printf(MSG_INFO, "Ignore HomeSP/NetworkID without mandatory SSID node");
1076 hessid_node = get_node(ctx->xml, node, "HESSID");
1078 ssid = xml_node_get_text(ctx->xml, ssid_node);
1081 hessid = hessid_node ? xml_node_get_text(ctx->xml, hessid_node) : NULL;
1083 wpa_printf(MSG_INFO, "- HomeSP/NetworkID/<X+>/SSID = %s", ssid);
1085 wpa_printf(MSG_INFO, "- HomeSP/NetworkID/<X+>/HESSID = %s",
1088 /* TODO: Configure to wpa_supplicant */
1090 xml_node_get_text_free(ctx->xml, ssid);
1091 xml_node_get_text_free(ctx->xml, hessid);
1095 static void set_pps_cred_home_sp_network_ids(struct hs20_osu_client *ctx,
1096 int id, xml_node_t *node)
1100 wpa_printf(MSG_INFO, "- HomeSP/NetworkID");
1102 xml_node_for_each_child(ctx->xml, child, node) {
1103 xml_node_for_each_check(ctx->xml, child);
1104 set_pps_cred_home_sp_network_id(ctx, id, child);
1109 static void set_pps_cred_home_sp_friendly_name(struct hs20_osu_client *ctx,
1110 int id, xml_node_t *node)
1112 char *str = xml_node_get_text(ctx->xml, node);
1115 wpa_printf(MSG_INFO, "- HomeSP/FriendlyName = %s", str);
1116 /* not used within wpa_supplicant(?) */
1117 xml_node_get_text_free(ctx->xml, str);
1121 static void set_pps_cred_home_sp_icon_url(struct hs20_osu_client *ctx,
1122 int id, xml_node_t *node)
1124 char *str = xml_node_get_text(ctx->xml, node);
1127 wpa_printf(MSG_INFO, "- HomeSP/IconURL = %s", str);
1128 /* not used within wpa_supplicant */
1129 xml_node_get_text_free(ctx->xml, str);
1133 static void set_pps_cred_home_sp_fqdn(struct hs20_osu_client *ctx, int id,
1136 char *str = xml_node_get_text(ctx->xml, node);
1139 wpa_printf(MSG_INFO, "- HomeSP/FQDN = %s", str);
1140 if (set_cred_quoted(ctx->ifname, id, "domain", str) < 0)
1141 wpa_printf(MSG_INFO, "Failed to set cred domain");
1142 if (set_cred_quoted(ctx->ifname, id, "domain_suffix_match", str) < 0)
1143 wpa_printf(MSG_INFO, "Failed to set cred domain_suffix_match");
1144 xml_node_get_text_free(ctx->xml, str);
1148 static void set_pps_cred_home_sp_oi(struct hs20_osu_client *ctx, int id,
1153 char *homeoi = NULL;
1157 xml_node_for_each_child(ctx->xml, child, node) {
1158 xml_node_for_each_check(ctx->xml, child);
1159 name = xml_node_get_localname(ctx->xml, child);
1160 if (strcasecmp(name, "HomeOI") == 0 && !homeoi) {
1161 homeoi = xml_node_get_text(ctx->xml, child);
1162 wpa_printf(MSG_INFO, "- HomeSP/HomeOIList/<X+>/HomeOI = %s",
1164 } else if (strcasecmp(name, "HomeOIRequired") == 0) {
1165 str = xml_node_get_text(ctx->xml, child);
1166 wpa_printf(MSG_INFO, "- HomeSP/HomeOIList/<X+>/HomeOIRequired = '%s'",
1170 required = strcasecmp(str, "true") == 0;
1171 xml_node_get_text_free(ctx->xml, str);
1173 wpa_printf(MSG_INFO, "Unknown HomeOIList node '%s'",
1177 if (homeoi == NULL) {
1178 wpa_printf(MSG_INFO, "- HomeSP/HomeOIList/<X+> without HomeOI ignored");
1182 wpa_printf(MSG_INFO, "- HomeSP/HomeOIList/<X+> '%s' required=%d",
1186 if (set_cred(ctx->ifname, id, "required_roaming_consortium",
1188 wpa_printf(MSG_INFO, "Failed to set cred required_roaming_consortium");
1190 if (set_cred_quoted(ctx->ifname, id, "roaming_consortium",
1192 wpa_printf(MSG_INFO, "Failed to set cred roaming_consortium");
1195 xml_node_get_text_free(ctx->xml, homeoi);
1199 static void set_pps_cred_home_sp_oi_list(struct hs20_osu_client *ctx, int id,
1204 wpa_printf(MSG_INFO, "- HomeSP/HomeOIList");
1206 xml_node_for_each_child(ctx->xml, child, node) {
1207 xml_node_for_each_check(ctx->xml, child);
1208 set_pps_cred_home_sp_oi(ctx, id, child);
1213 static void set_pps_cred_home_sp_other_partner(struct hs20_osu_client *ctx,
1214 int id, xml_node_t *node)
1220 xml_node_for_each_child(ctx->xml, child, node) {
1221 xml_node_for_each_check(ctx->xml, child);
1222 name = xml_node_get_localname(ctx->xml, child);
1223 if (os_strcasecmp(name, "FQDN") == 0 && !fqdn) {
1224 fqdn = xml_node_get_text(ctx->xml, child);
1225 wpa_printf(MSG_INFO, "- HomeSP/OtherHomePartners/<X+>/FQDN = %s",
1228 wpa_printf(MSG_INFO, "Unknown OtherHomePartners node '%s'",
1233 wpa_printf(MSG_INFO, "- HomeSP/OtherHomePartners/<X+> without FQDN ignored");
1237 if (set_cred_quoted(ctx->ifname, id, "domain", fqdn) < 0)
1238 wpa_printf(MSG_INFO, "Failed to set cred domain for OtherHomePartners node");
1240 xml_node_get_text_free(ctx->xml, fqdn);
1244 static void set_pps_cred_home_sp_other_partners(struct hs20_osu_client *ctx,
1250 wpa_printf(MSG_INFO, "- HomeSP/OtherHomePartners");
1252 xml_node_for_each_child(ctx->xml, child, node) {
1253 xml_node_for_each_check(ctx->xml, child);
1254 set_pps_cred_home_sp_other_partner(ctx, id, child);
1259 static void set_pps_cred_home_sp_roaming_consortium_oi(
1260 struct hs20_osu_client *ctx, int id, xml_node_t *node)
1262 char *str = xml_node_get_text(ctx->xml, node);
1265 wpa_printf(MSG_INFO, "- HomeSP/RoamingConsortiumOI = %s", str);
1266 /* TODO: Set to wpa_supplicant */
1267 xml_node_get_text_free(ctx->xml, str);
1271 static void set_pps_cred_home_sp(struct hs20_osu_client *ctx, int id,
1277 wpa_printf(MSG_INFO, "- HomeSP");
1279 xml_node_for_each_child(ctx->xml, child, node) {
1280 xml_node_for_each_check(ctx->xml, child);
1281 name = xml_node_get_localname(ctx->xml, child);
1282 if (os_strcasecmp(name, "NetworkID") == 0)
1283 set_pps_cred_home_sp_network_ids(ctx, id, child);
1284 else if (os_strcasecmp(name, "FriendlyName") == 0)
1285 set_pps_cred_home_sp_friendly_name(ctx, id, child);
1286 else if (os_strcasecmp(name, "IconURL") == 0)
1287 set_pps_cred_home_sp_icon_url(ctx, id, child);
1288 else if (os_strcasecmp(name, "FQDN") == 0)
1289 set_pps_cred_home_sp_fqdn(ctx, id, child);
1290 else if (os_strcasecmp(name, "HomeOIList") == 0)
1291 set_pps_cred_home_sp_oi_list(ctx, id, child);
1292 else if (os_strcasecmp(name, "OtherHomePartners") == 0)
1293 set_pps_cred_home_sp_other_partners(ctx, id, child);
1294 else if (os_strcasecmp(name, "RoamingConsortiumOI") == 0)
1295 set_pps_cred_home_sp_roaming_consortium_oi(ctx, id,
1298 wpa_printf(MSG_INFO, "Unknown HomeSP node '%s'", name);
1303 static void set_pps_cred_sub_params(struct hs20_osu_client *ctx, int id,
1306 wpa_printf(MSG_INFO, "- SubscriptionParameters");
1307 /* not used within wpa_supplicant */
1311 static void set_pps_cred_creation_date(struct hs20_osu_client *ctx, int id,
1314 char *str = xml_node_get_text(ctx->xml, node);
1317 wpa_printf(MSG_INFO, "- Credential/CreationDate = %s", str);
1318 /* not used within wpa_supplicant */
1319 xml_node_get_text_free(ctx->xml, str);
1323 static void set_pps_cred_expiration_date(struct hs20_osu_client *ctx, int id,
1326 char *str = xml_node_get_text(ctx->xml, node);
1329 wpa_printf(MSG_INFO, "- Credential/ExpirationDate = %s", str);
1330 /* not used within wpa_supplicant */
1331 xml_node_get_text_free(ctx->xml, str);
1335 static void set_pps_cred_username(struct hs20_osu_client *ctx, int id,
1338 char *str = xml_node_get_text(ctx->xml, node);
1341 wpa_printf(MSG_INFO, "- Credential/UsernamePassword/Username = %s",
1343 if (set_cred_quoted(ctx->ifname, id, "username", str) < 0)
1344 wpa_printf(MSG_INFO, "Failed to set cred username");
1345 xml_node_get_text_free(ctx->xml, str);
1349 static void set_pps_cred_password(struct hs20_osu_client *ctx, int id,
1353 char *pw, *hex, *pos, *end;
1355 pw = xml_node_get_base64_text(ctx->xml, node, &len);
1359 wpa_printf(MSG_INFO, "- Credential/UsernamePassword/Password = %s", pw);
1361 hex = malloc(len * 2 + 1);
1366 end = hex + len * 2 + 1;
1368 for (i = 0; i < len; i++) {
1369 snprintf(pos, end - pos, "%02x", pw[i]);
1374 if (set_cred(ctx->ifname, id, "password", hex) < 0)
1375 wpa_printf(MSG_INFO, "Failed to set cred password");
1380 static void set_pps_cred_machine_managed(struct hs20_osu_client *ctx, int id,
1383 char *str = xml_node_get_text(ctx->xml, node);
1386 wpa_printf(MSG_INFO, "- Credential/UsernamePassword/MachineManaged = %s",
1388 /* not used within wpa_supplicant */
1389 xml_node_get_text_free(ctx->xml, str);
1393 static void set_pps_cred_soft_token_app(struct hs20_osu_client *ctx, int id,
1396 char *str = xml_node_get_text(ctx->xml, node);
1399 wpa_printf(MSG_INFO, "- Credential/UsernamePassword/SoftTokenApp = %s",
1401 /* not used within wpa_supplicant */
1402 xml_node_get_text_free(ctx->xml, str);
1406 static void set_pps_cred_able_to_share(struct hs20_osu_client *ctx, int id,
1409 char *str = xml_node_get_text(ctx->xml, node);
1412 wpa_printf(MSG_INFO, "- Credential/UsernamePassword/AbleToShare = %s",
1414 /* not used within wpa_supplicant */
1415 xml_node_get_text_free(ctx->xml, str);
1419 static void set_pps_cred_eap_method(struct hs20_osu_client *ctx, int id,
1422 wpa_printf(MSG_INFO, "- Credential/UsernamePassword/EAPMethod - TODO");
1426 static void set_pps_cred_username_password(struct hs20_osu_client *ctx, int id,
1432 wpa_printf(MSG_INFO, "- Credential/UsernamePassword");
1434 xml_node_for_each_child(ctx->xml, child, node) {
1435 xml_node_for_each_check(ctx->xml, child);
1436 name = xml_node_get_localname(ctx->xml, child);
1437 if (os_strcasecmp(name, "Username") == 0)
1438 set_pps_cred_username(ctx, id, child);
1439 else if (os_strcasecmp(name, "Password") == 0)
1440 set_pps_cred_password(ctx, id, child);
1441 else if (os_strcasecmp(name, "MachineManaged") == 0)
1442 set_pps_cred_machine_managed(ctx, id, child);
1443 else if (os_strcasecmp(name, "SoftTokenApp") == 0)
1444 set_pps_cred_soft_token_app(ctx, id, child);
1445 else if (os_strcasecmp(name, "AbleToShare") == 0)
1446 set_pps_cred_able_to_share(ctx, id, child);
1447 else if (os_strcasecmp(name, "EAPMethod") == 0)
1448 set_pps_cred_eap_method(ctx, id, child);
1450 wpa_printf(MSG_INFO, "Unknown Credential/UsernamePassword node '%s'",
1456 static void set_pps_cred_digital_cert(struct hs20_osu_client *ctx, int id,
1457 xml_node_t *node, const char *fqdn)
1459 char buf[200], dir[200];
1461 wpa_printf(MSG_INFO, "- Credential/DigitalCertificate");
1463 if (getcwd(dir, sizeof(dir)) == NULL)
1466 /* TODO: could build username from Subject of Subject AltName */
1467 if (set_cred_quoted(ctx->ifname, id, "username", "cert") < 0) {
1468 wpa_printf(MSG_INFO, "Failed to set username");
1471 snprintf(buf, sizeof(buf), "%s/SP/%s/client-cert.pem", dir, fqdn);
1472 if (os_file_exists(buf)) {
1473 if (set_cred_quoted(ctx->ifname, id, "client_cert", buf) < 0) {
1474 wpa_printf(MSG_INFO, "Failed to set client_cert");
1478 snprintf(buf, sizeof(buf), "%s/SP/%s/client-key.pem", dir, fqdn);
1479 if (os_file_exists(buf)) {
1480 if (set_cred_quoted(ctx->ifname, id, "private_key", buf) < 0) {
1481 wpa_printf(MSG_INFO, "Failed to set private_key");
1487 static void set_pps_cred_realm(struct hs20_osu_client *ctx, int id,
1488 xml_node_t *node, const char *fqdn, int sim)
1490 char *str = xml_node_get_text(ctx->xml, node);
1491 char buf[200], dir[200];
1496 wpa_printf(MSG_INFO, "- Credential/Realm = %s", str);
1497 if (set_cred_quoted(ctx->ifname, id, "realm", str) < 0)
1498 wpa_printf(MSG_INFO, "Failed to set cred realm");
1499 xml_node_get_text_free(ctx->xml, str);
1504 if (getcwd(dir, sizeof(dir)) == NULL)
1506 snprintf(buf, sizeof(buf), "%s/SP/%s/aaa-ca.pem", dir, fqdn);
1507 if (os_file_exists(buf)) {
1508 if (set_cred_quoted(ctx->ifname, id, "ca_cert", buf) < 0) {
1509 wpa_printf(MSG_INFO, "Failed to set CA cert");
1515 static void set_pps_cred_check_aaa_cert_status(struct hs20_osu_client *ctx,
1516 int id, xml_node_t *node)
1518 char *str = xml_node_get_text(ctx->xml, node);
1523 wpa_printf(MSG_INFO, "- Credential/CheckAAAServerCertStatus = %s", str);
1524 if (os_strcasecmp(str, "true") == 0 &&
1525 set_cred(ctx->ifname, id, "ocsp", "2") < 0)
1526 wpa_printf(MSG_INFO, "Failed to set cred ocsp");
1527 xml_node_get_text_free(ctx->xml, str);
1531 static void set_pps_cred_sim(struct hs20_osu_client *ctx, int id,
1532 xml_node_t *sim, xml_node_t *realm)
1535 char *imsi, *eaptype, *str, buf[20];
1540 node = get_node(ctx->xml, sim, "EAPType");
1542 wpa_printf(MSG_INFO, "No SIM/EAPType node in credential");
1545 eaptype = xml_node_get_text(ctx->xml, node);
1546 if (eaptype == NULL) {
1547 wpa_printf(MSG_INFO, "Could not extract SIM/EAPType");
1550 wpa_printf(MSG_INFO, " - Credential/SIM/EAPType = %s", eaptype);
1551 type = atoi(eaptype);
1552 xml_node_get_text_free(ctx->xml, eaptype);
1556 if (set_cred(ctx->ifname, id, "eap", "SIM") < 0)
1557 wpa_printf(MSG_INFO, "Could not set eap=SIM");
1560 if (set_cred(ctx->ifname, id, "eap", "AKA") < 0)
1561 wpa_printf(MSG_INFO, "Could not set eap=SIM");
1563 case EAP_TYPE_AKA_PRIME:
1564 if (set_cred(ctx->ifname, id, "eap", "AKA'") < 0)
1565 wpa_printf(MSG_INFO, "Could not set eap=SIM");
1568 wpa_printf(MSG_INFO, "Unsupported SIM/EAPType %d", type);
1572 node = get_node(ctx->xml, sim, "IMSI");
1574 wpa_printf(MSG_INFO, "No SIM/IMSI node in credential");
1577 imsi = xml_node_get_text(ctx->xml, node);
1579 wpa_printf(MSG_INFO, "Could not extract SIM/IMSI");
1582 wpa_printf(MSG_INFO, " - Credential/SIM/IMSI = %s", imsi);
1583 imsi_len = os_strlen(imsi);
1584 if (imsi_len < 7 || imsi_len + 2 > sizeof(buf)) {
1585 wpa_printf(MSG_INFO, "Invalid IMSI length");
1586 xml_node_get_text_free(ctx->xml, imsi);
1590 str = xml_node_get_text(ctx->xml, node);
1593 pos = os_strstr(str, "mnc");
1594 if (pos && os_strlen(pos) >= 6) {
1595 if (os_strncmp(imsi + 3, pos + 3, 3) == 0)
1597 else if (os_strncmp(imsi + 3, pos + 4, 2) == 0)
1600 xml_node_get_text_free(ctx->xml, str);
1603 os_memcpy(buf, imsi, 3 + mnc_len);
1604 buf[3 + mnc_len] = '-';
1605 os_strlcpy(buf + 3 + mnc_len + 1, imsi + 3 + mnc_len,
1606 sizeof(buf) - 3 - mnc_len - 1);
1608 xml_node_get_text_free(ctx->xml, imsi);
1610 if (set_cred_quoted(ctx->ifname, id, "imsi", buf) < 0)
1611 wpa_printf(MSG_INFO, "Could not set IMSI");
1613 if (set_cred_quoted(ctx->ifname, id, "milenage",
1614 "90dca4eda45b53cf0f12d7c9c3bc6a89:"
1615 "cb9cccc4b9258e6dca4760379fb82581:000000000123") <
1617 wpa_printf(MSG_INFO, "Could not set Milenage parameters");
1621 static void set_pps_cred_credential(struct hs20_osu_client *ctx, int id,
1622 xml_node_t *node, const char *fqdn)
1624 xml_node_t *child, *sim, *realm;
1627 wpa_printf(MSG_INFO, "- Credential");
1629 sim = get_node(ctx->xml, node, "SIM");
1630 realm = get_node(ctx->xml, node, "Realm");
1632 xml_node_for_each_child(ctx->xml, child, node) {
1633 xml_node_for_each_check(ctx->xml, child);
1634 name = xml_node_get_localname(ctx->xml, child);
1635 if (os_strcasecmp(name, "CreationDate") == 0)
1636 set_pps_cred_creation_date(ctx, id, child);
1637 else if (os_strcasecmp(name, "ExpirationDate") == 0)
1638 set_pps_cred_expiration_date(ctx, id, child);
1639 else if (os_strcasecmp(name, "UsernamePassword") == 0)
1640 set_pps_cred_username_password(ctx, id, child);
1641 else if (os_strcasecmp(name, "DigitalCertificate") == 0)
1642 set_pps_cred_digital_cert(ctx, id, child, fqdn);
1643 else if (os_strcasecmp(name, "Realm") == 0)
1644 set_pps_cred_realm(ctx, id, child, fqdn, sim != NULL);
1645 else if (os_strcasecmp(name, "CheckAAAServerCertStatus") == 0)
1646 set_pps_cred_check_aaa_cert_status(ctx, id, child);
1647 else if (os_strcasecmp(name, "SIM") == 0)
1648 set_pps_cred_sim(ctx, id, child, realm);
1650 wpa_printf(MSG_INFO, "Unknown Credential node '%s'",
1656 static void set_pps_credential(struct hs20_osu_client *ctx, int id,
1657 xml_node_t *cred, const char *fqdn)
1662 xml_node_for_each_child(ctx->xml, child, cred) {
1663 xml_node_for_each_check(ctx->xml, child);
1664 name = xml_node_get_localname(ctx->xml, child);
1665 if (os_strcasecmp(name, "Policy") == 0)
1666 set_pps_cred_policy(ctx, id, child);
1667 else if (os_strcasecmp(name, "CredentialPriority") == 0)
1668 set_pps_cred_priority(ctx, id, child);
1669 else if (os_strcasecmp(name, "AAAServerTrustRoot") == 0)
1670 set_pps_cred_aaa_server_trust_root(ctx, id, child);
1671 else if (os_strcasecmp(name, "SubscriptionUpdate") == 0)
1672 set_pps_cred_sub_update(ctx, id, child);
1673 else if (os_strcasecmp(name, "HomeSP") == 0)
1674 set_pps_cred_home_sp(ctx, id, child);
1675 else if (os_strcasecmp(name, "SubscriptionParameters") == 0)
1676 set_pps_cred_sub_params(ctx, id, child);
1677 else if (os_strcasecmp(name, "Credential") == 0)
1678 set_pps_cred_credential(ctx, id, child, fqdn);
1680 wpa_printf(MSG_INFO, "Unknown credential node '%s'",
1686 static void set_pps(struct hs20_osu_client *ctx, xml_node_t *pps,
1692 char *update_identifier = NULL;
1695 * TODO: Could consider more complex mechanism that would remove
1696 * credentials only if there are changes in the information sent to
1699 remove_sp_creds(ctx, fqdn);
1701 xml_node_for_each_child(ctx->xml, child, pps) {
1702 xml_node_for_each_check(ctx->xml, child);
1703 name = xml_node_get_localname(ctx->xml, child);
1704 if (os_strcasecmp(name, "UpdateIdentifier") == 0) {
1705 update_identifier = xml_node_get_text(ctx->xml, child);
1706 if (update_identifier) {
1707 wpa_printf(MSG_INFO, "- UpdateIdentifier = %s",
1714 xml_node_for_each_child(ctx->xml, child, pps) {
1715 xml_node_for_each_check(ctx->xml, child);
1716 name = xml_node_get_localname(ctx->xml, child);
1717 if (os_strcasecmp(name, "UpdateIdentifier") == 0)
1719 id = add_cred(ctx->ifname);
1721 wpa_printf(MSG_INFO, "Failed to add credential to wpa_supplicant");
1722 write_summary(ctx, "Failed to add credential to wpa_supplicant");
1725 write_summary(ctx, "Add a credential to wpa_supplicant");
1726 if (update_identifier &&
1727 set_cred(ctx->ifname, id, "update_identifier",
1728 update_identifier) < 0)
1729 wpa_printf(MSG_INFO, "Failed to set update_identifier");
1730 if (set_cred_quoted(ctx->ifname, id, "provisioning_sp", fqdn) <
1732 wpa_printf(MSG_INFO, "Failed to set provisioning_sp");
1733 wpa_printf(MSG_INFO, "credential localname: '%s'", name);
1734 set_pps_credential(ctx, id, child, fqdn);
1735 ctx->pps_cred_set = 1;
1738 xml_node_get_text_free(ctx->xml, update_identifier);
1742 void cmd_set_pps(struct hs20_osu_client *ctx, const char *pps_fname)
1746 char *fqdn_buf = NULL, *pos;
1748 pps = node_from_file(ctx->xml, pps_fname);
1750 wpa_printf(MSG_INFO, "Could not read or parse '%s'", pps_fname);
1754 fqdn = os_strstr(pps_fname, "SP/");
1756 fqdn_buf = os_strdup(fqdn + 3);
1757 if (fqdn_buf == NULL)
1759 pos = os_strchr(fqdn_buf, '/');
1766 wpa_printf(MSG_INFO, "Set PPS MO info to wpa_supplicant - SP FQDN %s",
1768 set_pps(ctx, pps, fqdn);
1771 xml_node_free(ctx->xml, pps);
1775 static int cmd_get_fqdn(struct hs20_osu_client *ctx, const char *pps_fname)
1777 xml_node_t *pps, *node;
1780 pps = node_from_file(ctx->xml, pps_fname);
1782 wpa_printf(MSG_INFO, "Could not read or parse '%s'", pps_fname);
1786 node = get_child_node(ctx->xml, pps, "HomeSP/FQDN");
1788 fqdn = xml_node_get_text(ctx->xml, node);
1790 xml_node_free(ctx->xml, pps);
1793 FILE *f = fopen("pps-fqdn", "w");
1795 fprintf(f, "%s", fqdn);
1798 xml_node_get_text_free(ctx->xml, fqdn);
1802 xml_node_get_text_free(ctx->xml, fqdn);
1807 static void cmd_to_tnds(struct hs20_osu_client *ctx, const char *in_fname,
1808 const char *out_fname, const char *urn, int use_path)
1810 xml_node_t *mo, *node;
1812 mo = node_from_file(ctx->xml, in_fname);
1814 wpa_printf(MSG_INFO, "Could not read or parse '%s'", in_fname);
1818 node = mo_to_tnds(ctx->xml, mo, use_path, urn, NULL);
1820 node_to_file(ctx->xml, out_fname, node);
1821 xml_node_free(ctx->xml, node);
1824 xml_node_free(ctx->xml, mo);
1828 static void cmd_from_tnds(struct hs20_osu_client *ctx, const char *in_fname,
1829 const char *out_fname)
1831 xml_node_t *tnds, *mo;
1833 tnds = node_from_file(ctx->xml, in_fname);
1835 wpa_printf(MSG_INFO, "Could not read or parse '%s'", in_fname);
1839 mo = tnds_to_mo(ctx->xml, tnds);
1841 node_to_file(ctx->xml, out_fname, mo);
1842 xml_node_free(ctx->xml, mo);
1845 xml_node_free(ctx->xml, tnds);
1852 char mime_type[256];
1859 unsigned int methods;
1862 struct osu_lang_text friendly_name[MAX_OSU_VALS];
1863 size_t friendly_name_count;
1864 struct osu_lang_text serv_desc[MAX_OSU_VALS];
1865 size_t serv_desc_count;
1866 struct osu_icon icon[MAX_OSU_VALS];
1871 static struct osu_data * parse_osu_providers(const char *fname, size_t *count)
1875 struct osu_data *osu = NULL, *last = NULL;
1876 size_t osu_count = 0;
1879 f = fopen(fname, "r");
1881 wpa_printf(MSG_ERROR, "Could not open %s", fname);
1885 while (fgets(buf, sizeof(buf), f)) {
1886 pos = strchr(buf, '\n');
1890 if (strncmp(buf, "OSU-PROVIDER ", 13) == 0) {
1891 last = realloc(osu, (osu_count + 1) * sizeof(*osu));
1895 last = &osu[osu_count++];
1896 memset(last, 0, sizeof(*last));
1897 snprintf(last->bssid, sizeof(last->bssid), "%s",
1904 if (strncmp(buf, "uri=", 4) == 0) {
1905 snprintf(last->url, sizeof(last->url), "%s", buf + 4);
1909 if (strncmp(buf, "methods=", 8) == 0) {
1910 last->methods = strtol(buf + 8, NULL, 16);
1914 if (strncmp(buf, "osu_ssid=", 9) == 0) {
1915 snprintf(last->osu_ssid, sizeof(last->osu_ssid),
1920 if (os_strncmp(buf, "osu_nai=", 8) == 0) {
1921 os_snprintf(last->osu_nai, sizeof(last->osu_nai),
1926 if (strncmp(buf, "friendly_name=", 14) == 0) {
1927 struct osu_lang_text *txt;
1928 if (last->friendly_name_count == MAX_OSU_VALS)
1930 pos = strchr(buf + 14, ':');
1934 txt = &last->friendly_name[last->friendly_name_count++];
1935 snprintf(txt->lang, sizeof(txt->lang), "%s", buf + 14);
1936 snprintf(txt->text, sizeof(txt->text), "%s", pos);
1939 if (strncmp(buf, "desc=", 5) == 0) {
1940 struct osu_lang_text *txt;
1941 if (last->serv_desc_count == MAX_OSU_VALS)
1943 pos = strchr(buf + 5, ':');
1947 txt = &last->serv_desc[last->serv_desc_count++];
1948 snprintf(txt->lang, sizeof(txt->lang), "%s", buf + 5);
1949 snprintf(txt->text, sizeof(txt->text), "%s", pos);
1952 if (strncmp(buf, "icon=", 5) == 0) {
1953 struct osu_icon *icon;
1954 if (last->icon_count == MAX_OSU_VALS)
1956 icon = &last->icon[last->icon_count++];
1957 icon->id = atoi(buf + 5);
1958 pos = strchr(buf, ':');
1961 pos = strchr(pos + 1, ':');
1964 pos = strchr(pos + 1, ':');
1968 end = strchr(pos, ':');
1972 snprintf(icon->lang, sizeof(icon->lang), "%s", pos);
1975 end = strchr(pos, ':');
1978 snprintf(icon->mime_type, sizeof(icon->mime_type),
1984 end = strchr(pos, ':');
1987 snprintf(icon->filename, sizeof(icon->filename),
2000 static int osu_connect(struct hs20_osu_client *ctx, const char *bssid,
2001 const char *ssid, const char *url,
2002 unsigned int methods, int no_prod_assoc,
2003 const char *osu_nai)
2006 const char *ifname = ctx->ifname;
2008 struct wpa_ctrl *mon;
2011 id = add_network(ifname);
2014 if (set_network_quoted(ifname, id, "ssid", ssid) < 0)
2016 if (osu_nai && os_strlen(osu_nai) > 0) {
2017 char dir[255], fname[300];
2018 if (getcwd(dir, sizeof(dir)) == NULL)
2020 os_snprintf(fname, sizeof(fname), "%s/osu-ca.pem", dir);
2022 if (set_network(ifname, id, "proto", "OSEN") < 0 ||
2023 set_network(ifname, id, "key_mgmt", "OSEN") < 0 ||
2024 set_network(ifname, id, "pairwise", "CCMP") < 0 ||
2025 set_network(ifname, id, "group", "GTK_NOT_USED") < 0 ||
2026 set_network(ifname, id, "eap", "WFA-UNAUTH-TLS") < 0 ||
2027 set_network(ifname, id, "ocsp", "2") < 0 ||
2028 set_network_quoted(ifname, id, "identity", osu_nai) < 0 ||
2029 set_network_quoted(ifname, id, "ca_cert", fname) < 0)
2032 if (set_network(ifname, id, "key_mgmt", "NONE") < 0)
2036 mon = open_wpa_mon(ifname);
2040 wpa_printf(MSG_INFO, "Associate with OSU SSID");
2041 write_summary(ctx, "Associate with OSU SSID");
2042 snprintf(buf, sizeof(buf), "SELECT_NETWORK %d", id);
2043 if (wpa_command(ifname, buf) < 0)
2046 res = get_wpa_cli_event(mon, "CTRL-EVENT-CONNECTED",
2049 wpa_ctrl_detach(mon);
2050 wpa_ctrl_close(mon);
2053 wpa_printf(MSG_INFO, "Could not connect");
2054 write_summary(ctx, "Could not connect to OSU network");
2055 wpa_printf(MSG_INFO, "Remove OSU network connection");
2056 snprintf(buf, sizeof(buf), "REMOVE_NETWORK %d", id);
2057 wpa_command(ifname, buf);
2061 write_summary(ctx, "Waiting for IP address for subscription registration");
2062 if (wait_ip_addr(ifname, 15) < 0) {
2063 wpa_printf(MSG_INFO, "Could not get IP address for WLAN - try connection anyway");
2066 if (no_prod_assoc) {
2069 wpa_printf(MSG_INFO, "No production connection used for testing purposes");
2070 write_summary(ctx, "No production connection used for testing purposes");
2074 ctx->no_reconnect = 1;
2076 res = cmd_prov(ctx, url);
2077 else if (methods & 0x01)
2078 res = cmd_oma_dm_prov(ctx, url);
2080 wpa_printf(MSG_INFO, "Remove OSU network connection");
2081 write_summary(ctx, "Remove OSU network connection");
2082 snprintf(buf, sizeof(buf), "REMOVE_NETWORK %d", id);
2083 wpa_command(ifname, buf);
2088 wpa_printf(MSG_INFO, "Requesting reconnection with updated configuration");
2089 write_summary(ctx, "Requesting reconnection with updated configuration");
2090 if (wpa_command(ctx->ifname, "INTERWORKING_SELECT auto") < 0) {
2091 wpa_printf(MSG_INFO, "Failed to request wpa_supplicant to reconnect");
2092 write_summary(ctx, "Failed to request wpa_supplicant to reconnect");
2100 static int cmd_osu_select(struct hs20_osu_client *ctx, const char *dir,
2101 int connect, int no_prod_assoc,
2102 const char *friendly_name)
2106 struct osu_data *osu = NULL, *last = NULL;
2107 size_t osu_count, i, j;
2110 write_summary(ctx, "OSU provider selection");
2113 wpa_printf(MSG_INFO, "Missing dir parameter to osu_select");
2117 snprintf(fname, sizeof(fname), "%s/osu-providers.txt", dir);
2118 osu = parse_osu_providers(fname, &osu_count);
2120 wpa_printf(MSG_INFO, "Could not any OSU providers from %s",
2122 write_result(ctx, "No OSU providers available");
2126 if (friendly_name) {
2127 for (i = 0; i < osu_count; i++) {
2129 for (j = 0; j < last->friendly_name_count; j++) {
2130 if (os_strcmp(last->friendly_name[j].text,
2131 friendly_name) == 0)
2134 if (j < last->friendly_name_count)
2137 if (i == osu_count) {
2138 wpa_printf(MSG_INFO, "Requested operator friendly name '%s' not found in the list of available providers",
2140 write_summary(ctx, "Requested operator friendly name '%s' not found in the list of available providers",
2146 wpa_printf(MSG_INFO, "OSU Provider selected based on requested operator friendly name '%s'",
2148 write_summary(ctx, "OSU Provider selected based on requested operator friendly name '%s'",
2154 snprintf(fname, sizeof(fname), "%s/osu-providers.html", dir);
2155 f = fopen(fname, "w");
2157 wpa_printf(MSG_INFO, "Could not open %s", fname);
2162 fprintf(f, "<html><head>"
2163 "<meta http-equiv=\"Content-type\" content=\"text/html; "
2164 "charset=utf-8\"<title>Select service operator</title>"
2165 "</head><body><h1>Select service operator</h1>\n");
2168 fprintf(f, "No online signup available\n");
2170 for (i = 0; i < osu_count; i++) {
2174 "<a href=\"http://localhost:12345/osu/%d\">"
2175 "<table><tr><td>", (int) i + 1);
2178 "<a href=\"osu://%d\">"
2179 "<table><tr><td>", (int) i + 1);
2180 #endif /* ANDROID */
2181 for (j = 0; j < last->icon_count; j++) {
2182 fprintf(f, "<img src=\"osu-icon-%d.%s\">\n",
2184 strcasecmp(last->icon[j].mime_type,
2185 "image/png") == 0 ? "png" : "icon");
2188 for (j = 0; j < last->friendly_name_count; j++) {
2189 fprintf(f, "<small>[%s]</small> %s<br>\n",
2190 last->friendly_name[j].lang,
2191 last->friendly_name[j].text);
2193 fprintf(f, "<tr><td colspan=2>");
2194 for (j = 0; j < last->serv_desc_count; j++) {
2195 fprintf(f, "<small>[%s]</small> %s<br>\n",
2196 last->serv_desc[j].lang,
2197 last->serv_desc[j].text);
2199 fprintf(f, "</table></a><br><small>BSSID: %s<br>\n"
2201 last->bssid, last->osu_ssid);
2203 fprintf(f, "NAI: %s<br>\n", last->osu_nai);
2204 fprintf(f, "URL: %s<br>\n"
2205 "methods:%s%s<br>\n"
2208 last->methods & 0x01 ? " OMA-DM" : "",
2209 last->methods & 0x02 ? " SOAP-XML-SPP" : "");
2212 fprintf(f, "</body></html>\n");
2216 snprintf(fname, sizeof(fname), "file://%s/osu-providers.html", dir);
2217 write_summary(ctx, "Start web browser with OSU provider selection page");
2218 ret = hs20_web_browser(fname);
2221 if (ret > 0 && (size_t) ret <= osu_count) {
2225 wpa_printf(MSG_INFO, "Selected OSU id=%d", ret);
2226 last = &osu[ret - 1];
2228 wpa_printf(MSG_INFO, "BSSID: %s", last->bssid);
2229 wpa_printf(MSG_INFO, "SSID: %s", last->osu_ssid);
2230 wpa_printf(MSG_INFO, "URL: %s", last->url);
2231 write_summary(ctx, "Selected OSU provider id=%d BSSID=%s SSID=%s URL=%s",
2232 ret, last->bssid, last->osu_ssid, last->url);
2234 ctx->friendly_name_count = last->friendly_name_count;
2235 for (j = 0; j < last->friendly_name_count; j++) {
2236 wpa_printf(MSG_INFO, "FRIENDLY_NAME: [%s]%s",
2237 last->friendly_name[j].lang,
2238 last->friendly_name[j].text);
2239 os_strlcpy(ctx->friendly_name[j].lang,
2240 last->friendly_name[j].lang,
2241 sizeof(ctx->friendly_name[j].lang));
2242 os_strlcpy(ctx->friendly_name[j].text,
2243 last->friendly_name[j].text,
2244 sizeof(ctx->friendly_name[j].text));
2247 ctx->icon_count = last->icon_count;
2248 for (j = 0; j < last->icon_count; j++) {
2251 os_snprintf(fname, sizeof(fname), "%s/osu-icon-%d.%s",
2252 dir, last->icon[j].id,
2253 strcasecmp(last->icon[j].mime_type,
2256 wpa_printf(MSG_INFO, "ICON: %s (%s)",
2257 fname, last->icon[j].filename);
2258 os_strlcpy(ctx->icon_filename[j],
2259 last->icon[j].filename,
2260 sizeof(ctx->icon_filename[j]));
2262 data = os_readfile(fname, &data_len);
2264 sha256_vector(1, (const u8 **) &data, &data_len,
2271 if (last->methods & 0x02)
2272 ret = cmd_prov(ctx, last->url);
2273 else if (last->methods & 0x01)
2274 ret = cmd_oma_dm_prov(ctx, last->url);
2278 ret = osu_connect(ctx, last->bssid, last->osu_ssid,
2279 last->url, last->methods,
2280 no_prod_assoc, last->osu_nai);
2290 static int cmd_signup(struct hs20_osu_client *ctx, int no_prod_assoc,
2291 const char *friendly_name)
2294 char fname[300], buf[400];
2295 struct wpa_ctrl *mon;
2299 ifname = ctx->ifname;
2301 if (getcwd(dir, sizeof(dir)) == NULL)
2304 snprintf(fname, sizeof(fname), "%s/osu-info", dir);
2305 if (mkdir(fname, S_IRWXU | S_IRWXG) < 0 && errno != EEXIST) {
2306 wpa_printf(MSG_INFO, "mkdir(%s) failed: %s",
2307 fname, strerror(errno));
2311 snprintf(buf, sizeof(buf), "SET osu_dir %s", fname);
2312 if (wpa_command(ifname, buf) < 0) {
2313 wpa_printf(MSG_INFO, "Failed to configure osu_dir to wpa_supplicant");
2317 mon = open_wpa_mon(ifname);
2321 wpa_printf(MSG_INFO, "Starting OSU fetch");
2322 write_summary(ctx, "Starting OSU provider information fetch");
2323 if (wpa_command(ifname, "FETCH_OSU") < 0) {
2324 wpa_printf(MSG_INFO, "Could not start OSU fetch");
2325 wpa_ctrl_detach(mon);
2326 wpa_ctrl_close(mon);
2329 res = get_wpa_cli_event(mon, "OSU provider fetch completed",
2332 wpa_ctrl_detach(mon);
2333 wpa_ctrl_close(mon);
2336 wpa_printf(MSG_INFO, "OSU fetch did not complete");
2337 write_summary(ctx, "OSU fetch did not complete");
2340 wpa_printf(MSG_INFO, "OSU provider fetch completed");
2342 return cmd_osu_select(ctx, fname, 1, no_prod_assoc, friendly_name);
2346 static int cmd_sub_rem(struct hs20_osu_client *ctx, const char *address,
2347 const char *pps_fname, const char *ca_fname)
2349 xml_node_t *pps, *node;
2350 char pps_fname_buf[300];
2351 char ca_fname_buf[200];
2352 char *cred_username = NULL;
2353 char *cred_password = NULL;
2354 char *sub_rem_uri = NULL;
2355 char client_cert_buf[200];
2356 char *client_cert = NULL;
2357 char client_key_buf[200];
2358 char *client_key = NULL;
2361 wpa_printf(MSG_INFO, "Subscription remediation requested with Server URL: %s",
2366 wpa_printf(MSG_INFO, "Determining PPS file based on Home SP information");
2367 if (os_strncmp(address, "fqdn=", 5) == 0) {
2368 wpa_printf(MSG_INFO, "Use requested FQDN from command line");
2369 os_snprintf(buf, sizeof(buf), "%s", address + 5);
2371 } else if (get_wpa_status(ctx->ifname, "provisioning_sp", buf,
2373 wpa_printf(MSG_INFO, "Could not get provisioning Home SP FQDN from wpa_supplicant");
2377 ctx->fqdn = os_strdup(buf);
2378 if (ctx->fqdn == NULL)
2380 wpa_printf(MSG_INFO, "Home SP FQDN for current credential: %s",
2382 os_snprintf(pps_fname_buf, sizeof(pps_fname_buf),
2383 "SP/%s/pps.xml", ctx->fqdn);
2384 pps_fname = pps_fname_buf;
2386 os_snprintf(ca_fname_buf, sizeof(ca_fname_buf), "SP/%s/ca.pem",
2388 ca_fname = ca_fname_buf;
2391 if (!os_file_exists(pps_fname)) {
2392 wpa_printf(MSG_INFO, "PPS file '%s' does not exist or is not accessible",
2396 wpa_printf(MSG_INFO, "Using PPS file: %s", pps_fname);
2398 if (ca_fname && !os_file_exists(ca_fname)) {
2399 wpa_printf(MSG_INFO, "CA file '%s' does not exist or is not accessible",
2403 wpa_printf(MSG_INFO, "Using server trust root: %s", ca_fname);
2404 ctx->ca_fname = ca_fname;
2406 pps = node_from_file(ctx->xml, pps_fname);
2408 wpa_printf(MSG_INFO, "Could not read PPS MO");
2414 node = get_child_node(ctx->xml, pps, "HomeSP/FQDN");
2416 wpa_printf(MSG_INFO, "No HomeSP/FQDN found from PPS");
2419 tmp = xml_node_get_text(ctx->xml, node);
2421 wpa_printf(MSG_INFO, "No HomeSP/FQDN text found from PPS");
2424 ctx->fqdn = os_strdup(tmp);
2425 xml_node_get_text_free(ctx->xml, tmp);
2427 wpa_printf(MSG_INFO, "No FQDN known");
2432 node = get_child_node(ctx->xml, pps,
2433 "SubscriptionUpdate/UpdateMethod");
2436 tmp = xml_node_get_text(ctx->xml, node);
2437 if (tmp && os_strcasecmp(tmp, "OMA-DM-ClientInitiated") == 0)
2442 wpa_printf(MSG_INFO, "No UpdateMethod specified - assume SPP");
2446 get_user_pw(ctx, pps, "SubscriptionUpdate/UsernamePassword",
2447 &cred_username, &cred_password);
2449 wpa_printf(MSG_INFO, "Using username: %s", cred_username);
2451 wpa_printf(MSG_DEBUG, "Using password: %s", cred_password);
2453 if (cred_username == NULL && cred_password == NULL &&
2454 get_child_node(ctx->xml, pps, "Credential/DigitalCertificate")) {
2455 wpa_printf(MSG_INFO, "Using client certificate");
2456 os_snprintf(client_cert_buf, sizeof(client_cert_buf),
2457 "SP/%s/client-cert.pem", ctx->fqdn);
2458 client_cert = client_cert_buf;
2459 os_snprintf(client_key_buf, sizeof(client_key_buf),
2460 "SP/%s/client-key.pem", ctx->fqdn);
2461 client_key = client_key_buf;
2462 ctx->client_cert_present = 1;
2465 node = get_child_node(ctx->xml, pps, "SubscriptionUpdate/URI");
2467 sub_rem_uri = xml_node_get_text(ctx->xml, node);
2469 (!address || os_strcmp(address, sub_rem_uri) != 0)) {
2470 wpa_printf(MSG_INFO, "Override sub rem URI based on PPS: %s",
2472 address = sub_rem_uri;
2476 wpa_printf(MSG_INFO, "Server URL not known");
2480 write_summary(ctx, "Wait for IP address for subscriptiom remediation");
2481 wpa_printf(MSG_INFO, "Wait for IP address before starting subscription remediation");
2483 if (wait_ip_addr(ctx->ifname, 15) < 0) {
2484 wpa_printf(MSG_INFO, "Could not get IP address for WLAN - try connection anyway");
2488 spp_sub_rem(ctx, address, pps_fname,
2489 client_cert, client_key,
2490 cred_username, cred_password, pps);
2492 oma_dm_sub_rem(ctx, address, pps_fname,
2493 client_cert, client_key,
2494 cred_username, cred_password, pps);
2496 xml_node_get_text_free(ctx->xml, sub_rem_uri);
2497 xml_node_get_text_free(ctx->xml, cred_username);
2498 str_clear_free(cred_password);
2499 xml_node_free(ctx->xml, pps);
2504 static int cmd_pol_upd(struct hs20_osu_client *ctx, const char *address,
2505 const char *pps_fname, const char *ca_fname)
2509 char pps_fname_buf[300];
2510 char ca_fname_buf[200];
2512 char *cred_username = NULL;
2513 char *cred_password = NULL;
2514 char client_cert_buf[200];
2515 char *client_cert = NULL;
2516 char client_key_buf[200];
2517 char *client_key = NULL;
2520 wpa_printf(MSG_INFO, "Policy update requested");
2524 wpa_printf(MSG_INFO, "Determining PPS file based on Home SP information");
2525 if (os_strncmp(address, "fqdn=", 5) == 0) {
2526 wpa_printf(MSG_INFO, "Use requested FQDN from command line");
2527 os_snprintf(buf, sizeof(buf), "%s", address + 5);
2529 } else if (get_wpa_status(ctx->ifname, "provisioning_sp", buf,
2531 wpa_printf(MSG_INFO, "Could not get provisioning Home SP FQDN from wpa_supplicant");
2535 ctx->fqdn = os_strdup(buf);
2536 if (ctx->fqdn == NULL)
2538 wpa_printf(MSG_INFO, "Home SP FQDN for current credential: %s",
2540 os_snprintf(pps_fname_buf, sizeof(pps_fname_buf),
2541 "SP/%s/pps.xml", ctx->fqdn);
2542 pps_fname = pps_fname_buf;
2544 os_snprintf(ca_fname_buf, sizeof(ca_fname_buf), "SP/%s/ca.pem",
2546 ca_fname = ca_fname_buf;
2549 if (!os_file_exists(pps_fname)) {
2550 wpa_printf(MSG_INFO, "PPS file '%s' does not exist or is not accessible",
2554 wpa_printf(MSG_INFO, "Using PPS file: %s", pps_fname);
2556 if (ca_fname && !os_file_exists(ca_fname)) {
2557 wpa_printf(MSG_INFO, "CA file '%s' does not exist or is not accessible",
2561 wpa_printf(MSG_INFO, "Using server trust root: %s", ca_fname);
2562 ctx->ca_fname = ca_fname;
2564 pps = node_from_file(ctx->xml, pps_fname);
2566 wpa_printf(MSG_INFO, "Could not read PPS MO");
2572 node = get_child_node(ctx->xml, pps, "HomeSP/FQDN");
2574 wpa_printf(MSG_INFO, "No HomeSP/FQDN found from PPS");
2577 tmp = xml_node_get_text(ctx->xml, node);
2579 wpa_printf(MSG_INFO, "No HomeSP/FQDN text found from PPS");
2582 ctx->fqdn = os_strdup(tmp);
2583 xml_node_get_text_free(ctx->xml, tmp);
2585 wpa_printf(MSG_INFO, "No FQDN known");
2590 node = get_child_node(ctx->xml, pps,
2591 "Policy/PolicyUpdate/UpdateMethod");
2594 tmp = xml_node_get_text(ctx->xml, node);
2595 if (tmp && os_strcasecmp(tmp, "OMA-DM-ClientInitiated") == 0)
2600 wpa_printf(MSG_INFO, "No UpdateMethod specified - assume SPP");
2604 get_user_pw(ctx, pps, "Policy/PolicyUpdate/UsernamePassword",
2605 &cred_username, &cred_password);
2607 wpa_printf(MSG_INFO, "Using username: %s", cred_username);
2609 wpa_printf(MSG_DEBUG, "Using password: %s", cred_password);
2611 if (cred_username == NULL && cred_password == NULL &&
2612 get_child_node(ctx->xml, pps, "Credential/DigitalCertificate")) {
2613 wpa_printf(MSG_INFO, "Using client certificate");
2614 os_snprintf(client_cert_buf, sizeof(client_cert_buf),
2615 "SP/%s/client-cert.pem", ctx->fqdn);
2616 client_cert = client_cert_buf;
2617 os_snprintf(client_key_buf, sizeof(client_key_buf),
2618 "SP/%s/client-key.pem", ctx->fqdn);
2619 client_key = client_key_buf;
2623 node = get_child_node(ctx->xml, pps, "Policy/PolicyUpdate/URI");
2625 uri = xml_node_get_text(ctx->xml, node);
2626 wpa_printf(MSG_INFO, "URI based on PPS: %s", uri);
2631 wpa_printf(MSG_INFO, "Server URL not known");
2636 spp_pol_upd(ctx, address, pps_fname,
2637 client_cert, client_key,
2638 cred_username, cred_password, pps);
2640 oma_dm_pol_upd(ctx, address, pps_fname,
2641 client_cert, client_key,
2642 cred_username, cred_password, pps);
2644 xml_node_get_text_free(ctx->xml, uri);
2645 xml_node_get_text_free(ctx->xml, cred_username);
2646 str_clear_free(cred_password);
2647 xml_node_free(ctx->xml, pps);
2653 static char * get_hostname(const char *url)
2655 const char *pos, *end, *end2;
2661 pos = os_strchr(url, '/');
2669 end = os_strchr(pos, '/');
2670 end2 = os_strchr(pos, ':');
2671 if (end && end2 && end2 < end)
2683 ret = os_malloc(end - pos + 2);
2687 os_memcpy(ret, pos, end - pos + 1);
2688 ret[end - pos + 1] = '\0';
2694 static int osu_cert_cb(void *_ctx, struct http_cert *cert)
2696 struct hs20_osu_client *ctx = _ctx;
2701 wpa_printf(MSG_INFO, "osu_cert_cb(osu_cert_validation=%d)",
2702 !ctx->no_osu_cert_validation);
2704 host = get_hostname(ctx->server_url);
2706 for (i = 0; i < ctx->server_dnsname_count; i++)
2707 os_free(ctx->server_dnsname[i]);
2708 os_free(ctx->server_dnsname);
2709 ctx->server_dnsname = os_calloc(cert->num_dnsname, sizeof(char *));
2710 ctx->server_dnsname_count = 0;
2713 for (i = 0; i < cert->num_dnsname; i++) {
2714 if (ctx->server_dnsname) {
2715 ctx->server_dnsname[ctx->server_dnsname_count] =
2716 os_strdup(cert->dnsname[i]);
2717 if (ctx->server_dnsname[ctx->server_dnsname_count])
2718 ctx->server_dnsname_count++;
2720 if (host && os_strcasecmp(host, cert->dnsname[i]) == 0)
2722 wpa_printf(MSG_INFO, "dNSName '%s'", cert->dnsname[i]);
2725 if (host && !found) {
2726 wpa_printf(MSG_INFO, "Server name from URL (%s) did not match any dNSName - abort connection",
2728 write_result(ctx, "Server name from URL (%s) did not match any dNSName - abort connection",
2736 for (i = 0; i < cert->num_othername; i++) {
2737 if (os_strcmp(cert->othername[i].oid,
2738 "1.3.6.1.4.1.40808.1.1.1") == 0) {
2739 wpa_hexdump_ascii(MSG_INFO,
2740 "id-wfa-hotspot-friendlyName",
2741 cert->othername[i].data,
2742 cert->othername[i].len);
2746 for (j = 0; !ctx->no_osu_cert_validation &&
2747 j < ctx->friendly_name_count; j++) {
2749 for (i = 0; i < cert->num_othername; i++) {
2750 if (os_strcmp(cert->othername[i].oid,
2751 "1.3.6.1.4.1.40808.1.1.1") != 0)
2753 if (cert->othername[i].len < 3)
2755 if (os_strncasecmp((char *) cert->othername[i].data,
2756 ctx->friendly_name[j].lang, 3) != 0)
2758 if (os_strncmp((char *) cert->othername[i].data + 3,
2759 ctx->friendly_name[j].text,
2760 cert->othername[i].len - 3) == 0) {
2767 wpa_printf(MSG_INFO, "No friendly name match found for '[%s]%s'",
2768 ctx->friendly_name[j].lang,
2769 ctx->friendly_name[j].text);
2770 write_result(ctx, "No friendly name match found for '[%s]%s'",
2771 ctx->friendly_name[j].lang,
2772 ctx->friendly_name[j].text);
2777 for (i = 0; i < cert->num_logo; i++) {
2778 struct http_logo *logo = &cert->logo[i];
2780 wpa_printf(MSG_INFO, "logo hash alg %s uri '%s'",
2781 logo->alg_oid, logo->uri);
2782 wpa_hexdump_ascii(MSG_INFO, "hashValue",
2783 logo->hash, logo->hash_len);
2786 for (j = 0; !ctx->no_osu_cert_validation && j < ctx->icon_count; j++) {
2788 char *name = ctx->icon_filename[j];
2789 size_t name_len = os_strlen(name);
2791 wpa_printf(MSG_INFO, "Looking for icon file name '%s' match",
2793 for (i = 0; i < cert->num_logo; i++) {
2794 struct http_logo *logo = &cert->logo[i];
2795 size_t uri_len = os_strlen(logo->uri);
2798 wpa_printf(MSG_INFO, "Comparing to '%s' uri_len=%d name_len=%d",
2799 logo->uri, (int) uri_len, (int) name_len);
2800 if (uri_len < 1 + name_len)
2802 pos = &logo->uri[uri_len - name_len - 1];
2806 if (os_strcmp(pos, name) == 0) {
2813 wpa_printf(MSG_INFO, "No icon filename match found for '%s'",
2816 "No icon filename match found for '%s'",
2822 for (j = 0; !ctx->no_osu_cert_validation && j < ctx->icon_count; j++) {
2825 for (i = 0; i < cert->num_logo; i++) {
2826 struct http_logo *logo = &cert->logo[i];
2828 if (logo->hash_len != 32)
2830 if (os_memcmp(logo->hash, ctx->icon_hash[j], 32) == 0) {
2837 wpa_printf(MSG_INFO, "No icon hash match found");
2838 write_result(ctx, "No icon hash match found");
2847 static int init_ctx(struct hs20_osu_client *ctx)
2849 xml_node_t *devinfo, *devid;
2851 os_memset(ctx, 0, sizeof(*ctx));
2852 ctx->ifname = "wlan0";
2853 ctx->xml = xml_node_init_ctx(ctx, NULL);
2854 if (ctx->xml == NULL)
2857 devinfo = node_from_file(ctx->xml, "devinfo.xml");
2859 wpa_printf(MSG_ERROR, "devinfo.xml not found");
2863 devid = get_node(ctx->xml, devinfo, "DevId");
2865 char *tmp = xml_node_get_text(ctx->xml, devid);
2867 ctx->devid = os_strdup(tmp);
2868 xml_node_get_text_free(ctx->xml, tmp);
2871 xml_node_free(ctx->xml, devinfo);
2873 if (ctx->devid == NULL) {
2874 wpa_printf(MSG_ERROR, "Could not fetch DevId from devinfo.xml");
2878 ctx->http = http_init_ctx(ctx, ctx->xml);
2879 if (ctx->http == NULL) {
2880 xml_node_deinit_ctx(ctx->xml);
2883 http_ocsp_set(ctx->http, 2);
2884 http_set_cert_cb(ctx->http, osu_cert_cb, ctx);
2890 static void deinit_ctx(struct hs20_osu_client *ctx)
2894 http_deinit_ctx(ctx->http);
2895 xml_node_deinit_ctx(ctx->xml);
2897 os_free(ctx->server_url);
2898 os_free(ctx->devid);
2900 for (i = 0; i < ctx->server_dnsname_count; i++)
2901 os_free(ctx->server_dnsname[i]);
2902 os_free(ctx->server_dnsname);
2906 static void check_workarounds(struct hs20_osu_client *ctx)
2910 unsigned long int val = 0;
2912 f = fopen("hs20-osu-client.workarounds", "r");
2916 if (fgets(buf, sizeof(buf), f))
2917 val = strtoul(buf, NULL, 16);
2922 wpa_printf(MSG_INFO, "Workarounds enabled: 0x%lx", val);
2923 ctx->workarounds = val;
2924 if (ctx->workarounds & WORKAROUND_OCSP_OPTIONAL)
2925 http_ocsp_set(ctx->http, 1);
2930 static void usage(void)
2932 printf("usage: hs20-osu-client [-dddqqKt] [-S<station ifname>] \\\n"
2933 " [-w<wpa_supplicant ctrl_iface dir>] "
2934 "[-r<result file>] [-f<debug file>] \\\n"
2935 " [-s<summary file>] \\\n"
2936 " <command> [arguments..]\n"
2938 "- to_tnds <XML MO> <XML MO in TNDS format> [URN]\n"
2939 "- to_tnds2 <XML MO> <XML MO in TNDS format (Path) "
2941 "- from_tnds <XML MO in TNDS format> <XML MO>\n"
2942 "- set_pps <PerProviderSubscription XML file name>\n"
2943 "- get_fqdn <PerProviderSubscription XML file name>\n"
2944 "- pol_upd [Server URL] [PPS] [CA cert]\n"
2945 "- sub_rem <Server URL> [PPS] [CA cert]\n"
2946 "- prov <Server URL> [CA cert]\n"
2947 "- oma_dm_prov <Server URL> [CA cert]\n"
2948 "- sim_prov <Server URL> [CA cert]\n"
2949 "- oma_dm_sim_prov <Server URL> [CA cert]\n"
2950 "- signup [CA cert]\n"
2951 "- dl_osu_ca <PPS> <CA file>\n"
2952 "- dl_polupd_ca <PPS> <CA file>\n"
2953 "- dl_aaa_ca <PPS> <CA file>\n"
2955 "- parse_cert <X.509 certificate (DER)>\n"
2956 "- osu_select <OSU info directory> [CA cert]\n");
2960 int main(int argc, char *argv[])
2962 struct hs20_osu_client ctx;
2965 int no_prod_assoc = 0;
2966 const char *friendly_name = NULL;
2967 const char *wpa_debug_file_path = NULL;
2968 extern char *wpas_ctrl_path;
2969 extern int wpa_debug_level;
2970 extern int wpa_debug_show_keys;
2971 extern int wpa_debug_timestamp;
2973 if (init_ctx(&ctx) < 0)
2977 c = getopt(argc, argv, "df:hi:KNO:qr:s:S:tw:");
2982 if (wpa_debug_level > 0)
2986 wpa_debug_file_path = optarg;
2989 wpa_debug_show_keys++;
2995 friendly_name = optarg;
3001 ctx.result_file = optarg;
3004 ctx.summary_file = optarg;
3007 ctx.ifname = optarg;
3010 wpa_debug_timestamp++;
3013 wpas_ctrl_path = optarg;
3023 if (argc - optind < 1) {
3028 wpa_debug_open_file(wpa_debug_file_path);
3032 #endif /* __linux__ */
3034 if (ctx.result_file)
3035 unlink(ctx.result_file);
3036 wpa_printf(MSG_DEBUG, "===[hs20-osu-client START - command: %s ]======"
3037 "================", argv[optind]);
3038 check_workarounds(&ctx);
3040 if (strcmp(argv[optind], "to_tnds") == 0) {
3041 if (argc - optind < 2) {
3045 cmd_to_tnds(&ctx, argv[optind + 1], argv[optind + 2],
3046 argc > optind + 3 ? argv[optind + 3] : NULL,
3048 } else if (strcmp(argv[optind], "to_tnds2") == 0) {
3049 if (argc - optind < 2) {
3053 cmd_to_tnds(&ctx, argv[optind + 1], argv[optind + 2],
3054 argc > optind + 3 ? argv[optind + 3] : NULL,
3056 } else if (strcmp(argv[optind], "from_tnds") == 0) {
3057 if (argc - optind < 2) {
3061 cmd_from_tnds(&ctx, argv[optind + 1], argv[optind + 2]);
3062 } else if (strcmp(argv[optind], "sub_rem") == 0) {
3063 if (argc - optind < 2) {
3067 if (argc - optind < 2)
3068 wpa_printf(MSG_ERROR, "Server URL missing from command line");
3070 ret = cmd_sub_rem(&ctx, argv[optind + 1],
3072 argv[optind + 2] : NULL,
3074 argv[optind + 3] : NULL);
3075 } else if (strcmp(argv[optind], "pol_upd") == 0) {
3076 if (argc - optind < 2) {
3080 ret = cmd_pol_upd(&ctx, argc > 2 ? argv[optind + 1] : NULL,
3081 argc > optind + 2 ? argv[optind + 2] : NULL,
3082 argc > optind + 3 ? argv[optind + 3] : NULL);
3083 } else if (strcmp(argv[optind], "prov") == 0) {
3084 if (argc - optind < 2) {
3088 ctx.ca_fname = argv[optind + 2];
3089 cmd_prov(&ctx, argv[optind + 1]);
3090 } else if (strcmp(argv[optind], "sim_prov") == 0) {
3091 if (argc - optind < 2) {
3095 ctx.ca_fname = argv[optind + 2];
3096 cmd_sim_prov(&ctx, argv[optind + 1]);
3097 } else if (strcmp(argv[optind], "dl_osu_ca") == 0) {
3098 if (argc - optind < 2) {
3102 cmd_dl_osu_ca(&ctx, argv[optind + 1], argv[optind + 2]);
3103 } else if (strcmp(argv[optind], "dl_polupd_ca") == 0) {
3104 if (argc - optind < 2) {
3108 cmd_dl_polupd_ca(&ctx, argv[optind + 1], argv[optind + 2]);
3109 } else if (strcmp(argv[optind], "dl_aaa_ca") == 0) {
3110 if (argc - optind < 2) {
3114 cmd_dl_aaa_ca(&ctx, argv[optind + 1], argv[optind + 2]);
3115 } else if (strcmp(argv[optind], "osu_select") == 0) {
3116 if (argc - optind < 2) {
3120 ctx.ca_fname = argc > optind + 2 ? argv[optind + 2] : NULL;
3121 cmd_osu_select(&ctx, argv[optind + 1], 2, 1, NULL);
3122 } else if (strcmp(argv[optind], "signup") == 0) {
3123 ctx.ca_fname = argc > optind + 1 ? argv[optind + 1] : NULL;
3124 ret = cmd_signup(&ctx, no_prod_assoc, friendly_name);
3125 } else if (strcmp(argv[optind], "set_pps") == 0) {
3126 if (argc - optind < 2) {
3130 cmd_set_pps(&ctx, argv[optind + 1]);
3131 } else if (strcmp(argv[optind], "get_fqdn") == 0) {
3132 if (argc - optind < 1) {
3136 ret = cmd_get_fqdn(&ctx, argv[optind + 1]);
3137 } else if (strcmp(argv[optind], "oma_dm_prov") == 0) {
3138 if (argc - optind < 2) {
3142 ctx.ca_fname = argv[optind + 2];
3143 cmd_oma_dm_prov(&ctx, argv[optind + 1]);
3144 } else if (strcmp(argv[optind], "oma_dm_sim_prov") == 0) {
3145 if (argc - optind < 2) {
3149 ctx.ca_fname = argv[optind + 2];
3150 if (cmd_oma_dm_sim_prov(&ctx, argv[optind + 1]) < 0) {
3151 write_summary(&ctx, "Failed to complete OMA DM SIM provisioning");
3154 } else if (strcmp(argv[optind], "oma_dm_add") == 0) {
3155 if (argc - optind < 2) {
3159 cmd_oma_dm_add(&ctx, argv[optind + 1], argv[optind + 2]);
3160 } else if (strcmp(argv[optind], "oma_dm_replace") == 0) {
3161 if (argc - optind < 2) {
3165 cmd_oma_dm_replace(&ctx, argv[optind + 1], argv[optind + 2]);
3166 } else if (strcmp(argv[optind], "est_csr") == 0) {
3167 if (argc - optind < 2) {
3171 mkdir("Cert", S_IRWXU);
3172 est_build_csr(&ctx, argv[optind + 1]);
3173 } else if (strcmp(argv[optind], "browser") == 0) {
3176 if (argc - optind < 2) {
3181 wpa_printf(MSG_INFO, "Launch web browser to URL %s",
3183 ret = hs20_web_browser(argv[optind + 1]);
3184 wpa_printf(MSG_INFO, "Web browser result: %d", ret);
3185 } else if (strcmp(argv[optind], "parse_cert") == 0) {
3186 if (argc - optind < 2) {
3191 wpa_debug_level = MSG_MSGDUMP;
3192 http_parse_x509_certificate(ctx.http, argv[optind + 1]);
3193 wpa_debug_level = MSG_INFO;
3195 wpa_printf(MSG_INFO, "Unknown command '%s'", argv[optind]);
3199 wpa_printf(MSG_DEBUG,
3200 "===[hs20-osu-client END ]======================");
3202 wpa_debug_close_file();