pulse.c 87 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978
  1. /*
  2. * OpenConnect (SSL + DTLS) VPN client
  3. *
  4. * Copyright © 2019 David Woodhouse.
  5. *
  6. * Author: David Woodhouse <dwmw2@infradead.org>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public License
  10. * version 2.1, as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. */
  17. #include <config.h>
  18. #include "openconnect-internal.h"
  19. #include <unistd.h>
  20. #include <fcntl.h>
  21. #include <sys/types.h>
  22. #include <time.h>
  23. #include <string.h>
  24. #include <ctype.h>
  25. #include <errno.h>
  26. #include <stdlib.h>
  27. #include <stdio.h>
  28. #include <stdarg.h>
  29. #define VENDOR_JUNIPER 0xa4c
  30. #define VENDOR_JUNIPER2 0x583
  31. #define VENDOR_TCG 0x5597
  32. #define IFT_VERSION_REQUEST 1
  33. #define IFT_VERSION_RESPONSE 2
  34. #define IFT_CLIENT_AUTH_REQUEST 3
  35. #define IFT_CLIENT_AUTH_SELECTION 4
  36. #define IFT_CLIENT_AUTH_CHALLENGE 5
  37. #define IFT_CLIENT_AUTH_RESPONSE 6
  38. #define IFT_CLIENT_AUTH_SUCCESS 7
  39. /* IF-T/TLS v1 authentication messages all start
  40. * with the Auth Type Vendor (Juniper) + Type (1) */
  41. #define JUNIPER_1 ((VENDOR_JUNIPER << 8) | 1)
  42. #define AVP_VENDOR 0x80
  43. #define AVP_MANDATORY 0x40
  44. #define EAP_REQUEST 1
  45. #define EAP_RESPONSE 2
  46. #define EAP_SUCCESS 3
  47. #define EAP_FAILURE 4
  48. #define EAP_TYPE_IDENTITY 1
  49. #define EAP_TYPE_GTC 6
  50. #define EAP_TYPE_TLS 0x0d
  51. #define EAP_TYPE_TTLS 0x15
  52. #define EAP_TYPE_EXPANDED 0xfe
  53. #define EXPANDED_JUNIPER ((EAP_TYPE_EXPANDED << 24) | VENDOR_JUNIPER)
  54. #define AVP_CODE_EAP_MESSAGE 79
  55. #if defined(OPENCONNECT_OPENSSL)
  56. #define TTLS_SEND SSL_write
  57. #define TTLS_RECV SSL_read
  58. #elif defined(OPENCONNECT_GNUTLS)
  59. #define TTLS_SEND gnutls_record_send
  60. #define TTLS_RECV gnutls_record_recv
  61. #endif
  62. /* Flags for prompt handling during authentication, based on the contents of the 0xd73 AVP (qv). */
  63. #define PROMPT_PRIMARY 1
  64. #define PROMPT_USERNAME 2
  65. #define PROMPT_PASSWORD 4
  66. #define PROMPT_GTC_NEXT 0x10000
  67. /* Request codes for the Juniper Expanded/2 auth requests. */
  68. #define J2_PASSCHANGE 0x43
  69. #define J2_PASSREQ 0x01
  70. #define J2_PASSRETRY 0x81
  71. #define J2_PASSFAIL 0xc5
  72. /* Limit to TLS record size. */
  73. #define TLS_RECORD_MAX (16384)
  74. /* Outbound fragment size limit */
  75. #define TTLS_MAXFRAG (8192)
  76. #define TTLS_LENGTH (1<<7)
  77. #define TTLS_MOREFRAGS (1<<6)
  78. #define TTLS_START (1<<5)
  79. static void buf_append_ift_hdr(struct oc_text_buf *buf, uint32_t vendor, uint32_t type)
  80. {
  81. uint32_t b[4];
  82. store_be32(&b[0], vendor);
  83. store_be32(&b[1], type);
  84. b[2] = 0; /* Length will be filled in later. */
  85. b[3] = 0;
  86. buf_append_bytes(buf, b, 16);
  87. }
  88. /* Append EAP header, using VENDOR_JUNIPER and the given subtype if
  89. * the main type is EAP_TYPE_EXPANDED */
  90. static int buf_append_eap_hdr(struct oc_text_buf *buf, uint8_t code, uint8_t ident, uint8_t type,
  91. uint32_t subtype)
  92. {
  93. unsigned char b[24];
  94. int len_ofs = -1;
  95. if (!buf_error(buf))
  96. len_ofs = buf->pos;
  97. b[0] = code;
  98. b[1] = ident;
  99. b[2] = b[3] = 0; /* Length is filled in later. */
  100. if (type == EAP_TYPE_EXPANDED) {
  101. store_be32(b + 4, EXPANDED_JUNIPER);
  102. store_be32(b + 8, subtype);
  103. buf_append_bytes(buf, b, 12);
  104. } else {
  105. b[4] = type;
  106. buf_append_bytes(buf, b, 5);
  107. }
  108. return len_ofs;
  109. }
  110. /* For an IF-T/TLS auth frame containing the Juniper/1 Auth Type,
  111. * the EAP header is at offset 0x14. Fill in the length field,
  112. * based on the current length of the buf */
  113. static void buf_fill_eap_len(struct oc_text_buf *buf, int ofs)
  114. {
  115. /* EAP length word is always at 0x16, and counts bytes from 0x14 */
  116. if (ofs >= 0 && !buf_error(buf) && buf->pos >= ofs + 4)
  117. store_be16(buf->data + ofs + 2, buf->pos - ofs);
  118. }
  119. static void buf_append_avp(struct oc_text_buf *buf, uint32_t type, const void *bytes, int len)
  120. {
  121. buf_append_be32(buf, type);
  122. buf_append_be16(buf, 0x8000);
  123. buf_append_be16(buf, len + 12);
  124. buf_append_be32(buf, VENDOR_JUNIPER2);
  125. buf_append_bytes(buf, bytes, len);
  126. if (len & 3) {
  127. uint32_t pad = 0;
  128. buf_append_bytes(buf, &pad, 4 - ( len & 3 ));
  129. }
  130. }
  131. static void buf_append_avp_string(struct oc_text_buf *buf, uint32_t type, const char *str)
  132. {
  133. buf_append_avp(buf, type, str, strlen(str));
  134. }
  135. static void buf_append_avp_be32(struct oc_text_buf *buf, uint32_t type, uint32_t val)
  136. {
  137. uint32_t val_be;
  138. store_be32(&val_be, val);
  139. buf_append_avp(buf, type, &val_be, sizeof(val_be));
  140. }
  141. static int valid_ift_success(unsigned char *bytes, int len)
  142. {
  143. if (len != 0x18 || (load_be32(bytes) & 0xffffff) != VENDOR_TCG ||
  144. load_be32(bytes + 4) != IFT_CLIENT_AUTH_SUCCESS ||
  145. load_be32(bytes + 8) != len ||
  146. load_be32(bytes + 0x10) != JUNIPER_1 ||
  147. bytes[0x14] != EAP_SUCCESS ||
  148. load_be16(bytes + 0x16) != len - 0x14)
  149. return 0;
  150. return 1;
  151. }
  152. /* Check for a valid IF-T/TLS auth challenge of the Juniper/1 Auth Type */
  153. static int valid_ift_auth(unsigned char *bytes, int len)
  154. {
  155. if (len < 0x14 || (load_be32(bytes) & 0xffffff) != VENDOR_TCG ||
  156. load_be32(bytes + 4) != IFT_CLIENT_AUTH_CHALLENGE ||
  157. load_be32(bytes + 8) != len ||
  158. load_be32(bytes + 0x10) != JUNIPER_1)
  159. return 0;
  160. return 1;
  161. }
  162. static int valid_ift_auth_eap(unsigned char *bytes, int len)
  163. {
  164. /* Needs to be a valid IF-T/TLS auth challenge with the
  165. * expect Auth Type, *and* the payload has to be a valid
  166. * EAP request with correct length field. */
  167. if (!valid_ift_auth(bytes, len) || len < 0x19 ||
  168. bytes[0x14] != EAP_REQUEST ||
  169. load_be16(bytes + 0x16) != len - 0x14)
  170. return 0;
  171. return 1;
  172. }
  173. static int valid_ift_auth_eap_exj1(unsigned char *bytes, int len)
  174. {
  175. /* Also needs to be the Expanded Juniper/1 EAP Type */
  176. if (!valid_ift_auth_eap(bytes, len) || len < 0x20 ||
  177. load_be32(bytes + 0x18) != EXPANDED_JUNIPER ||
  178. load_be32(bytes + 0x1c) != 1)
  179. return 0;
  180. return 1;
  181. }
  182. static int process_attr(struct openconnect_info *vpninfo, struct oc_vpn_option **new_opts,
  183. struct oc_ip_info *new_ip_info, uint16_t type,
  184. unsigned char *data, int attrlen)
  185. {
  186. struct oc_split_include *xc;
  187. char buf[80];
  188. int i;
  189. switch (type) {
  190. case 0x0001:
  191. if (attrlen != 4)
  192. goto badlen;
  193. snprintf(buf, sizeof(buf), "%d.%d.%d.%d", data[0], data[1], data[2], data[3]);
  194. vpn_progress(vpninfo, PRG_DEBUG, _("Received internal Legacy IP address %s\n"), buf);
  195. new_ip_info->addr = add_option_dup(new_opts, "ipaddr", buf, -1);
  196. break;
  197. case 0x0002:
  198. if (attrlen != 4)
  199. goto badlen;
  200. snprintf(buf, sizeof(buf), "%d.%d.%d.%d", data[0], data[1], data[2], data[3]);
  201. vpn_progress(vpninfo, PRG_DEBUG, _("Received netmask %s\n"), buf);
  202. new_ip_info->netmask = add_option_dup(new_opts, "netmask", buf, -1);
  203. break;
  204. case 0x0003:
  205. if (attrlen != 4)
  206. goto badlen;
  207. snprintf(buf, sizeof(buf), "%d.%d.%d.%d", data[0], data[1], data[2], data[3]);
  208. vpn_progress(vpninfo, PRG_DEBUG, _("Received DNS server %s\n"), buf);
  209. for (i = 0; i < 3; i++) {
  210. if (!new_ip_info->dns[i]) {
  211. new_ip_info->dns[i] = add_option_dup(new_opts, "DNS", buf, -1);
  212. break;
  213. }
  214. }
  215. break;
  216. case 0x0004:
  217. if (attrlen != 4)
  218. goto badlen;
  219. snprintf(buf, sizeof(buf), "%d.%d.%d.%d", data[0], data[1], data[2], data[3]);
  220. vpn_progress(vpninfo, PRG_DEBUG, _("Received WINS server %s\n"), buf);
  221. for (i = 0; i < 3; i++) {
  222. if (!new_ip_info->nbns[i]) {
  223. new_ip_info->nbns[i] = add_option_dup(new_opts, "WINS", buf, -1);
  224. break;
  225. }
  226. }
  227. break;
  228. case 0x0008:
  229. if (attrlen != 17)
  230. goto badlen;
  231. if (!inet_ntop(AF_INET6, data, buf, sizeof(buf))) {
  232. vpn_progress(vpninfo, PRG_ERR,
  233. _("Failed to handle IPv6 address\n"));
  234. return -EINVAL;
  235. }
  236. if (!vpninfo->disable_ipv6) {
  237. new_ip_info->addr6 = add_option_dup(new_opts, "ip6addr", buf, -1);
  238. i = strlen(buf);
  239. snprintf(buf + i, sizeof(buf) - i, "/%d", data[16]);
  240. new_ip_info->netmask6 = add_option_dup(new_opts, "ip6netmask", buf, -1);
  241. }
  242. vpn_progress(vpninfo, PRG_DEBUG, _("Received internal IPv6 address %s\n"), buf);
  243. break;
  244. case 0x000a:
  245. if (attrlen != 16)
  246. goto badlen;
  247. if (!inet_ntop(AF_INET6, data, buf, sizeof(buf))) {
  248. vpn_progress(vpninfo, PRG_ERR,
  249. _("Failed to handle IPv6 address\n"));
  250. return -EINVAL;
  251. }
  252. for (i = 0; i < 3; i++) {
  253. if (!new_ip_info->dns[i]) {
  254. new_ip_info->dns[i] = add_option_dup(new_opts, "DNS", buf, -1);
  255. break;
  256. }
  257. }
  258. vpn_progress(vpninfo, PRG_DEBUG, _("Received DNS server %s\n"), buf);
  259. break;
  260. case 0x000f:
  261. if (attrlen != 17)
  262. goto badlen;
  263. if (!inet_ntop(AF_INET6, data, buf, sizeof(buf))) {
  264. vpn_progress(vpninfo, PRG_ERR,
  265. _("Failed to handle IPv6 address\n"));
  266. return -EINVAL;
  267. }
  268. i = strlen(buf);
  269. snprintf(buf + i, sizeof(buf) - i, "/%d", data[16]);
  270. xc = malloc(sizeof(*xc));
  271. if (xc) {
  272. xc->route = add_option_dup(new_opts, "split-include6", buf, -1);
  273. if (xc->route) {
  274. xc->next = new_ip_info->split_includes;
  275. new_ip_info->split_includes = xc;
  276. } else
  277. free(xc);
  278. }
  279. vpn_progress(vpninfo, PRG_DEBUG, _("Received IPv6 split include %s\n"), buf);
  280. break;
  281. case 0x0010:
  282. if (attrlen != 17)
  283. goto badlen;
  284. if (!inet_ntop(AF_INET6, data, buf, sizeof(buf))) {
  285. vpn_progress(vpninfo, PRG_ERR,
  286. _("Failed to handle IPv6 address\n"));
  287. return -EINVAL;
  288. }
  289. i = strlen(buf);
  290. snprintf(buf + i, sizeof(buf) - i, "/%d", data[16]);
  291. xc = malloc(sizeof(*xc));
  292. if (xc) {
  293. xc->route = add_option_dup(new_opts, "split-exclude6", buf, -1);
  294. if (xc->route) {
  295. xc->next = new_ip_info->split_excludes;
  296. new_ip_info->split_excludes = xc;
  297. } else
  298. free(xc);
  299. }
  300. vpn_progress(vpninfo, PRG_DEBUG, _("Received IPv6 split exclude %s\n"), buf);
  301. break;
  302. case 0x4005:
  303. if (attrlen != 4) {
  304. badlen:
  305. vpn_progress(vpninfo, PRG_ERR,
  306. _("Unexpected length %d for attr 0x%x\n"),
  307. attrlen, type);
  308. return -EINVAL;
  309. }
  310. new_ip_info->mtu = load_be32(data);
  311. vpn_progress(vpninfo, PRG_DEBUG,
  312. _("Received MTU %d from server\n"),
  313. new_ip_info->mtu);
  314. break;
  315. case 0x4006:
  316. if (!attrlen)
  317. goto badlen;
  318. if (!data[attrlen-1])
  319. attrlen--;
  320. vpn_progress(vpninfo, PRG_DEBUG, _("Received DNS search domain %.*s\n"),
  321. attrlen, (char *)data);
  322. new_ip_info->domain = add_option_dup(new_opts, "search", (char *)data, attrlen);
  323. if (new_ip_info->domain) {
  324. char *p = (char *)new_ip_info->domain;
  325. while ((p = strchr(p, ',')))
  326. *p = ' ';
  327. }
  328. break;
  329. case 0x400b:
  330. if (attrlen != 4)
  331. goto badlen;
  332. snprintf(buf, sizeof(buf), "%d.%d.%d.%d", data[0], data[1], data[2], data[3]);
  333. vpn_progress(vpninfo, PRG_DEBUG, _("Received internal gateway address %s\n"), buf);
  334. /* Hm, what are we supposed to do with this? It's a tunnel;
  335. having a gateway is meaningless. */
  336. add_option_dup(new_opts, "gateway", buf, -1);
  337. break;
  338. case 0x4010: {
  339. const char *enctype;
  340. uint16_t val;
  341. if (attrlen != 2)
  342. goto badlen;
  343. val = load_be16(data);
  344. if (val == ENC_AES_128_CBC) {
  345. enctype = "AES-128";
  346. vpninfo->enc_key_len = 16;
  347. } else if (val == ENC_AES_256_CBC) {
  348. enctype = "AES-256";
  349. vpninfo->enc_key_len = 32;
  350. } else
  351. enctype = "unknown";
  352. vpn_progress(vpninfo, PRG_DEBUG, _("ESP encryption: 0x%04x (%s)\n"),
  353. val, enctype);
  354. vpninfo->esp_enc = val;
  355. break;
  356. }
  357. case 0x4011: {
  358. const char *mactype;
  359. uint16_t val;
  360. if (attrlen != 2)
  361. goto badlen;
  362. val = load_be16(data);
  363. if (val == HMAC_MD5) {
  364. mactype = "MD5";
  365. vpninfo->hmac_key_len = 16;
  366. } else if (val == HMAC_SHA1) {
  367. mactype = "SHA1";
  368. vpninfo->hmac_key_len = 20;
  369. } else if (val == HMAC_SHA256) {
  370. mactype = "SHA256";
  371. vpninfo->hmac_key_len = 32;
  372. } else
  373. mactype = "unknown";
  374. vpn_progress(vpninfo, PRG_DEBUG, _("ESP HMAC: 0x%04x (%s)\n"),
  375. val, mactype);
  376. vpninfo->esp_hmac = val;
  377. break;
  378. }
  379. case 0x4012:
  380. if (attrlen != 4)
  381. goto badlen;
  382. vpninfo->esp_lifetime_seconds = load_be32(data);
  383. vpn_progress(vpninfo, PRG_DEBUG, _("ESP key lifetime: %u seconds\n"),
  384. vpninfo->esp_lifetime_seconds);
  385. break;
  386. case 0x4013:
  387. if (attrlen != 4)
  388. goto badlen;
  389. vpninfo->esp_lifetime_bytes = load_be32(data);
  390. vpn_progress(vpninfo, PRG_DEBUG, _("ESP key lifetime: %u bytes\n"),
  391. vpninfo->esp_lifetime_bytes);
  392. break;
  393. case 0x4014:
  394. if (attrlen != 4)
  395. goto badlen;
  396. vpninfo->esp_replay_protect = load_be32(data);
  397. vpn_progress(vpninfo, PRG_DEBUG, _("ESP replay protection: %d\n"),
  398. load_be32(data));
  399. break;
  400. case 0x4016:
  401. if (attrlen != 2)
  402. goto badlen;
  403. i = load_be16(data);
  404. udp_sockaddr(vpninfo, i);
  405. vpn_progress(vpninfo, PRG_DEBUG, _("ESP port: %d\n"), i);
  406. break;
  407. case 0x4017:
  408. if (attrlen != 4)
  409. goto badlen;
  410. vpninfo->esp_ssl_fallback = load_be32(data);
  411. vpn_progress(vpninfo, PRG_DEBUG, _("ESP to SSL fallback: %u seconds\n"),
  412. vpninfo->esp_ssl_fallback);
  413. break;
  414. case 0x401a:
  415. if (attrlen != 1)
  416. goto badlen;
  417. /* Amusingly, this isn't enforced. It's client-only */
  418. vpn_progress(vpninfo, PRG_DEBUG, _("ESP only: %d\n"),
  419. data[0]);
  420. break;
  421. case 0x401e:
  422. if (attrlen != 16)
  423. goto badlen;
  424. if (!inet_ntop(AF_INET6, data, buf, sizeof(buf))) {
  425. vpn_progress(vpninfo, PRG_ERR,
  426. _("Failed to handle IPv6 address\n"));
  427. return -EINVAL;
  428. }
  429. vpn_progress(vpninfo, PRG_DEBUG, _("Received internal gateway IPv6 address %s\n"), buf);
  430. /* Hm, what are we supposed to do with this? It's a tunnel;
  431. having a gateway is meaningless. */
  432. add_option_dup(new_opts, "gateway6", buf, -1);
  433. break;
  434. /* 0x4022: disable proxy
  435. 0x400a: preserve proxy
  436. 0x4008: proxy (string)
  437. 0x4000: disconnect when routes changed
  438. 0x4015: tos copy
  439. 0x4001: tunnel routes take precedence
  440. 0x401f: tunnel routes with subnet access (also 4001 set)
  441. 0x4020: Enforce IPv4
  442. 0x4021: Enforce IPv6
  443. */
  444. default:
  445. buf[0] = 0;
  446. for (i=0; i < 16 && i < attrlen; i++)
  447. sprintf(buf + strlen(buf), " %02x", data[i]);
  448. if (attrlen > 16)
  449. sprintf(buf + strlen(buf), "...");
  450. vpn_progress(vpninfo, PRG_DEBUG,
  451. _("Unknown attr 0x%x len %d:%s\n"),
  452. type, attrlen, buf);
  453. }
  454. return 0;
  455. }
  456. static int recv_ift_packet(struct openconnect_info *vpninfo, void *buf, int len)
  457. {
  458. int ret = vpninfo->ssl_read(vpninfo, buf, len);
  459. if (ret > 0 && vpninfo->dump_http_traffic) {
  460. vpn_progress(vpninfo, PRG_TRACE,
  461. _("Read %d bytes of IF-T/TLS record\n"), ret);
  462. dump_buf_hex(vpninfo, PRG_TRACE, '<', buf, ret);
  463. }
  464. return ret;
  465. }
  466. static int send_ift_bytes(struct openconnect_info *vpninfo, void *bytes, int len)
  467. {
  468. int ret;
  469. store_be32(((char *)bytes) + 12, vpninfo->ift_seq++);
  470. dump_buf_hex(vpninfo, PRG_DEBUG, '>', (void *)bytes, len);
  471. ret = vpninfo->ssl_write(vpninfo, bytes, len);
  472. if (ret != len) {
  473. if (ret >= 0) {
  474. vpn_progress(vpninfo, PRG_ERR,
  475. _("Short write to IF-T/TLS\n"));
  476. ret = -EIO;
  477. }
  478. return ret;
  479. }
  480. return 0;
  481. }
  482. static int send_ift_packet(struct openconnect_info *vpninfo, struct oc_text_buf *buf)
  483. {
  484. if (buf_error(buf) || buf->pos < 16) {
  485. vpn_progress(vpninfo, PRG_ERR,
  486. _("Error creating IF-T packet\n"));
  487. return buf_error(buf);
  488. }
  489. /* Fill in the length word in the header with the full length of the buffer.
  490. * Also populate the sequence number. */
  491. store_be32(buf->data + 8, buf->pos);
  492. return send_ift_bytes(vpninfo, buf->data, buf->pos);
  493. }
  494. /* We create packets with IF-T/TLS headers prepended because that's the
  495. * larger header. In the case where they need to be sent over EAP-TTLS,
  496. * convert the header to the EAP-Message AVP instead. */
  497. static int send_eap_packet(struct openconnect_info *vpninfo, void *ttls, struct oc_text_buf *buf)
  498. {
  499. int ret;
  500. if (buf_error(buf) || buf->pos < 16) {
  501. vpn_progress(vpninfo, PRG_ERR,
  502. _("Error creating EAP packet\n"));
  503. return buf_error(buf);
  504. }
  505. if (!ttls)
  506. return send_ift_packet(vpninfo, buf);
  507. /* AVP EAP-Message header */
  508. store_be32(buf->data + 0x0c, AVP_CODE_EAP_MESSAGE);
  509. store_be32(buf->data + 0x10, buf->pos - 0xc);
  510. dump_buf_hex(vpninfo, PRG_DEBUG, '.', (void *)(buf->data + 0x0c), buf->pos - 0x0c);
  511. ret = TTLS_SEND(ttls, buf->data + 0x0c, buf->pos - 0x0c);
  512. if (ret != buf->pos - 0x0c)
  513. return -EIO;
  514. return 0;
  515. }
  516. /*
  517. * Using the given buffer, receive and validate an EAP request of the
  518. * Expanded Juniper/1 type, either natively over IF-T/TLS or by EAP-TTLS
  519. * over IF-T/TLS. Return a pointer to the EAP header, with its length and
  520. * type already validated.
  521. */
  522. static void *recv_eap_packet(struct openconnect_info *vpninfo, void *ttls, void *buf, int len)
  523. {
  524. unsigned char *cbuf = buf;
  525. int ret;
  526. if (!ttls) {
  527. ret = recv_ift_packet(vpninfo, buf, len);
  528. if (ret < 0)
  529. return NULL;
  530. if (!valid_ift_auth_eap_exj1(buf, ret)) {
  531. vpn_progress(vpninfo, PRG_ERR,
  532. _("Unexpected IF-T/TLS authentication challenge:\n"));
  533. dump_buf_hex(vpninfo, PRG_ERR, '<', (void *)buf, ret);
  534. return NULL;
  535. }
  536. return cbuf + 0x14;
  537. } else {
  538. ret = TTLS_RECV(ttls, buf, len);
  539. if (ret <= 8)
  540. return NULL;
  541. if (/* EAP-Message AVP */
  542. load_be32(cbuf) != AVP_CODE_EAP_MESSAGE ||
  543. /* Ignore the mandatory bit */
  544. (load_be32(cbuf+0x04) & ~0x40000000) != ret ||
  545. cbuf[0x08] != EAP_REQUEST ||
  546. load_be16(cbuf+0x0a) != ret - 8 ||
  547. load_be32(cbuf+0x0c) != EXPANDED_JUNIPER ||
  548. load_be32(cbuf+0x10) != 1) {
  549. vpn_progress(vpninfo, PRG_ERR,
  550. _("Unexpected EAP-TTLS payload:\n"));
  551. dump_buf_hex(vpninfo, PRG_ERR, '<', buf, ret);
  552. return NULL;
  553. }
  554. return cbuf + 0x08;
  555. }
  556. }
  557. static void dump_avp(struct openconnect_info *vpninfo, uint8_t flags,
  558. uint32_t vendor, uint32_t code, void *p, int len)
  559. {
  560. struct oc_text_buf *buf = buf_alloc();
  561. const char *pretty;
  562. int i;
  563. for (i = 0; i < len; i++)
  564. if (!isprint( ((char *)p)[i] ))
  565. break;
  566. if (i == len) {
  567. buf_append(buf, " '");
  568. buf_append_bytes(buf, p, len);
  569. buf_append(buf, "'");
  570. } else {
  571. for (i = 0; i < len; i++)
  572. buf_append(buf, " %02x", ((unsigned char *)p)[i]);
  573. }
  574. if (buf_error(buf))
  575. pretty = " <error>";
  576. else
  577. pretty = buf->data;
  578. if (flags & AVP_VENDOR)
  579. vpn_progress(vpninfo, PRG_TRACE, _("AVP 0x%x/0x%x:%s\n"), vendor, code, pretty);
  580. else
  581. vpn_progress(vpninfo, PRG_TRACE, _("AVP %d:%s\n"), code, pretty);
  582. buf_free(buf);
  583. }
  584. /* RFC5281 §10 */
  585. static int parse_avp(struct openconnect_info *vpninfo, void **pkt, int *pkt_len,
  586. void **avp_out, int *avp_len, uint8_t *avp_flags,
  587. uint32_t *avp_vendor, uint32_t *avp_code)
  588. {
  589. unsigned char *p = *pkt;
  590. int l = *pkt_len;
  591. uint32_t code, len, vendor = 0;
  592. uint8_t flags;
  593. if (l < 8)
  594. return -EINVAL;
  595. code = load_be32(p);
  596. len = load_be32(p + 4) & 0xffffff;
  597. flags = p[4];
  598. if (len > l || len < 8)
  599. return -EINVAL;
  600. p += 8;
  601. l -= 8;
  602. len -= 8;
  603. /* Vendor field is optional. */
  604. if (flags & AVP_VENDOR) {
  605. if (l < 4)
  606. return -EINVAL;
  607. vendor = load_be32(p);
  608. p += 4;
  609. l -= 4;
  610. len -= 4;
  611. }
  612. *avp_vendor = vendor;
  613. *avp_flags = flags;
  614. *avp_code = code;
  615. *avp_out = p;
  616. *avp_len = len;
  617. /* Now set up packet pointer and length for next AVP,
  618. * aligned to 4 octets (if they exist in the packet) */
  619. len = (len + 3) & ~3;
  620. if (len > l)
  621. len = l;
  622. *pkt = p + len;
  623. *pkt_len = l - len;
  624. return 0;
  625. }
  626. static int pulse_request_realm_entry(struct openconnect_info *vpninfo, struct oc_text_buf *reqbuf)
  627. {
  628. struct oc_auth_form f;
  629. struct oc_form_opt o;
  630. int ret;
  631. memset(&f, 0, sizeof(f));
  632. memset(&o, 0, sizeof(o));
  633. f.auth_id = (char *)"pulse_realm_entry";
  634. f.opts = &o;
  635. f.message = _("Enter Pulse user realm:");
  636. o.next = NULL;
  637. o.type = OC_FORM_OPT_TEXT;
  638. o.name = (char *)"realm";
  639. o.label = (char *)_("Realm:");
  640. ret = process_auth_form(vpninfo, &f);
  641. if (ret)
  642. return ret;
  643. if (o._value) {
  644. buf_append_avp_string(reqbuf, 0xd50, o._value);
  645. free_pass(&o._value);
  646. return 0;
  647. }
  648. return -EINVAL;
  649. }
  650. static int pulse_request_realm_choice(struct openconnect_info *vpninfo, struct oc_text_buf *reqbuf,
  651. int realms, unsigned char *eap)
  652. {
  653. uint8_t avp_flags;
  654. uint32_t avp_code;
  655. uint32_t avp_vendor;
  656. int avp_len;
  657. void *avp_p;
  658. struct oc_auth_form f;
  659. struct oc_form_opt_select o;
  660. int i = 0, ret;
  661. void *p;
  662. int l;
  663. l = load_be16(eap + 2) - 0x0c; /* Already validated */
  664. p = eap + 0x0c;
  665. memset(&f, 0, sizeof(f));
  666. memset(&o, 0, sizeof(o));
  667. f.auth_id = (char *)"pulse_realm_choice";
  668. f.opts = &o.form;
  669. f.authgroup_opt = &o;
  670. f.authgroup_selection = 1;
  671. f.message = _("Choose Pulse user realm:");
  672. o.form.next = NULL;
  673. o.form.type = OC_FORM_OPT_SELECT;
  674. o.form.name = (char *)"realm_choice";
  675. o.form.label = (char *)_("Realm:");
  676. o.nr_choices = realms;
  677. o.choices = calloc(realms, sizeof(*o.choices));
  678. if (!o.choices)
  679. return -ENOMEM;
  680. while (l) {
  681. if (parse_avp(vpninfo, &p, &l, &avp_p, &avp_len, &avp_flags,
  682. &avp_vendor, &avp_code)) {
  683. vpn_progress(vpninfo, PRG_ERR,
  684. _("Failed to parse AVP\n"));
  685. ret = -EINVAL;
  686. goto out;
  687. }
  688. if (avp_vendor != VENDOR_JUNIPER2 || avp_code != 0xd4e)
  689. continue;
  690. o.choices[i] = malloc(sizeof(struct oc_choice));
  691. if (!o.choices[i]) {
  692. ret = -ENOMEM;
  693. goto out;
  694. }
  695. o.choices[i]->name = o.choices[i]->label = strndup(avp_p, avp_len);
  696. if (!o.choices[i]->name) {
  697. ret = -ENOMEM;
  698. goto out;
  699. }
  700. i++;
  701. }
  702. /* We don't need to do anything on group changes. */
  703. do {
  704. ret = process_auth_form(vpninfo, &f);
  705. } while (ret == OC_FORM_RESULT_NEWGROUP);
  706. if (!ret)
  707. buf_append_avp_string(reqbuf, 0xd50, o.form._value);
  708. out:
  709. if (o.choices) {
  710. for (i = 0; i < realms; i++) {
  711. if (o.choices[i]) {
  712. free(o.choices[i]->name);
  713. free(o.choices[i]);
  714. }
  715. }
  716. free(o.choices);
  717. }
  718. return ret;
  719. }
  720. static int pulse_request_session_kill(struct openconnect_info *vpninfo, struct oc_text_buf *reqbuf,
  721. int sessions, unsigned char *eap)
  722. {
  723. uint8_t avp_flags;
  724. uint32_t avp_code;
  725. uint32_t avp_vendor;
  726. int avp_len, avp_len2;
  727. void *avp_p, *avp_p2;
  728. struct oc_auth_form f;
  729. struct oc_form_opt_select o;
  730. int i = 0, ret;
  731. void *p;
  732. int l;
  733. struct oc_text_buf *form_msg = buf_alloc();
  734. char tmbuf[80];
  735. struct tm tm;
  736. l = load_be16(eap + 2) - 0x0c; /* Already validated */
  737. p = eap + 0x0c;
  738. memset(&f, 0, sizeof(f));
  739. memset(&o, 0, sizeof(o));
  740. f.auth_id = (char *)"pulse_session_kill";
  741. f.opts = &o.form;
  742. buf_append(form_msg, _("Session limit reached. Choose session to kill:\n"));
  743. o.form.next = NULL;
  744. o.form.type = OC_FORM_OPT_SELECT;
  745. o.form.name = (char *)"session_choice";
  746. o.form.label = (char *)_("Session:");
  747. o.nr_choices = sessions;
  748. o.choices = calloc(sessions, sizeof(*o.choices));
  749. if (!o.choices) {
  750. ret = -ENOMEM;
  751. goto out;
  752. }
  753. while (l) {
  754. char *from = NULL;
  755. time_t when = 0;
  756. char *sessid = NULL;
  757. if (parse_avp(vpninfo, &p, &l, &avp_p, &avp_len, &avp_flags,
  758. &avp_vendor, &avp_code)) {
  759. badlist:
  760. free(from);
  761. free(sessid);
  762. vpn_progress(vpninfo, PRG_ERR,
  763. _("Failed to parse session list\n"));
  764. ret = -EINVAL;
  765. goto out;
  766. }
  767. if (avp_vendor != VENDOR_JUNIPER2 || avp_code != 0xd65)
  768. continue;
  769. while (avp_len) {
  770. if (parse_avp(vpninfo, &avp_p, &avp_len, &avp_p2, &avp_len2,
  771. &avp_flags, &avp_vendor, &avp_code))
  772. goto badlist;
  773. dump_avp(vpninfo, avp_flags, avp_vendor, avp_code, avp_p2, avp_len2);
  774. if (avp_vendor == VENDOR_JUNIPER2 && avp_code == 0xd66) {
  775. free(sessid);
  776. sessid = strndup(avp_p2, avp_len2);
  777. } else if (avp_vendor == VENDOR_JUNIPER2 && avp_code == 0xd67) {
  778. free(from);
  779. from = strndup(avp_p2, avp_len2);
  780. } else if (avp_vendor == VENDOR_JUNIPER2 && avp_code == 0xd68 &&
  781. avp_len2 == 8) {
  782. when = load_be32((char *)avp_p2 + 4);
  783. if (sizeof(time_t) > 4)
  784. when |= ((uint64_t)load_be32(avp_p2)) << 32;
  785. }
  786. }
  787. if (!from || !sessid || !when)
  788. goto badlist;
  789. if (0
  790. #ifdef HAVE_LOCALTIME_S
  791. || !localtime_s(&tm, &when)
  792. #endif
  793. #ifdef HAVE_LOCALTIME_R
  794. || localtime_r(&when, &tm)
  795. #endif
  796. ) {
  797. strftime(tmbuf, sizeof(tmbuf), "%a, %d %b %Y %H:%M:%S %Z", &tm);
  798. } else
  799. snprintf(tmbuf, sizeof(tmbuf), "@%lu", (unsigned long)when);
  800. buf_append(form_msg, " - %s from %s at %s\n", sessid, from, tmbuf);
  801. free(from);
  802. from = NULL;
  803. o.choices[i] = malloc(sizeof(struct oc_choice));
  804. if (!o.choices[i]) {
  805. free(sessid);
  806. ret = -ENOMEM;
  807. goto out;
  808. }
  809. o.choices[i]->name = o.choices[i]->label = sessid;
  810. i++;
  811. }
  812. ret = buf_error(form_msg);
  813. if (ret)
  814. goto out;
  815. f.message = form_msg->data;
  816. ret = process_auth_form(vpninfo, &f);
  817. if (!ret)
  818. buf_append_avp_string(reqbuf, 0xd69, o.form._value);
  819. out:
  820. if (o.choices) {
  821. for (i = 0; i < sessions; i++) {
  822. if (o.choices[i]) {
  823. free(o.choices[i]->name);
  824. free(o.choices[i]);
  825. }
  826. }
  827. free(o.choices);
  828. }
  829. buf_free(form_msg);
  830. return ret;
  831. }
  832. static int pulse_request_user_auth(struct openconnect_info *vpninfo, struct oc_text_buf *reqbuf,
  833. uint8_t eap_ident, int prompt_flags, char *user_prompt, char *pass_prompt)
  834. {
  835. struct oc_auth_form f;
  836. struct oc_form_opt o[2];
  837. unsigned char eap_avp[23];
  838. int l;
  839. int ret;
  840. memset(&f, 0, sizeof(f));
  841. memset(o, 0, sizeof(o));
  842. f.auth_id = (char *) ((prompt_flags & PROMPT_PRIMARY) ? "pulse_user" : "pulse_secondary");
  843. f.opts = &o[1]; /* Point to password prompt in case that's all we use */
  844. f.message = (prompt_flags & PROMPT_PRIMARY) ? _("Enter user credentials:") : _("Enter secondary credentials:");
  845. if (prompt_flags & PROMPT_USERNAME) {
  846. f.opts = &o[0];
  847. o[0].next = NULL; /* Again, for now */
  848. o[0].type = OC_FORM_OPT_TEXT;
  849. o[0].name = (char *)"username";
  850. if (user_prompt)
  851. o[0].label = user_prompt;
  852. else
  853. o[0].label = (char *) ((prompt_flags & PROMPT_PRIMARY) ? _("Username:") : _("Secondary username:"));
  854. }
  855. if (prompt_flags & PROMPT_PASSWORD) {
  856. /* Might be referenced from o[0] or directly from f.opts */
  857. o[0].next = &o[1];
  858. o[1].type = OC_FORM_OPT_PASSWORD;
  859. o[1].name = (char *)"password";
  860. if (pass_prompt)
  861. o[1].label = pass_prompt;
  862. else
  863. o[1].label = (char *) ((prompt_flags & PROMPT_PRIMARY) ? _("Password:") : _("Secondary password:"));
  864. }
  865. ret = process_auth_form(vpninfo, &f);
  866. if (ret)
  867. goto out;
  868. if (o[0]._value) {
  869. buf_append_avp_string(reqbuf, 0xd6d, o[0]._value);
  870. free_pass(&o[0]._value);
  871. }
  872. if (o[1]._value) {
  873. l = strlen(o[1]._value);
  874. if (l > 253) {
  875. free_pass(&o[1]._value);
  876. return -EINVAL;
  877. }
  878. } else {
  879. /* Their client actually resubmits the primary password when
  880. * a secondary password is requested. But it doesn't seem to
  881. * be necessary; might even just be a bug. */
  882. l = 0;
  883. }
  884. /* AVP flags+mandatory+length */
  885. store_be32(eap_avp, AVP_CODE_EAP_MESSAGE);
  886. store_be32(eap_avp + 4, (AVP_MANDATORY << 24) + sizeof(eap_avp) + l);
  887. /* EAP header: code/ident/len */
  888. eap_avp[8] = EAP_RESPONSE;
  889. eap_avp[9] = eap_ident;
  890. store_be16(eap_avp + 10, l + 15); /* EAP length */
  891. store_be32(eap_avp + 12, EXPANDED_JUNIPER);
  892. store_be32(eap_avp + 16, 2);
  893. /* EAP Juniper/2 payload: 02 02 <len> <password> */
  894. eap_avp[20] = eap_avp[21] = 0x02;
  895. eap_avp[22] = l + 2; /* Why 2? */
  896. buf_append_bytes(reqbuf, eap_avp, sizeof(eap_avp));
  897. if (o[1]._value) {
  898. buf_append_bytes(reqbuf, o[1]._value, l);
  899. free_pass(&o[1]._value);
  900. }
  901. /* Padding */
  902. if ((sizeof(eap_avp) + l) & 3) {
  903. uint32_t pad = 0;
  904. buf_append_bytes(reqbuf, &pad,
  905. 4 - ((sizeof(eap_avp) + l) & 3));
  906. }
  907. ret = 0;
  908. out:
  909. return ret;
  910. }
  911. static int pulse_request_pass_change(struct openconnect_info *vpninfo, struct oc_text_buf *reqbuf,
  912. uint8_t eap_ident, int prompt_flags)
  913. {
  914. struct oc_auth_form f;
  915. struct oc_form_opt o[3];
  916. unsigned char eap_avp[23];
  917. int l1, l2;
  918. int ret;
  919. memset(&f, 0, sizeof(f));
  920. memset(o, 0, sizeof(o));
  921. f.auth_id = (char *) ((prompt_flags & PROMPT_PRIMARY) ? "pulse_user_change" : "pulse_secondary_change");
  922. f.opts = &o[0];
  923. f.message = _("Password expired. Please change password:");
  924. o[0].type = OC_FORM_OPT_PASSWORD;
  925. o[0].name = (char *)"oldpass";
  926. o[0].label = (char *) _("Current password:");
  927. o[0].next = &o[1];
  928. o[1].type = OC_FORM_OPT_PASSWORD;
  929. o[1].name = (char *)"newpass1";
  930. o[1].label = (char *) _("New password:");
  931. o[1].next = &o[2];
  932. o[2].type = OC_FORM_OPT_PASSWORD;
  933. o[2].name = (char *)"newpass1";
  934. o[2].label = (char *) _("Verify new password:");
  935. retry:
  936. free_pass(&o[0]._value);
  937. free_pass(&o[1]._value);
  938. free_pass(&o[2]._value);
  939. ret = process_auth_form(vpninfo, &f);
  940. if (ret)
  941. goto out;
  942. if (!o[0]._value || !o[1]._value || !o[2]._value) {
  943. vpn_progress(vpninfo, PRG_DEBUG, _("Passwords not provided.\n"));
  944. ret = -EINVAL;
  945. goto out;
  946. }
  947. if (strcmp(o[1]._value, o[2]._value)) {
  948. vpn_progress(vpninfo, PRG_ERR, _("Passwords do not match.\n"));
  949. goto retry;
  950. }
  951. l1 = strlen(o[0]._value);
  952. if (l1 > 253) {
  953. vpn_progress(vpninfo, PRG_ERR, _("Current password too long.\n"));
  954. goto retry;
  955. }
  956. l2 = strlen(o[1]._value);
  957. if (l2 > 253) {
  958. vpn_progress(vpninfo, PRG_ERR, _("New password too long.\n"));
  959. goto retry;
  960. }
  961. /* AVP flags+mandatory+length */
  962. store_be32(eap_avp, AVP_CODE_EAP_MESSAGE);
  963. store_be32(eap_avp + 4, (AVP_MANDATORY << 24) + sizeof(eap_avp) + l1 + 2 + l2);
  964. /* EAP header: code/ident/len */
  965. eap_avp[8] = EAP_RESPONSE;
  966. eap_avp[9] = eap_ident;
  967. store_be16(eap_avp + 10, l1 + l2 + 17); /* EAP length */
  968. store_be32(eap_avp + 12, EXPANDED_JUNIPER);
  969. store_be32(eap_avp + 16, 2);
  970. /* EAP Juniper/2 payload: 02 02 <len> <password> */
  971. eap_avp[20] = eap_avp[21] = 0x02;
  972. eap_avp[22] = l1 + 2; /* Why 2? */
  973. buf_append_bytes(reqbuf, eap_avp, sizeof(eap_avp));
  974. buf_append_bytes(reqbuf, o[0]._value, l1);
  975. /* Reuse eap_avp to append the new password */
  976. eap_avp[0] = 0x03;
  977. eap_avp[1] = l2 + 2;
  978. buf_append_bytes(reqbuf, eap_avp, 2);
  979. buf_append_bytes(reqbuf, o[1]._value, l2);
  980. /* Padding */
  981. if ((sizeof(eap_avp) + l1 + 2 + l2) & 3) {
  982. uint32_t pad = 0;
  983. buf_append_bytes(reqbuf, &pad,
  984. 4 - ((sizeof(eap_avp) + l1 + 2 + l2) & 3));
  985. }
  986. ret = 0;
  987. out:
  988. free_pass(&o[0]._value);
  989. free_pass(&o[1]._value);
  990. free_pass(&o[2]._value);
  991. return ret;
  992. }
  993. static int pulse_request_gtc(struct openconnect_info *vpninfo, struct oc_text_buf *reqbuf,
  994. uint8_t eap_ident, int prompt_flags, char *user_prompt, char *pass_prompt,
  995. char *gtc_prompt)
  996. {
  997. struct oc_auth_form f;
  998. struct oc_form_opt o[2];
  999. int ret;
  1000. memset(&f, 0, sizeof(f));
  1001. memset(o, 0, sizeof(o));
  1002. f.auth_id = (char *)"pulse_gtc";
  1003. /* The first prompt always seems to be 'Enter SecurID PASSCODE:' and is ignored. */
  1004. if (gtc_prompt && (prompt_flags & PROMPT_GTC_NEXT))
  1005. f.message = gtc_prompt;
  1006. else
  1007. f.message = _("Token code request:");
  1008. if (prompt_flags & PROMPT_USERNAME) {
  1009. f.opts = &o[0];
  1010. o[0].next = &o[1];
  1011. o[0].type = OC_FORM_OPT_TEXT;
  1012. o[0].name = (char *)"username";
  1013. if (user_prompt)
  1014. o[0].label = user_prompt;
  1015. else
  1016. o[0].label = (char *) ((prompt_flags & PROMPT_PRIMARY) ? _("Username:") : _("Secondary username:"));
  1017. } else {
  1018. f.opts = &o[1];
  1019. }
  1020. o[1].type = OC_FORM_OPT_PASSWORD;
  1021. o[1].name = (char *)"tokencode";
  1022. /*
  1023. * For retries, we have a gtc_prompt and we just say 'Please enter response:'.
  1024. * Otherwise, use the pass_prompt if it exists, or create our own based
  1025. * on whether it's primary authentication or not.
  1026. */
  1027. if (prompt_flags & PROMPT_GTC_NEXT) {
  1028. o[1].label = _("Please enter response:");
  1029. } else if (pass_prompt) {
  1030. o[1].label = pass_prompt;
  1031. } else if (prompt_flags & PROMPT_PRIMARY) {
  1032. o[1].label = _("Please enter your passcode:");
  1033. } else {
  1034. o[1].label = _("Please enter your secondary token information:");
  1035. }
  1036. if (!can_gen_tokencode(vpninfo, &f, &o[1]))
  1037. o[1].type = OC_FORM_OPT_TOKEN;
  1038. ret = process_auth_form(vpninfo, &f);
  1039. if (ret)
  1040. goto out;
  1041. ret = do_gen_tokencode(vpninfo, &f);
  1042. if (ret)
  1043. goto out;
  1044. if (o[0]._value) {
  1045. buf_append_avp_string(reqbuf, 0xd6d, o[0]._value);
  1046. free_pass(&o[0]._value);
  1047. }
  1048. if (o[1]._value) {
  1049. unsigned char eap_avp[13];
  1050. int l = strlen(o[1]._value);
  1051. if (l > 253) {
  1052. free_pass(&o[1]._value);
  1053. ret = -EINVAL;
  1054. goto out;
  1055. }
  1056. /* AVP flags+mandatory+length */
  1057. store_be32(eap_avp, AVP_CODE_EAP_MESSAGE);
  1058. store_be32(eap_avp + 4, (AVP_MANDATORY << 24) + sizeof(eap_avp) + l);
  1059. /* EAP header: code/ident/len */
  1060. eap_avp[8] = EAP_RESPONSE;
  1061. eap_avp[9] = eap_ident;
  1062. store_be16(eap_avp + 10, l + 5); /* EAP length */
  1063. eap_avp[12] = EAP_TYPE_GTC;
  1064. buf_append_bytes(reqbuf, eap_avp, sizeof(eap_avp));
  1065. buf_append_bytes(reqbuf, o[1]._value, l);
  1066. /* Padding */
  1067. if ((sizeof(eap_avp) + l) & 3) {
  1068. uint32_t pad = 0;
  1069. buf_append_bytes(reqbuf, &pad,
  1070. 4 - ((sizeof(eap_avp) + l) & 3));
  1071. }
  1072. free_pass(&o[1]._value);
  1073. } else {
  1074. ret = -EINVAL;
  1075. goto out;
  1076. }
  1077. ret = 0;
  1078. out:
  1079. return ret;
  1080. }
  1081. static int dup_prompt(char **p, uint8_t *avp_p, int avp_len)
  1082. {
  1083. char *ret = NULL;
  1084. free(*p);
  1085. *p = NULL;
  1086. if (!avp_len) {
  1087. return 0;
  1088. } else if (avp_p[avp_len - 1] == ':') {
  1089. ret = strndup((char *)avp_p, avp_len);
  1090. } else {
  1091. ret = calloc(avp_len + 2, 1);
  1092. if (ret) {
  1093. memcpy(ret, avp_p, avp_len);
  1094. ret[avp_len] = ':';
  1095. ret[avp_len + 1] = 0;
  1096. }
  1097. }
  1098. if (ret) {
  1099. *p = ret;
  1100. return 0;
  1101. } else
  1102. return -ENOMEM;
  1103. }
  1104. /*
  1105. * There is complex client-side logic around when to (re)prompt for a password.
  1106. * The first prompt always needs it, whether it's a TokenCode request (EAP-06)
  1107. * or a normal password request (EAP-Expanded-Juniper/2). If a password request
  1108. * fails (0x81) then we prompt for username again in case that's what was wrong.
  1109. *
  1110. * If there's a secondary password request, it might need a *secondary* username.
  1111. * The first request comes with a 0xd73 AVP which has a single integer:
  1112. * 1: prompt for both username and password.
  1113. * 3: Prompt for password only.
  1114. * 5: Prompt for username only.
  1115. *
  1116. */
  1117. /* IF-T/TLS session establishment is the same for both pulse_obtain_cookie() and
  1118. * pulse_connect(). We have to go through the EAP phase of the connection either
  1119. * way; it's just that we might do it with just the cookie, or we might need to
  1120. * use the password/cert etc. */
  1121. static int pulse_authenticate(struct openconnect_info *vpninfo, int connecting)
  1122. {
  1123. int ret;
  1124. struct oc_text_buf *reqbuf;
  1125. unsigned char bytes[16384];
  1126. int eap_ofs;
  1127. uint8_t eap_ident, eap2_ident = 0;
  1128. uint8_t avp_flags;
  1129. uint32_t avp_code;
  1130. uint32_t avp_vendor;
  1131. int avp_len, l;
  1132. void *avp_p, *p;
  1133. unsigned char *eap;
  1134. int cookie_found = 0;
  1135. int j2_found = 0, realms_found = 0, realm_entry = 0, old_sessions = 0, gtc_found = 0;
  1136. uint8_t j2_code = 0;
  1137. void *ttls = NULL;
  1138. char *user_prompt = NULL, *pass_prompt = NULL, *gtc_prompt = NULL, *signin_prompt = NULL;
  1139. char *user2_prompt = NULL, *pass2_prompt = NULL;
  1140. int prompt_flags = PROMPT_PRIMARY | PROMPT_USERNAME | PROMPT_PASSWORD;
  1141. ret = openconnect_open_https(vpninfo);
  1142. if (ret)
  1143. return ret;
  1144. reqbuf = buf_alloc();
  1145. buf_append(reqbuf, "GET /%s HTTP/1.1\r\n", vpninfo->urlpath ?: "");
  1146. http_common_headers(vpninfo, reqbuf);
  1147. buf_append(reqbuf, "Content-Type: EAP\r\n");
  1148. buf_append(reqbuf, "Upgrade: IF-T/TLS 1.0\r\n");
  1149. buf_append(reqbuf, "Content-Length: 0\r\n");
  1150. buf_append(reqbuf, "\r\n");
  1151. if (buf_error(reqbuf)) {
  1152. vpn_progress(vpninfo, PRG_ERR,
  1153. _("Error creating Pulse connection request\n"));
  1154. ret = buf_error(reqbuf);
  1155. goto out;
  1156. }
  1157. if (vpninfo->dump_http_traffic)
  1158. dump_buf(vpninfo, '>', reqbuf->data);
  1159. ret = vpninfo->ssl_write(vpninfo, reqbuf->data, reqbuf->pos);
  1160. if (ret < 0)
  1161. goto out;
  1162. ret = process_http_response(vpninfo, 1, NULL, reqbuf);
  1163. if (ret < 0)
  1164. goto out;
  1165. if (ret != 101) {
  1166. vpn_progress(vpninfo, PRG_ERR,
  1167. _("Unexpected %d result from server\n"),
  1168. ret);
  1169. ret = -EINVAL;
  1170. goto out;
  1171. }
  1172. vpninfo->ift_seq = 0;
  1173. /* IF-T version request. */
  1174. buf_truncate(reqbuf);
  1175. buf_append_ift_hdr(reqbuf, VENDOR_TCG, IFT_VERSION_REQUEST);
  1176. /* Min version 1, max 2, preferred 2. Not that we actually do v2; the auth is
  1177. * still all IF-T/TLS v1. But the server won't offer us HMAC-SHA256 unless we
  1178. * advertise v2 */
  1179. buf_append_be32(reqbuf, 0x00010202);
  1180. ret = send_ift_packet(vpninfo, reqbuf);
  1181. if (ret)
  1182. goto out;
  1183. ret = recv_ift_packet(vpninfo, (void *)bytes, sizeof(bytes));
  1184. if (ret < 0)
  1185. goto out;
  1186. if (ret != 0x14 || (load_be32(bytes) & 0xffffff) != VENDOR_TCG ||
  1187. load_be32(bytes + 4) != IFT_VERSION_RESPONSE ||
  1188. load_be32(bytes + 8) != 0x14) {
  1189. vpn_progress(vpninfo, PRG_ERR,
  1190. _("Unexpected response to IF-T/TLS version negotiation:\n"));
  1191. dump_buf_hex(vpninfo, PRG_ERR, '<', (void *)bytes, ret);
  1192. ret = -EINVAL;
  1193. goto out;
  1194. }
  1195. vpn_progress(vpninfo, PRG_TRACE, _("IF-T/TLS version from server: %d\n"),
  1196. bytes[0x13]);
  1197. /* Client information packet over IF-T/TLS */
  1198. buf_truncate(reqbuf);
  1199. buf_append_ift_hdr(reqbuf, VENDOR_JUNIPER, 0x88);
  1200. buf_append(reqbuf, "clientHostName=%s", vpninfo->localname);
  1201. bytes[0] = 0;
  1202. if (vpninfo->peer_addr && vpninfo->peer_addr->sa_family == AF_INET6) {
  1203. struct sockaddr_in6 a;
  1204. socklen_t l = sizeof(a);
  1205. if (!getsockname(vpninfo->ssl_fd, (void *)&a, &l))
  1206. inet_ntop(AF_INET6, &a.sin6_addr, (void *)bytes, sizeof(bytes));
  1207. } else if (vpninfo->peer_addr && vpninfo->peer_addr->sa_family == AF_INET) {
  1208. struct sockaddr_in a;
  1209. socklen_t l = sizeof(a);
  1210. if (!getsockname(vpninfo->ssl_fd, (void *)&a, &l))
  1211. inet_ntop(AF_INET, &a.sin_addr, (void *)bytes, sizeof(bytes));
  1212. }
  1213. if (bytes[0])
  1214. buf_append(reqbuf, " clientIp=%s", bytes);
  1215. buf_append(reqbuf, "\n%c", 0);
  1216. ret = send_ift_packet(vpninfo, reqbuf);
  1217. if (ret)
  1218. goto out;
  1219. /* Await start of auth negotiations */
  1220. ret = recv_ift_packet(vpninfo, (void *)bytes, sizeof(bytes));
  1221. if (ret < 0)
  1222. goto out;
  1223. /* Basically an empty IF-T/TLS auth challenge packet of type Juniper/1,
  1224. * without even an EAP header in the payload. */
  1225. if (!valid_ift_auth(bytes, ret) || ret != 0x14) {
  1226. vpn_progress(vpninfo, PRG_ERR,
  1227. _("Unexpected IF-T/TLS authentication challenge:\n"));
  1228. dump_buf_hex(vpninfo, PRG_ERR, '<', (void *)bytes, ret);
  1229. ret = -EINVAL;
  1230. goto out;
  1231. }
  1232. /* Start by sending an EAP Identity of 'anonymous'. At this point we
  1233. * aren't yet very far down the rabbithole...
  1234. *
  1235. * --------------------------------------
  1236. * | TCP/IP |
  1237. * |------------------------------------|
  1238. * | TLS |
  1239. * |------------------------------------|
  1240. * | IF-T/TLS |
  1241. * |------------------------------------|
  1242. * | EAP (IF-T/TLS Auth Type Juniper/1) |
  1243. * |------------------------------------|
  1244. * | EAP-Identity |
  1245. * --------------------------------------
  1246. */
  1247. buf_truncate(reqbuf);
  1248. buf_append_ift_hdr(reqbuf, VENDOR_TCG, IFT_CLIENT_AUTH_RESPONSE);
  1249. buf_append_be32(reqbuf, JUNIPER_1); /* IF-T/TLS Auth Type */
  1250. eap_ofs = buf_append_eap_hdr(reqbuf, EAP_RESPONSE, 1, EAP_TYPE_IDENTITY, 0);
  1251. buf_append(reqbuf, "anonymous");
  1252. buf_fill_eap_len(reqbuf, eap_ofs);
  1253. ret = send_ift_packet(vpninfo, reqbuf);
  1254. if (ret)
  1255. goto out;
  1256. /*
  1257. * Phase 2 may continue directly with EAP within IF-T/TLS, or if certificate
  1258. * auth is enabled, the server may use EAP-TTLS. In that case, we end up
  1259. * with EAP within EAP-Message AVPs within EAP-TTLS within IF-T/TLS.
  1260. * The send_eap_packet() and recv_eap_packet() functions cope with both
  1261. * formats. The buffers have 0x14 bytes of header space, to allow for
  1262. * the IF-T/TLS header which is the larger of the two.
  1263. *
  1264. * --------------------------------------
  1265. * | TCP/IP |
  1266. * |------------------------------------|
  1267. * | TLS |
  1268. * |------------------------------------|
  1269. * | IF-T/TLS |
  1270. * |------------------------------------|
  1271. * | EAP (IF-T/TLS Auth Type Juniper/1) |
  1272. * |------------------ |
  1273. * | EAP-TTLS | |
  1274. * |-----------------| (or directly) |
  1275. * | EAP-Message AVP | |
  1276. * |-----------------|------------------|
  1277. * | EAP-Juniper-1 |
  1278. * --------------------------------------
  1279. */
  1280. ret = recv_ift_packet(vpninfo, (void *)bytes, sizeof(bytes));
  1281. if (ret < 0)
  1282. goto out;
  1283. /* Check EAP header and length */
  1284. if (!valid_ift_auth_eap(bytes, ret)) {
  1285. bad_ift:
  1286. vpn_progress(vpninfo, PRG_ERR,
  1287. _("Unexpected IF-T/TLS authentication challenge:\n"));
  1288. dump_buf_hex(vpninfo, PRG_ERR, '<', (void *)bytes, ret);
  1289. ret = -EINVAL;
  1290. goto out;
  1291. }
  1292. /*
  1293. * We know the packet is valid at least down to the first layer of
  1294. * EAP in the diagram above, directly within the IF-T/TLS Auth Type
  1295. * of Juniper/1. Now, disambiguate between the two cases where the
  1296. * diagram diverges. Is it EAP-TTLS or is it EAP-Juniper-1 directly?
  1297. */
  1298. if (valid_ift_auth_eap_exj1(bytes, ret)) {
  1299. eap = bytes + 0x14;
  1300. } else {
  1301. /* If it isn't that, it'd better be EAP-TTLS... */
  1302. if (bytes[0x18] != EAP_TYPE_TTLS)
  1303. goto bad_ift;
  1304. vpninfo->ttls_eap_ident = bytes[0x15];
  1305. vpninfo->ttls_recvbuf = malloc(TLS_RECORD_MAX);
  1306. if (!vpninfo->ttls_recvbuf)
  1307. return -ENOMEM;
  1308. vpninfo->ttls_recvlen = 0;
  1309. vpninfo->ttls_recvpos = 0;
  1310. vpninfo->ttls_msgleft = 0;
  1311. ttls = establish_eap_ttls(vpninfo);
  1312. if (!ttls) {
  1313. vpn_progress(vpninfo, PRG_ERR,
  1314. _("Failed to establish EAP-TTLS session\n"));
  1315. ret = -EINVAL;
  1316. goto out;
  1317. }
  1318. /* Resend the EAP Identity 'anonymous' packet within EAP-TTLS */
  1319. ret = send_eap_packet(vpninfo, ttls, reqbuf);
  1320. if (ret)
  1321. goto out;
  1322. /*
  1323. * The recv_eap_packet() function receives and validates the EAP
  1324. * packet of type Extended Juniper-1, either natively or within
  1325. * EAP-TTLS according to whether 'ttls' is set.
  1326. */
  1327. eap = recv_eap_packet(vpninfo, ttls, bytes, sizeof(bytes));
  1328. if (!eap) {
  1329. ret = -EIO;
  1330. goto out;
  1331. }
  1332. }
  1333. /* Now we (hopefully) have the server information packet, in an EAP request
  1334. * from the server. Either it was received directly in IF-T/TLS, or within
  1335. * an EAP-Message within EAP-TTLS. Either way, the EAP message we're
  1336. * interested in will be at offset 0x14 in the packet, its header will
  1337. * have been checked, and is Expanded Juniper/1, and its payload thus
  1338. * starts at 0x20. And its length is sufficient that we won't underflow */
  1339. eap_ident = eap[1];
  1340. l = load_be16(eap + 2) - 0x0c; /* Already validated */
  1341. p = eap + 0x0c;
  1342. /* We don't actually use anything we get here. Typically it
  1343. * contains Juniper/0xd49 and Juniper/0xd4a word AVPs, and
  1344. * a Juniper/0xd56 AVP with server licensing information. */
  1345. while (l) {
  1346. if (parse_avp(vpninfo, &p, &l, &avp_p, &avp_len, &avp_flags,
  1347. &avp_vendor, &avp_code)) {
  1348. vpn_progress(vpninfo, PRG_ERR,
  1349. _("Failed to parse AVP\n"));
  1350. bad_eap:
  1351. dump_buf_hex(vpninfo, PRG_ERR, 'E', eap, load_be16(eap + 2));
  1352. ret = -EINVAL;
  1353. goto out;
  1354. }
  1355. dump_avp(vpninfo, avp_flags, avp_vendor, avp_code, avp_p, avp_len);
  1356. }
  1357. /* Present the client information and auth cookie */
  1358. buf_truncate(reqbuf);
  1359. buf_append_ift_hdr(reqbuf, VENDOR_TCG, IFT_CLIENT_AUTH_RESPONSE);
  1360. buf_append_be32(reqbuf, JUNIPER_1); /* IF-T/TLS Auth Type */
  1361. eap_ofs = buf_append_eap_hdr(reqbuf, EAP_RESPONSE, eap_ident, EAP_TYPE_EXPANDED, 1);
  1362. #if 0
  1363. /* Their client sends a lot of other stuff here, which we don't
  1364. * understand and which doesn't appear to be mandatory. So leave
  1365. * it out for now until/unless it becomes necessary. It seems that
  1366. * sending Pulse-Secure/4.0.0.0 or anything newer makes it do
  1367. * EAP-TLS *within* the EAP-TTLS session if you don't actually
  1368. * present a certificate. */
  1369. buf_append_avp_be32(reqbuf, 0xd49, 3);
  1370. buf_append_avp_be32(reqbuf, 0xd61, 0);
  1371. buf_append_avp_string(reqbuf, 0xd5e, "Windows");
  1372. buf_append_avp_string(reqbuf, 0xd70, "Pulse-Secure/9.0.3.1667 (Windows Server 2016) Pulse/9.0.3.1667");
  1373. buf_append_avp_string(reqbuf, 0xd63, "\xac\x1e\x8a\x78\x2d\x96\x45\x69\xb7\x7b\x80\x0f\xb7\x39\x2e\x41");
  1374. buf_append_avp_string(reqbuf, 0xd64, "\x1a\x3d\x9f\xa4\x07\xd9\xcb\x40\x9d\x61\x6a\x7a\x89\x24\x9b\x15");
  1375. buf_append_avp_string(reqbuf, 0xd5f, "en-US");
  1376. buf_append_avp_string(reqbuf, 0xd6c, "\x02\xe9\xa7\x51\x92\x4e");
  1377. buf_append_avp_be32(reqbuf, 0xd84, 0);
  1378. #else
  1379. buf_append_avp_string(reqbuf, 0xd70, vpninfo->useragent);
  1380. #endif
  1381. if (vpninfo->cookie)
  1382. buf_append_avp_string(reqbuf, 0xd53, vpninfo->cookie);
  1383. buf_fill_eap_len(reqbuf, eap_ofs);
  1384. ret = send_eap_packet(vpninfo, ttls, reqbuf);
  1385. if (ret)
  1386. goto out;
  1387. /* Await start of auth negotiations */
  1388. auth_response:
  1389. free(signin_prompt);
  1390. signin_prompt = NULL;
  1391. /* If there's a follow-on GTC prompt, remember it's not the first */
  1392. if (gtc_found)
  1393. prompt_flags |= PROMPT_GTC_NEXT;
  1394. else
  1395. prompt_flags &= ~PROMPT_GTC_NEXT;
  1396. realm_entry = realms_found = j2_found = old_sessions = 0, gtc_found = 0;
  1397. eap = recv_eap_packet(vpninfo, ttls, (void *)bytes, sizeof(bytes));
  1398. if (!eap) {
  1399. ret = -EIO;
  1400. goto out;
  1401. }
  1402. eap_ident = eap[1];
  1403. l = load_be16(eap + 2) - 0x0c; /* Already validated */
  1404. p = eap + 0x0c;
  1405. while (l) {
  1406. if (parse_avp(vpninfo, &p, &l, &avp_p, &avp_len, &avp_flags,
  1407. &avp_vendor, &avp_code)) {
  1408. vpn_progress(vpninfo, PRG_ERR,
  1409. _("Failed to parse AVP\n"));
  1410. goto bad_eap;
  1411. }
  1412. dump_avp(vpninfo, avp_flags, avp_vendor, avp_code, avp_p, avp_len);
  1413. /* It's a bit late for this given that we don't get it until after
  1414. * we provide the password. */
  1415. if (avp_vendor == VENDOR_JUNIPER2 && avp_code == 0xd55) {
  1416. char md5buf[MD5_SIZE * 2 + 1];
  1417. get_cert_md5_fingerprint(vpninfo, vpninfo->peer_cert, md5buf);
  1418. if (avp_len != MD5_SIZE * 2 || strncasecmp(avp_p, md5buf, MD5_SIZE * 2)) {
  1419. /* This actually happens in the wild and the official clients don't seem to
  1420. * care. It's too late because we've already authenticated at this point,
  1421. * and it's only MD5 anyway. I find it hard to care. Just whine and continue
  1422. * anyway. */
  1423. vpn_progress(vpninfo, PRG_INFO,
  1424. _("WARNING: Server provided certificate MD5 does not match its actual certificate.\n"));
  1425. continue;
  1426. }
  1427. }
  1428. if (avp_vendor == VENDOR_JUNIPER2 && avp_code == 0xd65) {
  1429. old_sessions++;
  1430. } else if (avp_vendor == VENDOR_JUNIPER2 && avp_code == 0xd60) {
  1431. uint32_t failcode;
  1432. if (avp_len != 4)
  1433. goto auth_unknown;
  1434. failcode = load_be32(avp_p);
  1435. if (failcode == 0x0d) {
  1436. vpn_progress(vpninfo, PRG_ERR,
  1437. _("Authentication failure: Account locked out\n"));
  1438. } else if (failcode == 0x0e) {
  1439. vpn_progress(vpninfo, PRG_ERR,
  1440. _("Authentication failure: Client certificate required\n"));
  1441. } else {
  1442. vpn_progress(vpninfo, PRG_ERR,
  1443. _("Authentication failure: Code 0x%02x\n"),
  1444. failcode);
  1445. }
  1446. ret = -EPERM;
  1447. goto out;
  1448. } else if (avp_vendor == VENDOR_JUNIPER2 && avp_code == 0xd80) {
  1449. dup_prompt(&user_prompt, avp_p, avp_len);
  1450. } else if (avp_vendor == VENDOR_JUNIPER2 && avp_code == 0xd81) {
  1451. dup_prompt(&pass_prompt, avp_p, avp_len);
  1452. } else if (avp_vendor == VENDOR_JUNIPER2 && avp_code == 0xd82) {
  1453. dup_prompt(&user2_prompt, avp_p, avp_len);
  1454. } else if (avp_vendor == VENDOR_JUNIPER2 && avp_code == 0xd83) {
  1455. dup_prompt(&pass2_prompt, avp_p, avp_len);
  1456. } else if (avp_vendor == VENDOR_JUNIPER2 && avp_code == 0xd73) {
  1457. uint32_t val;
  1458. if (avp_len != 4)
  1459. goto auth_unknown;
  1460. val = load_be32(avp_p);
  1461. switch (val) {
  1462. case 1: /* Prompt for both username and password. */
  1463. prompt_flags = PROMPT_PASSWORD | PROMPT_USERNAME;
  1464. break;
  1465. case 3: /* Prompt for password.*/
  1466. case 15:
  1467. prompt_flags = PROMPT_PASSWORD;
  1468. break;
  1469. case 5: /* Prompt for username.*/
  1470. prompt_flags = PROMPT_USERNAME;
  1471. break;
  1472. default:
  1473. /* It does no harm to submit both, as anything unwanted is ignored. */
  1474. vpn_progress(vpninfo, PRG_ERR,
  1475. _("Unknown D73 prompt value 0x%x. Will prompt for both username and password.\n"),
  1476. val);
  1477. vpn_progress(vpninfo, PRG_ERR,
  1478. _("Please report this value and the behaviour of the official client.\n"));
  1479. prompt_flags = PROMPT_PASSWORD | PROMPT_USERNAME;
  1480. break;
  1481. }
  1482. } else if (avp_vendor == VENDOR_JUNIPER2 && avp_code == 0xd7b) {
  1483. free(signin_prompt);
  1484. signin_prompt = strndup(avp_p, avp_len);
  1485. } else if (avp_vendor == VENDOR_JUNIPER2 && avp_code == 0xd4e) {
  1486. realms_found++;
  1487. } else if (avp_vendor == VENDOR_JUNIPER2 && avp_code == 0xd4f) {
  1488. realm_entry++;
  1489. } else if (avp_vendor == VENDOR_JUNIPER2 && avp_code == 0xd5c) {
  1490. if (avp_len != 4)
  1491. goto auth_unknown;
  1492. uint32_t val = load_be32(avp_p);
  1493. if (val)
  1494. vpninfo->auth_expiration = time(NULL) + val;
  1495. } else if (avp_vendor == VENDOR_JUNIPER2 && avp_code == 0xd75) {
  1496. if (avp_len != 4)
  1497. goto auth_unknown;
  1498. uint32_t val = load_be32(avp_p);
  1499. if (val)
  1500. vpninfo->idle_timeout = val;
  1501. } else if (avp_vendor == VENDOR_JUNIPER2 && avp_code == 0xd53) {
  1502. free(vpninfo->cookie);
  1503. vpninfo->cookie = strndup(avp_p, avp_len);
  1504. cookie_found = 1;
  1505. /* DSID cookie may be needed for fallback to oNCP/Juniper logout */
  1506. http_add_cookie(vpninfo, "DSID", vpninfo->cookie, 1 /* replace */);
  1507. } else if (!avp_vendor && avp_code == AVP_CODE_EAP_MESSAGE) {
  1508. char *avp_c = avp_p;
  1509. /* EAP within AVP within EAP within IF-T/TLS. Check EAP header. */
  1510. if (avp_len < 5 || avp_c[0] != EAP_REQUEST ||
  1511. load_be16(avp_c + 2) != avp_len)
  1512. goto auth_unknown;
  1513. eap2_ident = avp_c[1];
  1514. if (avp_c[4] == EAP_TYPE_GTC) {
  1515. gtc_found = 1;
  1516. free(gtc_prompt);
  1517. gtc_prompt = strndup(avp_c + 5, avp_len - 5);
  1518. } else if (avp_len >= 13 && load_be32(avp_c + 4) == EXPANDED_JUNIPER) {
  1519. switch (load_be32(avp_c + 8)) {
  1520. case 2: /* Expanded Juniper/2: password */
  1521. j2_found = 1;
  1522. j2_code = avp_c[12];
  1523. if (j2_code == J2_PASSREQ || j2_code == J2_PASSRETRY || j2_code == J2_PASSCHANGE) {
  1524. if (avp_len != 13)
  1525. goto auth_unknown;
  1526. /* Precisely one byte, which is j2_code. OK. */
  1527. } else if (j2_code == J2_PASSFAIL) {
  1528. /*
  1529. < 0000: 00 00 55 97 00 00 00 05 00 00 00 84 00 00 01 fa |..U.............|
  1530. < 0010: 00 0a 4c 01 01 05 00 70 fe 00 0a 4c 00 00 00 01 |..L....p...L....|
  1531. < 0020: 00 00 00 4f 40 00 00 62 01 02 00 5a fe 00 0a 4c |...O@..b...Z...L|
  1532. < 0030: 00 00 00 02 c5 01 4d 43 6f 75 6c 64 20 6e 6f 74 |......MCould not|
  1533. < 0040: 20 63 68 61 6e 67 65 20 70 61 73 73 77 6f 72 64 | change password|
  1534. < 0050: 2e 20 4e 65 77 20 70 61 73 73 77 6f 72 64 20 6d |. New password m|
  1535. < 0060: 75 73 74 20 62 65 20 61 74 20 6c 65 61 73 74 20 |ust be at least |
  1536. < 0070: 34 20 63 68 61 72 61 63 74 65 72 73 20 6c 6f 6e |4 characters lon|
  1537. < 0080: 67 2e 00 00 |g...|
  1538. */
  1539. if (avp_len > 15 && avp_c[13] == 0x01 && avp_c[14] == avp_len - 13) {
  1540. /* Failure message. */
  1541. vpn_progress(vpninfo, PRG_ERR,
  1542. _("Authentication failure: %.*s\n"), avp_len - 15, avp_c + 15);
  1543. ret = -EIO;
  1544. goto out;
  1545. } else
  1546. goto auth_unknown;
  1547. }
  1548. break;
  1549. case 3: /* TNCC */
  1550. vpn_progress(vpninfo, PRG_ERR,
  1551. _("Pulse server requested Host Checker; not yet supported\n"
  1552. "Try Juniper mode (--protocol=nc)\n"));
  1553. goto bad_eap;
  1554. default:
  1555. goto auth_unknown;
  1556. }
  1557. } else {
  1558. goto auth_unknown;
  1559. }
  1560. } else if (avp_flags & AVP_MANDATORY)
  1561. goto auth_unknown;
  1562. }
  1563. /* We want it to be precisely one type of request, not a mixture. */
  1564. if (realm_entry + !!realms_found + j2_found + gtc_found + cookie_found + !!old_sessions != 1 &&
  1565. !signin_prompt) {
  1566. auth_unknown:
  1567. vpn_progress(vpninfo, PRG_ERR,
  1568. _("Unhandled Pulse authentication packet, or authentication failure\n"));
  1569. goto bad_eap;
  1570. }
  1571. /* Prepare next response packet */
  1572. buf_truncate(reqbuf);
  1573. buf_append_ift_hdr(reqbuf, VENDOR_TCG, IFT_CLIENT_AUTH_RESPONSE);
  1574. buf_append_be32(reqbuf, JUNIPER_1); /* IF-T/TLS Auth Type */
  1575. eap_ofs = buf_append_eap_hdr(reqbuf, EAP_RESPONSE, eap_ident, EAP_TYPE_EXPANDED, 1);
  1576. if (!cookie_found) {
  1577. /* No user interaction when called from pulse_connect().
  1578. * We expect the cookie to work. */
  1579. if (connecting) {
  1580. vpn_progress(vpninfo, PRG_ERR,
  1581. _("Pulse authentication cookie not accepted\n"));
  1582. ret = -EPERM;
  1583. goto out;
  1584. }
  1585. if (realm_entry) {
  1586. vpn_progress(vpninfo, PRG_TRACE, _("Pulse realm entry\n"));
  1587. ret = pulse_request_realm_entry(vpninfo, reqbuf);
  1588. if (ret)
  1589. goto out;
  1590. } else if (realms_found) {
  1591. vpn_progress(vpninfo, PRG_TRACE, _("Pulse realm choice\n"));
  1592. ret = pulse_request_realm_choice(vpninfo, reqbuf, realms_found, eap);
  1593. if (ret)
  1594. goto out;
  1595. } else if (j2_found) {
  1596. vpn_progress(vpninfo, PRG_TRACE,
  1597. _("Pulse password auth request, code 0x%02x\n"),
  1598. j2_code);
  1599. if (j2_code == J2_PASSCHANGE) {
  1600. ret = pulse_request_pass_change(vpninfo, reqbuf, eap2_ident,
  1601. prompt_flags);
  1602. } else if (j2_code == J2_PASSREQ || j2_code == J2_PASSRETRY) {
  1603. /* Present user/password form to user */
  1604. ret = pulse_request_user_auth(vpninfo, reqbuf, eap2_ident, prompt_flags,
  1605. (prompt_flags & PROMPT_PRIMARY) ? user_prompt : user2_prompt,
  1606. (prompt_flags & PROMPT_PRIMARY) ? pass_prompt : pass2_prompt);
  1607. } else {
  1608. vpn_progress(vpninfo, PRG_ERR,
  1609. _("Pulse password request with unknown code 0x%02x. Please report.\n"),
  1610. j2_code);
  1611. ret = -EINVAL;
  1612. }
  1613. if (ret)
  1614. goto out;
  1615. } else if (gtc_found) {
  1616. vpn_progress(vpninfo, PRG_TRACE,
  1617. _("Pulse password general token code request\n"));
  1618. /* Present user/password form to user */
  1619. ret = pulse_request_gtc(vpninfo, reqbuf, eap2_ident, prompt_flags,
  1620. (prompt_flags & PROMPT_PRIMARY) ? user_prompt : user2_prompt,
  1621. (prompt_flags & PROMPT_PRIMARY) ? pass_prompt : pass2_prompt,
  1622. gtc_prompt);
  1623. if (ret)
  1624. goto out;
  1625. } else if (old_sessions) {
  1626. vpn_progress(vpninfo, PRG_TRACE,
  1627. _("Pulse session limit, %d sessions\n"),
  1628. old_sessions);
  1629. ret = pulse_request_session_kill(vpninfo, reqbuf, old_sessions, eap);
  1630. if (ret)
  1631. goto out;
  1632. } else if (signin_prompt) {
  1633. buf_append_avp_be32(reqbuf, 0xd7c, 1);
  1634. } else {
  1635. vpn_progress(vpninfo, PRG_ERR,
  1636. _("Unhandled Pulse auth request\n"));
  1637. goto bad_eap;
  1638. }
  1639. /* If we get here, something has filled in the next response */
  1640. buf_fill_eap_len(reqbuf, eap_ofs);
  1641. ret = send_eap_packet(vpninfo, ttls, reqbuf);
  1642. if (ret)
  1643. goto out;
  1644. goto auth_response;
  1645. }
  1646. /* We're done, but need to send an empty response to the above information
  1647. * in order that the EAP session can complete with 'success'. Not quite
  1648. * sure why they didn't send it as payload on the success frame, mind you. */
  1649. buf_fill_eap_len(reqbuf, eap_ofs);
  1650. ret = send_eap_packet(vpninfo, ttls, reqbuf);
  1651. if (ret)
  1652. goto out;
  1653. if (ttls) {
  1654. /* Normally we don't actually send the EAP-TTLS frame until
  1655. * we're waiting for a response, which allows us to coalesce.
  1656. * This time, we need to flush the outbound frames. The empty
  1657. * EAP response (within EAP-TTLS) causes the server to close
  1658. * the EAP-TTLS session and the next response is plain IF-T/TLS
  1659. * IFT_CLIENT_AUTH_SUCCESS just like the non-certificate mode. */
  1660. pulse_eap_ttls_recv(vpninfo, NULL, 0);
  1661. }
  1662. ret = recv_ift_packet(vpninfo, (void *)bytes, sizeof(bytes));
  1663. if (ret < 0)
  1664. goto out;
  1665. if (!valid_ift_success(bytes, ret)) {
  1666. vpn_progress(vpninfo, PRG_ERR,
  1667. _("Unexpected response instead of IF-T/TLS auth success:\n"));
  1668. dump_buf_hex(vpninfo, PRG_ERR, '<', (void *)bytes, ret);
  1669. ret = -EINVAL;
  1670. goto out;
  1671. }
  1672. ret = 0;
  1673. out:
  1674. if (ret)
  1675. openconnect_close_https(vpninfo, 0);
  1676. buf_free(reqbuf);
  1677. if (ttls)
  1678. destroy_eap_ttls(vpninfo, ttls);
  1679. buf_free(vpninfo->ttls_pushbuf);
  1680. vpninfo->ttls_pushbuf = NULL;
  1681. free(vpninfo->ttls_recvbuf);
  1682. vpninfo->ttls_recvbuf = NULL;
  1683. free(user_prompt);
  1684. free(pass_prompt);
  1685. free(user2_prompt);
  1686. free(pass2_prompt);
  1687. free(gtc_prompt);
  1688. free(signin_prompt);
  1689. return ret;
  1690. }
  1691. static void buf_append_ttls_headers(struct openconnect_info *vpninfo, struct oc_text_buf *buf, uint8_t flags)
  1692. {
  1693. buf_append_ift_hdr(buf, VENDOR_TCG, IFT_CLIENT_AUTH_RESPONSE);
  1694. buf_append_be32(buf, JUNIPER_1); /* IF-T/TLS Auth Type */
  1695. buf_append_eap_hdr(buf, EAP_RESPONSE, 0 /*vpninfo->ttls_eap_ident*/,
  1696. EAP_TYPE_TTLS, 0);
  1697. /* Flags byte for EAP-TTLS */
  1698. buf_append_bytes(buf, &flags, 1);
  1699. }
  1700. int pulse_eap_ttls_send(struct openconnect_info *vpninfo, const void *data, int len)
  1701. {
  1702. struct oc_text_buf *buf = vpninfo->ttls_pushbuf;
  1703. if (!buf) {
  1704. buf = vpninfo->ttls_pushbuf = buf_alloc();
  1705. if (!buf)
  1706. return -ENOMEM;
  1707. }
  1708. /* We concatenate sent data into a single EAP-TTLS frame which is
  1709. * sent just before we actually need to read something. */
  1710. if (!buf->pos)
  1711. buf_append_ttls_headers(vpninfo, buf, 0);
  1712. buf_append_bytes(buf, data, len);
  1713. return len;
  1714. }
  1715. int pulse_eap_ttls_recv(struct openconnect_info *vpninfo, void *data, int len)
  1716. {
  1717. struct oc_text_buf *pushbuf;
  1718. int ret;
  1719. if (!len && (vpninfo->ttls_recvlen || vpninfo->ttls_msgleft)) {
  1720. vpn_progress(vpninfo, PRG_ERR,
  1721. _("EAP-TTLS failure: Flushing output with pending input bytes\n"));
  1722. return -EIO;
  1723. }
  1724. if (!vpninfo->ttls_recvlen) {
  1725. uint8_t flags;
  1726. if (vpninfo->ttls_msgleft) {
  1727. /* Fragments left to receive of current message.
  1728. * Send an Acknowledge frame */
  1729. pushbuf = buf_alloc();
  1730. buf_append_ttls_headers(vpninfo, pushbuf, 0);
  1731. } else {
  1732. /* Send the pending outbound bytes as a single message */
  1733. pushbuf = vpninfo->ttls_pushbuf;
  1734. vpninfo->ttls_pushbuf = NULL;
  1735. }
  1736. if (buf_error(pushbuf))
  1737. return buf_free(pushbuf);
  1738. /* This can never happen. We *always* put the header in. */
  1739. if (pushbuf->pos < 0x1a) {
  1740. vpn_progress(vpninfo, PRG_ERR,
  1741. _("Error creating EAP-TTLS buffer\n"));
  1742. buf_free(pushbuf);
  1743. return -EIO;
  1744. }
  1745. /* Handle outbound fragmentation if necessary */
  1746. if (pushbuf->pos > TTLS_MAXFRAG + 0x1a) {
  1747. struct oc_text_buf *frag = buf_alloc();
  1748. uint8_t flags = TTLS_MOREFRAGS | TTLS_LENGTH;
  1749. uint32_t remaining;
  1750. char *msg;
  1751. if (buf_error(frag)) {
  1752. buf_free(pushbuf);
  1753. return buf_free(frag);
  1754. }
  1755. remaining = pushbuf->pos - 0x1a;
  1756. msg = pushbuf->data + 0x1a;
  1757. do {
  1758. buf_append_ttls_headers(vpninfo, frag, flags);
  1759. if (flags & TTLS_LENGTH) {
  1760. buf_append_be32(frag, remaining);
  1761. flags &= ~TTLS_LENGTH;
  1762. }
  1763. buf_append_bytes(frag, msg, TTLS_MAXFRAG);
  1764. msg += TTLS_MAXFRAG;
  1765. remaining -= TTLS_MAXFRAG;
  1766. if (buf_error(frag)) {
  1767. buf_free(pushbuf);
  1768. return buf_free(frag);
  1769. }
  1770. frag->data[0x15] = vpninfo->ttls_eap_ident;
  1771. buf_fill_eap_len(frag, 0x14);
  1772. ret = send_ift_packet(vpninfo, frag);
  1773. if (ret) {
  1774. buf_free(pushbuf);
  1775. buf_free(frag);
  1776. return ret;
  1777. }
  1778. buf_truncate(frag);
  1779. ret = vpninfo->ssl_read(vpninfo, (void *)vpninfo->ttls_recvbuf,
  1780. TLS_RECORD_MAX);
  1781. if (ret < 0) {
  1782. vpn_progress(vpninfo, PRG_ERR,
  1783. _("Failed to read EAP-TTLS Acknowledge: %s\n"),
  1784. strerror(-ret));
  1785. buf_free(pushbuf);
  1786. buf_free(frag);
  1787. return ret;
  1788. }
  1789. if (ret > 0 && vpninfo->dump_http_traffic) {
  1790. vpn_progress(vpninfo, PRG_TRACE,
  1791. _("Read %d bytes of IF-T/TLS EAP-TTLS record\n"),
  1792. ret);
  1793. dump_buf_hex(vpninfo, PRG_TRACE, '<',
  1794. (void *)vpninfo->ttls_recvbuf,
  1795. ret);
  1796. }
  1797. if (!valid_ift_auth_eap(vpninfo->ttls_recvbuf, ret) ||
  1798. ret != 0x1a ||
  1799. vpninfo->ttls_recvbuf[0x18] != EAP_TYPE_TTLS ||
  1800. vpninfo->ttls_recvbuf[0x19] != 0) {
  1801. vpn_progress(vpninfo, PRG_ERR,
  1802. _("Bad EAP-TTLS Acknowledge packet\n"));
  1803. buf_free(pushbuf);
  1804. buf_free(frag);
  1805. return -EIO;
  1806. }
  1807. vpninfo->ttls_eap_ident = vpninfo->ttls_recvbuf[0x15];
  1808. } while (remaining > TTLS_MAXFRAG);
  1809. buf_free(frag);
  1810. memmove(pushbuf->data + 0x1a, msg, remaining);
  1811. pushbuf->pos = remaining + 0x1a;
  1812. }
  1813. /* Fill in the EAP header ident and length */
  1814. pushbuf->data[0x15] = vpninfo->ttls_eap_ident;
  1815. buf_fill_eap_len(pushbuf, 0x14);
  1816. ret = send_ift_packet(vpninfo, pushbuf);
  1817. buf_free(pushbuf);
  1818. if (ret)
  1819. return ret;
  1820. /* If called just to flush outbound, return now. */
  1821. if (!len)
  1822. return 0;
  1823. vpninfo->ttls_recvlen = vpninfo->ssl_read(vpninfo, (void *)vpninfo->ttls_recvbuf,
  1824. TLS_RECORD_MAX);
  1825. if (vpninfo->ttls_recvlen > 0 && vpninfo->dump_http_traffic) {
  1826. vpn_progress(vpninfo, PRG_TRACE,
  1827. _("Read %d bytes of IF-T/TLS EAP-TTLS record\n"),
  1828. vpninfo->ttls_recvlen);
  1829. dump_buf_hex(vpninfo, PRG_TRACE, '<',
  1830. (void *)vpninfo->ttls_recvbuf,
  1831. vpninfo->ttls_recvlen);
  1832. }
  1833. if (!valid_ift_auth_eap(vpninfo->ttls_recvbuf, vpninfo->ttls_recvlen) ||
  1834. vpninfo->ttls_recvlen < 0x1a ||
  1835. vpninfo->ttls_recvbuf[0x18] != EAP_TYPE_TTLS) {
  1836. bad_pkt:
  1837. vpn_progress(vpninfo, PRG_ERR,
  1838. _("Bad EAP-TTLS packet (len %d, left %d)\n"),
  1839. vpninfo->ttls_recvlen, vpninfo->ttls_msgleft);
  1840. return -EIO;
  1841. }
  1842. vpninfo->ttls_eap_ident = vpninfo->ttls_recvbuf[0x15];
  1843. flags = vpninfo->ttls_recvbuf[0x19];
  1844. /* Start, Reserved bits and version (we only support version zero) */
  1845. if (flags & 0x3f)
  1846. goto bad_pkt;
  1847. if (vpninfo->ttls_msgleft) {
  1848. /* Second and subsequent fragments MUST NOT have L bit set */
  1849. if (flags & TTLS_LENGTH)
  1850. goto bad_pkt;
  1851. /* The header doesn't contain a length word. Just IF-T/TLS, EAP, TTLS */
  1852. vpninfo->ttls_recvpos = 0x1a;
  1853. vpninfo->ttls_recvlen -= 0x1a;
  1854. if (flags & TTLS_MOREFRAGS) {
  1855. /* If the More Fragments bit is set, this packet
  1856. * must contain fewer bytes than are left. */
  1857. if (vpninfo->ttls_recvlen >= vpninfo->ttls_msgleft)
  1858. goto bad_pkt;
  1859. } else {
  1860. /* If the More Fragments bit is set, this packet
  1861. must contain precisely the number of bytes left. */
  1862. if (vpninfo->ttls_recvlen != vpninfo->ttls_msgleft)
  1863. goto bad_pkt;
  1864. }
  1865. vpninfo->ttls_msgleft -= vpninfo->ttls_recvlen;
  1866. } else if (flags & TTLS_MOREFRAGS) {
  1867. /* First fragment MUST have Length */
  1868. if (!(flags & TTLS_LENGTH) || vpninfo->ttls_recvlen < 0x1e)
  1869. goto bad_pkt;
  1870. vpninfo->ttls_recvpos = 0x1e;
  1871. vpninfo->ttls_recvlen -= 0x1e;
  1872. vpninfo->ttls_msgleft = load_be32(vpninfo->ttls_recvbuf + 0x1a);
  1873. if (vpninfo->ttls_msgleft <= vpninfo->ttls_recvlen || !vpninfo->ttls_recvlen)
  1874. goto bad_pkt;
  1875. vpninfo->ttls_msgleft -= vpninfo->ttls_recvlen;
  1876. } else {
  1877. /* Unfragmented message */
  1878. if (flags & TTLS_LENGTH) {
  1879. /* Length bit. */
  1880. if (vpninfo->ttls_recvlen < 0x1e ||
  1881. load_be32(vpninfo->ttls_recvbuf + 0x1a) != vpninfo->ttls_recvlen - 0x1e)
  1882. goto bad_pkt;
  1883. vpninfo->ttls_recvpos = 0x1e;
  1884. vpninfo->ttls_recvlen -= 0x1e;
  1885. } else {
  1886. vpninfo->ttls_recvpos = 0x1a;
  1887. vpninfo->ttls_recvlen -= 0x1a;
  1888. }
  1889. vpninfo->ttls_msgleft = 0;
  1890. if (!vpninfo->ttls_recvlen)
  1891. goto bad_pkt;
  1892. }
  1893. }
  1894. if (len > vpninfo->ttls_recvlen) {
  1895. memcpy(data, vpninfo->ttls_recvbuf + vpninfo->ttls_recvpos,
  1896. vpninfo->ttls_recvlen);
  1897. len = vpninfo->ttls_recvlen;
  1898. vpninfo->ttls_recvlen = 0;
  1899. return len;
  1900. }
  1901. memcpy(data, vpninfo->ttls_recvbuf + vpninfo->ttls_recvpos, len);
  1902. vpninfo->ttls_recvpos += len;
  1903. vpninfo->ttls_recvlen -= len;
  1904. return len;
  1905. }
  1906. int pulse_obtain_cookie(struct openconnect_info *vpninfo)
  1907. {
  1908. return pulse_authenticate(vpninfo, 0);
  1909. }
  1910. /* Handler for config attributes, see handle_main_config_packet */
  1911. static int handle_attr_elements(struct openconnect_info *vpninfo,
  1912. unsigned char *bytes, int len,
  1913. struct oc_vpn_option **new_opts,
  1914. struct oc_ip_info *new_ip_info)
  1915. {
  1916. unsigned char *p = bytes;
  1917. int l = len;
  1918. /* No idea what this is */
  1919. if (l < 8 || load_be32(p + 4) != 0x03000000)
  1920. return -EINVAL;
  1921. p += 8;
  1922. l -= 8;
  1923. while (l) {
  1924. if (l < 4)
  1925. return -EINVAL;
  1926. uint16_t type = load_be16(p);
  1927. uint16_t attrlen = load_be16(p+2);
  1928. if (attrlen + 4 > l)
  1929. return -EINVAL;
  1930. p += 4;
  1931. l -= 4;
  1932. process_attr(vpninfo, new_opts, new_ip_info, type, p, attrlen);
  1933. p += attrlen;
  1934. l -= attrlen;
  1935. }
  1936. return 0;
  1937. }
  1938. /* Example config packet:
  1939. < 0000: 00 00 0a 4c 00 00 00 01 00 00 01 80 00 00 01 fb |...L............|
  1940. < 0010: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
  1941. < 0020: 2c 20 f0 00 00 00 00 00 00 00 01 70 2e 00 00 78 |, .........p...x|
  1942. < 0030: 07 00 00 00 07 00 00 10 00 00 ff ff 05 05 00 00 |................|
  1943. < 0040: 05 05 ff ff 07 00 00 10 00 00 ff ff 07 00 00 00 |................|
  1944. < 0050: 07 00 00 ff 07 00 00 10 00 00 ff ff 08 08 08 08 |................|
  1945. < 0060: 08 08 08 08 f1 00 00 10 00 00 ff ff 06 06 06 06 |................|
  1946. < 0070: 06 06 06 07 f1 00 00 10 00 00 ff ff 09 09 09 09 |................|
  1947. < 0080: 09 09 09 09 f1 00 00 10 00 00 ff ff 0a 0a 0a 0a |................|
  1948. < 0090: 0a 0a 0a 0a f1 00 00 10 00 00 ff ff 0b 0b 0b 0b |................|
  1949. < 00a0: 0b 0b 0b 0b 00 00 00 dc 03 00 00 00 40 00 00 01 |............@...|
  1950. < 00b0: 00 40 01 00 01 00 40 1f 00 01 00 40 20 00 01 00 |.@....@....@ ...|
  1951. < 00c0: 40 21 00 01 00 40 05 00 04 00 00 05 78 00 03 00 |@!...@......x...|
  1952. < 00d0: 04 08 08 08 08 00 03 00 04 08 08 04 04 40 06 00 |.............@..|
  1953. < 00e0: 0c 70 73 65 63 75 72 65 2e 6e 65 74 00 40 07 00 |.psecure.net.@..|
  1954. < 00f0: 04 00 00 00 00 00 04 00 04 01 01 01 01 40 19 00 |.............@..|
  1955. < 0100: 01 01 40 1a 00 01 00 40 0f 00 02 00 00 40 10 00 |..@....@.....@..|
  1956. < 0110: 02 00 05 40 11 00 02 00 02 40 12 00 04 00 00 04 |...@.....@......|
  1957. < 0120: b0 40 13 00 04 00 00 00 00 40 14 00 04 00 00 00 |.@.......@......|
  1958. < 0130: 01 40 15 00 04 00 00 00 00 40 16 00 02 11 94 40 |.@.......@.....@|
  1959. < 0140: 17 00 04 00 00 00 0f 40 18 00 04 00 00 00 3c 00 |.......@......<.|
  1960. < 0150: 01 00 04 0a 14 03 01 00 02 00 04 ff ff ff ff 40 |...............@|
  1961. < 0160: 0b 00 04 0a c8 c8 c8 40 0c 00 01 00 40 0d 00 01 |.......@....@...|
  1962. < 0170: 00 40 0e 00 01 00 40 1b 00 01 00 40 1c 00 01 00 |.@....@....@....|
  1963. It starts as an IF-T/TLS packet of type Juniper/1.
  1964. Lots of zeroes at the start, and at 0x20 there is a distinctive 0x2c20f000
  1965. signature which appears to be in all config packets.
  1966. At 0x28 it has the payload length (0x10 less than the full IF-T length).
  1967. 0x2c is the start of the routing information. The 0x2e byte always
  1968. seems to be there, and in this example 0x78 is the length of the
  1969. routing information block. The number of entries is in byte 0x30.
  1970. In the absence of IPv6 perhaps, the length at 0x2c seems always to be
  1971. the number of entries (in 0x30) * 0x10 + 8.
  1972. Routing entries are 0x10 bytes each, starting at 0x34. The ones starting
  1973. with 0x07 are include, with 0xf1 are exclude. No idea what the following 7
  1974. bytes 0f 00 00 10 00 00 ff ff mean; perhaps the 0010 is a length? The IP
  1975. address range is in bytes 8-11 (starting address) and the highest address
  1976. of the range (traditionally a broadcast address) is in bytes 12-15.
  1977. After the routing inforamation (in this example at 0xa4) comes another
  1978. length field, this time for the information elements which comprise
  1979. the rest of the packet. Not sure what the 03 00 00 00 at 0xa8 means;
  1980. it *could* be an element type 0x3000 with payload length zero but if it
  1981. is, we don't know what it means. Following that, the elements all have
  1982. two bytes of type followed by two bytes length, then their payload.
  1983. There follows an attempt to parse the packet based on the above
  1984. understanding. Having more examples, especially with IPv6 split includes
  1985. and excludes, would be useful...
  1986. */
  1987. static int handle_main_config_packet(struct openconnect_info *vpninfo,
  1988. unsigned char *bytes, int len)
  1989. {
  1990. int routes_len = 0;
  1991. int l;
  1992. unsigned char *p;
  1993. int offset = 0x2c;
  1994. struct oc_vpn_option *new_opts = NULL;
  1995. struct oc_ip_info new_ip_info = {};
  1996. if (len < 0x31) {
  1997. bad_config:
  1998. vpn_progress(vpninfo, PRG_ERR,
  1999. _("Unexpected Pulse config packet:\n"));
  2000. dump_buf_hex(vpninfo, PRG_ERR, '<', (void *)bytes, len);
  2001. free_optlist(new_opts);
  2002. free_split_routes(&new_ip_info);
  2003. return -EINVAL;
  2004. }
  2005. /* On Pulse 9.1R14, we see packet type 0x2e20f0000, whereas earlier
  2006. * versions had 0x2c20f0000.
  2007. * With the newer packet type, we seem to have a leading set of
  2008. * attribute elements. Example:
  2009. * 2c 00 (fixed)
  2010. * 00 0d (length 13)
  2011. * 03 00 00 00 (fixed)
  2012. * 40 25 00 01 01 (unknown attr 0x4025, length 1, value 0x01)
  2013. */
  2014. if (bytes[0x20] == 0x2e) {
  2015. /* Length of attributes section */
  2016. int attr_len = load_be16(bytes + 0x2e);
  2017. /* Start of attributes */
  2018. if (load_be16(bytes + 0x2c) != 0x2c00 ||
  2019. len < 0x2c + attr_len + 4 ||
  2020. /* Process the attributes */
  2021. handle_attr_elements(vpninfo, bytes + 0x2c, attr_len,
  2022. &new_opts, &new_ip_info) < 0) {
  2023. goto bad_config;
  2024. }
  2025. offset += attr_len;
  2026. }
  2027. /* First part of header, similar to ESP, has already been checked */
  2028. if (len < offset + 5 ||
  2029. /* Start of routing information */
  2030. load_be16(bytes + offset) != 0x2e00 ||
  2031. /* Routing length at offset+2 makes sense */
  2032. (routes_len = load_be16(bytes + offset + 2)) != ((int)bytes[offset + 4] * 0x10 + 8) ||
  2033. /* Make sure the next length field (at 0xa4 in the above example) is present */
  2034. len < offset + routes_len + 4 ||
  2035. /* Another length field, must match to end of packet */
  2036. load_be32(bytes + offset + routes_len) + routes_len + offset != len) {
  2037. }
  2038. p = bytes + offset + 8;
  2039. routes_len -= 8; /* The header including length and number of routes */
  2040. /* We know it's a multiple of 0x10 now. We checked. */
  2041. while (routes_len) {
  2042. char buf[80];
  2043. /* Probably not a whole be32 but let's see if anything ever changes */
  2044. uint32_t type = load_be32(p);
  2045. uint32_t ffff = load_be32(p+4);
  2046. if (ffff != 0xffff)
  2047. goto bad_config;
  2048. /* Convert the range end into a netmask by xor. Mask out the
  2049. * bits in the network address, leaving only the low bits set,
  2050. * then invert what's left so that only the high bits are set
  2051. * as in a normal netmask.
  2052. *
  2053. * e.g.
  2054. * 10.0.0.0-10.0.63.255 becomes 0.0.63.255 becomes 255.255.192.0
  2055. */
  2056. snprintf(buf, sizeof(buf), "%d.%d.%d.%d/%d.%d.%d.%d",
  2057. p[8], p[9], p[10], p[11],
  2058. 255 ^ (p[8] ^ p[12]), 255 ^ (p[9] ^ p[13]),
  2059. 255 ^ (p[10] ^ p[14]), 255 ^ (p[11] ^ p[15]));
  2060. if (type == 0x07000010) {
  2061. struct oc_split_include *inc;
  2062. vpn_progress(vpninfo, PRG_DEBUG, _("Received split include route %s\n"), buf);
  2063. inc = malloc(sizeof(*inc));
  2064. if (inc) {
  2065. inc->route = add_option_dup(&new_opts, "split-include", buf, -1);
  2066. if (inc->route) {
  2067. inc->next = new_ip_info.split_includes;
  2068. new_ip_info.split_includes = inc;
  2069. } else
  2070. free(inc);
  2071. }
  2072. } else if (type == 0xf1000010) {
  2073. struct oc_split_include *exc;
  2074. vpn_progress(vpninfo, PRG_DEBUG, _("Received split exclude route %s\n"), buf);
  2075. exc = malloc(sizeof(*exc));
  2076. if (exc) {
  2077. exc->route = add_option_dup(&new_opts, "split-exclude", buf, -1);
  2078. if (exc->route) {
  2079. exc->next = new_ip_info.split_excludes;
  2080. new_ip_info.split_excludes = exc;
  2081. } else
  2082. free(exc);
  2083. }
  2084. } else {
  2085. vpn_progress(vpninfo, PRG_ERR, _("Receive route of unknown type 0x%08x\n"),
  2086. type);
  2087. goto bad_config;
  2088. }
  2089. p += 0x10;
  2090. routes_len -= 0x10;
  2091. }
  2092. /* p now points at the length field of the final elements, which
  2093. was already checked. */
  2094. l = load_be32(p);
  2095. if (handle_attr_elements(vpninfo, p, l, &new_opts, &new_ip_info) < 0)
  2096. goto bad_config;
  2097. int ret = install_vpn_opts(vpninfo, new_opts, &new_ip_info);
  2098. if (ret) {
  2099. free_optlist(new_opts);
  2100. free_split_routes(&new_ip_info);
  2101. }
  2102. return ret;
  2103. }
  2104. /* Example ESP config packet:
  2105. < 0000: 00 00 0a 4c 00 00 00 01 00 00 00 80 00 00 01 fc |...L............|
  2106. < 0010: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
  2107. < 0020: 21 20 24 00 00 00 00 00 00 00 00 70 00 00 00 54 |! $........p...T|
  2108. < 0030: 01 00 00 00 ec 52 1b 6c 00 40 11 9d c5 f6 85 f3 |.....R.l.@......|
  2109. < 0040: 26 7d 70 75 44 45 63 eb 64 00 fb ba 89 4f 24 b2 |&}puDEc.d....O$.|
  2110. < 0050: 81 42 ce 24 b8 0a f8 b6 71 39 78 f8 5e 6f 5f d6 |.B.$....q9x.^o_.|
  2111. < 0060: 9e 5c 06 47 8d 1e f3 0e 5a 51 ae b2 3d 09 8d 27 |.\.G....ZQ..=..'|
  2112. < 0070: e0 50 76 6a 22 9a d1 20 86 78 00 00 00 00 00 00 |.Pvj".. .x......|
  2113. First 0x2c bytes are like the main config packet header.
  2114. At 0x2c there is another length field, covering the whole of the
  2115. rest of this packet. Then an unknown 0x01000000 at 0x30, followed
  2116. by the server->client SPI in little-endian(!) form at 0x34.
  2117. Then follows the secrets, with a 2-byte length field at 0x38 (which
  2118. is always 0x40), followed by the secrets themselves. As with
  2119. Juniper Network Connect, the HMAC secret immediately follows the
  2120. encryption key, however large the latter is.
  2121. */
  2122. static int handle_esp_config_packet(struct openconnect_info *vpninfo,
  2123. unsigned char *bytes, int len)
  2124. {
  2125. #ifdef HAVE_ESP
  2126. struct esp *esp;
  2127. int secretslen;
  2128. uint32_t spi;
  2129. int ret;
  2130. if (len < 0x6a ||
  2131. load_be32(bytes + 0x2c) != len - 0x2c ||
  2132. load_be32(bytes + 0x30) != 0x01000000 ||
  2133. load_be16(bytes + 0x38) != 0x40) {
  2134. vpn_progress(vpninfo, PRG_ERR,
  2135. _("Invalid ESP config packet:\n"));
  2136. dump_buf_hex(vpninfo, PRG_ERR, '<', bytes, len);
  2137. return -EINVAL;
  2138. }
  2139. /* We insist on this being 0x40 for now. But just in case it later changes... */
  2140. secretslen = load_be16(bytes + 0x38);
  2141. vpn_progress(vpninfo, PRG_DEBUG, _("%d bytes of ESP secrets\n"), secretslen);
  2142. if (!vpninfo->enc_key_len || !vpninfo->hmac_key_len ||
  2143. vpninfo->enc_key_len + vpninfo->hmac_key_len > secretslen) {
  2144. vpn_progress(vpninfo, PRG_ERR,
  2145. _("Invalid ESP setup\n"));
  2146. return -EINVAL;
  2147. }
  2148. /* Yes, bizarrely this is little-endian on the wire. I have no idea
  2149. * what made them do this. */
  2150. spi = load_le32(bytes + 0x34);
  2151. vpn_progress(vpninfo, PRG_DEBUG, _("ESP SPI (outbound): %x\n"), spi);
  2152. /* But we store it internally as big-endian because we never do any
  2153. * calculations on it; it's only set into outbound packets and matched
  2154. * on incoming ones... and we've NEVER had to see it in little-endian
  2155. * form ever before because that's insane! */
  2156. store_be32(&vpninfo->esp_out.spi, spi);
  2157. memcpy(vpninfo->esp_out.enc_key, bytes + 0x3a, vpninfo->enc_key_len);
  2158. memcpy(vpninfo->esp_out.hmac_key, bytes + 0x3a + vpninfo->enc_key_len,
  2159. vpninfo->hmac_key_len);
  2160. ret = openconnect_setup_esp_keys(vpninfo, 1);
  2161. if (ret)
  2162. return ret;
  2163. esp = &vpninfo->esp_in[vpninfo->current_esp_in];
  2164. /* Now, using the buffer in which we received the original packet (which
  2165. * we trust our caller made large enough), create an appropriate reply.
  2166. * A reply packet contains two sets of ESP information, as we are expected
  2167. * to send our own followed by a copy of what the server sent to us. */
  2168. /* Adjust the length in the IF-T/TLS header */
  2169. store_be32(bytes + 8, 0x40 + 2 * secretslen);
  2170. /* Copy the server's own ESP information into place */
  2171. memmove(bytes + secretslen + 0x3a, bytes + 0x34, secretslen + 0x06);
  2172. /* Adjust other length fields. */
  2173. store_be32(bytes + 0x28, 0x30 + 2 * secretslen);
  2174. store_be32(bytes + 0x2c, 0x14 + 2 * secretslen);
  2175. /* Store the SPI. Bizarrely little-endian again. */
  2176. store_le32(bytes + 0x34, load_be32(&esp->spi));
  2177. memcpy(bytes + 0x3a, esp->enc_key, vpninfo->enc_key_len);
  2178. memcpy(bytes + 0x3a + vpninfo->enc_key_len, esp->hmac_key, vpninfo->hmac_key_len);
  2179. memset(bytes + 0x3a + vpninfo->enc_key_len + vpninfo->hmac_key_len,
  2180. 0, 0x40 - vpninfo->enc_key_len - vpninfo->hmac_key_len);
  2181. return 0;
  2182. #else
  2183. return -EINVAL;
  2184. #endif
  2185. }
  2186. int pulse_connect(struct openconnect_info *vpninfo)
  2187. {
  2188. struct oc_text_buf *reqbuf;
  2189. unsigned char bytes[TLS_RECORD_MAX];
  2190. int ret;
  2191. /* If we already have a channel open, it's because we have just
  2192. * successfully authenticated on it from pulse_obtain_cookie(). */
  2193. if (vpninfo->ssl_fd == -1) {
  2194. ret = pulse_authenticate(vpninfo, 1);
  2195. if (ret)
  2196. return ret;
  2197. }
  2198. while (1) {
  2199. uint32_t pkt_type;
  2200. ret = recv_ift_packet(vpninfo, (void *)bytes, sizeof(bytes));
  2201. if (ret < 0)
  2202. return ret;
  2203. if (ret < 16 || load_be32(bytes + 8) != ret) {
  2204. vpn_progress(vpninfo, PRG_ERR,
  2205. _("Bad IF-T/TLS packet when expecting configuration:\n"));
  2206. dump_buf_hex(vpninfo, PRG_ERR, '<', bytes, ret);
  2207. return -EINVAL;
  2208. }
  2209. if (load_be32(bytes) != VENDOR_JUNIPER) {
  2210. bad_pkt:
  2211. vpn_progress(vpninfo, PRG_INFO,
  2212. _("Unexpected IF-T/TLS packet when expecting configuration.\n"));
  2213. dump_buf_hex(vpninfo, PRG_DEBUG, '<', bytes, ret);
  2214. continue;
  2215. }
  2216. pkt_type = load_be32(bytes + 4);
  2217. /* End of configuration? Seems to have a 4-byte payload of zeroes. */
  2218. if (pkt_type == 0x8f)
  2219. break;
  2220. /* The main and ESP config packets both start like this. The word at
  2221. * 0x20 is 0x2c20f000 for config and 0x0x21202400 for ESP, and the word
  2222. * at 0x2c is the length of the payload (0x10 less than the overall
  2223. * length including (and in) the IF-T/TLS header. e.g 0x170 here:
  2224. *
  2225. * < 0000: 00 00 0a 4c 00 00 00 01 00 00 01 80 00 00 01 fb |...L............|
  2226. * < 0010: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
  2227. * < 0020: 2c 20 f0 00 00 00 00 00 00 00 01 70 ... |, .........|
  2228. */
  2229. if (pkt_type != 1 || ret < 0x2c || load_be32(bytes + 0x10) ||
  2230. load_be32(bytes + 0x14) || load_be32(bytes + 0x18) ||
  2231. load_be32(bytes + 0x1c) || load_be32(bytes + 0x24) ||
  2232. load_be32(bytes + 0x28) != ret - 0x10)
  2233. goto bad_pkt;
  2234. switch(load_be32(bytes + 0x20)) {
  2235. case 0x2c20f000:
  2236. case 0x2e20f000: /* Variant seen on Pulse 9.1R14 */
  2237. ret = handle_main_config_packet(vpninfo, bytes, ret);
  2238. if (ret)
  2239. return ret;
  2240. break;
  2241. case 0x21202400:
  2242. ret = handle_esp_config_packet(vpninfo, bytes, ret);
  2243. if (ret) {
  2244. vpninfo->dtls_state = DTLS_DISABLED;
  2245. continue;
  2246. }
  2247. /* It has created a response packet to send. */
  2248. ret = send_ift_bytes(vpninfo, bytes, load_be32(bytes + 8));
  2249. if (ret)
  2250. return ret;
  2251. /* Tell server to enable ESP handling */
  2252. reqbuf = buf_alloc();
  2253. buf_append_ift_hdr(reqbuf, VENDOR_JUNIPER, 5);
  2254. buf_append(reqbuf, "ncmo=1\n%c", 0);
  2255. ret = send_ift_packet(vpninfo, reqbuf);
  2256. buf_free(reqbuf);
  2257. if (ret)
  2258. return ret;
  2259. break;
  2260. default:
  2261. goto bad_pkt;
  2262. }
  2263. }
  2264. if (!vpninfo->ip_info.mtu ||
  2265. (!vpninfo->ip_info.addr && !vpninfo->ip_info.addr6)) {
  2266. vpn_progress(vpninfo, PRG_ERR, _("Insufficient configuration found\n"));
  2267. return -EINVAL;
  2268. }
  2269. /* This should never happen, but be defensive and shut Coverity up */
  2270. if (vpninfo->ssl_fd == -1)
  2271. return -EIO;
  2272. ret = 0;
  2273. monitor_fd_new(vpninfo, ssl);
  2274. monitor_read_fd(vpninfo, ssl);
  2275. monitor_except_fd(vpninfo, ssl);
  2276. free_pkt(vpninfo, vpninfo->cstp_pkt);
  2277. vpninfo->cstp_pkt = NULL;
  2278. return ret;
  2279. }
  2280. int pulse_mainloop(struct openconnect_info *vpninfo, int *timeout, int readable)
  2281. {
  2282. int ret;
  2283. int work_done = 0;
  2284. if (vpninfo->ssl_fd == -1)
  2285. goto do_reconnect;
  2286. /* FIXME: The poll() handling here is fairly simplistic. Actually,
  2287. if the SSL connection stalls it could return a WANT_WRITE error
  2288. on _either_ of the SSL_read() or SSL_write() calls. In that case,
  2289. we should probably remove POLLIN from the events we're looking for,
  2290. and add POLLOUT. As it is, though, it'll just chew CPU time in that
  2291. fairly unlikely situation, until the write backlog clears. */
  2292. while (readable) {
  2293. /* Some servers send us packets that are larger than
  2294. negotiated MTU. We reserve some extra space to
  2295. handle that */
  2296. int receive_mtu = MAX(16384, vpninfo->deflate_pkt_size ? : vpninfo->ip_info.mtu);
  2297. struct pkt *pkt = vpninfo->cstp_pkt;
  2298. int len, payload_len;
  2299. if (!pkt) {
  2300. pkt = vpninfo->cstp_pkt = alloc_pkt(vpninfo, receive_mtu);
  2301. if (!pkt) {
  2302. vpn_progress(vpninfo, PRG_ERR, _("Allocation failed\n"));
  2303. break;
  2304. }
  2305. }
  2306. /* Receive packet header, if there's anything there... */
  2307. len = ssl_nonblock_read(vpninfo, 0, &pkt->pulse.vendor, 16);
  2308. if (!len)
  2309. break;
  2310. if (len < 0)
  2311. goto do_reconnect;
  2312. if (len < 16) {
  2313. vpn_progress(vpninfo, PRG_ERR, _("Short packet received (%d bytes)\n"), len);
  2314. vpninfo->quit_reason = "Short packet received";
  2315. return 1;
  2316. }
  2317. /* Packets shouldn't cross SSL record boundaries (we hope!), so if there
  2318. * was a header there, then rest of that packet should be there too. */
  2319. if (load_be32(&pkt->pulse.len) > receive_mtu + 0x10) {
  2320. /* This doesn't look right. Pull the rest of the SSL record
  2321. * and complain about it (which we will, since the length
  2322. * won't match the header */
  2323. len = receive_mtu;
  2324. } else
  2325. len = load_be32(&pkt->pulse.len) - 0x10;
  2326. payload_len = ssl_nonblock_read(vpninfo, 0, &pkt->data, len);
  2327. if (payload_len != load_be32(&pkt->pulse.len) - 0x10) {
  2328. if (payload_len < 0)
  2329. len = 0x10;
  2330. else
  2331. len = payload_len + 0x10;
  2332. goto unknown_pkt;
  2333. }
  2334. if (load_be32(&pkt->pulse.vendor) != VENDOR_JUNIPER)
  2335. goto unknown_pkt;
  2336. vpninfo->ssl_times.last_rx = time(NULL);
  2337. len = payload_len + 0x10;
  2338. switch(load_be32(&pkt->pulse.type)) {
  2339. case 4:
  2340. vpn_progress(vpninfo, PRG_TRACE,
  2341. _("Received data packet of %d bytes\n"),
  2342. payload_len);
  2343. dump_buf_hex(vpninfo, PRG_TRACE, '<', (void *)&vpninfo->cstp_pkt->pulse.vendor, len);
  2344. vpninfo->cstp_pkt->len = payload_len;
  2345. queue_packet(&vpninfo->incoming_queue, pkt);
  2346. vpninfo->cstp_pkt = pkt = NULL;
  2347. work_done = 1;
  2348. continue;
  2349. case 1:
  2350. if (payload_len < 0x6a ||
  2351. load_be32(pkt->data + 0x10) != 0x21202400 ||
  2352. load_be32(pkt->data + 0x18) != payload_len ||
  2353. load_be32(pkt->data + 0x1c) != payload_len - 0x1c ||
  2354. load_be32(pkt->data + 0x20) != 0x01000000 ||
  2355. load_be16(pkt->data + 0x28) != 0x40)
  2356. goto unknown_pkt;
  2357. dump_buf_hex(vpninfo, PRG_TRACE, '<', (void *)&vpninfo->cstp_pkt->pulse.vendor, len);
  2358. ret = handle_esp_config_packet(vpninfo, (void *)&pkt->pulse.vendor, len);
  2359. if (ret) {
  2360. vpn_progress(vpninfo, PRG_ERR,
  2361. _("ESP rekey failed\n"));
  2362. vpninfo->proto->udp_close(vpninfo);
  2363. continue;
  2364. }
  2365. vpninfo->cstp_pkt = NULL;
  2366. pkt->len = load_be32(&pkt->pulse.len) - 16;
  2367. queue_packet(&vpninfo->tcp_control_queue, pkt);
  2368. print_esp_keys(vpninfo, _("new incoming"), &vpninfo->esp_in[vpninfo->current_esp_in]);
  2369. print_esp_keys(vpninfo, _("new outgoing"), &vpninfo->esp_out);
  2370. continue;
  2371. case 0x93: {
  2372. /* Expected contents are "errorType=%d errorString=%s\n". Known values:
  2373. * 6: "agentd error" (another session started and kicked this one off)
  2374. * 7: "session has been terminated" (by client)
  2375. * 8: "session timed out" (idle timeout)
  2376. */
  2377. if (payload_len < 12 || strncmp("errorType=", (const char *)pkt->data, 10))
  2378. goto unknown_pkt;
  2379. pkt->data[payload_len - 1] = '\0'; /* overwrite final '\n' */
  2380. char *endp;
  2381. unsigned long reason = strtol((const char *)pkt->data + 10, &endp, 10);
  2382. if (strncmp(" errorString=", endp, 13))
  2383. goto unknown_pkt;
  2384. urldecode_inplace(endp+1);
  2385. vpn_progress(vpninfo, PRG_ERR, _("Pulse fatal error (reason: %ld): %s\n"),
  2386. reason, endp+13);
  2387. vpninfo->quit_reason = strdup(endp+13);
  2388. return -EPIPE;
  2389. }
  2390. case 0x96:
  2391. /* It sends the licence information once the connection is set up. For
  2392. * now, abuse this to deal with the race condition in ESP setup — it looks
  2393. * like the server doesn't process the ESP config until after we've sent
  2394. * the probes, in some cases. */
  2395. if (vpninfo->dtls_state == DTLS_SLEEPING)
  2396. vpninfo->proto->udp_send_probes(vpninfo);
  2397. break;
  2398. default:
  2399. unknown_pkt:
  2400. vpn_progress(vpninfo, PRG_ERR,
  2401. _("Unknown Pulse packet of %d bytes (vendor 0x%03x, type 0x%02x, hdr_len %d, ident %d)\n"),
  2402. len, load_be32(&pkt->pulse.vendor), load_be32(&pkt->pulse.type),
  2403. load_be32(&pkt->pulse.len), load_be32(&pkt->pulse.ident));
  2404. dump_buf_hex(vpninfo, PRG_TRACE, '<', (void *)&vpninfo->cstp_pkt->pulse.vendor, len);
  2405. continue;
  2406. }
  2407. }
  2408. /* If SSL_write() fails we are expected to try again. With exactly
  2409. the same data, at exactly the same location. So we keep the
  2410. packet we had before.... */
  2411. if (vpninfo->current_ssl_pkt) {
  2412. handle_outgoing:
  2413. vpninfo->ssl_times.last_tx = time(NULL);
  2414. unmonitor_write_fd(vpninfo, ssl);
  2415. vpn_progress(vpninfo, PRG_TRACE, _("Packet outgoing:\n"));
  2416. dump_buf_hex(vpninfo, PRG_TRACE, '>',
  2417. (void *)&vpninfo->current_ssl_pkt->pulse.vendor,
  2418. vpninfo->current_ssl_pkt->len + 16);
  2419. ret = ssl_nonblock_write(vpninfo, 0,
  2420. &vpninfo->current_ssl_pkt->pulse.vendor,
  2421. vpninfo->current_ssl_pkt->len + 16);
  2422. if (ret < 0) {
  2423. do_reconnect:
  2424. /* XXX: Do we have to do this or can we leave it open?
  2425. * Perhaps we could even reconnect asynchronously while
  2426. * the ESP is still running? */
  2427. #ifdef HAVE_ESP
  2428. esp_shutdown(vpninfo);
  2429. #endif
  2430. ret = ssl_reconnect(vpninfo);
  2431. if (ret) {
  2432. vpn_progress(vpninfo, PRG_ERR, _("Reconnect failed\n"));
  2433. vpninfo->quit_reason = "Pulse reconnect failed";
  2434. return ret;
  2435. }
  2436. vpninfo->dtls_need_reconnect = 1;
  2437. return 1;
  2438. } else if (!ret) {
  2439. #if 0 /* Not for Pulse yet */
  2440. /* -EAGAIN: ssl_nonblock_write() will have added the SSL
  2441. fd to ->select_wfds if appropriate, so we can just
  2442. return and wait. Unless it's been stalled for so long
  2443. that DPD kicks in and we kill the connection. */
  2444. switch (ka_stalled_action(&vpninfo->ssl_times, timeout)) {
  2445. case KA_DPD_DEAD:
  2446. goto peer_dead;
  2447. case KA_REKEY:
  2448. goto do_rekey;
  2449. case KA_NONE:
  2450. return work_done;
  2451. default:
  2452. /* This should never happen */
  2453. break;
  2454. }
  2455. #else
  2456. return work_done;
  2457. #endif
  2458. }
  2459. if (ret != vpninfo->current_ssl_pkt->len + 16) {
  2460. vpn_progress(vpninfo, PRG_ERR,
  2461. _("SSL wrote too few bytes! Asked for %d, sent %d\n"),
  2462. vpninfo->current_ssl_pkt->len + 8, ret);
  2463. vpninfo->quit_reason = "Internal error";
  2464. return 1;
  2465. }
  2466. /* Don't free the 'special' packets */
  2467. if (vpninfo->current_ssl_pkt == vpninfo->deflate_pkt) {
  2468. free_pkt(vpninfo, vpninfo->pending_deflated_pkt);
  2469. vpninfo->pending_deflated_pkt = NULL;
  2470. } else
  2471. free_pkt(vpninfo, vpninfo->current_ssl_pkt);
  2472. vpninfo->current_ssl_pkt = NULL;
  2473. }
  2474. #if 0 /* Not understood for Pulse yet */
  2475. if (vpninfo->owe_ssl_dpd_response) {
  2476. vpninfo->owe_ssl_dpd_response = 0;
  2477. vpninfo->current_ssl_pkt = (struct pkt *)&dpd_resp_pkt;
  2478. goto handle_outgoing;
  2479. }
  2480. switch (keepalive_action(&vpninfo->ssl_times, timeout)) {
  2481. case KA_REKEY:
  2482. do_rekey:
  2483. /* Not that this will ever happen; we don't even process
  2484. the setting when we're asked for it. */
  2485. vpn_progress(vpninfo, PRG_INFO, _("CSTP rekey due\n"));
  2486. if (vpninfo->ssl_times.rekey_method == REKEY_TUNNEL)
  2487. goto do_reconnect;
  2488. else if (vpninfo->ssl_times.rekey_method == REKEY_SSL) {
  2489. ret = cstp_handshake(vpninfo, 0);
  2490. if (ret) {
  2491. /* if we failed rehandshake try establishing a new-tunnel instead of failing */
  2492. vpn_progress(vpninfo, PRG_ERR, _("Rehandshake failed; attempting new-tunnel\n"));
  2493. goto do_reconnect;
  2494. }
  2495. goto do_dtls_reconnect;
  2496. }
  2497. break;
  2498. case KA_DPD_DEAD:
  2499. peer_dead:
  2500. vpn_progress(vpninfo, PRG_ERR,
  2501. _("CSTP Dead Peer Detection detected dead peer!\n"));
  2502. goto do_reconnect;
  2503. do_reconnect:
  2504. ret = cstp_reconnect(vpninfo);
  2505. if (ret) {
  2506. vpn_progress(vpninfo, PRG_ERR, _("Reconnect failed\n"));
  2507. vpninfo->quit_reason = "CSTP reconnect failed";
  2508. return ret;
  2509. }
  2510. do_dtls_reconnect:
  2511. /* succeeded, let's rekey DTLS, if it is not rekeying
  2512. * itself. */
  2513. if (vpninfo->dtls_state > DTLS_SLEEPING &&
  2514. vpninfo->dtls_times.rekey_method == REKEY_NONE) {
  2515. vpninfo->dtls_need_reconnect = 1;
  2516. }
  2517. return 1;
  2518. case KA_DPD:
  2519. vpn_progress(vpninfo, PRG_DEBUG, _("Send CSTP DPD\n"));
  2520. vpninfo->current_ssl_pkt = (struct pkt *)&dpd_pkt;
  2521. goto handle_outgoing;
  2522. case KA_KEEPALIVE:
  2523. /* No need to send an explicit keepalive
  2524. if we have real data to send */
  2525. if (vpninfo->dtls_state != DTLS_ESTABLISHED &&
  2526. vpninfo->outgoing_queue.head)
  2527. break;
  2528. vpn_progress(vpninfo, PRG_DEBUG, _("Send CSTP Keepalive\n"));
  2529. vpninfo->current_ssl_pkt = (struct pkt *)&keepalive_pkt;
  2530. goto handle_outgoing;
  2531. case KA_NONE:
  2532. ;
  2533. }
  2534. #endif
  2535. if (vpninfo->dtls_state == DTLS_CONNECTED) {
  2536. /* We don't currently do anything to make the server start sending
  2537. * data packets in ESP instead of over IF-T/TLS. Just go straight
  2538. * to CONNECTED mode. */
  2539. vpninfo->dtls_state = DTLS_ESTABLISHED;
  2540. work_done = 1;
  2541. }
  2542. vpninfo->current_ssl_pkt = dequeue_packet(&vpninfo->tcp_control_queue);
  2543. if (vpninfo->current_ssl_pkt) {
  2544. /* Anything on the control queue will have the rest of its
  2545. header filled in already. */
  2546. store_be32(&vpninfo->current_ssl_pkt->pulse.ident, vpninfo->ift_seq++);
  2547. goto handle_outgoing;
  2548. }
  2549. /* Service outgoing packet queue, if no DTLS */
  2550. while (vpninfo->dtls_state != DTLS_ESTABLISHED &&
  2551. (vpninfo->current_ssl_pkt = dequeue_packet(&vpninfo->outgoing_queue))) {
  2552. struct pkt *this = vpninfo->current_ssl_pkt;
  2553. store_be32(&this->pulse.vendor, VENDOR_JUNIPER);
  2554. store_be32(&this->pulse.type, 4);
  2555. store_be32(&this->pulse.len, this->len + 16);
  2556. store_be32(&this->pulse.ident, vpninfo->ift_seq++);
  2557. vpn_progress(vpninfo, PRG_TRACE,
  2558. _("Sending IF-T/TLS data packet of %d bytes\n"),
  2559. this->len);
  2560. vpninfo->current_ssl_pkt = this;
  2561. goto handle_outgoing;
  2562. }
  2563. /* Work is not done if we just got rid of packets off the queue */
  2564. return work_done;
  2565. }
  2566. int pulse_bye(struct openconnect_info *vpninfo, const char *reason)
  2567. {
  2568. int ret = -1;
  2569. if (vpninfo->ssl_fd != -1) {
  2570. struct oc_text_buf *buf = buf_alloc();
  2571. buf_append_ift_hdr(buf, VENDOR_JUNIPER, 0x89);
  2572. if (!buf_error(buf))
  2573. ret = send_ift_packet(vpninfo, buf);
  2574. buf_free(buf);
  2575. openconnect_close_https(vpninfo, 0);
  2576. }
  2577. /* Try Juniper logout if tunnel was already closed */
  2578. if (ret < 0)
  2579. ret = oncp_bye(vpninfo, reason);
  2580. return ret;
  2581. }