osu_client.c 82 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205
  1. /*
  2. * Hotspot 2.0 OSU client
  3. * Copyright (c) 2012-2014, Qualcomm Atheros, Inc.
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #include "includes.h"
  9. #include <time.h>
  10. #include <sys/stat.h>
  11. #include "common.h"
  12. #include "utils/browser.h"
  13. #include "utils/base64.h"
  14. #include "utils/xml-utils.h"
  15. #include "utils/http-utils.h"
  16. #include "common/wpa_ctrl.h"
  17. #include "common/wpa_helpers.h"
  18. #include "eap_common/eap_defs.h"
  19. #include "crypto/crypto.h"
  20. #include "crypto/sha256.h"
  21. #include "osu_client.h"
  22. void write_result(struct hs20_osu_client *ctx, const char *fmt, ...)
  23. {
  24. va_list ap;
  25. FILE *f;
  26. char buf[500];
  27. va_start(ap, fmt);
  28. vsnprintf(buf, sizeof(buf), fmt, ap);
  29. va_end(ap);
  30. write_summary(ctx, "%s", buf);
  31. if (!ctx->result_file)
  32. return;
  33. f = fopen(ctx->result_file, "w");
  34. if (f == NULL)
  35. return;
  36. va_start(ap, fmt);
  37. vfprintf(f, fmt, ap);
  38. va_end(ap);
  39. fprintf(f, "\n");
  40. fclose(f);
  41. }
  42. void write_summary(struct hs20_osu_client *ctx, const char *fmt, ...)
  43. {
  44. va_list ap;
  45. FILE *f;
  46. if (!ctx->summary_file)
  47. return;
  48. f = fopen(ctx->summary_file, "a");
  49. if (f == NULL)
  50. return;
  51. va_start(ap, fmt);
  52. vfprintf(f, fmt, ap);
  53. va_end(ap);
  54. fprintf(f, "\n");
  55. fclose(f);
  56. }
  57. void debug_dump_node(struct hs20_osu_client *ctx, const char *title,
  58. xml_node_t *node)
  59. {
  60. char *str = xml_node_to_str(ctx->xml, node);
  61. wpa_printf(MSG_DEBUG, "[hs20] %s: '%s'", title, str);
  62. free(str);
  63. }
  64. static int valid_fqdn(const char *fqdn)
  65. {
  66. const char *pos;
  67. /* TODO: could make this more complete.. */
  68. if (strchr(fqdn, '.') == 0 || strlen(fqdn) > 255)
  69. return 0;
  70. for (pos = fqdn; *pos; pos++) {
  71. if (*pos >= 'a' && *pos <= 'z')
  72. continue;
  73. if (*pos >= 'A' && *pos <= 'Z')
  74. continue;
  75. if (*pos >= '0' && *pos <= '9')
  76. continue;
  77. if (*pos == '-' || *pos == '.' || *pos == '_')
  78. continue;
  79. return 0;
  80. }
  81. return 1;
  82. }
  83. int osu_get_certificate(struct hs20_osu_client *ctx, xml_node_t *getcert)
  84. {
  85. xml_node_t *node;
  86. char *url, *user = NULL, *pw = NULL;
  87. char *proto;
  88. int ret = -1;
  89. proto = xml_node_get_attr_value(ctx->xml, getcert,
  90. "enrollmentProtocol");
  91. if (!proto)
  92. return -1;
  93. wpa_printf(MSG_INFO, "getCertificate - enrollmentProtocol=%s", proto);
  94. write_summary(ctx, "getCertificate - enrollmentProtocol=%s", proto);
  95. if (os_strcasecmp(proto, "EST") != 0) {
  96. wpa_printf(MSG_INFO, "Unsupported enrollmentProtocol");
  97. xml_node_get_attr_value_free(ctx->xml, proto);
  98. return -1;
  99. }
  100. xml_node_get_attr_value_free(ctx->xml, proto);
  101. node = get_node(ctx->xml, getcert, "enrollmentServerURI");
  102. if (node == NULL) {
  103. wpa_printf(MSG_INFO, "Could not find enrollmentServerURI node");
  104. xml_node_get_attr_value_free(ctx->xml, proto);
  105. return -1;
  106. }
  107. url = xml_node_get_text(ctx->xml, node);
  108. if (url == NULL) {
  109. wpa_printf(MSG_INFO, "Could not get URL text");
  110. return -1;
  111. }
  112. wpa_printf(MSG_INFO, "enrollmentServerURI: %s", url);
  113. write_summary(ctx, "enrollmentServerURI: %s", url);
  114. node = get_node(ctx->xml, getcert, "estUserID");
  115. if (node == NULL && !ctx->client_cert_present) {
  116. wpa_printf(MSG_INFO, "Could not find estUserID node");
  117. goto fail;
  118. }
  119. if (node) {
  120. user = xml_node_get_text(ctx->xml, node);
  121. if (user == NULL) {
  122. wpa_printf(MSG_INFO, "Could not get estUserID text");
  123. goto fail;
  124. }
  125. wpa_printf(MSG_INFO, "estUserID: %s", user);
  126. write_summary(ctx, "estUserID: %s", user);
  127. }
  128. node = get_node(ctx->xml, getcert, "estPassword");
  129. if (node == NULL && !ctx->client_cert_present) {
  130. wpa_printf(MSG_INFO, "Could not find estPassword node");
  131. goto fail;
  132. }
  133. if (node) {
  134. pw = xml_node_get_base64_text(ctx->xml, node, NULL);
  135. if (pw == NULL) {
  136. wpa_printf(MSG_INFO, "Could not get estPassword text");
  137. goto fail;
  138. }
  139. wpa_printf(MSG_INFO, "estPassword: %s", pw);
  140. }
  141. mkdir("Cert", S_IRWXU);
  142. if (est_load_cacerts(ctx, url) < 0 ||
  143. est_build_csr(ctx, url) < 0 ||
  144. est_simple_enroll(ctx, url, user, pw) < 0)
  145. goto fail;
  146. ret = 0;
  147. fail:
  148. xml_node_get_text_free(ctx->xml, url);
  149. xml_node_get_text_free(ctx->xml, user);
  150. xml_node_get_text_free(ctx->xml, pw);
  151. return ret;
  152. }
  153. static int process_est_cert(struct hs20_osu_client *ctx, xml_node_t *cert,
  154. const char *fqdn)
  155. {
  156. u8 digest1[SHA256_MAC_LEN], digest2[SHA256_MAC_LEN];
  157. char *der, *pem;
  158. size_t der_len, pem_len;
  159. char *fingerprint;
  160. char buf[200];
  161. wpa_printf(MSG_INFO, "PPS for certificate credential - fqdn=%s", fqdn);
  162. fingerprint = xml_node_get_text(ctx->xml, cert);
  163. if (fingerprint == NULL)
  164. return -1;
  165. if (hexstr2bin(fingerprint, digest1, SHA256_MAC_LEN) < 0) {
  166. wpa_printf(MSG_INFO, "Invalid SHA256 hash value");
  167. write_result(ctx, "Invalid client certificate SHA256 hash value in PPS");
  168. xml_node_get_text_free(ctx->xml, fingerprint);
  169. return -1;
  170. }
  171. xml_node_get_text_free(ctx->xml, fingerprint);
  172. der = os_readfile("Cert/est_cert.der", &der_len);
  173. if (der == NULL) {
  174. wpa_printf(MSG_INFO, "Could not find client certificate from EST");
  175. write_result(ctx, "Could not find client certificate from EST");
  176. return -1;
  177. }
  178. if (sha256_vector(1, (const u8 **) &der, &der_len, digest2) < 0) {
  179. os_free(der);
  180. return -1;
  181. }
  182. os_free(der);
  183. if (os_memcmp(digest1, digest2, sizeof(digest1)) != 0) {
  184. wpa_printf(MSG_INFO, "Client certificate from EST does not match fingerprint from PPS MO");
  185. write_result(ctx, "Client certificate from EST does not match fingerprint from PPS MO");
  186. return -1;
  187. }
  188. wpa_printf(MSG_INFO, "Client certificate from EST matches PPS MO");
  189. unlink("Cert/est_cert.der");
  190. os_snprintf(buf, sizeof(buf), "SP/%s/client-ca.pem", fqdn);
  191. if (rename("Cert/est-cacerts.pem", buf) < 0) {
  192. wpa_printf(MSG_INFO, "Could not move est-cacerts.pem to client-ca.pem: %s",
  193. strerror(errno));
  194. return -1;
  195. }
  196. pem = os_readfile(buf, &pem_len);
  197. os_snprintf(buf, sizeof(buf), "SP/%s/client-cert.pem", fqdn);
  198. if (rename("Cert/est_cert.pem", buf) < 0) {
  199. wpa_printf(MSG_INFO, "Could not move est_cert.pem to client-cert.pem: %s",
  200. strerror(errno));
  201. os_free(pem);
  202. return -1;
  203. }
  204. if (pem) {
  205. FILE *f = fopen(buf, "a");
  206. if (f) {
  207. fwrite(pem, pem_len, 1, f);
  208. fclose(f);
  209. }
  210. os_free(pem);
  211. }
  212. os_snprintf(buf, sizeof(buf), "SP/%s/client-key.pem", fqdn);
  213. if (rename("Cert/privkey-plain.pem", buf) < 0) {
  214. wpa_printf(MSG_INFO, "Could not move privkey-plain.pem to client-key.pem: %s",
  215. strerror(errno));
  216. return -1;
  217. }
  218. unlink("Cert/est-req.b64");
  219. unlink("Cert/est-req.pem");
  220. unlink("Cert/est-resp.raw");
  221. rmdir("Cert");
  222. return 0;
  223. }
  224. #define TMP_CERT_DL_FILE "tmp-cert-download"
  225. static int download_cert(struct hs20_osu_client *ctx, xml_node_t *params,
  226. const char *fname)
  227. {
  228. xml_node_t *url_node, *hash_node;
  229. char *url, *hash;
  230. char *cert;
  231. size_t len;
  232. u8 digest1[SHA256_MAC_LEN], digest2[SHA256_MAC_LEN];
  233. int res;
  234. unsigned char *b64;
  235. FILE *f;
  236. url_node = get_node(ctx->xml, params, "CertURL");
  237. hash_node = get_node(ctx->xml, params, "CertSHA256Fingerprint");
  238. if (url_node == NULL || hash_node == NULL)
  239. return -1;
  240. url = xml_node_get_text(ctx->xml, url_node);
  241. hash = xml_node_get_text(ctx->xml, hash_node);
  242. if (url == NULL || hash == NULL) {
  243. xml_node_get_text_free(ctx->xml, url);
  244. xml_node_get_text_free(ctx->xml, hash);
  245. return -1;
  246. }
  247. wpa_printf(MSG_INFO, "CertURL: %s", url);
  248. wpa_printf(MSG_INFO, "SHA256 hash: %s", hash);
  249. if (hexstr2bin(hash, digest1, SHA256_MAC_LEN) < 0) {
  250. wpa_printf(MSG_INFO, "Invalid SHA256 hash value");
  251. write_result(ctx, "Invalid SHA256 hash value for downloaded certificate");
  252. xml_node_get_text_free(ctx->xml, hash);
  253. return -1;
  254. }
  255. xml_node_get_text_free(ctx->xml, hash);
  256. write_summary(ctx, "Download certificate from %s", url);
  257. ctx->no_osu_cert_validation = 1;
  258. http_ocsp_set(ctx->http, 1);
  259. res = http_download_file(ctx->http, url, TMP_CERT_DL_FILE, NULL);
  260. http_ocsp_set(ctx->http,
  261. (ctx->workarounds & WORKAROUND_OCSP_OPTIONAL) ? 1 : 2);
  262. ctx->no_osu_cert_validation = 0;
  263. xml_node_get_text_free(ctx->xml, url);
  264. if (res < 0)
  265. return -1;
  266. cert = os_readfile(TMP_CERT_DL_FILE, &len);
  267. remove(TMP_CERT_DL_FILE);
  268. if (cert == NULL)
  269. return -1;
  270. if (sha256_vector(1, (const u8 **) &cert, &len, digest2) < 0) {
  271. os_free(cert);
  272. return -1;
  273. }
  274. if (os_memcmp(digest1, digest2, sizeof(digest1)) != 0) {
  275. wpa_printf(MSG_INFO, "Downloaded certificate fingerprint did not match");
  276. write_result(ctx, "Downloaded certificate fingerprint did not match");
  277. os_free(cert);
  278. return -1;
  279. }
  280. b64 = base64_encode((unsigned char *) cert, len, NULL);
  281. os_free(cert);
  282. if (b64 == NULL)
  283. return -1;
  284. f = fopen(fname, "wb");
  285. if (f == NULL) {
  286. os_free(b64);
  287. return -1;
  288. }
  289. fprintf(f, "-----BEGIN CERTIFICATE-----\n"
  290. "%s"
  291. "-----END CERTIFICATE-----\n",
  292. b64);
  293. os_free(b64);
  294. fclose(f);
  295. wpa_printf(MSG_INFO, "Downloaded certificate into %s and validated fingerprint",
  296. fname);
  297. write_summary(ctx, "Downloaded certificate into %s and validated fingerprint",
  298. fname);
  299. return 0;
  300. }
  301. static int cmd_dl_osu_ca(struct hs20_osu_client *ctx, const char *pps_fname,
  302. const char *ca_fname)
  303. {
  304. xml_node_t *pps, *node;
  305. int ret;
  306. pps = node_from_file(ctx->xml, pps_fname);
  307. if (pps == NULL) {
  308. wpa_printf(MSG_INFO, "Could not read or parse '%s'", pps_fname);
  309. return -1;
  310. }
  311. node = get_child_node(ctx->xml, pps,
  312. "SubscriptionUpdate/TrustRoot");
  313. if (node == NULL) {
  314. wpa_printf(MSG_INFO, "No SubscriptionUpdate/TrustRoot/CertURL found from PPS");
  315. xml_node_free(ctx->xml, pps);
  316. return -1;
  317. }
  318. ret = download_cert(ctx, node, ca_fname);
  319. xml_node_free(ctx->xml, pps);
  320. return ret;
  321. }
  322. static int cmd_dl_polupd_ca(struct hs20_osu_client *ctx, const char *pps_fname,
  323. const char *ca_fname)
  324. {
  325. xml_node_t *pps, *node;
  326. int ret;
  327. pps = node_from_file(ctx->xml, pps_fname);
  328. if (pps == NULL) {
  329. wpa_printf(MSG_INFO, "Could not read or parse '%s'", pps_fname);
  330. return -1;
  331. }
  332. node = get_child_node(ctx->xml, pps,
  333. "Policy/PolicyUpdate/TrustRoot");
  334. if (node == NULL) {
  335. wpa_printf(MSG_INFO, "No Policy/PolicyUpdate/TrustRoot/CertURL found from PPS");
  336. xml_node_free(ctx->xml, pps);
  337. return -1;
  338. }
  339. ret = download_cert(ctx, node, ca_fname);
  340. xml_node_free(ctx->xml, pps);
  341. return ret;
  342. }
  343. static int cmd_dl_aaa_ca(struct hs20_osu_client *ctx, const char *pps_fname,
  344. const char *ca_fname)
  345. {
  346. xml_node_t *pps, *node, *aaa;
  347. int ret;
  348. pps = node_from_file(ctx->xml, pps_fname);
  349. if (pps == NULL) {
  350. wpa_printf(MSG_INFO, "Could not read or parse '%s'", pps_fname);
  351. return -1;
  352. }
  353. node = get_child_node(ctx->xml, pps,
  354. "AAAServerTrustRoot");
  355. if (node == NULL) {
  356. wpa_printf(MSG_INFO, "No AAAServerTrustRoot/CertURL found from PPS");
  357. xml_node_free(ctx->xml, pps);
  358. return -1;
  359. }
  360. aaa = xml_node_first_child(ctx->xml, node);
  361. if (aaa == NULL) {
  362. wpa_printf(MSG_INFO, "No AAAServerTrustRoot/CertURL found from PPS");
  363. xml_node_free(ctx->xml, pps);
  364. return -1;
  365. }
  366. ret = download_cert(ctx, aaa, ca_fname);
  367. xml_node_free(ctx->xml, pps);
  368. return ret;
  369. }
  370. static int download_trust_roots(struct hs20_osu_client *ctx,
  371. const char *pps_fname)
  372. {
  373. char *dir, *pos;
  374. char fname[300];
  375. int ret;
  376. dir = os_strdup(pps_fname);
  377. if (dir == NULL)
  378. return -1;
  379. pos = os_strrchr(dir, '/');
  380. if (pos == NULL) {
  381. os_free(dir);
  382. return -1;
  383. }
  384. *pos = '\0';
  385. snprintf(fname, sizeof(fname), "%s/ca.pem", dir);
  386. ret = cmd_dl_osu_ca(ctx, pps_fname, fname);
  387. snprintf(fname, sizeof(fname), "%s/polupd-ca.pem", dir);
  388. cmd_dl_polupd_ca(ctx, pps_fname, fname);
  389. snprintf(fname, sizeof(fname), "%s/aaa-ca.pem", dir);
  390. cmd_dl_aaa_ca(ctx, pps_fname, fname);
  391. os_free(dir);
  392. return ret;
  393. }
  394. static int server_dnsname_suffix_match(struct hs20_osu_client *ctx,
  395. const char *fqdn)
  396. {
  397. size_t match_len, len, i;
  398. const char *val;
  399. match_len = os_strlen(fqdn);
  400. for (i = 0; i < ctx->server_dnsname_count; i++) {
  401. wpa_printf(MSG_INFO,
  402. "Checking suffix match against server dNSName %s",
  403. ctx->server_dnsname[i]);
  404. val = ctx->server_dnsname[i];
  405. len = os_strlen(val);
  406. if (match_len > len)
  407. continue;
  408. if (os_strncasecmp(val + len - match_len, fqdn, match_len) != 0)
  409. continue; /* no match */
  410. if (match_len == len)
  411. return 1; /* exact match */
  412. if (val[len - match_len - 1] == '.')
  413. return 1; /* full label match completes suffix match */
  414. /* Reject due to incomplete label match */
  415. }
  416. /* None of the dNSName(s) matched */
  417. return 0;
  418. }
  419. int hs20_add_pps_mo(struct hs20_osu_client *ctx, const char *uri,
  420. xml_node_t *add_mo, char *fname, size_t fname_len)
  421. {
  422. char *str;
  423. char *fqdn, *pos;
  424. xml_node_t *tnds, *mo, *cert;
  425. const char *name;
  426. int ret;
  427. if (strncmp(uri, "./Wi-Fi/", 8) != 0) {
  428. wpa_printf(MSG_INFO, "Unsupported location for addMO to add PPS MO: '%s'",
  429. uri);
  430. write_result(ctx, "Unsupported location for addMO to add PPS MO: '%s'",
  431. uri);
  432. return -1;
  433. }
  434. fqdn = strdup(uri + 8);
  435. if (fqdn == NULL)
  436. return -1;
  437. pos = strchr(fqdn, '/');
  438. if (pos) {
  439. if (os_strcasecmp(pos, "/PerProviderSubscription") != 0) {
  440. wpa_printf(MSG_INFO, "Unsupported location for addMO to add PPS MO (extra directory): '%s'",
  441. uri);
  442. write_result(ctx, "Unsupported location for addMO to "
  443. "add PPS MO (extra directory): '%s'", uri);
  444. return -1;
  445. }
  446. *pos = '\0'; /* remove trailing slash and PPS node name */
  447. }
  448. wpa_printf(MSG_INFO, "SP FQDN: %s", fqdn);
  449. if (!server_dnsname_suffix_match(ctx, fqdn)) {
  450. wpa_printf(MSG_INFO, "FQDN '%s' for new PPS MO did not have suffix match with server's dNSName values",
  451. fqdn);
  452. write_result(ctx, "FQDN '%s' for new PPS MO did not have suffix match with server's dNSName values",
  453. fqdn);
  454. free(fqdn);
  455. return -1;
  456. }
  457. if (!valid_fqdn(fqdn)) {
  458. wpa_printf(MSG_INFO, "Invalid FQDN '%s'", fqdn);
  459. write_result(ctx, "Invalid FQDN '%s'", fqdn);
  460. free(fqdn);
  461. return -1;
  462. }
  463. mkdir("SP", S_IRWXU);
  464. snprintf(fname, fname_len, "SP/%s", fqdn);
  465. if (mkdir(fname, S_IRWXU) < 0) {
  466. if (errno != EEXIST) {
  467. int err = errno;
  468. wpa_printf(MSG_INFO, "mkdir(%s) failed: %s",
  469. fname, strerror(err));
  470. free(fqdn);
  471. return -1;
  472. }
  473. }
  474. snprintf(fname, fname_len, "SP/%s/pps.xml", fqdn);
  475. if (os_file_exists(fname)) {
  476. wpa_printf(MSG_INFO, "PPS file '%s' exists - reject addMO",
  477. fname);
  478. write_result(ctx, "PPS file '%s' exists - reject addMO",
  479. fname);
  480. free(fqdn);
  481. return -2;
  482. }
  483. wpa_printf(MSG_INFO, "Using PPS file: %s", fname);
  484. str = xml_node_get_text(ctx->xml, add_mo);
  485. if (str == NULL) {
  486. wpa_printf(MSG_INFO, "Could not extract MO text");
  487. free(fqdn);
  488. return -1;
  489. }
  490. wpa_printf(MSG_DEBUG, "[hs20] addMO text: '%s'", str);
  491. tnds = xml_node_from_buf(ctx->xml, str);
  492. xml_node_get_text_free(ctx->xml, str);
  493. if (tnds == NULL) {
  494. wpa_printf(MSG_INFO, "[hs20] Could not parse addMO text");
  495. free(fqdn);
  496. return -1;
  497. }
  498. mo = tnds_to_mo(ctx->xml, tnds);
  499. if (mo == NULL) {
  500. wpa_printf(MSG_INFO, "[hs20] Could not parse addMO TNDS text");
  501. free(fqdn);
  502. return -1;
  503. }
  504. debug_dump_node(ctx, "Parsed TNDS", mo);
  505. name = xml_node_get_localname(ctx->xml, mo);
  506. if (os_strcasecmp(name, "PerProviderSubscription") != 0) {
  507. wpa_printf(MSG_INFO, "[hs20] Unexpected PPS MO root node name '%s'",
  508. name);
  509. free(fqdn);
  510. return -1;
  511. }
  512. cert = get_child_node(ctx->xml, mo,
  513. "Credential/DigitalCertificate/"
  514. "CertSHA256Fingerprint");
  515. if (cert && process_est_cert(ctx, cert, fqdn) < 0) {
  516. xml_node_free(ctx->xml, mo);
  517. free(fqdn);
  518. return -1;
  519. }
  520. free(fqdn);
  521. if (node_to_file(ctx->xml, fname, mo) < 0) {
  522. wpa_printf(MSG_INFO, "Could not write MO to file");
  523. xml_node_free(ctx->xml, mo);
  524. return -1;
  525. }
  526. xml_node_free(ctx->xml, mo);
  527. wpa_printf(MSG_INFO, "A new PPS MO added as '%s'", fname);
  528. write_summary(ctx, "A new PPS MO added as '%s'", fname);
  529. ret = download_trust_roots(ctx, fname);
  530. if (ret < 0) {
  531. wpa_printf(MSG_INFO, "Remove invalid PPS MO file");
  532. write_summary(ctx, "Remove invalid PPS MO file");
  533. unlink(fname);
  534. }
  535. return ret;
  536. }
  537. int update_pps_file(struct hs20_osu_client *ctx, const char *pps_fname,
  538. xml_node_t *pps)
  539. {
  540. char *str;
  541. FILE *f;
  542. char backup[300];
  543. if (ctx->client_cert_present) {
  544. xml_node_t *cert;
  545. cert = get_child_node(ctx->xml, pps,
  546. "Credential/DigitalCertificate/"
  547. "CertSHA256Fingerprint");
  548. if (cert && os_file_exists("Cert/est_cert.der") &&
  549. process_est_cert(ctx, cert, ctx->fqdn) < 0) {
  550. wpa_printf(MSG_INFO, "EST certificate update processing failed on PPS MO update");
  551. return -1;
  552. }
  553. }
  554. wpa_printf(MSG_INFO, "Updating PPS MO %s", pps_fname);
  555. str = xml_node_to_str(ctx->xml, pps);
  556. wpa_printf(MSG_MSGDUMP, "[hs20] Updated PPS: '%s'", str);
  557. snprintf(backup, sizeof(backup), "%s.bak", pps_fname);
  558. rename(pps_fname, backup);
  559. f = fopen(pps_fname, "w");
  560. if (f == NULL) {
  561. wpa_printf(MSG_INFO, "Could not write PPS");
  562. rename(backup, pps_fname);
  563. free(str);
  564. return -1;
  565. }
  566. fprintf(f, "%s\n", str);
  567. fclose(f);
  568. free(str);
  569. return 0;
  570. }
  571. void get_user_pw(struct hs20_osu_client *ctx, xml_node_t *pps,
  572. const char *alt_loc, char **user, char **pw)
  573. {
  574. xml_node_t *node;
  575. node = get_child_node(ctx->xml, pps,
  576. "Credential/UsernamePassword/Username");
  577. if (node)
  578. *user = xml_node_get_text(ctx->xml, node);
  579. node = get_child_node(ctx->xml, pps,
  580. "Credential/UsernamePassword/Password");
  581. if (node)
  582. *pw = xml_node_get_base64_text(ctx->xml, node, NULL);
  583. node = get_child_node(ctx->xml, pps, alt_loc);
  584. if (node) {
  585. xml_node_t *a;
  586. a = get_node(ctx->xml, node, "Username");
  587. if (a) {
  588. xml_node_get_text_free(ctx->xml, *user);
  589. *user = xml_node_get_text(ctx->xml, a);
  590. wpa_printf(MSG_INFO, "Use OSU username '%s'", *user);
  591. }
  592. a = get_node(ctx->xml, node, "Password");
  593. if (a) {
  594. free(*pw);
  595. *pw = xml_node_get_base64_text(ctx->xml, a, NULL);
  596. wpa_printf(MSG_INFO, "Use OSU password");
  597. }
  598. }
  599. }
  600. /* Remove old credentials based on HomeSP/FQDN */
  601. static void remove_sp_creds(struct hs20_osu_client *ctx, const char *fqdn)
  602. {
  603. char cmd[300];
  604. os_snprintf(cmd, sizeof(cmd), "REMOVE_CRED provisioning_sp=%s", fqdn);
  605. if (wpa_command(ctx->ifname, cmd) < 0)
  606. wpa_printf(MSG_INFO, "Failed to remove old credential(s)");
  607. }
  608. static void set_pps_cred_policy_spe(struct hs20_osu_client *ctx, int id,
  609. xml_node_t *spe)
  610. {
  611. xml_node_t *ssid;
  612. char *txt;
  613. ssid = get_node(ctx->xml, spe, "SSID");
  614. if (ssid == NULL)
  615. return;
  616. txt = xml_node_get_text(ctx->xml, ssid);
  617. if (txt == NULL)
  618. return;
  619. wpa_printf(MSG_DEBUG, "- Policy/SPExclusionList/<X+>/SSID = %s", txt);
  620. if (set_cred_quoted(ctx->ifname, id, "excluded_ssid", txt) < 0)
  621. wpa_printf(MSG_INFO, "Failed to set cred excluded_ssid");
  622. xml_node_get_text_free(ctx->xml, txt);
  623. }
  624. static void set_pps_cred_policy_spel(struct hs20_osu_client *ctx, int id,
  625. xml_node_t *spel)
  626. {
  627. xml_node_t *child;
  628. xml_node_for_each_child(ctx->xml, child, spel) {
  629. xml_node_for_each_check(ctx->xml, child);
  630. set_pps_cred_policy_spe(ctx, id, child);
  631. }
  632. }
  633. static void set_pps_cred_policy_prp(struct hs20_osu_client *ctx, int id,
  634. xml_node_t *prp)
  635. {
  636. xml_node_t *node;
  637. char *txt = NULL, *pos;
  638. char *prio, *country_buf = NULL;
  639. const char *country;
  640. char val[200];
  641. int priority;
  642. node = get_node(ctx->xml, prp, "Priority");
  643. if (node == NULL)
  644. return;
  645. prio = xml_node_get_text(ctx->xml, node);
  646. if (prio == NULL)
  647. return;
  648. wpa_printf(MSG_INFO, "- Policy/PreferredRoamingPartnerList/<X+>/Priority = %s",
  649. prio);
  650. priority = atoi(prio);
  651. xml_node_get_text_free(ctx->xml, prio);
  652. node = get_node(ctx->xml, prp, "Country");
  653. if (node) {
  654. country_buf = xml_node_get_text(ctx->xml, node);
  655. if (country_buf == NULL)
  656. return;
  657. country = country_buf;
  658. wpa_printf(MSG_INFO, "- Policy/PreferredRoamingPartnerList/<X+>/Country = %s",
  659. country);
  660. } else {
  661. country = "*";
  662. }
  663. node = get_node(ctx->xml, prp, "FQDN_Match");
  664. if (node == NULL)
  665. goto out;
  666. txt = xml_node_get_text(ctx->xml, node);
  667. if (txt == NULL)
  668. goto out;
  669. wpa_printf(MSG_INFO, "- Policy/PreferredRoamingPartnerList/<X+>/FQDN_Match = %s",
  670. txt);
  671. pos = strrchr(txt, ',');
  672. if (pos == NULL)
  673. goto out;
  674. *pos++ = '\0';
  675. snprintf(val, sizeof(val), "%s,%d,%d,%s", txt,
  676. strcmp(pos, "includeSubdomains") != 0, priority, country);
  677. if (set_cred_quoted(ctx->ifname, id, "roaming_partner", val) < 0)
  678. wpa_printf(MSG_INFO, "Failed to set cred roaming_partner");
  679. out:
  680. xml_node_get_text_free(ctx->xml, country_buf);
  681. xml_node_get_text_free(ctx->xml, txt);
  682. }
  683. static void set_pps_cred_policy_prpl(struct hs20_osu_client *ctx, int id,
  684. xml_node_t *prpl)
  685. {
  686. xml_node_t *child;
  687. xml_node_for_each_child(ctx->xml, child, prpl) {
  688. xml_node_for_each_check(ctx->xml, child);
  689. set_pps_cred_policy_prp(ctx, id, child);
  690. }
  691. }
  692. static void set_pps_cred_policy_min_backhaul(struct hs20_osu_client *ctx, int id,
  693. xml_node_t *min_backhaul)
  694. {
  695. xml_node_t *node;
  696. char *type, *dl = NULL, *ul = NULL;
  697. int home;
  698. node = get_node(ctx->xml, min_backhaul, "NetworkType");
  699. if (node == NULL) {
  700. wpa_printf(MSG_INFO, "Ignore MinBackhaulThreshold without mandatory NetworkType node");
  701. return;
  702. }
  703. type = xml_node_get_text(ctx->xml, node);
  704. if (type == NULL)
  705. return;
  706. wpa_printf(MSG_INFO, "- Policy/MinBackhaulThreshold/<X+>/NetworkType = %s",
  707. type);
  708. if (os_strcasecmp(type, "home") == 0)
  709. home = 1;
  710. else if (os_strcasecmp(type, "roaming") == 0)
  711. home = 0;
  712. else {
  713. wpa_printf(MSG_INFO, "Ignore MinBackhaulThreshold with invalid NetworkType");
  714. xml_node_get_text_free(ctx->xml, type);
  715. return;
  716. }
  717. xml_node_get_text_free(ctx->xml, type);
  718. node = get_node(ctx->xml, min_backhaul, "DLBandwidth");
  719. if (node)
  720. dl = xml_node_get_text(ctx->xml, node);
  721. node = get_node(ctx->xml, min_backhaul, "ULBandwidth");
  722. if (node)
  723. ul = xml_node_get_text(ctx->xml, node);
  724. if (dl == NULL && ul == NULL) {
  725. wpa_printf(MSG_INFO, "Ignore MinBackhaulThreshold without either DLBandwidth or ULBandwidth nodes");
  726. return;
  727. }
  728. if (dl)
  729. wpa_printf(MSG_INFO, "- Policy/MinBackhaulThreshold/<X+>/DLBandwidth = %s",
  730. dl);
  731. if (ul)
  732. wpa_printf(MSG_INFO, "- Policy/MinBackhaulThreshold/<X+>/ULBandwidth = %s",
  733. ul);
  734. if (home) {
  735. if (dl &&
  736. set_cred(ctx->ifname, id, "min_dl_bandwidth_home", dl) < 0)
  737. wpa_printf(MSG_INFO, "Failed to set cred bandwidth limit");
  738. if (ul &&
  739. set_cred(ctx->ifname, id, "min_ul_bandwidth_home", ul) < 0)
  740. wpa_printf(MSG_INFO, "Failed to set cred bandwidth limit");
  741. } else {
  742. if (dl &&
  743. set_cred(ctx->ifname, id, "min_dl_bandwidth_roaming", dl) <
  744. 0)
  745. wpa_printf(MSG_INFO, "Failed to set cred bandwidth limit");
  746. if (ul &&
  747. set_cred(ctx->ifname, id, "min_ul_bandwidth_roaming", ul) <
  748. 0)
  749. wpa_printf(MSG_INFO, "Failed to set cred bandwidth limit");
  750. }
  751. xml_node_get_text_free(ctx->xml, dl);
  752. xml_node_get_text_free(ctx->xml, ul);
  753. }
  754. static void set_pps_cred_policy_min_backhaul_list(struct hs20_osu_client *ctx,
  755. int id, xml_node_t *node)
  756. {
  757. xml_node_t *child;
  758. wpa_printf(MSG_INFO, "- Policy/MinBackhaulThreshold");
  759. xml_node_for_each_child(ctx->xml, child, node) {
  760. xml_node_for_each_check(ctx->xml, child);
  761. set_pps_cred_policy_min_backhaul(ctx, id, child);
  762. }
  763. }
  764. static void set_pps_cred_policy_update(struct hs20_osu_client *ctx, int id,
  765. xml_node_t *node)
  766. {
  767. wpa_printf(MSG_INFO, "- Policy/PolicyUpdate");
  768. /* Not used in wpa_supplicant */
  769. }
  770. static void set_pps_cred_policy_required_proto_port(struct hs20_osu_client *ctx,
  771. int id, xml_node_t *tuple)
  772. {
  773. xml_node_t *node;
  774. char *proto, *port;
  775. char *buf;
  776. size_t buflen;
  777. node = get_node(ctx->xml, tuple, "IPProtocol");
  778. if (node == NULL) {
  779. wpa_printf(MSG_INFO, "Ignore RequiredProtoPortTuple without mandatory IPProtocol node");
  780. return;
  781. }
  782. proto = xml_node_get_text(ctx->xml, node);
  783. if (proto == NULL)
  784. return;
  785. wpa_printf(MSG_INFO, "- Policy/RequiredProtoPortTuple/<X+>/IPProtocol = %s",
  786. proto);
  787. node = get_node(ctx->xml, tuple, "PortNumber");
  788. port = node ? xml_node_get_text(ctx->xml, node) : NULL;
  789. if (port) {
  790. wpa_printf(MSG_INFO, "- Policy/RequiredProtoPortTuple/<X+>/PortNumber = %s",
  791. port);
  792. buflen = os_strlen(proto) + os_strlen(port) + 10;
  793. buf = os_malloc(buflen);
  794. if (buf)
  795. os_snprintf(buf, buflen, "%s:%s", proto, port);
  796. xml_node_get_text_free(ctx->xml, port);
  797. } else {
  798. buflen = os_strlen(proto) + 10;
  799. buf = os_malloc(buflen);
  800. if (buf)
  801. os_snprintf(buf, buflen, "%s", proto);
  802. }
  803. xml_node_get_text_free(ctx->xml, proto);
  804. if (buf == NULL)
  805. return;
  806. if (set_cred(ctx->ifname, id, "req_conn_capab", buf) < 0)
  807. wpa_printf(MSG_INFO, "Could not set req_conn_capab");
  808. os_free(buf);
  809. }
  810. static void set_pps_cred_policy_required_proto_ports(struct hs20_osu_client *ctx,
  811. int id, xml_node_t *node)
  812. {
  813. xml_node_t *child;
  814. wpa_printf(MSG_INFO, "- Policy/RequiredProtoPortTuple");
  815. xml_node_for_each_child(ctx->xml, child, node) {
  816. xml_node_for_each_check(ctx->xml, child);
  817. set_pps_cred_policy_required_proto_port(ctx, id, child);
  818. }
  819. }
  820. static void set_pps_cred_policy_max_bss_load(struct hs20_osu_client *ctx, int id,
  821. xml_node_t *node)
  822. {
  823. char *str = xml_node_get_text(ctx->xml, node);
  824. if (str == NULL)
  825. return;
  826. wpa_printf(MSG_INFO, "- Policy/MaximumBSSLoadValue - %s", str);
  827. if (set_cred(ctx->ifname, id, "max_bss_load", str) < 0)
  828. wpa_printf(MSG_INFO, "Failed to set cred max_bss_load limit");
  829. xml_node_get_text_free(ctx->xml, str);
  830. }
  831. static void set_pps_cred_policy(struct hs20_osu_client *ctx, int id,
  832. xml_node_t *node)
  833. {
  834. xml_node_t *child;
  835. const char *name;
  836. wpa_printf(MSG_INFO, "- Policy");
  837. xml_node_for_each_child(ctx->xml, child, node) {
  838. xml_node_for_each_check(ctx->xml, child);
  839. name = xml_node_get_localname(ctx->xml, child);
  840. if (os_strcasecmp(name, "PreferredRoamingPartnerList") == 0)
  841. set_pps_cred_policy_prpl(ctx, id, child);
  842. else if (os_strcasecmp(name, "MinBackhaulThreshold") == 0)
  843. set_pps_cred_policy_min_backhaul_list(ctx, id, child);
  844. else if (os_strcasecmp(name, "PolicyUpdate") == 0)
  845. set_pps_cred_policy_update(ctx, id, child);
  846. else if (os_strcasecmp(name, "SPExclusionList") == 0)
  847. set_pps_cred_policy_spel(ctx, id, child);
  848. else if (os_strcasecmp(name, "RequiredProtoPortTuple") == 0)
  849. set_pps_cred_policy_required_proto_ports(ctx, id, child);
  850. else if (os_strcasecmp(name, "MaximumBSSLoadValue") == 0)
  851. set_pps_cred_policy_max_bss_load(ctx, id, child);
  852. else
  853. wpa_printf(MSG_INFO, "Unknown Policy node '%s'", name);
  854. }
  855. }
  856. static void set_pps_cred_priority(struct hs20_osu_client *ctx, int id,
  857. xml_node_t *node)
  858. {
  859. char *str = xml_node_get_text(ctx->xml, node);
  860. if (str == NULL)
  861. return;
  862. wpa_printf(MSG_INFO, "- CredentialPriority = %s", str);
  863. if (set_cred(ctx->ifname, id, "sp_priority", str) < 0)
  864. wpa_printf(MSG_INFO, "Failed to set cred sp_priority");
  865. xml_node_get_text_free(ctx->xml, str);
  866. }
  867. static void set_pps_cred_aaa_server_trust_root(struct hs20_osu_client *ctx,
  868. int id, xml_node_t *node)
  869. {
  870. wpa_printf(MSG_INFO, "- AAAServerTrustRoot - TODO");
  871. }
  872. static void set_pps_cred_sub_update(struct hs20_osu_client *ctx, int id,
  873. xml_node_t *node)
  874. {
  875. wpa_printf(MSG_INFO, "- SubscriptionUpdate");
  876. /* not used within wpa_supplicant */
  877. }
  878. static void set_pps_cred_home_sp_network_id(struct hs20_osu_client *ctx,
  879. int id, xml_node_t *node)
  880. {
  881. xml_node_t *ssid_node, *hessid_node;
  882. char *ssid, *hessid;
  883. ssid_node = get_node(ctx->xml, node, "SSID");
  884. if (ssid_node == NULL) {
  885. wpa_printf(MSG_INFO, "Ignore HomeSP/NetworkID without mandatory SSID node");
  886. return;
  887. }
  888. hessid_node = get_node(ctx->xml, node, "HESSID");
  889. ssid = xml_node_get_text(ctx->xml, ssid_node);
  890. if (ssid == NULL)
  891. return;
  892. hessid = hessid_node ? xml_node_get_text(ctx->xml, hessid_node) : NULL;
  893. wpa_printf(MSG_INFO, "- HomeSP/NetworkID/<X+>/SSID = %s", ssid);
  894. if (hessid)
  895. wpa_printf(MSG_INFO, "- HomeSP/NetworkID/<X+>/HESSID = %s",
  896. hessid);
  897. /* TODO: Configure to wpa_supplicant */
  898. xml_node_get_text_free(ctx->xml, ssid);
  899. xml_node_get_text_free(ctx->xml, hessid);
  900. }
  901. static void set_pps_cred_home_sp_network_ids(struct hs20_osu_client *ctx,
  902. int id, xml_node_t *node)
  903. {
  904. xml_node_t *child;
  905. wpa_printf(MSG_INFO, "- HomeSP/NetworkID");
  906. xml_node_for_each_child(ctx->xml, child, node) {
  907. xml_node_for_each_check(ctx->xml, child);
  908. set_pps_cred_home_sp_network_id(ctx, id, child);
  909. }
  910. }
  911. static void set_pps_cred_home_sp_friendly_name(struct hs20_osu_client *ctx,
  912. int id, xml_node_t *node)
  913. {
  914. char *str = xml_node_get_text(ctx->xml, node);
  915. if (str == NULL)
  916. return;
  917. wpa_printf(MSG_INFO, "- HomeSP/FriendlyName = %s", str);
  918. /* not used within wpa_supplicant(?) */
  919. xml_node_get_text_free(ctx->xml, str);
  920. }
  921. static void set_pps_cred_home_sp_icon_url(struct hs20_osu_client *ctx,
  922. int id, xml_node_t *node)
  923. {
  924. char *str = xml_node_get_text(ctx->xml, node);
  925. if (str == NULL)
  926. return;
  927. wpa_printf(MSG_INFO, "- HomeSP/IconURL = %s", str);
  928. /* not used within wpa_supplicant */
  929. xml_node_get_text_free(ctx->xml, str);
  930. }
  931. static void set_pps_cred_home_sp_fqdn(struct hs20_osu_client *ctx, int id,
  932. xml_node_t *node)
  933. {
  934. char *str = xml_node_get_text(ctx->xml, node);
  935. if (str == NULL)
  936. return;
  937. wpa_printf(MSG_INFO, "- HomeSP/FQDN = %s", str);
  938. if (set_cred_quoted(ctx->ifname, id, "domain", str) < 0)
  939. wpa_printf(MSG_INFO, "Failed to set cred domain");
  940. if (set_cred_quoted(ctx->ifname, id, "domain_suffix_match", str) < 0)
  941. wpa_printf(MSG_INFO, "Failed to set cred domain_suffix_match");
  942. xml_node_get_text_free(ctx->xml, str);
  943. }
  944. static void set_pps_cred_home_sp_oi(struct hs20_osu_client *ctx, int id,
  945. xml_node_t *node)
  946. {
  947. xml_node_t *child;
  948. const char *name;
  949. char *homeoi = NULL;
  950. int required = 0;
  951. char *str;
  952. xml_node_for_each_child(ctx->xml, child, node) {
  953. xml_node_for_each_check(ctx->xml, child);
  954. name = xml_node_get_localname(ctx->xml, child);
  955. if (strcasecmp(name, "HomeOI") == 0 && !homeoi) {
  956. homeoi = xml_node_get_text(ctx->xml, child);
  957. wpa_printf(MSG_INFO, "- HomeSP/HomeOIList/<X+>/HomeOI = %s",
  958. homeoi);
  959. } else if (strcasecmp(name, "HomeOIRequired") == 0) {
  960. str = xml_node_get_text(ctx->xml, child);
  961. wpa_printf(MSG_INFO, "- HomeSP/HomeOIList/<X+>/HomeOIRequired = '%s'",
  962. str);
  963. if (str == NULL)
  964. continue;
  965. required = strcasecmp(str, "true") == 0;
  966. xml_node_get_text_free(ctx->xml, str);
  967. } else
  968. wpa_printf(MSG_INFO, "Unknown HomeOIList node '%s'",
  969. name);
  970. }
  971. if (homeoi == NULL) {
  972. wpa_printf(MSG_INFO, "- HomeSP/HomeOIList/<X+> without HomeOI ignored");
  973. return;
  974. }
  975. wpa_printf(MSG_INFO, "- HomeSP/HomeOIList/<X+> '%s' required=%d",
  976. homeoi, required);
  977. if (required) {
  978. if (set_cred(ctx->ifname, id, "required_roaming_consortium",
  979. homeoi) < 0)
  980. wpa_printf(MSG_INFO, "Failed to set cred required_roaming_consortium");
  981. } else {
  982. if (set_cred_quoted(ctx->ifname, id, "roaming_consortium",
  983. homeoi) < 0)
  984. wpa_printf(MSG_INFO, "Failed to set cred roaming_consortium");
  985. }
  986. xml_node_get_text_free(ctx->xml, homeoi);
  987. }
  988. static void set_pps_cred_home_sp_oi_list(struct hs20_osu_client *ctx, int id,
  989. xml_node_t *node)
  990. {
  991. xml_node_t *child;
  992. wpa_printf(MSG_INFO, "- HomeSP/HomeOIList");
  993. xml_node_for_each_child(ctx->xml, child, node) {
  994. xml_node_for_each_check(ctx->xml, child);
  995. set_pps_cred_home_sp_oi(ctx, id, child);
  996. }
  997. }
  998. static void set_pps_cred_home_sp_other_partner(struct hs20_osu_client *ctx,
  999. int id, xml_node_t *node)
  1000. {
  1001. xml_node_t *child;
  1002. const char *name;
  1003. char *fqdn = NULL;
  1004. xml_node_for_each_child(ctx->xml, child, node) {
  1005. xml_node_for_each_check(ctx->xml, child);
  1006. name = xml_node_get_localname(ctx->xml, child);
  1007. if (os_strcasecmp(name, "FQDN") == 0 && !fqdn) {
  1008. fqdn = xml_node_get_text(ctx->xml, child);
  1009. wpa_printf(MSG_INFO, "- HomeSP/OtherHomePartners/<X+>/FQDN = %s",
  1010. fqdn);
  1011. } else
  1012. wpa_printf(MSG_INFO, "Unknown OtherHomePartners node '%s'",
  1013. name);
  1014. }
  1015. if (fqdn == NULL) {
  1016. wpa_printf(MSG_INFO, "- HomeSP/OtherHomePartners/<X+> without FQDN ignored");
  1017. return;
  1018. }
  1019. if (set_cred_quoted(ctx->ifname, id, "domain", fqdn) < 0)
  1020. wpa_printf(MSG_INFO, "Failed to set cred domain for OtherHomePartners node");
  1021. xml_node_get_text_free(ctx->xml, fqdn);
  1022. }
  1023. static void set_pps_cred_home_sp_other_partners(struct hs20_osu_client *ctx,
  1024. int id,
  1025. xml_node_t *node)
  1026. {
  1027. xml_node_t *child;
  1028. wpa_printf(MSG_INFO, "- HomeSP/OtherHomePartners");
  1029. xml_node_for_each_child(ctx->xml, child, node) {
  1030. xml_node_for_each_check(ctx->xml, child);
  1031. set_pps_cred_home_sp_other_partner(ctx, id, child);
  1032. }
  1033. }
  1034. static void set_pps_cred_home_sp_roaming_consortium_oi(
  1035. struct hs20_osu_client *ctx, int id, xml_node_t *node)
  1036. {
  1037. char *str = xml_node_get_text(ctx->xml, node);
  1038. if (str == NULL)
  1039. return;
  1040. wpa_printf(MSG_INFO, "- HomeSP/RoamingConsortiumOI = %s", str);
  1041. /* TODO: Set to wpa_supplicant */
  1042. xml_node_get_text_free(ctx->xml, str);
  1043. }
  1044. static void set_pps_cred_home_sp(struct hs20_osu_client *ctx, int id,
  1045. xml_node_t *node)
  1046. {
  1047. xml_node_t *child;
  1048. const char *name;
  1049. wpa_printf(MSG_INFO, "- HomeSP");
  1050. xml_node_for_each_child(ctx->xml, child, node) {
  1051. xml_node_for_each_check(ctx->xml, child);
  1052. name = xml_node_get_localname(ctx->xml, child);
  1053. if (os_strcasecmp(name, "NetworkID") == 0)
  1054. set_pps_cred_home_sp_network_ids(ctx, id, child);
  1055. else if (os_strcasecmp(name, "FriendlyName") == 0)
  1056. set_pps_cred_home_sp_friendly_name(ctx, id, child);
  1057. else if (os_strcasecmp(name, "IconURL") == 0)
  1058. set_pps_cred_home_sp_icon_url(ctx, id, child);
  1059. else if (os_strcasecmp(name, "FQDN") == 0)
  1060. set_pps_cred_home_sp_fqdn(ctx, id, child);
  1061. else if (os_strcasecmp(name, "HomeOIList") == 0)
  1062. set_pps_cred_home_sp_oi_list(ctx, id, child);
  1063. else if (os_strcasecmp(name, "OtherHomePartners") == 0)
  1064. set_pps_cred_home_sp_other_partners(ctx, id, child);
  1065. else if (os_strcasecmp(name, "RoamingConsortiumOI") == 0)
  1066. set_pps_cred_home_sp_roaming_consortium_oi(ctx, id,
  1067. child);
  1068. else
  1069. wpa_printf(MSG_INFO, "Unknown HomeSP node '%s'", name);
  1070. }
  1071. }
  1072. static void set_pps_cred_sub_params(struct hs20_osu_client *ctx, int id,
  1073. xml_node_t *node)
  1074. {
  1075. wpa_printf(MSG_INFO, "- SubscriptionParameters");
  1076. /* not used within wpa_supplicant */
  1077. }
  1078. static void set_pps_cred_creation_date(struct hs20_osu_client *ctx, int id,
  1079. xml_node_t *node)
  1080. {
  1081. char *str = xml_node_get_text(ctx->xml, node);
  1082. if (str == NULL)
  1083. return;
  1084. wpa_printf(MSG_INFO, "- Credential/CreationDate = %s", str);
  1085. /* not used within wpa_supplicant */
  1086. xml_node_get_text_free(ctx->xml, str);
  1087. }
  1088. static void set_pps_cred_expiration_date(struct hs20_osu_client *ctx, int id,
  1089. xml_node_t *node)
  1090. {
  1091. char *str = xml_node_get_text(ctx->xml, node);
  1092. if (str == NULL)
  1093. return;
  1094. wpa_printf(MSG_INFO, "- Credential/ExpirationDate = %s", str);
  1095. /* not used within wpa_supplicant */
  1096. xml_node_get_text_free(ctx->xml, str);
  1097. }
  1098. static void set_pps_cred_username(struct hs20_osu_client *ctx, int id,
  1099. xml_node_t *node)
  1100. {
  1101. char *str = xml_node_get_text(ctx->xml, node);
  1102. if (str == NULL)
  1103. return;
  1104. wpa_printf(MSG_INFO, "- Credential/UsernamePassword/Username = %s",
  1105. str);
  1106. if (set_cred_quoted(ctx->ifname, id, "username", str) < 0)
  1107. wpa_printf(MSG_INFO, "Failed to set cred username");
  1108. xml_node_get_text_free(ctx->xml, str);
  1109. }
  1110. static void set_pps_cred_password(struct hs20_osu_client *ctx, int id,
  1111. xml_node_t *node)
  1112. {
  1113. int len, i;
  1114. char *pw, *hex, *pos, *end;
  1115. pw = xml_node_get_base64_text(ctx->xml, node, &len);
  1116. if (pw == NULL)
  1117. return;
  1118. wpa_printf(MSG_INFO, "- Credential/UsernamePassword/Password = %s", pw);
  1119. hex = malloc(len * 2 + 1);
  1120. if (hex == NULL) {
  1121. free(pw);
  1122. return;
  1123. }
  1124. end = hex + len * 2 + 1;
  1125. pos = hex;
  1126. for (i = 0; i < len; i++) {
  1127. snprintf(pos, end - pos, "%02x", pw[i]);
  1128. pos += 2;
  1129. }
  1130. free(pw);
  1131. if (set_cred(ctx->ifname, id, "password", hex) < 0)
  1132. wpa_printf(MSG_INFO, "Failed to set cred password");
  1133. free(hex);
  1134. }
  1135. static void set_pps_cred_machine_managed(struct hs20_osu_client *ctx, int id,
  1136. xml_node_t *node)
  1137. {
  1138. char *str = xml_node_get_text(ctx->xml, node);
  1139. if (str == NULL)
  1140. return;
  1141. wpa_printf(MSG_INFO, "- Credential/UsernamePassword/MachineManaged = %s",
  1142. str);
  1143. /* not used within wpa_supplicant */
  1144. xml_node_get_text_free(ctx->xml, str);
  1145. }
  1146. static void set_pps_cred_soft_token_app(struct hs20_osu_client *ctx, int id,
  1147. xml_node_t *node)
  1148. {
  1149. char *str = xml_node_get_text(ctx->xml, node);
  1150. if (str == NULL)
  1151. return;
  1152. wpa_printf(MSG_INFO, "- Credential/UsernamePassword/SoftTokenApp = %s",
  1153. str);
  1154. /* not used within wpa_supplicant */
  1155. xml_node_get_text_free(ctx->xml, str);
  1156. }
  1157. static void set_pps_cred_able_to_share(struct hs20_osu_client *ctx, int id,
  1158. xml_node_t *node)
  1159. {
  1160. char *str = xml_node_get_text(ctx->xml, node);
  1161. if (str == NULL)
  1162. return;
  1163. wpa_printf(MSG_INFO, "- Credential/UsernamePassword/AbleToShare = %s",
  1164. str);
  1165. /* not used within wpa_supplicant */
  1166. xml_node_get_text_free(ctx->xml, str);
  1167. }
  1168. static void set_pps_cred_eap_method(struct hs20_osu_client *ctx, int id,
  1169. xml_node_t *node)
  1170. {
  1171. wpa_printf(MSG_INFO, "- Credential/UsernamePassword/EAPMethod - TODO");
  1172. }
  1173. static void set_pps_cred_username_password(struct hs20_osu_client *ctx, int id,
  1174. xml_node_t *node)
  1175. {
  1176. xml_node_t *child;
  1177. const char *name;
  1178. wpa_printf(MSG_INFO, "- Credential/UsernamePassword");
  1179. xml_node_for_each_child(ctx->xml, child, node) {
  1180. xml_node_for_each_check(ctx->xml, child);
  1181. name = xml_node_get_localname(ctx->xml, child);
  1182. if (os_strcasecmp(name, "Username") == 0)
  1183. set_pps_cred_username(ctx, id, child);
  1184. else if (os_strcasecmp(name, "Password") == 0)
  1185. set_pps_cred_password(ctx, id, child);
  1186. else if (os_strcasecmp(name, "MachineManaged") == 0)
  1187. set_pps_cred_machine_managed(ctx, id, child);
  1188. else if (os_strcasecmp(name, "SoftTokenApp") == 0)
  1189. set_pps_cred_soft_token_app(ctx, id, child);
  1190. else if (os_strcasecmp(name, "AbleToShare") == 0)
  1191. set_pps_cred_able_to_share(ctx, id, child);
  1192. else if (os_strcasecmp(name, "EAPMethod") == 0)
  1193. set_pps_cred_eap_method(ctx, id, child);
  1194. else
  1195. wpa_printf(MSG_INFO, "Unknown Credential/UsernamePassword node '%s'",
  1196. name);
  1197. }
  1198. }
  1199. static void set_pps_cred_digital_cert(struct hs20_osu_client *ctx, int id,
  1200. xml_node_t *node, const char *fqdn)
  1201. {
  1202. char buf[200], dir[200];
  1203. wpa_printf(MSG_INFO, "- Credential/DigitalCertificate");
  1204. if (getcwd(dir, sizeof(dir)) == NULL)
  1205. return;
  1206. /* TODO: could build username from Subject of Subject AltName */
  1207. if (set_cred_quoted(ctx->ifname, id, "username", "cert") < 0) {
  1208. wpa_printf(MSG_INFO, "Failed to set username");
  1209. }
  1210. snprintf(buf, sizeof(buf), "%s/SP/%s/client-cert.pem", dir, fqdn);
  1211. if (os_file_exists(buf)) {
  1212. if (set_cred_quoted(ctx->ifname, id, "client_cert", buf) < 0) {
  1213. wpa_printf(MSG_INFO, "Failed to set client_cert");
  1214. }
  1215. }
  1216. snprintf(buf, sizeof(buf), "%s/SP/%s/client-key.pem", dir, fqdn);
  1217. if (os_file_exists(buf)) {
  1218. if (set_cred_quoted(ctx->ifname, id, "private_key", buf) < 0) {
  1219. wpa_printf(MSG_INFO, "Failed to set private_key");
  1220. }
  1221. }
  1222. }
  1223. static void set_pps_cred_realm(struct hs20_osu_client *ctx, int id,
  1224. xml_node_t *node, const char *fqdn, int sim)
  1225. {
  1226. char *str = xml_node_get_text(ctx->xml, node);
  1227. char buf[200], dir[200];
  1228. if (str == NULL)
  1229. return;
  1230. wpa_printf(MSG_INFO, "- Credential/Realm = %s", str);
  1231. if (set_cred_quoted(ctx->ifname, id, "realm", str) < 0)
  1232. wpa_printf(MSG_INFO, "Failed to set cred realm");
  1233. xml_node_get_text_free(ctx->xml, str);
  1234. if (sim)
  1235. return;
  1236. if (getcwd(dir, sizeof(dir)) == NULL)
  1237. return;
  1238. snprintf(buf, sizeof(buf), "%s/SP/%s/aaa-ca.pem", dir, fqdn);
  1239. if (os_file_exists(buf)) {
  1240. if (set_cred_quoted(ctx->ifname, id, "ca_cert", buf) < 0) {
  1241. wpa_printf(MSG_INFO, "Failed to set CA cert");
  1242. }
  1243. }
  1244. }
  1245. static void set_pps_cred_check_aaa_cert_status(struct hs20_osu_client *ctx,
  1246. int id, xml_node_t *node)
  1247. {
  1248. char *str = xml_node_get_text(ctx->xml, node);
  1249. if (str == NULL)
  1250. return;
  1251. wpa_printf(MSG_INFO, "- Credential/CheckAAAServerCertStatus = %s", str);
  1252. if (os_strcasecmp(str, "true") == 0 &&
  1253. set_cred(ctx->ifname, id, "ocsp", "2") < 0)
  1254. wpa_printf(MSG_INFO, "Failed to set cred ocsp");
  1255. xml_node_get_text_free(ctx->xml, str);
  1256. }
  1257. static void set_pps_cred_sim(struct hs20_osu_client *ctx, int id,
  1258. xml_node_t *sim, xml_node_t *realm)
  1259. {
  1260. xml_node_t *node;
  1261. char *imsi, *eaptype, *str, buf[20];
  1262. int type;
  1263. int mnc_len = 3;
  1264. size_t imsi_len;
  1265. node = get_node(ctx->xml, sim, "EAPType");
  1266. if (node == NULL) {
  1267. wpa_printf(MSG_INFO, "No SIM/EAPType node in credential");
  1268. return;
  1269. }
  1270. eaptype = xml_node_get_text(ctx->xml, node);
  1271. if (eaptype == NULL) {
  1272. wpa_printf(MSG_INFO, "Could not extract SIM/EAPType");
  1273. return;
  1274. }
  1275. wpa_printf(MSG_INFO, " - Credential/SIM/EAPType = %s", eaptype);
  1276. type = atoi(eaptype);
  1277. xml_node_get_text_free(ctx->xml, eaptype);
  1278. switch (type) {
  1279. case EAP_TYPE_SIM:
  1280. if (set_cred(ctx->ifname, id, "eap", "SIM") < 0)
  1281. wpa_printf(MSG_INFO, "Could not set eap=SIM");
  1282. break;
  1283. case EAP_TYPE_AKA:
  1284. if (set_cred(ctx->ifname, id, "eap", "AKA") < 0)
  1285. wpa_printf(MSG_INFO, "Could not set eap=SIM");
  1286. break;
  1287. case EAP_TYPE_AKA_PRIME:
  1288. if (set_cred(ctx->ifname, id, "eap", "AKA'") < 0)
  1289. wpa_printf(MSG_INFO, "Could not set eap=SIM");
  1290. break;
  1291. default:
  1292. wpa_printf(MSG_INFO, "Unsupported SIM/EAPType %d", type);
  1293. return;
  1294. }
  1295. node = get_node(ctx->xml, sim, "IMSI");
  1296. if (node == NULL) {
  1297. wpa_printf(MSG_INFO, "No SIM/IMSI node in credential");
  1298. return;
  1299. }
  1300. imsi = xml_node_get_text(ctx->xml, node);
  1301. if (imsi == NULL) {
  1302. wpa_printf(MSG_INFO, "Could not extract SIM/IMSI");
  1303. return;
  1304. }
  1305. wpa_printf(MSG_INFO, " - Credential/SIM/IMSI = %s", imsi);
  1306. imsi_len = os_strlen(imsi);
  1307. if (imsi_len < 7 || imsi_len + 2 > sizeof(buf)) {
  1308. wpa_printf(MSG_INFO, "Invalid IMSI length");
  1309. xml_node_get_text_free(ctx->xml, imsi);
  1310. return;
  1311. }
  1312. str = xml_node_get_text(ctx->xml, node);
  1313. if (str) {
  1314. char *pos;
  1315. pos = os_strstr(str, "mnc");
  1316. if (pos && os_strlen(pos) >= 6) {
  1317. if (os_strncmp(imsi + 3, pos + 3, 3) == 0)
  1318. mnc_len = 3;
  1319. else if (os_strncmp(imsi + 3, pos + 4, 2) == 0)
  1320. mnc_len = 2;
  1321. }
  1322. xml_node_get_text_free(ctx->xml, str);
  1323. }
  1324. os_memcpy(buf, imsi, 3 + mnc_len);
  1325. buf[3 + mnc_len] = '-';
  1326. os_strlcpy(buf + 3 + mnc_len + 1, imsi + 3 + mnc_len,
  1327. sizeof(buf) - 3 - mnc_len - 1);
  1328. xml_node_get_text_free(ctx->xml, imsi);
  1329. if (set_cred_quoted(ctx->ifname, id, "imsi", buf) < 0)
  1330. wpa_printf(MSG_INFO, "Could not set IMSI");
  1331. if (set_cred_quoted(ctx->ifname, id, "milenage",
  1332. "90dca4eda45b53cf0f12d7c9c3bc6a89:"
  1333. "cb9cccc4b9258e6dca4760379fb82581:000000000123") <
  1334. 0)
  1335. wpa_printf(MSG_INFO, "Could not set Milenage parameters");
  1336. }
  1337. static void set_pps_cred_credential(struct hs20_osu_client *ctx, int id,
  1338. xml_node_t *node, const char *fqdn)
  1339. {
  1340. xml_node_t *child, *sim, *realm;
  1341. const char *name;
  1342. wpa_printf(MSG_INFO, "- Credential");
  1343. sim = get_node(ctx->xml, node, "SIM");
  1344. realm = get_node(ctx->xml, node, "Realm");
  1345. xml_node_for_each_child(ctx->xml, child, node) {
  1346. xml_node_for_each_check(ctx->xml, child);
  1347. name = xml_node_get_localname(ctx->xml, child);
  1348. if (os_strcasecmp(name, "CreationDate") == 0)
  1349. set_pps_cred_creation_date(ctx, id, child);
  1350. else if (os_strcasecmp(name, "ExpirationDate") == 0)
  1351. set_pps_cred_expiration_date(ctx, id, child);
  1352. else if (os_strcasecmp(name, "UsernamePassword") == 0)
  1353. set_pps_cred_username_password(ctx, id, child);
  1354. else if (os_strcasecmp(name, "DigitalCertificate") == 0)
  1355. set_pps_cred_digital_cert(ctx, id, child, fqdn);
  1356. else if (os_strcasecmp(name, "Realm") == 0)
  1357. set_pps_cred_realm(ctx, id, child, fqdn, sim != NULL);
  1358. else if (os_strcasecmp(name, "CheckAAAServerCertStatus") == 0)
  1359. set_pps_cred_check_aaa_cert_status(ctx, id, child);
  1360. else if (os_strcasecmp(name, "SIM") == 0)
  1361. set_pps_cred_sim(ctx, id, child, realm);
  1362. else
  1363. wpa_printf(MSG_INFO, "Unknown Credential node '%s'",
  1364. name);
  1365. }
  1366. }
  1367. static void set_pps_credential(struct hs20_osu_client *ctx, int id,
  1368. xml_node_t *cred, const char *fqdn)
  1369. {
  1370. xml_node_t *child;
  1371. const char *name;
  1372. xml_node_for_each_child(ctx->xml, child, cred) {
  1373. xml_node_for_each_check(ctx->xml, child);
  1374. name = xml_node_get_localname(ctx->xml, child);
  1375. if (os_strcasecmp(name, "Policy") == 0)
  1376. set_pps_cred_policy(ctx, id, child);
  1377. else if (os_strcasecmp(name, "CredentialPriority") == 0)
  1378. set_pps_cred_priority(ctx, id, child);
  1379. else if (os_strcasecmp(name, "AAAServerTrustRoot") == 0)
  1380. set_pps_cred_aaa_server_trust_root(ctx, id, child);
  1381. else if (os_strcasecmp(name, "SubscriptionUpdate") == 0)
  1382. set_pps_cred_sub_update(ctx, id, child);
  1383. else if (os_strcasecmp(name, "HomeSP") == 0)
  1384. set_pps_cred_home_sp(ctx, id, child);
  1385. else if (os_strcasecmp(name, "SubscriptionParameters") == 0)
  1386. set_pps_cred_sub_params(ctx, id, child);
  1387. else if (os_strcasecmp(name, "Credential") == 0)
  1388. set_pps_cred_credential(ctx, id, child, fqdn);
  1389. else
  1390. wpa_printf(MSG_INFO, "Unknown credential node '%s'",
  1391. name);
  1392. }
  1393. }
  1394. static void set_pps(struct hs20_osu_client *ctx, xml_node_t *pps,
  1395. const char *fqdn)
  1396. {
  1397. xml_node_t *child;
  1398. const char *name;
  1399. int id;
  1400. char *update_identifier = NULL;
  1401. /*
  1402. * TODO: Could consider more complex mechanism that would remove
  1403. * credentials only if there are changes in the information sent to
  1404. * wpa_supplicant.
  1405. */
  1406. remove_sp_creds(ctx, fqdn);
  1407. xml_node_for_each_child(ctx->xml, child, pps) {
  1408. xml_node_for_each_check(ctx->xml, child);
  1409. name = xml_node_get_localname(ctx->xml, child);
  1410. if (os_strcasecmp(name, "UpdateIdentifier") == 0) {
  1411. update_identifier = xml_node_get_text(ctx->xml, child);
  1412. if (update_identifier) {
  1413. wpa_printf(MSG_INFO, "- UpdateIdentifier = %s",
  1414. update_identifier);
  1415. break;
  1416. }
  1417. }
  1418. }
  1419. xml_node_for_each_child(ctx->xml, child, pps) {
  1420. xml_node_for_each_check(ctx->xml, child);
  1421. name = xml_node_get_localname(ctx->xml, child);
  1422. if (os_strcasecmp(name, "UpdateIdentifier") == 0)
  1423. continue;
  1424. id = add_cred(ctx->ifname);
  1425. if (id < 0) {
  1426. wpa_printf(MSG_INFO, "Failed to add credential to wpa_supplicant");
  1427. write_summary(ctx, "Failed to add credential to wpa_supplicant");
  1428. break;
  1429. }
  1430. write_summary(ctx, "Add a credential to wpa_supplicant");
  1431. if (update_identifier &&
  1432. set_cred(ctx->ifname, id, "update_identifier",
  1433. update_identifier) < 0)
  1434. wpa_printf(MSG_INFO, "Failed to set update_identifier");
  1435. if (set_cred_quoted(ctx->ifname, id, "provisioning_sp", fqdn) <
  1436. 0)
  1437. wpa_printf(MSG_INFO, "Failed to set provisioning_sp");
  1438. wpa_printf(MSG_INFO, "credential localname: '%s'", name);
  1439. set_pps_credential(ctx, id, child, fqdn);
  1440. ctx->pps_cred_set = 1;
  1441. }
  1442. xml_node_get_text_free(ctx->xml, update_identifier);
  1443. }
  1444. void cmd_set_pps(struct hs20_osu_client *ctx, const char *pps_fname)
  1445. {
  1446. xml_node_t *pps;
  1447. const char *fqdn;
  1448. char *fqdn_buf = NULL, *pos;
  1449. pps = node_from_file(ctx->xml, pps_fname);
  1450. if (pps == NULL) {
  1451. wpa_printf(MSG_INFO, "Could not read or parse '%s'", pps_fname);
  1452. return;
  1453. }
  1454. fqdn = os_strstr(pps_fname, "SP/");
  1455. if (fqdn) {
  1456. fqdn_buf = os_strdup(fqdn + 3);
  1457. if (fqdn_buf == NULL)
  1458. return;
  1459. pos = os_strchr(fqdn_buf, '/');
  1460. if (pos)
  1461. *pos = '\0';
  1462. fqdn = fqdn_buf;
  1463. } else
  1464. fqdn = "wi-fi.org";
  1465. wpa_printf(MSG_INFO, "Set PPS MO info to wpa_supplicant - SP FQDN %s",
  1466. fqdn);
  1467. set_pps(ctx, pps, fqdn);
  1468. os_free(fqdn_buf);
  1469. xml_node_free(ctx->xml, pps);
  1470. }
  1471. static int cmd_get_fqdn(struct hs20_osu_client *ctx, const char *pps_fname)
  1472. {
  1473. xml_node_t *pps, *node;
  1474. char *fqdn = NULL;
  1475. pps = node_from_file(ctx->xml, pps_fname);
  1476. if (pps == NULL) {
  1477. wpa_printf(MSG_INFO, "Could not read or parse '%s'", pps_fname);
  1478. return -1;
  1479. }
  1480. node = get_child_node(ctx->xml, pps, "HomeSP/FQDN");
  1481. if (node)
  1482. fqdn = xml_node_get_text(ctx->xml, node);
  1483. xml_node_free(ctx->xml, pps);
  1484. if (fqdn) {
  1485. FILE *f = fopen("pps-fqdn", "w");
  1486. if (f) {
  1487. fprintf(f, "%s", fqdn);
  1488. fclose(f);
  1489. }
  1490. xml_node_get_text_free(ctx->xml, fqdn);
  1491. return 0;
  1492. }
  1493. xml_node_get_text_free(ctx->xml, fqdn);
  1494. return -1;
  1495. }
  1496. static void cmd_to_tnds(struct hs20_osu_client *ctx, const char *in_fname,
  1497. const char *out_fname, const char *urn, int use_path)
  1498. {
  1499. xml_node_t *mo, *node;
  1500. mo = node_from_file(ctx->xml, in_fname);
  1501. if (mo == NULL) {
  1502. wpa_printf(MSG_INFO, "Could not read or parse '%s'", in_fname);
  1503. return;
  1504. }
  1505. node = mo_to_tnds(ctx->xml, mo, use_path, urn, NULL);
  1506. if (node) {
  1507. node_to_file(ctx->xml, out_fname, node);
  1508. xml_node_free(ctx->xml, node);
  1509. }
  1510. xml_node_free(ctx->xml, mo);
  1511. }
  1512. static void cmd_from_tnds(struct hs20_osu_client *ctx, const char *in_fname,
  1513. const char *out_fname)
  1514. {
  1515. xml_node_t *tnds, *mo;
  1516. tnds = node_from_file(ctx->xml, in_fname);
  1517. if (tnds == NULL) {
  1518. wpa_printf(MSG_INFO, "Could not read or parse '%s'", in_fname);
  1519. return;
  1520. }
  1521. mo = tnds_to_mo(ctx->xml, tnds);
  1522. if (mo) {
  1523. node_to_file(ctx->xml, out_fname, mo);
  1524. xml_node_free(ctx->xml, mo);
  1525. }
  1526. xml_node_free(ctx->xml, tnds);
  1527. }
  1528. struct osu_icon {
  1529. int id;
  1530. char lang[4];
  1531. char mime_type[256];
  1532. char filename[256];
  1533. };
  1534. struct osu_data {
  1535. char bssid[20];
  1536. char url[256];
  1537. unsigned int methods;
  1538. char osu_ssid[33];
  1539. char osu_nai[256];
  1540. struct osu_lang_text friendly_name[MAX_OSU_VALS];
  1541. size_t friendly_name_count;
  1542. struct osu_lang_text serv_desc[MAX_OSU_VALS];
  1543. size_t serv_desc_count;
  1544. struct osu_icon icon[MAX_OSU_VALS];
  1545. size_t icon_count;
  1546. };
  1547. static struct osu_data * parse_osu_providers(const char *fname, size_t *count)
  1548. {
  1549. FILE *f;
  1550. char buf[1000];
  1551. struct osu_data *osu = NULL, *last = NULL;
  1552. size_t osu_count = 0;
  1553. char *pos, *end;
  1554. f = fopen(fname, "r");
  1555. if (f == NULL) {
  1556. wpa_printf(MSG_ERROR, "Could not open %s", fname);
  1557. return NULL;
  1558. }
  1559. while (fgets(buf, sizeof(buf), f)) {
  1560. pos = strchr(buf, '\n');
  1561. if (pos)
  1562. *pos = '\0';
  1563. if (strncmp(buf, "OSU-PROVIDER ", 13) == 0) {
  1564. last = realloc(osu, (osu_count + 1) * sizeof(*osu));
  1565. if (last == NULL)
  1566. break;
  1567. osu = last;
  1568. last = &osu[osu_count++];
  1569. memset(last, 0, sizeof(*last));
  1570. snprintf(last->bssid, sizeof(last->bssid), "%s",
  1571. buf + 13);
  1572. continue;
  1573. }
  1574. if (!last)
  1575. continue;
  1576. if (strncmp(buf, "uri=", 4) == 0) {
  1577. snprintf(last->url, sizeof(last->url), "%s", buf + 4);
  1578. continue;
  1579. }
  1580. if (strncmp(buf, "methods=", 8) == 0) {
  1581. last->methods = strtol(buf + 8, NULL, 16);
  1582. continue;
  1583. }
  1584. if (strncmp(buf, "osu_ssid=", 9) == 0) {
  1585. snprintf(last->osu_ssid, sizeof(last->osu_ssid),
  1586. "%s", buf + 9);
  1587. continue;
  1588. }
  1589. if (os_strncmp(buf, "osu_nai=", 8) == 0) {
  1590. os_snprintf(last->osu_nai, sizeof(last->osu_nai),
  1591. "%s", buf + 8);
  1592. continue;
  1593. }
  1594. if (strncmp(buf, "friendly_name=", 14) == 0) {
  1595. struct osu_lang_text *txt;
  1596. if (last->friendly_name_count == MAX_OSU_VALS)
  1597. continue;
  1598. pos = strchr(buf + 14, ':');
  1599. if (pos == NULL)
  1600. continue;
  1601. *pos++ = '\0';
  1602. txt = &last->friendly_name[last->friendly_name_count++];
  1603. snprintf(txt->lang, sizeof(txt->lang), "%s", buf + 14);
  1604. snprintf(txt->text, sizeof(txt->text), "%s", pos);
  1605. }
  1606. if (strncmp(buf, "desc=", 5) == 0) {
  1607. struct osu_lang_text *txt;
  1608. if (last->serv_desc_count == MAX_OSU_VALS)
  1609. continue;
  1610. pos = strchr(buf + 5, ':');
  1611. if (pos == NULL)
  1612. continue;
  1613. *pos++ = '\0';
  1614. txt = &last->serv_desc[last->serv_desc_count++];
  1615. snprintf(txt->lang, sizeof(txt->lang), "%s", buf + 5);
  1616. snprintf(txt->text, sizeof(txt->text), "%s", pos);
  1617. }
  1618. if (strncmp(buf, "icon=", 5) == 0) {
  1619. struct osu_icon *icon;
  1620. if (last->icon_count == MAX_OSU_VALS)
  1621. continue;
  1622. icon = &last->icon[last->icon_count++];
  1623. icon->id = atoi(buf + 5);
  1624. pos = strchr(buf, ':');
  1625. if (pos == NULL)
  1626. continue;
  1627. pos = strchr(pos + 1, ':');
  1628. if (pos == NULL)
  1629. continue;
  1630. pos = strchr(pos + 1, ':');
  1631. if (pos == NULL)
  1632. continue;
  1633. pos++;
  1634. end = strchr(pos, ':');
  1635. if (!end)
  1636. continue;
  1637. *end = '\0';
  1638. snprintf(icon->lang, sizeof(icon->lang), "%s", pos);
  1639. pos = end + 1;
  1640. end = strchr(pos, ':');
  1641. if (end)
  1642. *end = '\0';
  1643. snprintf(icon->mime_type, sizeof(icon->mime_type),
  1644. "%s", pos);
  1645. if (!pos)
  1646. continue;
  1647. pos = end + 1;
  1648. end = strchr(pos, ':');
  1649. if (end)
  1650. *end = '\0';
  1651. snprintf(icon->filename, sizeof(icon->filename),
  1652. "%s", pos);
  1653. continue;
  1654. }
  1655. }
  1656. fclose(f);
  1657. *count = osu_count;
  1658. return osu;
  1659. }
  1660. static int osu_connect(struct hs20_osu_client *ctx, const char *bssid,
  1661. const char *ssid, const char *url,
  1662. unsigned int methods, int no_prod_assoc,
  1663. const char *osu_nai)
  1664. {
  1665. int id;
  1666. const char *ifname = ctx->ifname;
  1667. char buf[200];
  1668. struct wpa_ctrl *mon;
  1669. int res;
  1670. id = add_network(ifname);
  1671. if (id < 0)
  1672. return -1;
  1673. if (set_network_quoted(ifname, id, "ssid", ssid) < 0)
  1674. return -1;
  1675. if (osu_nai && os_strlen(osu_nai) > 0) {
  1676. char dir[255], fname[300];
  1677. if (getcwd(dir, sizeof(dir)) == NULL)
  1678. return -1;
  1679. os_snprintf(fname, sizeof(fname), "%s/osu-ca.pem", dir);
  1680. if (set_network(ifname, id, "proto", "OSEN") < 0 ||
  1681. set_network(ifname, id, "key_mgmt", "OSEN") < 0 ||
  1682. set_network(ifname, id, "pairwise", "CCMP") < 0 ||
  1683. set_network(ifname, id, "group", "GTK_NOT_USED") < 0 ||
  1684. set_network(ifname, id, "eap", "WFA-UNAUTH-TLS") < 0 ||
  1685. set_network(ifname, id, "ocsp", "2") < 0 ||
  1686. set_network_quoted(ifname, id, "identity", osu_nai) < 0 ||
  1687. set_network_quoted(ifname, id, "ca_cert", fname) < 0)
  1688. return -1;
  1689. } else {
  1690. if (set_network(ifname, id, "key_mgmt", "NONE") < 0)
  1691. return -1;
  1692. }
  1693. mon = open_wpa_mon(ifname);
  1694. if (mon == NULL)
  1695. return -1;
  1696. wpa_printf(MSG_INFO, "Associate with OSU SSID");
  1697. write_summary(ctx, "Associate with OSU SSID");
  1698. snprintf(buf, sizeof(buf), "SELECT_NETWORK %d", id);
  1699. if (wpa_command(ifname, buf) < 0)
  1700. return -1;
  1701. res = get_wpa_cli_event(mon, "CTRL-EVENT-CONNECTED",
  1702. buf, sizeof(buf));
  1703. wpa_ctrl_detach(mon);
  1704. wpa_ctrl_close(mon);
  1705. if (res < 0) {
  1706. wpa_printf(MSG_INFO, "Could not connect");
  1707. write_summary(ctx, "Could not connect to OSU network");
  1708. wpa_printf(MSG_INFO, "Remove OSU network connection");
  1709. snprintf(buf, sizeof(buf), "REMOVE_NETWORK %d", id);
  1710. wpa_command(ifname, buf);
  1711. return -1;
  1712. }
  1713. write_summary(ctx, "Waiting for IP address for subscription registration");
  1714. if (wait_ip_addr(ifname, 15) < 0) {
  1715. wpa_printf(MSG_INFO, "Could not get IP address for WLAN - try connection anyway");
  1716. }
  1717. if (no_prod_assoc) {
  1718. if (res < 0)
  1719. return -1;
  1720. wpa_printf(MSG_INFO, "No production connection used for testing purposes");
  1721. write_summary(ctx, "No production connection used for testing purposes");
  1722. return 0;
  1723. }
  1724. ctx->no_reconnect = 1;
  1725. if (methods & 0x02)
  1726. res = cmd_prov(ctx, url);
  1727. else if (methods & 0x01)
  1728. res = cmd_oma_dm_prov(ctx, url);
  1729. wpa_printf(MSG_INFO, "Remove OSU network connection");
  1730. write_summary(ctx, "Remove OSU network connection");
  1731. snprintf(buf, sizeof(buf), "REMOVE_NETWORK %d", id);
  1732. wpa_command(ifname, buf);
  1733. if (res < 0)
  1734. return -1;
  1735. wpa_printf(MSG_INFO, "Requesting reconnection with updated configuration");
  1736. write_summary(ctx, "Requesting reconnection with updated configuration");
  1737. if (wpa_command(ctx->ifname, "INTERWORKING_SELECT auto") < 0) {
  1738. wpa_printf(MSG_INFO, "Failed to request wpa_supplicant to reconnect");
  1739. write_summary(ctx, "Failed to request wpa_supplicant to reconnect");
  1740. return -1;
  1741. }
  1742. return 0;
  1743. }
  1744. static int cmd_osu_select(struct hs20_osu_client *ctx, const char *dir,
  1745. int connect, int no_prod_assoc,
  1746. const char *friendly_name)
  1747. {
  1748. char fname[255];
  1749. FILE *f;
  1750. struct osu_data *osu = NULL, *last = NULL;
  1751. size_t osu_count, i, j;
  1752. int ret;
  1753. write_summary(ctx, "OSU provider selection");
  1754. if (dir == NULL) {
  1755. wpa_printf(MSG_INFO, "Missing dir parameter to osu_select");
  1756. return -1;
  1757. }
  1758. snprintf(fname, sizeof(fname), "%s/osu-providers.txt", dir);
  1759. osu = parse_osu_providers(fname, &osu_count);
  1760. if (osu == NULL) {
  1761. wpa_printf(MSG_INFO, "Could not any OSU providers from %s",
  1762. fname);
  1763. write_result(ctx, "No OSU providers available");
  1764. return -1;
  1765. }
  1766. if (friendly_name) {
  1767. for (i = 0; i < osu_count; i++) {
  1768. last = &osu[i];
  1769. for (j = 0; j < last->friendly_name_count; j++) {
  1770. if (os_strcmp(last->friendly_name[j].text,
  1771. friendly_name) == 0)
  1772. break;
  1773. }
  1774. if (j < last->friendly_name_count)
  1775. break;
  1776. }
  1777. if (i == osu_count) {
  1778. wpa_printf(MSG_INFO, "Requested operator friendly name '%s' not found in the list of available providers",
  1779. friendly_name);
  1780. write_summary(ctx, "Requested operator friendly name '%s' not found in the list of available providers",
  1781. friendly_name);
  1782. free(osu);
  1783. return -1;
  1784. }
  1785. wpa_printf(MSG_INFO, "OSU Provider selected based on requested operator friendly name '%s'",
  1786. friendly_name);
  1787. write_summary(ctx, "OSU Provider selected based on requested operator friendly name '%s'",
  1788. friendly_name);
  1789. ret = i + 1;
  1790. goto selected;
  1791. }
  1792. snprintf(fname, sizeof(fname), "%s/osu-providers.html", dir);
  1793. f = fopen(fname, "w");
  1794. if (f == NULL) {
  1795. wpa_printf(MSG_INFO, "Could not open %s", fname);
  1796. free(osu);
  1797. return -1;
  1798. }
  1799. fprintf(f, "<html><head>"
  1800. "<meta http-equiv=\"Content-type\" content=\"text/html; "
  1801. "charset=utf-8\"<title>Select service operator</title>"
  1802. "</head><body><h1>Select service operator</h1>\n");
  1803. if (osu_count == 0)
  1804. fprintf(f, "No online signup available\n");
  1805. for (i = 0; i < osu_count; i++) {
  1806. last = &osu[i];
  1807. #ifdef ANDROID
  1808. fprintf(f, "<p>\n"
  1809. "<a href=\"http://localhost:12345/osu/%d\">"
  1810. "<table><tr><td>", (int) i + 1);
  1811. #else /* ANDROID */
  1812. fprintf(f, "<p>\n"
  1813. "<a href=\"osu://%d\">"
  1814. "<table><tr><td>", (int) i + 1);
  1815. #endif /* ANDROID */
  1816. for (j = 0; j < last->icon_count; j++) {
  1817. fprintf(f, "<img src=\"osu-icon-%d.%s\">\n",
  1818. last->icon[j].id,
  1819. strcasecmp(last->icon[j].mime_type,
  1820. "image/png") == 0 ? "png" : "icon");
  1821. }
  1822. fprintf(f, "<td>");
  1823. for (j = 0; j < last->friendly_name_count; j++) {
  1824. fprintf(f, "<small>[%s]</small> %s<br>\n",
  1825. last->friendly_name[j].lang,
  1826. last->friendly_name[j].text);
  1827. }
  1828. fprintf(f, "<tr><td colspan=2>");
  1829. for (j = 0; j < last->serv_desc_count; j++) {
  1830. fprintf(f, "<small>[%s]</small> %s<br>\n",
  1831. last->serv_desc[j].lang,
  1832. last->serv_desc[j].text);
  1833. }
  1834. fprintf(f, "</table></a><br><small>BSSID: %s<br>\n"
  1835. "SSID: %s<br>\n",
  1836. last->bssid, last->osu_ssid);
  1837. if (last->osu_nai)
  1838. fprintf(f, "NAI: %s<br>\n", last->osu_nai);
  1839. fprintf(f, "URL: %s<br>\n"
  1840. "methods:%s%s<br>\n"
  1841. "</small></p>\n",
  1842. last->url,
  1843. last->methods & 0x01 ? " OMA-DM" : "",
  1844. last->methods & 0x02 ? " SOAP-XML-SPP" : "");
  1845. }
  1846. fprintf(f, "</body></html>\n");
  1847. fclose(f);
  1848. snprintf(fname, sizeof(fname), "file://%s/osu-providers.html", dir);
  1849. write_summary(ctx, "Start web browser with OSU provider selection page");
  1850. ret = hs20_web_browser(fname);
  1851. selected:
  1852. if (ret > 0 && (size_t) ret <= osu_count) {
  1853. char *data;
  1854. size_t data_len;
  1855. wpa_printf(MSG_INFO, "Selected OSU id=%d", ret);
  1856. last = &osu[ret - 1];
  1857. ret = 0;
  1858. wpa_printf(MSG_INFO, "BSSID: %s", last->bssid);
  1859. wpa_printf(MSG_INFO, "SSID: %s", last->osu_ssid);
  1860. wpa_printf(MSG_INFO, "URL: %s", last->url);
  1861. write_summary(ctx, "Selected OSU provider id=%d BSSID=%s SSID=%s URL=%s",
  1862. ret, last->bssid, last->osu_ssid, last->url);
  1863. ctx->friendly_name_count = last->friendly_name_count;
  1864. for (j = 0; j < last->friendly_name_count; j++) {
  1865. wpa_printf(MSG_INFO, "FRIENDLY_NAME: [%s]%s",
  1866. last->friendly_name[j].lang,
  1867. last->friendly_name[j].text);
  1868. os_strlcpy(ctx->friendly_name[j].lang,
  1869. last->friendly_name[j].lang,
  1870. sizeof(ctx->friendly_name[j].lang));
  1871. os_strlcpy(ctx->friendly_name[j].text,
  1872. last->friendly_name[j].text,
  1873. sizeof(ctx->friendly_name[j].text));
  1874. }
  1875. ctx->icon_count = last->icon_count;
  1876. for (j = 0; j < last->icon_count; j++) {
  1877. char fname[256];
  1878. os_snprintf(fname, sizeof(fname), "%s/osu-icon-%d.%s",
  1879. dir, last->icon[j].id,
  1880. strcasecmp(last->icon[j].mime_type,
  1881. "image/png") == 0 ?
  1882. "png" : "icon");
  1883. wpa_printf(MSG_INFO, "ICON: %s (%s)",
  1884. fname, last->icon[j].filename);
  1885. os_strlcpy(ctx->icon_filename[j],
  1886. last->icon[j].filename,
  1887. sizeof(ctx->icon_filename[j]));
  1888. data = os_readfile(fname, &data_len);
  1889. if (data) {
  1890. sha256_vector(1, (const u8 **) &data, &data_len,
  1891. ctx->icon_hash[j]);
  1892. os_free(data);
  1893. }
  1894. }
  1895. if (connect == 2) {
  1896. if (last->methods & 0x02)
  1897. ret = cmd_prov(ctx, last->url);
  1898. else if (last->methods & 0x01)
  1899. ret = cmd_oma_dm_prov(ctx, last->url);
  1900. else
  1901. ret = -1;
  1902. } else if (connect)
  1903. ret = osu_connect(ctx, last->bssid, last->osu_ssid,
  1904. last->url, last->methods,
  1905. no_prod_assoc, last->osu_nai);
  1906. } else
  1907. ret = -1;
  1908. free(osu);
  1909. return ret;
  1910. }
  1911. static int cmd_signup(struct hs20_osu_client *ctx, int no_prod_assoc,
  1912. const char *friendly_name)
  1913. {
  1914. char dir[255];
  1915. char fname[300], buf[400];
  1916. struct wpa_ctrl *mon;
  1917. const char *ifname;
  1918. int res;
  1919. ifname = ctx->ifname;
  1920. if (getcwd(dir, sizeof(dir)) == NULL)
  1921. return -1;
  1922. snprintf(fname, sizeof(fname), "%s/osu-info", dir);
  1923. if (mkdir(fname, S_IRWXU | S_IRWXG) < 0 && errno != EEXIST) {
  1924. wpa_printf(MSG_INFO, "mkdir(%s) failed: %s",
  1925. fname, strerror(errno));
  1926. return -1;
  1927. }
  1928. snprintf(buf, sizeof(buf), "SET osu_dir %s", fname);
  1929. if (wpa_command(ifname, buf) < 0) {
  1930. wpa_printf(MSG_INFO, "Failed to configure osu_dir to wpa_supplicant");
  1931. return -1;
  1932. }
  1933. mon = open_wpa_mon(ifname);
  1934. if (mon == NULL)
  1935. return -1;
  1936. wpa_printf(MSG_INFO, "Starting OSU fetch");
  1937. write_summary(ctx, "Starting OSU provider information fetch");
  1938. if (wpa_command(ifname, "FETCH_OSU") < 0) {
  1939. wpa_printf(MSG_INFO, "Could not start OSU fetch");
  1940. wpa_ctrl_detach(mon);
  1941. wpa_ctrl_close(mon);
  1942. return -1;
  1943. }
  1944. res = get_wpa_cli_event(mon, "OSU provider fetch completed",
  1945. buf, sizeof(buf));
  1946. wpa_ctrl_detach(mon);
  1947. wpa_ctrl_close(mon);
  1948. if (res < 0) {
  1949. wpa_printf(MSG_INFO, "OSU fetch did not complete");
  1950. write_summary(ctx, "OSU fetch did not complete");
  1951. return -1;
  1952. }
  1953. wpa_printf(MSG_INFO, "OSU provider fetch completed");
  1954. return cmd_osu_select(ctx, fname, 1, no_prod_assoc, friendly_name);
  1955. }
  1956. static int cmd_sub_rem(struct hs20_osu_client *ctx, const char *address,
  1957. const char *pps_fname, const char *ca_fname)
  1958. {
  1959. xml_node_t *pps, *node;
  1960. char pps_fname_buf[300];
  1961. char ca_fname_buf[200];
  1962. char *cred_username = NULL;
  1963. char *cred_password = NULL;
  1964. char *sub_rem_uri = NULL;
  1965. char client_cert_buf[200];
  1966. char *client_cert = NULL;
  1967. char client_key_buf[200];
  1968. char *client_key = NULL;
  1969. int spp;
  1970. wpa_printf(MSG_INFO, "Subscription remediation requested with Server URL: %s",
  1971. address);
  1972. if (!pps_fname) {
  1973. char buf[256];
  1974. wpa_printf(MSG_INFO, "Determining PPS file based on Home SP information");
  1975. if (os_strncmp(address, "fqdn=", 5) == 0) {
  1976. wpa_printf(MSG_INFO, "Use requested FQDN from command line");
  1977. os_snprintf(buf, sizeof(buf), "%s", address + 5);
  1978. address = NULL;
  1979. } else if (get_wpa_status(ctx->ifname, "provisioning_sp", buf,
  1980. sizeof(buf)) < 0) {
  1981. wpa_printf(MSG_INFO, "Could not get provisioning Home SP FQDN from wpa_supplicant");
  1982. return -1;
  1983. }
  1984. os_free(ctx->fqdn);
  1985. ctx->fqdn = os_strdup(buf);
  1986. if (ctx->fqdn == NULL)
  1987. return -1;
  1988. wpa_printf(MSG_INFO, "Home SP FQDN for current credential: %s",
  1989. buf);
  1990. os_snprintf(pps_fname_buf, sizeof(pps_fname_buf),
  1991. "SP/%s/pps.xml", ctx->fqdn);
  1992. pps_fname = pps_fname_buf;
  1993. os_snprintf(ca_fname_buf, sizeof(ca_fname_buf), "SP/%s/ca.pem",
  1994. ctx->fqdn);
  1995. ca_fname = ca_fname_buf;
  1996. }
  1997. if (!os_file_exists(pps_fname)) {
  1998. wpa_printf(MSG_INFO, "PPS file '%s' does not exist or is not accessible",
  1999. pps_fname);
  2000. return -1;
  2001. }
  2002. wpa_printf(MSG_INFO, "Using PPS file: %s", pps_fname);
  2003. if (ca_fname && !os_file_exists(ca_fname)) {
  2004. wpa_printf(MSG_INFO, "CA file '%s' does not exist or is not accessible",
  2005. ca_fname);
  2006. return -1;
  2007. }
  2008. wpa_printf(MSG_INFO, "Using server trust root: %s", ca_fname);
  2009. ctx->ca_fname = ca_fname;
  2010. pps = node_from_file(ctx->xml, pps_fname);
  2011. if (pps == NULL) {
  2012. wpa_printf(MSG_INFO, "Could not read PPS MO");
  2013. return -1;
  2014. }
  2015. if (!ctx->fqdn) {
  2016. char *tmp;
  2017. node = get_child_node(ctx->xml, pps, "HomeSP/FQDN");
  2018. if (node == NULL) {
  2019. wpa_printf(MSG_INFO, "No HomeSP/FQDN found from PPS");
  2020. return -1;
  2021. }
  2022. tmp = xml_node_get_text(ctx->xml, node);
  2023. if (tmp == NULL) {
  2024. wpa_printf(MSG_INFO, "No HomeSP/FQDN text found from PPS");
  2025. return -1;
  2026. }
  2027. ctx->fqdn = os_strdup(tmp);
  2028. xml_node_get_text_free(ctx->xml, tmp);
  2029. if (!ctx->fqdn) {
  2030. wpa_printf(MSG_INFO, "No FQDN known");
  2031. return -1;
  2032. }
  2033. }
  2034. node = get_child_node(ctx->xml, pps,
  2035. "SubscriptionUpdate/UpdateMethod");
  2036. if (node) {
  2037. char *tmp;
  2038. tmp = xml_node_get_text(ctx->xml, node);
  2039. if (tmp && os_strcasecmp(tmp, "OMA-DM-ClientInitiated") == 0)
  2040. spp = 0;
  2041. else
  2042. spp = 1;
  2043. } else {
  2044. wpa_printf(MSG_INFO, "No UpdateMethod specified - assume SPP");
  2045. spp = 1;
  2046. }
  2047. get_user_pw(ctx, pps, "SubscriptionUpdate/UsernamePassword",
  2048. &cred_username, &cred_password);
  2049. if (cred_username)
  2050. wpa_printf(MSG_INFO, "Using username: %s", cred_username);
  2051. if (cred_password)
  2052. wpa_printf(MSG_DEBUG, "Using password: %s", cred_password);
  2053. if (cred_username == NULL && cred_password == NULL &&
  2054. get_child_node(ctx->xml, pps, "Credential/DigitalCertificate")) {
  2055. wpa_printf(MSG_INFO, "Using client certificate");
  2056. os_snprintf(client_cert_buf, sizeof(client_cert_buf),
  2057. "SP/%s/client-cert.pem", ctx->fqdn);
  2058. client_cert = client_cert_buf;
  2059. os_snprintf(client_key_buf, sizeof(client_key_buf),
  2060. "SP/%s/client-key.pem", ctx->fqdn);
  2061. client_key = client_key_buf;
  2062. ctx->client_cert_present = 1;
  2063. }
  2064. node = get_child_node(ctx->xml, pps, "SubscriptionUpdate/URI");
  2065. if (node) {
  2066. sub_rem_uri = xml_node_get_text(ctx->xml, node);
  2067. if (sub_rem_uri &&
  2068. (!address || os_strcmp(address, sub_rem_uri) != 0)) {
  2069. wpa_printf(MSG_INFO, "Override sub rem URI based on PPS: %s",
  2070. sub_rem_uri);
  2071. address = sub_rem_uri;
  2072. }
  2073. }
  2074. if (!address) {
  2075. wpa_printf(MSG_INFO, "Server URL not known");
  2076. return -1;
  2077. }
  2078. write_summary(ctx, "Wait for IP address for subscriptiom remediation");
  2079. wpa_printf(MSG_INFO, "Wait for IP address before starting subscription remediation");
  2080. if (wait_ip_addr(ctx->ifname, 15) < 0) {
  2081. wpa_printf(MSG_INFO, "Could not get IP address for WLAN - try connection anyway");
  2082. }
  2083. if (spp)
  2084. spp_sub_rem(ctx, address, pps_fname,
  2085. client_cert, client_key,
  2086. cred_username, cred_password, pps);
  2087. else
  2088. oma_dm_sub_rem(ctx, address, pps_fname,
  2089. client_cert, client_key,
  2090. cred_username, cred_password, pps);
  2091. xml_node_get_text_free(ctx->xml, sub_rem_uri);
  2092. xml_node_get_text_free(ctx->xml, cred_username);
  2093. str_clear_free(cred_password);
  2094. xml_node_free(ctx->xml, pps);
  2095. return 0;
  2096. }
  2097. static int cmd_pol_upd(struct hs20_osu_client *ctx, const char *address,
  2098. const char *pps_fname, const char *ca_fname)
  2099. {
  2100. xml_node_t *pps;
  2101. xml_node_t *node;
  2102. char pps_fname_buf[300];
  2103. char ca_fname_buf[200];
  2104. char *uri = NULL;
  2105. char *cred_username = NULL;
  2106. char *cred_password = NULL;
  2107. char client_cert_buf[200];
  2108. char *client_cert = NULL;
  2109. char client_key_buf[200];
  2110. char *client_key = NULL;
  2111. int spp;
  2112. wpa_printf(MSG_INFO, "Policy update requested");
  2113. if (!pps_fname) {
  2114. char buf[256];
  2115. wpa_printf(MSG_INFO, "Determining PPS file based on Home SP information");
  2116. if (os_strncmp(address, "fqdn=", 5) == 0) {
  2117. wpa_printf(MSG_INFO, "Use requested FQDN from command line");
  2118. os_snprintf(buf, sizeof(buf), "%s", address + 5);
  2119. address = NULL;
  2120. } else if (get_wpa_status(ctx->ifname, "provisioning_sp", buf,
  2121. sizeof(buf)) < 0) {
  2122. wpa_printf(MSG_INFO, "Could not get provisioning Home SP FQDN from wpa_supplicant");
  2123. return -1;
  2124. }
  2125. os_free(ctx->fqdn);
  2126. ctx->fqdn = os_strdup(buf);
  2127. if (ctx->fqdn == NULL)
  2128. return -1;
  2129. wpa_printf(MSG_INFO, "Home SP FQDN for current credential: %s",
  2130. buf);
  2131. os_snprintf(pps_fname_buf, sizeof(pps_fname_buf),
  2132. "SP/%s/pps.xml", ctx->fqdn);
  2133. pps_fname = pps_fname_buf;
  2134. os_snprintf(ca_fname_buf, sizeof(ca_fname_buf), "SP/%s/ca.pem",
  2135. buf);
  2136. ca_fname = ca_fname_buf;
  2137. }
  2138. if (!os_file_exists(pps_fname)) {
  2139. wpa_printf(MSG_INFO, "PPS file '%s' does not exist or is not accessible",
  2140. pps_fname);
  2141. return -1;
  2142. }
  2143. wpa_printf(MSG_INFO, "Using PPS file: %s", pps_fname);
  2144. if (ca_fname && !os_file_exists(ca_fname)) {
  2145. wpa_printf(MSG_INFO, "CA file '%s' does not exist or is not accessible",
  2146. ca_fname);
  2147. return -1;
  2148. }
  2149. wpa_printf(MSG_INFO, "Using server trust root: %s", ca_fname);
  2150. ctx->ca_fname = ca_fname;
  2151. pps = node_from_file(ctx->xml, pps_fname);
  2152. if (pps == NULL) {
  2153. wpa_printf(MSG_INFO, "Could not read PPS MO");
  2154. return -1;
  2155. }
  2156. if (!ctx->fqdn) {
  2157. char *tmp;
  2158. node = get_child_node(ctx->xml, pps, "HomeSP/FQDN");
  2159. if (node == NULL) {
  2160. wpa_printf(MSG_INFO, "No HomeSP/FQDN found from PPS");
  2161. return -1;
  2162. }
  2163. tmp = xml_node_get_text(ctx->xml, node);
  2164. if (tmp == NULL) {
  2165. wpa_printf(MSG_INFO, "No HomeSP/FQDN text found from PPS");
  2166. return -1;
  2167. }
  2168. ctx->fqdn = os_strdup(tmp);
  2169. xml_node_get_text_free(ctx->xml, tmp);
  2170. if (!ctx->fqdn) {
  2171. wpa_printf(MSG_INFO, "No FQDN known");
  2172. return -1;
  2173. }
  2174. }
  2175. node = get_child_node(ctx->xml, pps,
  2176. "Policy/PolicyUpdate/UpdateMethod");
  2177. if (node) {
  2178. char *tmp;
  2179. tmp = xml_node_get_text(ctx->xml, node);
  2180. if (tmp && os_strcasecmp(tmp, "OMA-DM-ClientInitiated") == 0)
  2181. spp = 0;
  2182. else
  2183. spp = 1;
  2184. } else {
  2185. wpa_printf(MSG_INFO, "No UpdateMethod specified - assume SPP");
  2186. spp = 1;
  2187. }
  2188. get_user_pw(ctx, pps, "Policy/PolicyUpdate/UsernamePassword",
  2189. &cred_username, &cred_password);
  2190. if (cred_username)
  2191. wpa_printf(MSG_INFO, "Using username: %s", cred_username);
  2192. if (cred_password)
  2193. wpa_printf(MSG_DEBUG, "Using password: %s", cred_password);
  2194. if (cred_username == NULL && cred_password == NULL &&
  2195. get_child_node(ctx->xml, pps, "Credential/DigitalCertificate")) {
  2196. wpa_printf(MSG_INFO, "Using client certificate");
  2197. os_snprintf(client_cert_buf, sizeof(client_cert_buf),
  2198. "SP/%s/client-cert.pem", ctx->fqdn);
  2199. client_cert = client_cert_buf;
  2200. os_snprintf(client_key_buf, sizeof(client_key_buf),
  2201. "SP/%s/client-key.pem", ctx->fqdn);
  2202. client_key = client_key_buf;
  2203. }
  2204. if (!address) {
  2205. node = get_child_node(ctx->xml, pps, "Policy/PolicyUpdate/URI");
  2206. if (node) {
  2207. uri = xml_node_get_text(ctx->xml, node);
  2208. wpa_printf(MSG_INFO, "URI based on PPS: %s", uri);
  2209. address = uri;
  2210. }
  2211. }
  2212. if (!address) {
  2213. wpa_printf(MSG_INFO, "Server URL not known");
  2214. return -1;
  2215. }
  2216. if (spp)
  2217. spp_pol_upd(ctx, address, pps_fname,
  2218. client_cert, client_key,
  2219. cred_username, cred_password, pps);
  2220. else
  2221. oma_dm_pol_upd(ctx, address, pps_fname,
  2222. client_cert, client_key,
  2223. cred_username, cred_password, pps);
  2224. xml_node_get_text_free(ctx->xml, uri);
  2225. xml_node_get_text_free(ctx->xml, cred_username);
  2226. str_clear_free(cred_password);
  2227. xml_node_free(ctx->xml, pps);
  2228. return 0;
  2229. }
  2230. static char * get_hostname(const char *url)
  2231. {
  2232. const char *pos, *end, *end2;
  2233. char *ret;
  2234. if (url == NULL)
  2235. return NULL;
  2236. pos = os_strchr(url, '/');
  2237. if (pos == NULL)
  2238. return NULL;
  2239. pos++;
  2240. if (*pos != '/')
  2241. return NULL;
  2242. pos++;
  2243. end = os_strchr(pos, '/');
  2244. end2 = os_strchr(pos, ':');
  2245. if (end && end2 && end2 < end)
  2246. end = end2;
  2247. if (end)
  2248. end--;
  2249. else {
  2250. end = pos;
  2251. while (*end)
  2252. end++;
  2253. if (end > pos)
  2254. end--;
  2255. }
  2256. ret = os_malloc(end - pos + 2);
  2257. if (ret == NULL)
  2258. return NULL;
  2259. os_memcpy(ret, pos, end - pos + 1);
  2260. ret[end - pos + 1] = '\0';
  2261. return ret;
  2262. }
  2263. static int osu_cert_cb(void *_ctx, struct http_cert *cert)
  2264. {
  2265. struct hs20_osu_client *ctx = _ctx;
  2266. unsigned int i, j;
  2267. int found;
  2268. char *host = NULL;
  2269. wpa_printf(MSG_INFO, "osu_cert_cb(osu_cert_validation=%d)",
  2270. !ctx->no_osu_cert_validation);
  2271. host = get_hostname(ctx->server_url);
  2272. for (i = 0; i < ctx->server_dnsname_count; i++)
  2273. os_free(ctx->server_dnsname[i]);
  2274. os_free(ctx->server_dnsname);
  2275. ctx->server_dnsname = os_calloc(cert->num_dnsname, sizeof(char *));
  2276. ctx->server_dnsname_count = 0;
  2277. found = 0;
  2278. for (i = 0; i < cert->num_dnsname; i++) {
  2279. if (ctx->server_dnsname) {
  2280. ctx->server_dnsname[ctx->server_dnsname_count] =
  2281. os_strdup(cert->dnsname[i]);
  2282. if (ctx->server_dnsname[ctx->server_dnsname_count])
  2283. ctx->server_dnsname_count++;
  2284. }
  2285. if (host && os_strcasecmp(host, cert->dnsname[i]) == 0)
  2286. found = 1;
  2287. wpa_printf(MSG_INFO, "dNSName '%s'", cert->dnsname[i]);
  2288. }
  2289. if (host && !found) {
  2290. wpa_printf(MSG_INFO, "Server name from URL (%s) did not match any dNSName - abort connection",
  2291. host);
  2292. write_result(ctx, "Server name from URL (%s) did not match any dNSName - abort connection",
  2293. host);
  2294. os_free(host);
  2295. return -1;
  2296. }
  2297. os_free(host);
  2298. for (i = 0; i < cert->num_othername; i++) {
  2299. if (os_strcmp(cert->othername[i].oid,
  2300. "1.3.6.1.4.1.40808.1.1.1") == 0) {
  2301. wpa_hexdump_ascii(MSG_INFO,
  2302. "id-wfa-hotspot-friendlyName",
  2303. cert->othername[i].data,
  2304. cert->othername[i].len);
  2305. }
  2306. }
  2307. for (j = 0; !ctx->no_osu_cert_validation &&
  2308. j < ctx->friendly_name_count; j++) {
  2309. int found = 0;
  2310. for (i = 0; i < cert->num_othername; i++) {
  2311. if (os_strcmp(cert->othername[i].oid,
  2312. "1.3.6.1.4.1.40808.1.1.1") != 0)
  2313. continue;
  2314. if (cert->othername[i].len < 3)
  2315. continue;
  2316. if (os_strncasecmp((char *) cert->othername[i].data,
  2317. ctx->friendly_name[j].lang, 3) != 0)
  2318. continue;
  2319. if (os_strncmp((char *) cert->othername[i].data + 3,
  2320. ctx->friendly_name[j].text,
  2321. cert->othername[i].len - 3) == 0) {
  2322. found = 1;
  2323. break;
  2324. }
  2325. }
  2326. if (!found) {
  2327. wpa_printf(MSG_INFO, "No friendly name match found for '[%s]%s'",
  2328. ctx->friendly_name[j].lang,
  2329. ctx->friendly_name[j].text);
  2330. write_result(ctx, "No friendly name match found for '[%s]%s'",
  2331. ctx->friendly_name[j].lang,
  2332. ctx->friendly_name[j].text);
  2333. return -1;
  2334. }
  2335. }
  2336. for (i = 0; i < cert->num_logo; i++) {
  2337. struct http_logo *logo = &cert->logo[i];
  2338. wpa_printf(MSG_INFO, "logo hash alg %s uri '%s'",
  2339. logo->alg_oid, logo->uri);
  2340. wpa_hexdump_ascii(MSG_INFO, "hashValue",
  2341. logo->hash, logo->hash_len);
  2342. }
  2343. for (j = 0; !ctx->no_osu_cert_validation && j < ctx->icon_count; j++) {
  2344. int found = 0;
  2345. char *name = ctx->icon_filename[j];
  2346. size_t name_len = os_strlen(name);
  2347. wpa_printf(MSG_INFO, "Looking for icon file name '%s' match",
  2348. name);
  2349. for (i = 0; i < cert->num_logo; i++) {
  2350. struct http_logo *logo = &cert->logo[i];
  2351. size_t uri_len = os_strlen(logo->uri);
  2352. char *pos;
  2353. wpa_printf(MSG_INFO, "Comparing to '%s' uri_len=%d name_len=%d",
  2354. logo->uri, (int) uri_len, (int) name_len);
  2355. if (uri_len < 1 + name_len)
  2356. continue;
  2357. pos = &logo->uri[uri_len - name_len - 1];
  2358. if (*pos != '/')
  2359. continue;
  2360. pos++;
  2361. if (os_strcmp(pos, name) == 0) {
  2362. found = 1;
  2363. break;
  2364. }
  2365. }
  2366. if (!found) {
  2367. wpa_printf(MSG_INFO, "No icon filename match found for '%s'",
  2368. name);
  2369. write_result(ctx,
  2370. "No icon filename match found for '%s'",
  2371. name);
  2372. return -1;
  2373. }
  2374. }
  2375. for (j = 0; !ctx->no_osu_cert_validation && j < ctx->icon_count; j++) {
  2376. int found = 0;
  2377. for (i = 0; i < cert->num_logo; i++) {
  2378. struct http_logo *logo = &cert->logo[i];
  2379. if (logo->hash_len != 32)
  2380. continue;
  2381. if (os_memcmp(logo->hash, ctx->icon_hash[j], 32) == 0) {
  2382. found = 1;
  2383. break;
  2384. }
  2385. }
  2386. if (!found) {
  2387. wpa_printf(MSG_INFO, "No icon hash match found");
  2388. write_result(ctx, "No icon hash match found");
  2389. return -1;
  2390. }
  2391. }
  2392. return 0;
  2393. }
  2394. static int init_ctx(struct hs20_osu_client *ctx)
  2395. {
  2396. xml_node_t *devinfo, *devid;
  2397. os_memset(ctx, 0, sizeof(*ctx));
  2398. ctx->ifname = "wlan0";
  2399. ctx->xml = xml_node_init_ctx(ctx, NULL);
  2400. if (ctx->xml == NULL)
  2401. return -1;
  2402. devinfo = node_from_file(ctx->xml, "devinfo.xml");
  2403. if (!devinfo) {
  2404. wpa_printf(MSG_ERROR, "devinfo.xml not found");
  2405. return -1;
  2406. }
  2407. devid = get_node(ctx->xml, devinfo, "DevId");
  2408. if (devid) {
  2409. char *tmp = xml_node_get_text(ctx->xml, devid);
  2410. if (tmp) {
  2411. ctx->devid = os_strdup(tmp);
  2412. xml_node_get_text_free(ctx->xml, tmp);
  2413. }
  2414. }
  2415. xml_node_free(ctx->xml, devinfo);
  2416. if (ctx->devid == NULL) {
  2417. wpa_printf(MSG_ERROR, "Could not fetch DevId from devinfo.xml");
  2418. return -1;
  2419. }
  2420. ctx->http = http_init_ctx(ctx, ctx->xml);
  2421. if (ctx->http == NULL) {
  2422. xml_node_deinit_ctx(ctx->xml);
  2423. return -1;
  2424. }
  2425. http_ocsp_set(ctx->http, 2);
  2426. http_set_cert_cb(ctx->http, osu_cert_cb, ctx);
  2427. return 0;
  2428. }
  2429. static void deinit_ctx(struct hs20_osu_client *ctx)
  2430. {
  2431. size_t i;
  2432. http_deinit_ctx(ctx->http);
  2433. xml_node_deinit_ctx(ctx->xml);
  2434. os_free(ctx->fqdn);
  2435. os_free(ctx->server_url);
  2436. os_free(ctx->devid);
  2437. for (i = 0; i < ctx->server_dnsname_count; i++)
  2438. os_free(ctx->server_dnsname[i]);
  2439. os_free(ctx->server_dnsname);
  2440. }
  2441. static void check_workarounds(struct hs20_osu_client *ctx)
  2442. {
  2443. FILE *f;
  2444. char buf[100];
  2445. unsigned long int val = 0;
  2446. f = fopen("hs20-osu-client.workarounds", "r");
  2447. if (f == NULL)
  2448. return;
  2449. if (fgets(buf, sizeof(buf), f))
  2450. val = strtoul(buf, NULL, 16);
  2451. fclose(f);
  2452. if (val) {
  2453. wpa_printf(MSG_INFO, "Workarounds enabled: 0x%lx", val);
  2454. ctx->workarounds = val;
  2455. if (ctx->workarounds & WORKAROUND_OCSP_OPTIONAL)
  2456. http_ocsp_set(ctx->http, 1);
  2457. }
  2458. }
  2459. static void usage(void)
  2460. {
  2461. printf("usage: hs20-osu-client [-dddqqKt] [-S<station ifname>] \\\n"
  2462. " [-w<wpa_supplicant ctrl_iface dir>] "
  2463. "[-r<result file>] [-f<debug file>] \\\n"
  2464. " [-s<summary file>] \\\n"
  2465. " <command> [arguments..]\n"
  2466. "commands:\n"
  2467. "- to_tnds <XML MO> <XML MO in TNDS format> [URN]\n"
  2468. "- to_tnds2 <XML MO> <XML MO in TNDS format (Path) "
  2469. "[URN]>\n"
  2470. "- from_tnds <XML MO in TNDS format> <XML MO>\n"
  2471. "- set_pps <PerProviderSubscription XML file name>\n"
  2472. "- get_fqdn <PerProviderSubscription XML file name>\n"
  2473. "- pol_upd [Server URL] [PPS] [CA cert]\n"
  2474. "- sub_rem <Server URL> [PPS] [CA cert]\n"
  2475. "- prov <Server URL> [CA cert]\n"
  2476. "- oma_dm_prov <Server URL> [CA cert]\n"
  2477. "- sim_prov <Server URL> [CA cert]\n"
  2478. "- oma_dm_sim_prov <Server URL> [CA cert]\n"
  2479. "- signup [CA cert]\n"
  2480. "- dl_osu_ca <PPS> <CA file>\n"
  2481. "- dl_polupd_ca <PPS> <CA file>\n"
  2482. "- dl_aaa_ca <PPS> <CA file>\n"
  2483. "- browser <URL>\n"
  2484. "- parse_cert <X.509 certificate (DER)>\n"
  2485. "- osu_select <OSU info directory> [CA cert]\n");
  2486. }
  2487. int main(int argc, char *argv[])
  2488. {
  2489. struct hs20_osu_client ctx;
  2490. int c;
  2491. int ret = 0;
  2492. int no_prod_assoc = 0;
  2493. const char *friendly_name = NULL;
  2494. const char *wpa_debug_file_path = NULL;
  2495. extern char *wpas_ctrl_path;
  2496. extern int wpa_debug_level;
  2497. extern int wpa_debug_show_keys;
  2498. extern int wpa_debug_timestamp;
  2499. if (init_ctx(&ctx) < 0)
  2500. return -1;
  2501. for (;;) {
  2502. c = getopt(argc, argv, "df:hi:KNO:qr:s:S:tw:");
  2503. if (c < 0)
  2504. break;
  2505. switch (c) {
  2506. case 'd':
  2507. if (wpa_debug_level > 0)
  2508. wpa_debug_level--;
  2509. break;
  2510. case 'f':
  2511. wpa_debug_file_path = optarg;
  2512. break;
  2513. case 'K':
  2514. wpa_debug_show_keys++;
  2515. break;
  2516. case 'N':
  2517. no_prod_assoc = 1;
  2518. break;
  2519. case 'O':
  2520. friendly_name = optarg;
  2521. break;
  2522. case 'q':
  2523. wpa_debug_level++;
  2524. break;
  2525. case 'r':
  2526. ctx.result_file = optarg;
  2527. break;
  2528. case 's':
  2529. ctx.summary_file = optarg;
  2530. break;
  2531. case 'S':
  2532. ctx.ifname = optarg;
  2533. break;
  2534. case 't':
  2535. wpa_debug_timestamp++;
  2536. break;
  2537. case 'w':
  2538. wpas_ctrl_path = optarg;
  2539. break;
  2540. case 'h':
  2541. default:
  2542. usage();
  2543. exit(0);
  2544. break;
  2545. }
  2546. }
  2547. if (argc - optind < 1) {
  2548. usage();
  2549. exit(0);
  2550. }
  2551. wpa_debug_open_file(wpa_debug_file_path);
  2552. #ifdef __linux__
  2553. setlinebuf(stdout);
  2554. #endif /* __linux__ */
  2555. if (ctx.result_file)
  2556. unlink(ctx.result_file);
  2557. wpa_printf(MSG_DEBUG, "===[hs20-osu-client START - command: %s ]======"
  2558. "================", argv[optind]);
  2559. check_workarounds(&ctx);
  2560. if (strcmp(argv[optind], "to_tnds") == 0) {
  2561. if (argc - optind < 2) {
  2562. usage();
  2563. exit(0);
  2564. }
  2565. cmd_to_tnds(&ctx, argv[optind + 1], argv[optind + 2],
  2566. argc > optind + 3 ? argv[optind + 3] : NULL,
  2567. 0);
  2568. } else if (strcmp(argv[optind], "to_tnds2") == 0) {
  2569. if (argc - optind < 2) {
  2570. usage();
  2571. exit(0);
  2572. }
  2573. cmd_to_tnds(&ctx, argv[optind + 1], argv[optind + 2],
  2574. argc > optind + 3 ? argv[optind + 3] : NULL,
  2575. 1);
  2576. } else if (strcmp(argv[optind], "from_tnds") == 0) {
  2577. if (argc - optind < 2) {
  2578. usage();
  2579. exit(0);
  2580. }
  2581. cmd_from_tnds(&ctx, argv[optind + 1], argv[optind + 2]);
  2582. } else if (strcmp(argv[optind], "sub_rem") == 0) {
  2583. if (argc - optind < 2) {
  2584. usage();
  2585. exit(0);
  2586. }
  2587. if (argc - optind < 2)
  2588. wpa_printf(MSG_ERROR, "Server URL missing from command line");
  2589. else
  2590. ret = cmd_sub_rem(&ctx, argv[optind + 1],
  2591. argc > optind + 2 ?
  2592. argv[optind + 2] : NULL,
  2593. argc > optind + 3 ?
  2594. argv[optind + 3] : NULL);
  2595. } else if (strcmp(argv[optind], "pol_upd") == 0) {
  2596. if (argc - optind < 2) {
  2597. usage();
  2598. exit(0);
  2599. }
  2600. ret = cmd_pol_upd(&ctx, argc > 2 ? argv[optind + 1] : NULL,
  2601. argc > optind + 2 ? argv[optind + 2] : NULL,
  2602. argc > optind + 3 ? argv[optind + 3] : NULL);
  2603. } else if (strcmp(argv[optind], "prov") == 0) {
  2604. if (argc - optind < 2) {
  2605. usage();
  2606. exit(0);
  2607. }
  2608. ctx.ca_fname = argv[optind + 2];
  2609. cmd_prov(&ctx, argv[optind + 1]);
  2610. } else if (strcmp(argv[optind], "sim_prov") == 0) {
  2611. if (argc - optind < 2) {
  2612. usage();
  2613. exit(0);
  2614. }
  2615. ctx.ca_fname = argv[optind + 2];
  2616. cmd_sim_prov(&ctx, argv[optind + 1]);
  2617. } else if (strcmp(argv[optind], "dl_osu_ca") == 0) {
  2618. if (argc - optind < 2) {
  2619. usage();
  2620. exit(0);
  2621. }
  2622. cmd_dl_osu_ca(&ctx, argv[optind + 1], argv[optind + 2]);
  2623. } else if (strcmp(argv[optind], "dl_polupd_ca") == 0) {
  2624. if (argc - optind < 2) {
  2625. usage();
  2626. exit(0);
  2627. }
  2628. cmd_dl_polupd_ca(&ctx, argv[optind + 1], argv[optind + 2]);
  2629. } else if (strcmp(argv[optind], "dl_aaa_ca") == 0) {
  2630. if (argc - optind < 2) {
  2631. usage();
  2632. exit(0);
  2633. }
  2634. cmd_dl_aaa_ca(&ctx, argv[optind + 1], argv[optind + 2]);
  2635. } else if (strcmp(argv[optind], "osu_select") == 0) {
  2636. if (argc - optind < 2) {
  2637. usage();
  2638. exit(0);
  2639. }
  2640. ctx.ca_fname = argc > optind + 2 ? argv[optind + 2] : NULL;
  2641. cmd_osu_select(&ctx, argv[optind + 1], 2, 1, NULL);
  2642. } else if (strcmp(argv[optind], "signup") == 0) {
  2643. ctx.ca_fname = argc > optind + 1 ? argv[optind + 1] : NULL;
  2644. ret = cmd_signup(&ctx, no_prod_assoc, friendly_name);
  2645. } else if (strcmp(argv[optind], "set_pps") == 0) {
  2646. if (argc - optind < 2) {
  2647. usage();
  2648. exit(0);
  2649. }
  2650. cmd_set_pps(&ctx, argv[optind + 1]);
  2651. } else if (strcmp(argv[optind], "get_fqdn") == 0) {
  2652. if (argc - optind < 1) {
  2653. usage();
  2654. exit(0);
  2655. }
  2656. ret = cmd_get_fqdn(&ctx, argv[optind + 1]);
  2657. } else if (strcmp(argv[optind], "oma_dm_prov") == 0) {
  2658. if (argc - optind < 2) {
  2659. usage();
  2660. exit(0);
  2661. }
  2662. ctx.ca_fname = argv[optind + 2];
  2663. cmd_oma_dm_prov(&ctx, argv[optind + 1]);
  2664. } else if (strcmp(argv[optind], "oma_dm_sim_prov") == 0) {
  2665. if (argc - optind < 2) {
  2666. usage();
  2667. exit(0);
  2668. }
  2669. ctx.ca_fname = argv[optind + 2];
  2670. if (cmd_oma_dm_sim_prov(&ctx, argv[optind + 1]) < 0) {
  2671. write_summary(&ctx, "Failed to complete OMA DM SIM provisioning");
  2672. return -1;
  2673. }
  2674. } else if (strcmp(argv[optind], "oma_dm_add") == 0) {
  2675. if (argc - optind < 2) {
  2676. usage();
  2677. exit(0);
  2678. }
  2679. cmd_oma_dm_add(&ctx, argv[optind + 1], argv[optind + 2]);
  2680. } else if (strcmp(argv[optind], "oma_dm_replace") == 0) {
  2681. if (argc - optind < 2) {
  2682. usage();
  2683. exit(0);
  2684. }
  2685. cmd_oma_dm_replace(&ctx, argv[optind + 1], argv[optind + 2]);
  2686. } else if (strcmp(argv[optind], "est_csr") == 0) {
  2687. if (argc - optind < 2) {
  2688. usage();
  2689. exit(0);
  2690. }
  2691. mkdir("Cert", S_IRWXU);
  2692. est_build_csr(&ctx, argv[optind + 1]);
  2693. } else if (strcmp(argv[optind], "browser") == 0) {
  2694. int ret;
  2695. if (argc - optind < 2) {
  2696. usage();
  2697. exit(0);
  2698. }
  2699. wpa_printf(MSG_INFO, "Launch web browser to URL %s",
  2700. argv[optind + 1]);
  2701. ret = hs20_web_browser(argv[optind + 1]);
  2702. wpa_printf(MSG_INFO, "Web browser result: %d", ret);
  2703. } else if (strcmp(argv[optind], "parse_cert") == 0) {
  2704. if (argc - optind < 2) {
  2705. usage();
  2706. exit(0);
  2707. }
  2708. wpa_debug_level = MSG_MSGDUMP;
  2709. http_parse_x509_certificate(ctx.http, argv[optind + 1]);
  2710. wpa_debug_level = MSG_INFO;
  2711. } else {
  2712. wpa_printf(MSG_INFO, "Unknown command '%s'", argv[optind]);
  2713. }
  2714. deinit_ctx(&ctx);
  2715. wpa_printf(MSG_DEBUG,
  2716. "===[hs20-osu-client END ]======================");
  2717. wpa_debug_close_file();
  2718. return ret;
  2719. }