osu_client.c 84 KB

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