osu_client.c 84 KB

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