osu_client.c 83 KB

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