osu_client.c 82 KB

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