osu_client.c 82 KB

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