osu_client.c 82 KB

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