osu_client.c 84 KB

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