osu_client.c 85 KB

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