osu_client.c 83 KB

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