gnutls.c 95 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555
  1. /*
  2. * OpenConnect (SSL + DTLS) VPN client
  3. *
  4. * Copyright © 2008-2015 Intel Corporation.
  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 "gnutls.h"
  20. #include <gnutls/gnutls.h>
  21. #include <gnutls/x509.h>
  22. #include <gnutls/crypto.h>
  23. #include <gnutls/pkcs12.h>
  24. #include <gnutls/abstract.h>
  25. #ifdef HAVE_P11KIT
  26. #include <p11-kit/p11-kit.h>
  27. #include <p11-kit/pkcs11.h>
  28. #include <p11-kit/pin.h>
  29. #endif
  30. #include <sys/types.h>
  31. #include <sys/stat.h>
  32. #include <fcntl.h>
  33. #include <string.h>
  34. #include <ctype.h>
  35. #include <stdio.h>
  36. #include <errno.h>
  37. #include <stdarg.h>
  38. #include <stdlib.h>
  39. #if defined(HAVE_P11KIT) || defined(HAVE_GNUTLS_SYSTEM_KEYS)
  40. static int gnutls_pin_callback(void *priv, int attempt, const char *uri,
  41. const char *token_label, unsigned int flags,
  42. char *pin, size_t pin_max);
  43. #endif /* HAVE_P11KIT || HAVE_GNUTLS_SYSTEM_KEYS */
  44. /* GnuTLS 2.x lacked this. But GNUTLS_E_UNEXPECTED_PACKET_LENGTH basically
  45. * does the same thing.
  46. * https://lists.infradead.org/pipermail/openconnect-devel/2014-March/001726.html
  47. */
  48. #ifndef GNUTLS_E_PREMATURE_TERMINATION
  49. #define GNUTLS_E_PREMATURE_TERMINATION GNUTLS_E_UNEXPECTED_PACKET_LENGTH
  50. #endif
  51. /* GnuTLS 3.5.0 added this flag to send a client cert, even if its issuer is
  52. * mismatched to the list of issuers requested by the server. OpenSSL does
  53. * this by default.
  54. * https://github.com/curl/curl/issues/1411
  55. */
  56. #ifndef GNUTLS_FORCE_CLIENT_CERT
  57. #define GNUTLS_FORCE_CLIENT_CERT 0
  58. #endif
  59. static char tls_library_version[32] = "";
  60. const char *openconnect_get_tls_library_version(void)
  61. {
  62. if (!*tls_library_version) {
  63. snprintf(tls_library_version, sizeof(tls_library_version), "GnuTLS %s",
  64. gnutls_check_version(NULL));
  65. }
  66. return tls_library_version;
  67. }
  68. int can_enable_insecure_crypto(void)
  69. {
  70. /* XX: As of GnuTLS 3.6.13, no released version has (yet) removed 3DES/RC4 from default builds,
  71. * but like OpenSSL (removed in 1.1.0) it may happen. */
  72. if (gnutls_cipher_get_id("3DES-CBC") == GNUTLS_CIPHER_UNKNOWN ||
  73. gnutls_cipher_get_id("ARCFOUR-128") == GNUTLS_CIPHER_UNKNOWN)
  74. return -ENOENT;
  75. return 0;
  76. }
  77. /* Helper functions for reading/writing lines over TLS/DTLS. */
  78. static int _openconnect_gnutls_write(gnutls_session_t ses, int fd, struct openconnect_info *vpninfo, char *buf, size_t len)
  79. {
  80. size_t orig_len = len;
  81. while (len) {
  82. int done = gnutls_record_send(ses, buf, len);
  83. if (done > 0)
  84. len -= done;
  85. else if (done == GNUTLS_E_AGAIN || done == GNUTLS_E_INTERRUPTED) {
  86. /* Wait for something to happen on the socket, or on cmd_fd */
  87. fd_set wr_set, rd_set;
  88. int maxfd = fd;
  89. FD_ZERO(&wr_set);
  90. FD_ZERO(&rd_set);
  91. if (gnutls_record_get_direction(ses))
  92. FD_SET(fd, &wr_set);
  93. else
  94. FD_SET(fd, &rd_set);
  95. cmd_fd_set(vpninfo, &rd_set, &maxfd);
  96. if (select(maxfd + 1, &rd_set, &wr_set, NULL, NULL) < 0 &&
  97. errno != EINTR) {
  98. vpn_perror(vpninfo, _("Failed select() for TLS"));
  99. return -EIO;
  100. }
  101. if (is_cancel_pending(vpninfo, &rd_set)) {
  102. vpn_progress(vpninfo, PRG_ERR, _("TLS/DTLS write cancelled\n"));
  103. return -EINTR;
  104. }
  105. } else {
  106. vpn_progress(vpninfo, PRG_ERR, _("Failed to write to TLS/DTLS socket: %s\n"),
  107. gnutls_strerror(done));
  108. return -EIO;
  109. }
  110. }
  111. return orig_len;
  112. }
  113. static int openconnect_gnutls_write(struct openconnect_info *vpninfo, char *buf, size_t len)
  114. {
  115. return _openconnect_gnutls_write(vpninfo->https_sess, vpninfo->ssl_fd, vpninfo, buf, len);
  116. }
  117. int openconnect_dtls_write(struct openconnect_info *vpninfo, void *buf, size_t len)
  118. {
  119. return _openconnect_gnutls_write(vpninfo->dtls_ssl, vpninfo->dtls_fd, vpninfo, buf, len);
  120. }
  121. static int _openconnect_gnutls_read(gnutls_session_t ses, int fd, struct openconnect_info *vpninfo, char *buf, size_t len, unsigned ms)
  122. {
  123. int done, ret;
  124. struct timeval timeout, *tv = NULL;
  125. if (ms) {
  126. timeout.tv_sec = ms/1000;
  127. timeout.tv_usec = (ms%1000)*1000;
  128. tv = &timeout;
  129. }
  130. while ((done = gnutls_record_recv(ses, buf, len)) < 0) {
  131. if (done == GNUTLS_E_AGAIN || done == GNUTLS_E_INTERRUPTED) {
  132. /* Wait for something to happen on the socket, or on cmd_fd */
  133. fd_set wr_set, rd_set;
  134. int maxfd = fd;
  135. FD_ZERO(&wr_set);
  136. FD_ZERO(&rd_set);
  137. if (gnutls_record_get_direction(ses))
  138. FD_SET(fd, &wr_set);
  139. else
  140. FD_SET(fd, &rd_set);
  141. cmd_fd_set(vpninfo, &rd_set, &maxfd);
  142. ret = select(maxfd + 1, &rd_set, &wr_set, NULL, tv);
  143. if (ret < 0 && errno != EINTR) {
  144. vpn_perror(vpninfo, _("Failed select() for TLS/DTLS"));
  145. return -EIO;
  146. }
  147. if (is_cancel_pending(vpninfo, &rd_set)) {
  148. vpn_progress(vpninfo, PRG_ERR, _("TLS/DTLS read cancelled\n"));
  149. done = -EINTR;
  150. goto cleanup;
  151. }
  152. if (ret == 0) {
  153. done = -ETIMEDOUT;
  154. goto cleanup;
  155. }
  156. } else if (done == GNUTLS_E_PREMATURE_TERMINATION) {
  157. /* We've seen this with HTTP 1.0 responses followed by abrupt
  158. socket closure and no clean SSL shutdown.
  159. https://bugs.launchpad.net/bugs/1225276 */
  160. vpn_progress(vpninfo, PRG_DEBUG, _("TLS/DTLS socket closed uncleanly\n"));
  161. done = 0;
  162. goto cleanup;
  163. } else if (done == GNUTLS_E_REHANDSHAKE) {
  164. int ret = cstp_handshake(vpninfo, 0);
  165. if (ret) {
  166. done = ret;
  167. goto cleanup;
  168. }
  169. } else {
  170. vpn_progress(vpninfo, PRG_ERR, _("Failed to read from TLS/DTLS socket: %s\n"),
  171. gnutls_strerror(done));
  172. if (done == GNUTLS_E_TIMEDOUT) {
  173. done = -ETIMEDOUT;
  174. goto cleanup;
  175. } else {
  176. done = -EIO;
  177. goto cleanup;
  178. }
  179. }
  180. }
  181. cleanup:
  182. return done;
  183. }
  184. static int openconnect_gnutls_read(struct openconnect_info *vpninfo, char *buf, size_t len)
  185. {
  186. return _openconnect_gnutls_read(vpninfo->https_sess, vpninfo->ssl_fd, vpninfo, buf, len, 0);
  187. }
  188. int openconnect_dtls_read(struct openconnect_info *vpninfo, void *buf, size_t len, unsigned ms)
  189. {
  190. return _openconnect_gnutls_read(vpninfo->dtls_ssl, vpninfo->dtls_fd, vpninfo, buf, len, ms);
  191. }
  192. static int openconnect_gnutls_gets(struct openconnect_info *vpninfo, char *buf, size_t len)
  193. {
  194. int i = 0;
  195. int ret;
  196. if (len < 2)
  197. return -EINVAL;
  198. while (1) {
  199. ret = gnutls_record_recv(vpninfo->https_sess, buf + i, 1);
  200. if (ret == 1) {
  201. if (buf[i] == '\n') {
  202. buf[i] = 0;
  203. if (i && buf[i-1] == '\r') {
  204. buf[i-1] = 0;
  205. i--;
  206. }
  207. return i;
  208. }
  209. i++;
  210. if (i >= len - 1) {
  211. buf[i] = 0;
  212. return i;
  213. }
  214. } else if (ret == GNUTLS_E_AGAIN || ret == GNUTLS_E_INTERRUPTED) {
  215. /* Wait for something to happen on the socket, or on cmd_fd */
  216. fd_set rd_set, wr_set;
  217. int maxfd = vpninfo->ssl_fd;
  218. FD_ZERO(&rd_set);
  219. FD_ZERO(&wr_set);
  220. if (gnutls_record_get_direction(vpninfo->https_sess))
  221. FD_SET(vpninfo->ssl_fd, &wr_set);
  222. else
  223. FD_SET(vpninfo->ssl_fd, &rd_set);
  224. cmd_fd_set(vpninfo, &rd_set, &maxfd);
  225. if (select(maxfd + 1, &rd_set, &wr_set, NULL, NULL) < 0 &&
  226. errno != EINTR) {
  227. vpn_perror(vpninfo, _("Failed select() for TLS"));
  228. return -EIO;
  229. }
  230. if (is_cancel_pending(vpninfo, &rd_set)) {
  231. vpn_progress(vpninfo, PRG_ERR, _("TLS/DTLS read cancelled\n"));
  232. ret = -EINTR;
  233. break;
  234. }
  235. } else if (ret == GNUTLS_E_REHANDSHAKE) {
  236. ret = cstp_handshake(vpninfo, 0);
  237. if (ret)
  238. return ret;
  239. } else {
  240. vpn_progress(vpninfo, PRG_ERR, _("Failed to read from TLS/DTLS socket: %s\n"),
  241. gnutls_strerror(ret));
  242. ret = -EIO;
  243. break;
  244. }
  245. }
  246. buf[i] = 0;
  247. return i ?: ret;
  248. }
  249. int ssl_nonblock_read(struct openconnect_info *vpninfo, int dtls, void *buf, int maxlen)
  250. {
  251. gnutls_session_t sess = dtls ? vpninfo->dtls_ssl : vpninfo->https_sess;
  252. int ret;
  253. if (!sess) {
  254. vpn_progress(vpninfo, PRG_ERR,
  255. _("Attempted to read from non-existent %s session\n"),
  256. dtls ? "DTLS" : "TLS");
  257. return -1;
  258. }
  259. ret = gnutls_record_recv(sess, buf, maxlen);
  260. if (ret > 0)
  261. return ret;
  262. if (ret == GNUTLS_E_AGAIN || ret == GNUTLS_E_INTERRUPTED)
  263. return 0;
  264. vpn_progress(vpninfo, PRG_ERR, _("Read error on %s session: %s\n"),
  265. dtls ? "DTLS" : "SSL", gnutls_strerror(ret));
  266. return -1;
  267. }
  268. int ssl_nonblock_write(struct openconnect_info *vpninfo, int dtls, void *buf, int buflen)
  269. {
  270. gnutls_session_t sess = dtls ? vpninfo->dtls_ssl : vpninfo->https_sess;
  271. int ret;
  272. if (!sess) {
  273. vpn_progress(vpninfo, PRG_ERR,
  274. _("Attempted to write to non-existent %s session\n"),
  275. dtls ? "DTLS" : "TLS");
  276. return -1;
  277. }
  278. ret = gnutls_record_send(sess, buf, buflen);
  279. if (ret > 0)
  280. return ret;
  281. if (ret == GNUTLS_E_AGAIN || ret == GNUTLS_E_INTERRUPTED) {
  282. /*
  283. * Before 3.3.13, GnuTLS could return zero instead of one,
  284. * indicating that it was waiting for a read when in fact
  285. * it was waiting for a write. That caused us to block for
  286. * ever, waiting for the read that it said it wanted.
  287. *
  288. * So instead, just *assume* it actually wants a write.
  289. * Which is true most of the time, and on the rare occasion
  290. * that it *isn't* true, the failure mode will just be that
  291. * we keep waking up and calling GnuTLS again until the read
  292. * that it's waiting for does arrive.
  293. */
  294. if (GNUTLS_VERSION_NUMBER < 0x03030d ||
  295. gnutls_record_get_direction(sess)) {
  296. /* Waiting for the socket to become writable — it's
  297. probably stalled, and/or the buffers are full */
  298. if (dtls)
  299. monitor_write_fd(vpninfo, dtls);
  300. else
  301. monitor_write_fd(vpninfo, ssl);
  302. }
  303. return 0;
  304. }
  305. vpn_progress(vpninfo, PRG_ERR, _("Write error on %s session: %s\n"),
  306. dtls ? "DTLS" : "SSL", gnutls_strerror(ret));
  307. return -1;
  308. }
  309. static int check_certificate_expiry(struct openconnect_info *vpninfo, struct cert_info *certinfo,
  310. gnutls_x509_crt_t cert)
  311. {
  312. const char *reason = NULL;
  313. time_t expires = gnutls_x509_crt_get_expiration_time(cert);
  314. time_t now = time(NULL);
  315. if (expires == -1) {
  316. vpn_progress(vpninfo, PRG_ERR,
  317. _("Could not extract expiration time of certificate\n"));
  318. return -EINVAL;
  319. }
  320. if (expires < now)
  321. reason = certinfo_string(certinfo, _("Client certificate has expired at"),
  322. _("Secondary client certificate has expired at"));
  323. else if (expires < now + vpninfo->cert_expire_warning)
  324. reason = certinfo_string(certinfo, _("Client certificate expires soon at"),
  325. _("Secondary client certificate expires soon at"));
  326. if (reason) {
  327. char buf[80];
  328. #ifdef _WIN32
  329. /*
  330. * Windows doesn't have gmtime_r but apparently its gmtime()
  331. * *is* thread-safe because it uses a per-thread static buffer.
  332. * cf. https://sourceforge.net/p/mingw/bugs/1625/
  333. *
  334. * We also explicitly say 'GMT' because %Z would give us the
  335. * Microsoft stupidity "GMT Standard Time". Which is not only
  336. * silly, but also ambiguous because Windows actually says that
  337. * even when it means British Summer Time (GMT+1). And having
  338. * used gmtime() we really *are* giving the time in GMT.
  339. */
  340. struct tm *tm = gmtime(&expires);
  341. strftime(buf, 80, "%a, %d %b %Y %H:%M:%S GMT", tm);
  342. #else
  343. struct tm tm;
  344. gmtime_r(&expires, &tm);
  345. strftime(buf, 80, "%a, %d %b %Y %T %Z", &tm);
  346. #endif
  347. vpn_progress(vpninfo, PRG_ERR, "%s: %s\n", reason, buf);
  348. }
  349. return 0;
  350. }
  351. static int load_datum(struct openconnect_info *vpninfo,
  352. gnutls_datum_t *datum, const char *fname)
  353. {
  354. struct stat st;
  355. int fd;
  356. #ifdef ANDROID_KEYSTORE
  357. if (!strncmp(fname, "keystore:", 9)) {
  358. int len;
  359. const char *p = fname + 9;
  360. /* Skip first two slashes if the user has given it as
  361. keystore://foo ... */
  362. if (*p == '/')
  363. p++;
  364. if (*p == '/')
  365. p++;
  366. len = keystore_fetch(p, &datum->data);
  367. if (len <= 0) {
  368. vpn_progress(vpninfo, PRG_ERR,
  369. _("Failed to load item '%s' from keystore: %s\n"),
  370. p, keystore_strerror(len));
  371. return -EINVAL;
  372. }
  373. datum->size = len;
  374. return 0;
  375. }
  376. #endif /* ANDROID_KEYSTORE */
  377. fd = openconnect_open_utf8(vpninfo, fname, O_RDONLY|O_CLOEXEC|O_BINARY);
  378. if (fd == -1) {
  379. vpn_progress(vpninfo, PRG_ERR,
  380. _("Failed to open key/certificate file %s: %s\n"),
  381. fname, strerror(errno));
  382. return -ENOENT;
  383. }
  384. if (fstat(fd, &st)) {
  385. vpn_progress(vpninfo, PRG_ERR,
  386. _("Failed to stat key/certificate file %s: %s\n"),
  387. fname, strerror(errno));
  388. close(fd);
  389. return -EIO;
  390. }
  391. datum->size = st.st_size;
  392. datum->data = gnutls_malloc(st.st_size + 1);
  393. if (!datum->data) {
  394. vpn_progress(vpninfo, PRG_ERR,
  395. _("Failed to allocate certificate buffer\n"));
  396. close(fd);
  397. return -ENOMEM;
  398. }
  399. errno = EAGAIN;
  400. if (read(fd, datum->data, datum->size) != datum->size) {
  401. vpn_progress(vpninfo, PRG_ERR,
  402. _("Failed to read certificate into memory: %s\n"),
  403. strerror(errno));
  404. close(fd);
  405. gnutls_free(datum->data);
  406. return -EIO;
  407. }
  408. datum->data[st.st_size] = 0;
  409. close(fd);
  410. return 0;
  411. }
  412. /* A non-zero, non-error return to make load_certificate() continue and
  413. interpreting the file as other types */
  414. #define NOT_PKCS12 1
  415. static int load_pkcs12_certificate(struct openconnect_info *vpninfo,
  416. struct cert_info *certinfo,
  417. gnutls_datum_t *datum,
  418. gnutls_x509_privkey_t *key,
  419. gnutls_x509_crt_t **chain,
  420. unsigned int *chain_len,
  421. gnutls_x509_crt_t **extra_certs,
  422. unsigned int *extra_certs_len,
  423. gnutls_x509_crl_t *crl)
  424. {
  425. gnutls_pkcs12_t p12;
  426. char *pass;
  427. int err;
  428. err = gnutls_pkcs12_init(&p12);
  429. if (err) {
  430. vpn_progress(vpninfo, PRG_ERR,
  431. _("Failed to setup PKCS#12 data structure: %s\n"),
  432. gnutls_strerror(err));
  433. return -EIO;
  434. }
  435. err = gnutls_pkcs12_import(p12, datum, GNUTLS_X509_FMT_DER, 0);
  436. if (err) {
  437. gnutls_pkcs12_deinit(p12);
  438. return NOT_PKCS12;
  439. }
  440. pass = certinfo->password;
  441. while ((err = gnutls_pkcs12_verify_mac(p12, pass)) == GNUTLS_E_MAC_VERIFY_FAILED) {
  442. if (!pass) {
  443. /* OpenSSL's PKCS12_parse() code will try both NULL and "" automatically,
  444. * but GnuTLS requires two separate attempts. */
  445. err = gnutls_pkcs12_verify_mac(p12, "");
  446. if (!err) {
  447. pass = strdup("");
  448. break;
  449. }
  450. } else
  451. vpn_progress(vpninfo, PRG_ERR,
  452. _("Failed to decrypt PKCS#12 certificate file\n"));
  453. free_pass(&pass);
  454. certinfo->password = NULL;
  455. err = request_passphrase(vpninfo,
  456. certinfo_string(certinfo, "openconnect_pkcs12",
  457. "openconnect_secondary_pkcs12"),
  458. &pass,
  459. certinfo_string(certinfo, _("Enter PKCS#12 pass phrase:"),
  460. _("Enter secondary PKCS#12 pass phrase:")));
  461. if (err) {
  462. gnutls_pkcs12_deinit(p12);
  463. return -EINVAL;
  464. }
  465. }
  466. /* If it wasn't GNUTLS_E_MAC_VERIFY_FAILED, then the problem wasn't just a
  467. bad password. Give up. */
  468. if (err) {
  469. int level = PRG_ERR;
  470. int ret = -EINVAL;
  471. gnutls_pkcs12_deinit(p12);
  472. /* If the first attempt, and we didn't know for sure it was PKCS#12
  473. anyway, bail out and try loading it as something different. */
  474. if (pass == certinfo->password) {
  475. /* Make it non-fatal... */
  476. level = PRG_DEBUG;
  477. ret = NOT_PKCS12;
  478. }
  479. vpn_progress(vpninfo, level,
  480. _("Failed to process PKCS#12 file: %s\n"),
  481. gnutls_strerror(err));
  482. return ret;
  483. }
  484. err = gnutls_pkcs12_simple_parse(p12, pass, key, chain, chain_len,
  485. extra_certs, extra_certs_len, crl, 0);
  486. free_pass(&pass);
  487. certinfo->password = NULL;
  488. gnutls_pkcs12_deinit(p12);
  489. if (err) {
  490. vpn_progress(vpninfo, PRG_ERR,
  491. certinfo_string(certinfo, _("Failed to load PKCS#12 certificate: %s\n"),
  492. _("Failed to load secondary PKCS#12 certificate: %s\n")),
  493. gnutls_strerror(err));
  494. return -EINVAL;
  495. }
  496. return 0;
  497. }
  498. static int count_x509_certificates(gnutls_datum_t *datum)
  499. {
  500. int count = 0;
  501. char *p = (char *)datum->data;
  502. while (p) {
  503. p = strstr(p, "-----BEGIN ");
  504. if (!p)
  505. break;
  506. p += 11;
  507. if (!strncmp(p, "CERTIFICATE", 11) ||
  508. !strncmp(p, "X509 CERTIFICATE", 16))
  509. count++;
  510. }
  511. return count;
  512. }
  513. static int get_cert_name(gnutls_x509_crt_t cert, char *name, size_t namelen)
  514. {
  515. /* When the name buffer is not big enough, gnutls_x509_crt_get_dn*() will
  516. * update the length argument to the required size, and return
  517. * GNUTLS_E_SHORT_MEMORY_BUFFER. We need to avoid clobbering the original
  518. * length variable. */
  519. size_t nl = namelen;
  520. if (gnutls_x509_crt_get_dn_by_oid(cert, GNUTLS_OID_X520_COMMON_NAME,
  521. 0, 0, name, &nl)) {
  522. nl = namelen;
  523. if (gnutls_x509_crt_get_dn(cert, name, &nl)) {
  524. name[namelen-1] = 0;
  525. snprintf(name, namelen-1, "<unknown>");
  526. return -EINVAL;
  527. }
  528. }
  529. return 0;
  530. }
  531. #if defined(HAVE_P11KIT) || defined(HAVE_TROUSERS) || defined(HAVE_TSS2) || defined (HAVE_GNUTLS_SYSTEM_KEYS)
  532. static int verify_signed_data(gnutls_pubkey_t pubkey, gnutls_privkey_t privkey,
  533. gnutls_digest_algorithm_t dig,
  534. const gnutls_datum_t *data, const gnutls_datum_t *sig)
  535. {
  536. gnutls_sign_algorithm_t algo;
  537. unsigned flags = 0;
  538. #ifdef GNUTLS_VERIFY_ALLOW_BROKEN
  539. flags |= GNUTLS_VERIFY_ALLOW_BROKEN;
  540. #endif
  541. algo = gnutls_pk_to_sign(gnutls_privkey_get_pk_algorithm(privkey, NULL),
  542. dig);
  543. return gnutls_pubkey_verify_data2(pubkey, algo, flags, data, sig);
  544. }
  545. #endif /* (P11KIT || TROUSERS || TSS2 || SYSTEM_KEYS) */
  546. static int openssl_hash_password(struct openconnect_info *vpninfo, char *pass,
  547. gnutls_datum_t *key, gnutls_datum_t *salt)
  548. {
  549. unsigned char md5[16];
  550. gnutls_hash_hd_t hash;
  551. int count = 0;
  552. int err;
  553. while (count < key->size) {
  554. err = gnutls_hash_init(&hash, GNUTLS_DIG_MD5);
  555. if (err) {
  556. vpn_progress(vpninfo, PRG_ERR,
  557. _("Could not initialise MD5 hash: %s\n"),
  558. gnutls_strerror(err));
  559. return -EIO;
  560. }
  561. if (count) {
  562. err = gnutls_hash(hash, md5, sizeof(md5));
  563. if (err) {
  564. hash_err:
  565. gnutls_hash_deinit(hash, NULL);
  566. vpn_progress(vpninfo, PRG_ERR,
  567. _("MD5 hash error: %s\n"),
  568. gnutls_strerror(err));
  569. return -EIO;
  570. }
  571. }
  572. if (pass) {
  573. err = gnutls_hash(hash, pass, strlen(pass));
  574. if (err)
  575. goto hash_err;
  576. }
  577. /* We only use the first 8 bytes of the salt for this */
  578. err = gnutls_hash(hash, salt->data, 8);
  579. if (err)
  580. goto hash_err;
  581. gnutls_hash_deinit(hash, md5);
  582. if (key->size - count <= sizeof(md5)) {
  583. memcpy(&key->data[count], md5, key->size - count);
  584. break;
  585. }
  586. memcpy(&key->data[count], md5, sizeof(md5));
  587. count += sizeof(md5);
  588. }
  589. return 0;
  590. }
  591. static int import_openssl_pem(struct openconnect_info *vpninfo, struct cert_info *certinfo,
  592. gnutls_x509_privkey_t key,
  593. char type, char *pem_header, size_t pem_size)
  594. {
  595. gnutls_cipher_hd_t handle;
  596. gnutls_cipher_algorithm_t cipher;
  597. gnutls_datum_t constructed_pem;
  598. gnutls_datum_t b64_data;
  599. gnutls_datum_t salt, enc_key;
  600. unsigned char *key_data;
  601. const char *begin;
  602. char *pass, *p;
  603. char *pem_start = pem_header;
  604. int ret, err, i;
  605. if (type == 'E')
  606. begin = "EC PRIVATE KEY";
  607. else if (type == 'R')
  608. begin = "RSA PRIVATE KEY";
  609. else if (type == 'D')
  610. begin = "DSA PRIVATE KEY";
  611. else
  612. return -EINVAL;
  613. while (*pem_header == '\r' || *pem_header == '\n')
  614. pem_header++;
  615. if (strncmp(pem_header, "DEK-Info: ", 10)) {
  616. vpn_progress(vpninfo, PRG_ERR,
  617. _("Missing DEK-Info: header from OpenSSL encrypted key\n"));
  618. return -EIO;
  619. }
  620. pem_header += 10;
  621. p = strchr(pem_header, ',');
  622. if (!p) {
  623. vpn_progress(vpninfo, PRG_ERR,
  624. _("Cannot determine PEM encryption type\n"));
  625. return -EINVAL;
  626. }
  627. *p = 0;
  628. cipher = gnutls_cipher_get_id(pem_header);
  629. /* GnuTLS calls this '3DES-CBC' but all other names match */
  630. if (cipher == GNUTLS_CIPHER_UNKNOWN &&
  631. !strcmp(pem_header, "DES-EDE3-CBC"))
  632. cipher = GNUTLS_CIPHER_3DES_CBC;
  633. if (cipher == GNUTLS_CIPHER_UNKNOWN) {
  634. vpn_progress(vpninfo, PRG_ERR,
  635. _("Unsupported PEM encryption type: %s\n"),
  636. pem_header);
  637. return -EINVAL;
  638. }
  639. pem_header = p + 1;
  640. /* No supported algorithms have an IV larger than this, and dynamically
  641. allocating it would be painful. */
  642. salt.size = 64;
  643. salt.data = malloc(salt.size);
  644. if (!salt.data)
  645. return -ENOMEM;
  646. for (i = 0; i < salt.size * 2; i++) {
  647. unsigned char x;
  648. char *c = &pem_header[i];
  649. if (*c >= '0' && *c <= '9')
  650. x = (*c) - '0';
  651. else if (*c >= 'A' && *c <= 'F')
  652. x = (*c) - 'A' + 10;
  653. else if ((*c == '\r' || *c == '\n') && i >= 16 && !(i % 16)) {
  654. salt.size = i / 2;
  655. break;
  656. } else {
  657. vpn_progress(vpninfo, PRG_ERR,
  658. _("Invalid salt in encrypted PEM file\n"));
  659. ret = -EINVAL;
  660. goto out_salt;
  661. }
  662. if (i & 1)
  663. salt.data[i/2] |= x;
  664. else
  665. salt.data[i/2] = x << 4;
  666. }
  667. pem_header += salt.size * 2;
  668. if (*pem_header != '\r' && *pem_header != '\n') {
  669. vpn_progress(vpninfo, PRG_ERR,
  670. _("Invalid salt in encrypted PEM file\n"));
  671. ret = -EINVAL;
  672. goto out_salt;
  673. }
  674. while (*pem_header == '\n' || *pem_header == '\r')
  675. pem_header++;
  676. /* pem_header should now point to the start of the base64 content.
  677. Put a -----BEGIN banner in place before it, so that we can use
  678. gnutls_pem_base64_decode_alloc(). The banner has to match the
  679. -----END banner, so make sure we get it right... */
  680. pem_header -= 6;
  681. memcpy(pem_header, "-----\n", 6);
  682. pem_header -= strlen(begin);
  683. memcpy(pem_header, begin, strlen(begin));
  684. pem_header -= 11;
  685. memcpy(pem_header, "-----BEGIN ", 11);
  686. constructed_pem.data = (void *)pem_header;
  687. constructed_pem.size = pem_size - (pem_header - pem_start);
  688. err = gnutls_pem_base64_decode_alloc(begin, &constructed_pem, &b64_data);
  689. if (err) {
  690. vpn_progress(vpninfo, PRG_ERR,
  691. _("Error base64-decoding encrypted PEM file: %s\n"),
  692. gnutls_strerror(err));
  693. ret = -EINVAL;
  694. goto out_salt;
  695. }
  696. if (b64_data.size < 16) {
  697. /* Just to be sure our parsing is OK */
  698. vpn_progress(vpninfo, PRG_ERR,
  699. _("Encrypted PEM file too short\n"));
  700. ret = -EINVAL;
  701. goto out_b64;
  702. }
  703. ret = -ENOMEM;
  704. enc_key.size = gnutls_cipher_get_key_size(cipher);
  705. enc_key.data = malloc(enc_key.size);
  706. if (!enc_key.data)
  707. goto out_b64;
  708. key_data = malloc(b64_data.size);
  709. if (!key_data)
  710. goto out_enc_key;
  711. pass = certinfo->password;
  712. certinfo->password = NULL;
  713. while (1) {
  714. memcpy(key_data, b64_data.data, b64_data.size);
  715. ret = openssl_hash_password(vpninfo, pass, &enc_key, &salt);
  716. if (ret)
  717. goto out;
  718. err = gnutls_cipher_init(&handle, cipher, &enc_key, &salt);
  719. if (err) {
  720. vpn_progress(vpninfo, PRG_ERR,
  721. _("Failed to initialise cipher for decrypting PEM file: %s\n"),
  722. gnutls_strerror(err));
  723. gnutls_cipher_deinit(handle);
  724. ret = -EIO;
  725. goto out;
  726. }
  727. err = gnutls_cipher_decrypt(handle, key_data, b64_data.size);
  728. gnutls_cipher_deinit(handle);
  729. if (err) {
  730. vpn_progress(vpninfo, PRG_ERR,
  731. _("Failed to decrypt PEM key: %s\n"),
  732. gnutls_strerror(err));
  733. ret = -EIO;
  734. goto out;
  735. }
  736. /* We have to strip any padding for GnuTLS to accept it.
  737. So a bit more ASN.1 parsing for us.
  738. FIXME: Consolidate with similar code in gnutls_tpm.c */
  739. if (key_data[0] == 0x30) {
  740. gnutls_datum_t key_datum;
  741. int blocksize = gnutls_cipher_get_block_size(cipher);
  742. int keylen = key_data[1];
  743. int ofs = 2;
  744. if (keylen & 0x80) {
  745. int lenlen = keylen & 0x7f;
  746. keylen = 0;
  747. if (lenlen > 3)
  748. goto fail;
  749. while (lenlen) {
  750. keylen <<= 8;
  751. keylen |= key_data[ofs++];
  752. lenlen--;
  753. }
  754. }
  755. keylen += ofs;
  756. /* If there appears to be more or less padding than required, fail */
  757. if (b64_data.size - keylen > blocksize || b64_data.size < keylen + 1)
  758. goto fail;
  759. /* If the padding bytes aren't all equal to the amount of padding, fail */
  760. ofs = keylen;
  761. while (ofs < b64_data.size) {
  762. if (key_data[ofs] != b64_data.size - keylen)
  763. goto fail;
  764. ofs++;
  765. }
  766. key_datum.data = key_data;
  767. key_datum.size = keylen;
  768. err = gnutls_x509_privkey_import(key, &key_datum, GNUTLS_X509_FMT_DER);
  769. if (!err) {
  770. ret = 0;
  771. goto out;
  772. }
  773. }
  774. fail:
  775. if (pass) {
  776. vpn_progress(vpninfo, PRG_ERR, _("Decrypting PEM key failed\n"));
  777. free_pass(&pass);
  778. }
  779. err = request_passphrase(vpninfo,
  780. certinfo_string(certinfo, "openconnect_pem",
  781. "openconnect_secondary_pem"),
  782. &pass,
  783. certinfo_string(certinfo, _("Enter PEM pass phrase:"),
  784. _("Enter secondary PEM pass phrase:")));
  785. if (err) {
  786. ret = -EINVAL;
  787. goto out;
  788. }
  789. }
  790. out:
  791. free(key_data);
  792. free_pass(&pass);
  793. out_enc_key:
  794. free(enc_key.data);
  795. out_b64:
  796. free(b64_data.data);
  797. out_salt:
  798. free(salt.data);
  799. return ret;
  800. }
  801. static void fill_token_info(char *buf, size_t s, unsigned char *dst, size_t dstlen)
  802. {
  803. if (s && !gtls_ver(3,6,0))
  804. s--;
  805. if (s > dstlen)
  806. s = dstlen;
  807. memcpy(dst, buf, s);
  808. if (s < dstlen)
  809. memset(dst + s, ' ', dstlen - s);
  810. }
  811. struct gtls_cert_info {
  812. gnutls_x509_crl_t crl;
  813. gnutls_privkey_t pkey;
  814. gnutls_x509_crt_t *certs;
  815. unsigned int nr_certs;
  816. };
  817. void unload_certificate(struct cert_info *certinfo, int final)
  818. {
  819. if (!certinfo)
  820. return;
  821. if (certinfo->priv_info) {
  822. struct gtls_cert_info *gci = certinfo->priv_info;
  823. certinfo->priv_info = NULL;
  824. gnutls_x509_crl_deinit(gci->crl);
  825. gnutls_privkey_deinit(gci->pkey);
  826. for (size_t i = 0, end = gci->nr_certs; i < end; i++)
  827. gnutls_x509_crt_deinit(gci->certs[i]);
  828. gnutls_free(gci->certs);
  829. free(gci);
  830. }
  831. if (final) {
  832. #if defined(OPENCONNECT_GNUTLS) && defined(HAVE_TROUSERS)
  833. release_tpm1_ctx(certinfo->vpninfo, certinfo);
  834. #endif
  835. #if defined(OPENCONNECT_GNUTLS) && defined(HAVE_TSS2)
  836. release_tpm2_ctx(certinfo->vpninfo, certinfo);
  837. #endif
  838. }
  839. }
  840. static int import_cert(gnutls_x509_crt_t *cert, const gnutls_datum_t *der)
  841. {
  842. gnutls_x509_crt_t crt = NULL;
  843. int ret;
  844. if (!cert)
  845. return GNUTLS_E_INVALID_REQUEST;
  846. ret = gnutls_x509_crt_init(&crt);
  847. if (ret < 0)
  848. goto done;
  849. ret = gnutls_x509_crt_import(crt, der, GNUTLS_X509_FMT_DER);
  850. if (ret < 0) {
  851. gnutls_x509_crt_deinit(crt);
  852. crt = NULL;
  853. }
  854. done:
  855. *cert = crt;
  856. return ret;
  857. }
  858. static int copy_cert(gnutls_x509_crt_t *cert_copy, gnutls_x509_crt_t cert)
  859. {
  860. gnutls_datum_t data = { NULL, 0 };
  861. gnutls_x509_crt_t copy = NULL;
  862. int ret;
  863. if (!cert_copy)
  864. return GNUTLS_E_INVALID_REQUEST;
  865. ret = gnutls_x509_crt_export2(cert, GNUTLS_X509_FMT_DER, &data);
  866. if (ret < 0)
  867. goto done;
  868. ret = import_cert(&copy, &data);
  869. gnutls_free(data.data);
  870. done:
  871. *cert_copy = copy;
  872. return ret;
  873. }
  874. static int check_multicert_compat(struct openconnect_info *vpninfo, struct cert_info *certinfo);
  875. int load_certificate(struct openconnect_info *vpninfo, struct cert_info *certinfo, int flags)
  876. {
  877. gnutls_datum_t fdata;
  878. #if defined(HAVE_P11KIT) || defined(HAVE_TROUSERS) || defined(HAVE_TSS2) || defined(HAVE_GNUTLS_SYSTEM_KEYS)
  879. gnutls_datum_t pkey_sig = {NULL, 0};
  880. void *dummy_hash_data = &load_certificate;
  881. #endif
  882. #if defined(HAVE_P11KIT) || defined(HAVE_GNUTLS_SYSTEM_KEYS)
  883. char *cert_url = (char *)certinfo->cert;
  884. #endif
  885. #ifdef HAVE_P11KIT
  886. char *key_url = (char *)certinfo->key;
  887. gnutls_pkcs11_privkey_t p11key = NULL;
  888. #endif
  889. char *pem_header;
  890. gnutls_x509_crt_t last_cert, cert = NULL;
  891. gnutls_x509_crt_t *extra_certs = NULL;
  892. unsigned int nr_extra_certs = 0;
  893. int err; /* GnuTLS error */
  894. int ret;
  895. int i;
  896. int cert_is_p11 = 0, key_is_p11 = 0;
  897. int cert_is_sys = 0, key_is_sys = 0;
  898. unsigned char key_id[20];
  899. size_t key_id_size = sizeof(key_id);
  900. char name[80];
  901. gnutls_x509_privkey_t x509key = NULL;
  902. struct gtls_cert_info *gci = NULL;
  903. certinfo->vpninfo = vpninfo;
  904. fdata.data = NULL;
  905. key_is_p11 = !strncmp(certinfo->key, "pkcs11:", 7);
  906. cert_is_p11 = !strncmp(certinfo->cert, "pkcs11:", 7);
  907. /* GnuTLS returns true for pkcs11:, tpmkey:, system:, and custom URLs. */
  908. key_is_sys = !key_is_p11 && gnutls_url_is_supported(certinfo->key);
  909. cert_is_sys = !cert_is_p11 && gnutls_url_is_supported(certinfo->cert);
  910. #ifndef HAVE_GNUTLS_SYSTEM_KEYS
  911. if (key_is_sys || cert_is_sys) {
  912. vpn_progress(vpninfo, PRG_ERR,
  913. _("This binary built without system key support\n"));
  914. return -EINVAL;
  915. }
  916. #endif
  917. #ifndef HAVE_P11KIT
  918. if (key_is_p11 || cert_is_p11) {
  919. vpn_progress(vpninfo, PRG_ERR,
  920. _("This binary built without PKCS#11 support\n"));
  921. return -EINVAL;
  922. }
  923. #else
  924. /* Install PIN handler if either certificate or key are coming from PKCS#11 */
  925. if (key_is_p11 || cert_is_p11) {
  926. CK_OBJECT_CLASS class;
  927. CK_ATTRIBUTE attr;
  928. P11KitUri *uri;
  929. uri = p11_kit_uri_new();
  930. attr.type = CKA_CLASS;
  931. attr.pValue = &class;
  932. attr.ulValueLen = sizeof(class);
  933. /* Add appropriate pin-source and object-type attributes to
  934. both certificate and key URLs, unless they already exist. */
  935. if (cert_is_p11 &&
  936. !p11_kit_uri_parse(cert_url, P11_KIT_URI_FOR_ANY, uri)) {
  937. if (!p11_kit_uri_get_attribute(uri, CKA_CLASS)) {
  938. class = CKO_CERTIFICATE;
  939. p11_kit_uri_set_attribute(uri, &attr);
  940. }
  941. p11_kit_uri_format(uri, P11_KIT_URI_FOR_ANY, &cert_url);
  942. }
  943. if (key_is_p11 &&
  944. !p11_kit_uri_parse(key_url, P11_KIT_URI_FOR_ANY, uri)) {
  945. if (certinfo->key == certinfo->cert ||
  946. !p11_kit_uri_get_attribute(uri, CKA_CLASS)) {
  947. class = CKO_PRIVATE_KEY;
  948. p11_kit_uri_set_attribute(uri, &attr);
  949. }
  950. p11_kit_uri_format(uri, P11_KIT_URI_FOR_ANY, &key_url);
  951. }
  952. p11_kit_uri_free(uri);
  953. }
  954. #endif /* HAVE_PKCS11 */
  955. certinfo->priv_info = gci = calloc(1, sizeof(*gci));
  956. if (!gci) {
  957. ret = -ENOMEM;
  958. goto out;
  959. }
  960. #if defined (HAVE_P11KIT) || defined(HAVE_GNUTLS_SYSTEM_KEYS)
  961. /* Load certificate(s) first... */
  962. if (cert_is_p11 || cert_is_sys) {
  963. vpn_progress(vpninfo, PRG_DEBUG,
  964. cert_is_p11 ? _("Using PKCS#11 certificate %s\n") :
  965. _("Using system certificate %s\n"), cert_url);
  966. err = gnutls_x509_crt_init(&cert);
  967. if (err) {
  968. ret = -ENOMEM;
  969. goto out;
  970. }
  971. gnutls_x509_crt_set_pin_function(cert, gnutls_pin_callback, certinfo);
  972. /* Yes, even for *system* URLs the only API GnuTLS offers us is
  973. ...import_pkcs11_url(). */
  974. err = gnutls_x509_crt_import_pkcs11_url(cert, cert_url, 0);
  975. if (err == GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE)
  976. err = gnutls_x509_crt_import_pkcs11_url(cert, cert_url,
  977. GNUTLS_PKCS11_OBJ_FLAG_LOGIN);
  978. if (err) {
  979. vpn_progress(vpninfo, PRG_ERR,
  980. cert_is_p11 ? _("Error loading certificate from PKCS#11: %s\n") :
  981. _("Error loading system certificate: %s\n"),
  982. gnutls_strerror(err));
  983. ret = -EIO;
  984. goto out;
  985. }
  986. goto got_certs;
  987. }
  988. #endif /* HAVE_P11KIT || HAVE_GNUTLS_SYSTEM_KEYS */
  989. /* OK, not a PKCS#11 certificate so it must be coming from a file... */
  990. vpn_progress(vpninfo, PRG_DEBUG,
  991. certinfo_string(certinfo, _("Using certificate file %s\n"),
  992. _("Using secondary certificate file %s\n")),
  993. certinfo->cert);
  994. /* Load file contents */
  995. ret = load_datum(vpninfo, &fdata, certinfo->cert);
  996. if (ret)
  997. return ret;
  998. /* Is it PKCS#12? */
  999. if (!key_is_p11) {
  1000. /* PKCS#12 should actually contain certificates *and* private key */
  1001. ret = load_pkcs12_certificate(vpninfo, certinfo, &fdata, &x509key,
  1002. &gci->certs, &gci->nr_certs,
  1003. &extra_certs, &nr_extra_certs,
  1004. &gci->crl);
  1005. if (ret < 0)
  1006. goto out;
  1007. else if (!ret) {
  1008. if (gci->nr_certs) {
  1009. cert = gci->certs[0];
  1010. goto got_key;
  1011. }
  1012. vpn_progress(vpninfo, PRG_ERR,
  1013. _("PKCS#11 file contained no certificate\n"));
  1014. ret = -EINVAL;
  1015. goto out;
  1016. }
  1017. /* It returned NOT_PKCS12.
  1018. Fall through to try PEM formats. */
  1019. }
  1020. /* We need to know how many there are in *advance*; it won't just allocate
  1021. the array for us :( */
  1022. nr_extra_certs = count_x509_certificates(&fdata);
  1023. if (!nr_extra_certs)
  1024. nr_extra_certs = 1; /* wtf? Oh well, we'll fail later... */
  1025. extra_certs = gnutls_calloc(nr_extra_certs, sizeof(cert));
  1026. if (!extra_certs) {
  1027. nr_extra_certs = 0;
  1028. ret = -ENOMEM;
  1029. goto out;
  1030. }
  1031. err = gnutls_x509_crt_list_import(extra_certs, &nr_extra_certs, &fdata,
  1032. GNUTLS_X509_FMT_PEM, 0);
  1033. if (err <= 0) {
  1034. const char *reason;
  1035. if (!err || err == GNUTLS_E_NO_CERTIFICATE_FOUND)
  1036. reason = _("No certificate found in file");
  1037. else
  1038. reason = gnutls_strerror(err);
  1039. vpn_progress(vpninfo, PRG_ERR,
  1040. certinfo_string(certinfo, _("Loading certificate failed: %s\n"),
  1041. _("Loading secondary certificate failed: %s\n")),
  1042. reason);
  1043. nr_extra_certs = 0;
  1044. ret = -EINVAL;
  1045. goto out;
  1046. }
  1047. nr_extra_certs = err;
  1048. goto got_certs;
  1049. got_certs:
  1050. /* Now we have either a single certificate in 'cert', or an array of
  1051. them in extra_certs[]. Next we look for the private key ... */
  1052. #ifdef HAVE_GNUTLS_SYSTEM_KEYS
  1053. if (key_is_sys) {
  1054. vpn_progress(vpninfo, PRG_DEBUG,
  1055. certinfo_string(certinfo, _("Using system key %s\n"),
  1056. _("Using secondary system key %s\n")),
  1057. certinfo->key);
  1058. err = gnutls_privkey_init(&gci->pkey);
  1059. if (err) {
  1060. vpn_progress(vpninfo, PRG_ERR,
  1061. _("Error initialising private key structure: %s\n"),
  1062. gnutls_strerror(err));
  1063. ret = -EIO;
  1064. goto out;
  1065. }
  1066. gnutls_privkey_set_pin_function(gci->pkey, gnutls_pin_callback, certinfo);
  1067. err = gnutls_privkey_import_url(gci->pkey, certinfo->key, 0);
  1068. if (err) {
  1069. vpn_progress(vpninfo, PRG_ERR,
  1070. _("Error importing system key %s: %s\n"),
  1071. certinfo->key, gnutls_strerror(err));
  1072. ret = -EIO;
  1073. goto out;
  1074. }
  1075. goto match_cert;
  1076. }
  1077. #endif /* HAVE_GNUTLS_SYSTEM_KEYS */
  1078. #if defined(HAVE_P11KIT)
  1079. if (key_is_p11) {
  1080. vpn_progress(vpninfo, PRG_TRACE,
  1081. _("Trying PKCS#11 key URL %s\n"), key_url);
  1082. err = gnutls_pkcs11_privkey_init(&p11key);
  1083. if (err) {
  1084. vpn_progress(vpninfo, PRG_ERR,
  1085. _("Error initialising PKCS#11 key structure: %s\n"),
  1086. gnutls_strerror(err));
  1087. ret = -EIO;
  1088. goto out;
  1089. }
  1090. gnutls_pkcs11_privkey_set_pin_function(p11key, gnutls_pin_callback, certinfo);
  1091. err = gnutls_pkcs11_privkey_import_url(p11key, key_url, 0);
  1092. /* Annoyingly, some tokens don't even admit the *existence* of
  1093. the key until they're logged in. And thus a search doesn't
  1094. work unless it specifies the *token* too. But if the URI for
  1095. key and cert are the same, and the cert was found, then we
  1096. can work out what token the *cert* was found in and try that
  1097. before we give up... */
  1098. if (err == GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE &&
  1099. certinfo->cert == certinfo->key) {
  1100. gnutls_pkcs11_obj_t crt;
  1101. P11KitUri *uri;
  1102. CK_TOKEN_INFO *token;
  1103. char buf[65];
  1104. size_t s;
  1105. if (gnutls_pkcs11_obj_init(&crt))
  1106. goto key_err;
  1107. if (gnutls_pkcs11_obj_import_url(crt, cert_url, 0))
  1108. goto key_err_obj;
  1109. uri = p11_kit_uri_new();
  1110. if (!uri)
  1111. goto key_err_obj;
  1112. if (p11_kit_uri_parse(key_url, P11_KIT_URI_FOR_ANY, uri))
  1113. goto key_err_uri;
  1114. token = p11_kit_uri_get_token_info(uri);
  1115. if (!token)
  1116. goto key_err_uri;
  1117. if (!token->label[0]) {
  1118. s = sizeof(token->label) + 1;
  1119. if (!gnutls_pkcs11_obj_get_info(crt, GNUTLS_PKCS11_OBJ_TOKEN_LABEL,
  1120. buf, &s))
  1121. fill_token_info(buf, s, token->label, sizeof(token->label));
  1122. }
  1123. if (!token->manufacturerID[0]) {
  1124. s = sizeof(token->manufacturerID) + 1;
  1125. if (!gnutls_pkcs11_obj_get_info(crt, GNUTLS_PKCS11_OBJ_TOKEN_MANUFACTURER,
  1126. buf, &s))
  1127. fill_token_info(buf, s, token->manufacturerID, sizeof(token->manufacturerID));
  1128. }
  1129. if (!token->model[0]) {
  1130. s = sizeof(token->model) + 1;
  1131. if (!gnutls_pkcs11_obj_get_info(crt, GNUTLS_PKCS11_OBJ_TOKEN_MODEL,
  1132. buf, &s))
  1133. fill_token_info(buf, s, token->model, sizeof(token->model));
  1134. }
  1135. if (!token->serialNumber[0]) {
  1136. s = sizeof(token->serialNumber) + 1;
  1137. if (!gnutls_pkcs11_obj_get_info(crt, GNUTLS_PKCS11_OBJ_TOKEN_SERIAL,
  1138. buf, &s))
  1139. fill_token_info(buf, s, token->serialNumber, sizeof(token->serialNumber));
  1140. }
  1141. free(key_url);
  1142. key_url = NULL;
  1143. if (p11_kit_uri_format(uri, P11_KIT_URI_FOR_ANY, &key_url))
  1144. goto key_err_uri;
  1145. vpn_progress(vpninfo, PRG_TRACE,
  1146. _("Trying PKCS#11 key URL %s\n"), key_url);
  1147. err = gnutls_pkcs11_privkey_import_url(p11key, key_url, 0);
  1148. /* If it still doesn't work then try dropping CKA_LABEL and adding the
  1149. CKA_ID of the cert. */
  1150. if (err == GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE &&
  1151. (p11_kit_uri_get_attribute(uri, CKA_LABEL) ||
  1152. !p11_kit_uri_get_attribute(uri, CKA_ID))) {
  1153. CK_ATTRIBUTE attr;
  1154. s = sizeof(buf);
  1155. if (gnutls_pkcs11_obj_get_info(crt, GNUTLS_PKCS11_OBJ_ID,
  1156. buf, &s))
  1157. goto key_err_uri;
  1158. attr.type = CKA_ID;
  1159. attr.pValue = buf;
  1160. attr.ulValueLen = s;
  1161. p11_kit_uri_set_attribute(uri, &attr);
  1162. p11_kit_uri_clear_attribute(uri, CKA_LABEL);
  1163. free(key_url);
  1164. key_url = NULL;
  1165. if (p11_kit_uri_format(uri, P11_KIT_URI_FOR_ANY, &key_url))
  1166. goto key_err_uri;
  1167. vpn_progress(vpninfo, PRG_TRACE,
  1168. _("Trying PKCS#11 key URL %s\n"), key_url);
  1169. err = gnutls_pkcs11_privkey_import_url(p11key, key_url, 0);
  1170. }
  1171. key_err_uri:
  1172. p11_kit_uri_free(uri);
  1173. key_err_obj:
  1174. gnutls_pkcs11_obj_deinit(crt);
  1175. key_err:
  1176. ;
  1177. }
  1178. if (err) {
  1179. vpn_progress(vpninfo, PRG_ERR,
  1180. _("Error importing PKCS#11 URL %s: %s\n"),
  1181. key_url, gnutls_strerror(err));
  1182. gnutls_pkcs11_privkey_deinit(p11key);
  1183. ret = -EIO;
  1184. goto out;
  1185. }
  1186. vpn_progress(vpninfo, PRG_DEBUG,
  1187. _("Using PKCS#11 key %s\n"), key_url);
  1188. err = gnutls_privkey_init(&gci->pkey);
  1189. if (err) {
  1190. vpn_progress(vpninfo, PRG_ERR,
  1191. _("Error initialising private key structure: %s\n"),
  1192. gnutls_strerror(err));
  1193. gnutls_pkcs11_privkey_deinit(p11key);
  1194. ret = -EIO;
  1195. goto out;
  1196. }
  1197. err = gnutls_privkey_import_pkcs11(gci->pkey, p11key, GNUTLS_PRIVKEY_IMPORT_AUTO_RELEASE);
  1198. if (err) {
  1199. vpn_progress(vpninfo, PRG_ERR,
  1200. _("Error importing PKCS#11 key into private key structure: %s\n"),
  1201. gnutls_strerror(err));
  1202. gnutls_pkcs11_privkey_deinit(p11key);
  1203. ret = -EIO;
  1204. goto out;
  1205. }
  1206. goto match_cert;
  1207. }
  1208. #endif /* HAVE_P11KIT */
  1209. /* OK, not a PKCS#11 key so it must be coming from a file... load the
  1210. file into memory, unless it's the same as the cert file and we
  1211. already loaded that. */
  1212. if (!fdata.data || certinfo->key != certinfo->cert) {
  1213. gnutls_free(fdata.data);
  1214. fdata.data = NULL;
  1215. vpn_progress(vpninfo, PRG_DEBUG,
  1216. _("Using private key file %s\n"), certinfo->key);
  1217. ret = load_datum(vpninfo, &fdata, certinfo->key);
  1218. if (ret)
  1219. goto out;
  1220. }
  1221. /* Is it a PEM file with a TPM key blob? */
  1222. if (strstr((char *)fdata.data, "-----BEGIN TSS KEY BLOB-----")) {
  1223. #ifndef HAVE_TROUSERS
  1224. vpn_progress(vpninfo, PRG_ERR,
  1225. _("This version of OpenConnect was built without TPM support\n"));
  1226. return -EINVAL;
  1227. #else
  1228. ret = load_tpm1_key(vpninfo, certinfo, &fdata, &gci->pkey, &pkey_sig);
  1229. if (ret)
  1230. goto out;
  1231. goto match_cert;
  1232. #endif
  1233. }
  1234. /* Is it a PEM file with a TPM key blob? */
  1235. if (strstr((char *)fdata.data, "-----BEGIN TSS2 PRIVATE KEY-----") ||
  1236. strstr((char *)fdata.data, "-----BEGIN TSS2 KEY BLOB-----")) {
  1237. #ifndef HAVE_TSS2
  1238. vpn_progress(vpninfo, PRG_ERR,
  1239. _("This version of OpenConnect was built without TPM2 support\n"));
  1240. return -EINVAL;
  1241. #else
  1242. ret = load_tpm2_key(vpninfo, certinfo, &fdata, &gci->pkey, &pkey_sig);
  1243. if (ret)
  1244. goto out;
  1245. goto match_cert;
  1246. #endif
  1247. }
  1248. /* OK, try other PEM files... */
  1249. gnutls_x509_privkey_init(&x509key);
  1250. if ((pem_header = strstr((char *)fdata.data, "-----BEGIN RSA PRIVATE KEY-----")) ||
  1251. (pem_header = strstr((char *)fdata.data, "-----BEGIN DSA PRIVATE KEY-----")) ||
  1252. (pem_header = strstr((char *)fdata.data, "-----BEGIN EC PRIVATE KEY-----"))) {
  1253. /* PKCS#1 files, including OpenSSL's odd encrypted version */
  1254. char type = pem_header[11];
  1255. char *p = strchr(pem_header, '\n');
  1256. if (!p) {
  1257. vpn_progress(vpninfo, PRG_ERR,
  1258. _("Failed to interpret PEM file\n"));
  1259. ret = -EINVAL;
  1260. goto out;
  1261. }
  1262. while (*p == '\n' || *p == '\r')
  1263. p++;
  1264. if (!strncmp(p, "Proc-Type: 4,ENCRYPTED", 22)) {
  1265. p += 22;
  1266. while (*p == '\n' || *p == '\r')
  1267. p++;
  1268. ret = import_openssl_pem(vpninfo, certinfo, x509key, type, p,
  1269. fdata.size - (p - (char *)fdata.data));
  1270. if (ret)
  1271. goto out;
  1272. } else {
  1273. err = gnutls_x509_privkey_import(x509key, &fdata, GNUTLS_X509_FMT_PEM);
  1274. if (err) {
  1275. vpn_progress(vpninfo, PRG_ERR,
  1276. _("Failed to load PKCS#1 private key: %s\n"),
  1277. gnutls_strerror(err));
  1278. ret = -EINVAL;
  1279. goto out;
  1280. }
  1281. }
  1282. } else if (strstr((char *)fdata.data, "-----BEGIN PRIVATE KEY-----")) {
  1283. /* Unencrypted PKCS#8 */
  1284. err = gnutls_x509_privkey_import_pkcs8(x509key, &fdata,
  1285. GNUTLS_X509_FMT_PEM,
  1286. NULL, GNUTLS_PKCS_PLAIN);
  1287. if (err) {
  1288. vpn_progress(vpninfo, PRG_ERR,
  1289. _("Failed to load private key as PKCS#8: %s\n"),
  1290. gnutls_strerror(err));
  1291. ret = -EINVAL;
  1292. goto out;
  1293. }
  1294. } else if (strstr((char *)fdata.data, "-----BEGIN ENCRYPTED PRIVATE KEY-----")) {
  1295. /* Encrypted PKCS#8 */
  1296. char *pass = certinfo->password;
  1297. while ((err = gnutls_x509_privkey_import_pkcs8(x509key, &fdata,
  1298. GNUTLS_X509_FMT_PEM,
  1299. pass?:"", 0))) {
  1300. if (err != GNUTLS_E_DECRYPTION_FAILED) {
  1301. vpn_progress(vpninfo, PRG_ERR,
  1302. _("Failed to load private key as PKCS#8: %s\n"),
  1303. gnutls_strerror(err));
  1304. ret = -EINVAL;
  1305. goto out;
  1306. }
  1307. certinfo->password = NULL;
  1308. if (pass) {
  1309. vpn_progress(vpninfo, PRG_ERR,
  1310. _("Failed to decrypt PKCS#8 certificate file\n"));
  1311. free_pass(&pass);
  1312. }
  1313. err = request_passphrase(vpninfo, "openconnect_pem",
  1314. &pass, _("Enter PEM pass phrase:"));
  1315. if (err) {
  1316. ret = -EINVAL;
  1317. goto out;
  1318. }
  1319. }
  1320. free_pass(&pass);
  1321. certinfo->password = NULL;
  1322. } else if (!gnutls_x509_privkey_import(x509key, &fdata, GNUTLS_X509_FMT_DER) ||
  1323. !gnutls_x509_privkey_import_pkcs8(x509key, &fdata, GNUTLS_X509_FMT_DER,
  1324. NULL, GNUTLS_PKCS_PLAIN)) {
  1325. /* Unencrypted DER (PKCS#1 or PKCS#8) */
  1326. } else {
  1327. /* Last chance: try encrypted PKCS#8 DER. And give up if it's not that */
  1328. char *pass = certinfo->password;
  1329. while ((err = gnutls_x509_privkey_import_pkcs8(x509key, &fdata,
  1330. GNUTLS_X509_FMT_DER,
  1331. pass?:"", 0))) {
  1332. if (err != GNUTLS_E_DECRYPTION_FAILED) {
  1333. vpn_progress(vpninfo, PRG_ERR,
  1334. _("Failed to determine type of private key %s\n"),
  1335. certinfo->key);
  1336. ret = -EINVAL;
  1337. goto out;
  1338. }
  1339. certinfo->password = NULL;
  1340. if (pass) {
  1341. vpn_progress(vpninfo, PRG_ERR,
  1342. _("Failed to decrypt PKCS#8 certificate file\n"));
  1343. free_pass(&pass);
  1344. }
  1345. err = request_passphrase(vpninfo, "openconnect_pem",
  1346. &pass, _("Enter PKCS#8 pass phrase:"));
  1347. if (err) {
  1348. ret = -EINVAL;
  1349. goto out;
  1350. }
  1351. }
  1352. free_pass(&pass);
  1353. certinfo->password = NULL;
  1354. }
  1355. /* Now attempt to make sure we use the *correct* certificate, to match
  1356. the key. Since we have a software key, we can easily query it and
  1357. compare its key_id with each certificate till we find a match. */
  1358. err = gnutls_x509_privkey_get_key_id(x509key, 0, key_id, &key_id_size);
  1359. if (err) {
  1360. vpn_progress(vpninfo, PRG_ERR,
  1361. _("Failed to get key ID: %s\n"),
  1362. gnutls_strerror(err));
  1363. ret = -EINVAL;
  1364. goto out;
  1365. }
  1366. /* If extra_certs[] is NULL, we have one candidate in 'cert' to check. */
  1367. for (i = 0; i < (extra_certs ? nr_extra_certs : 1); i++) {
  1368. unsigned char cert_id[20];
  1369. size_t cert_id_size = sizeof(cert_id);
  1370. err = gnutls_x509_crt_get_key_id(extra_certs ? extra_certs[i] : cert, 0, cert_id, &cert_id_size);
  1371. if (err)
  1372. continue;
  1373. if (cert_id_size == key_id_size && !memcmp(cert_id, key_id, key_id_size)) {
  1374. if (extra_certs) {
  1375. cert = extra_certs[i];
  1376. extra_certs[i] = NULL;
  1377. }
  1378. goto got_key;
  1379. }
  1380. }
  1381. /* There's no pkey (there's an x509 key), so even if p11-kit or trousers is
  1382. enabled we'll fall straight through the bit at match_cert: below, and go
  1383. directly to the bit where it prints the 'no match found' error and exits. */
  1384. #if defined(HAVE_P11KIT) || defined(HAVE_TROUSERS) || defined(HAVE_TSS2) || defined(HAVE_GNUTLS_SYSTEM_KEYS)
  1385. match_cert:
  1386. /* If we have a privkey from PKCS#11 or TPM, we can't do the simple comparison
  1387. of key ID that we do for software keys to find which certificate is a
  1388. match. So sign some dummy data and then check the signature against each
  1389. of the available certificates until we find the right one. */
  1390. if (gci->pkey) {
  1391. /* The TPM code may have already signed it, to test authorisation. We
  1392. only sign here for PKCS#11 keys, in which case fdata might be
  1393. empty too so point it at dummy data. We try multiple hashes
  1394. because depending on the algorithm or device not all may be usable */
  1395. unsigned j;
  1396. gnutls_digest_algorithm_t *dig, digs[] = {
  1397. GNUTLS_DIG_SHA256, GNUTLS_DIG_SHA1,
  1398. GNUTLS_DIG_SHA512, GNUTLS_DIG_UNKNOWN
  1399. };
  1400. for (dig = digs; *dig != GNUTLS_DIG_UNKNOWN; dig++) {
  1401. if (!pkey_sig.data) {
  1402. if (!fdata.data) {
  1403. fdata.data = dummy_hash_data;
  1404. fdata.size = 20;
  1405. }
  1406. err = gnutls_privkey_sign_data(gci->pkey, *dig, 0,
  1407. &fdata, &pkey_sig);
  1408. if (err) {
  1409. vpn_progress(vpninfo, PRG_ERR,
  1410. _("Error signing test data with private key: %s\n"),
  1411. gnutls_strerror(err));
  1412. ret = -EINVAL;
  1413. goto out;
  1414. }
  1415. }
  1416. /* If extra_certs[] is NULL, we have one candidate in 'cert' to check. */
  1417. for (j = 0; j < (extra_certs ? nr_extra_certs : 1); j++) {
  1418. gnutls_pubkey_t pubkey = NULL;
  1419. err = gnutls_pubkey_init(&pubkey);
  1420. if (err >= 0)
  1421. err = gnutls_pubkey_import_x509(pubkey, extra_certs ? extra_certs[j] : cert, 0);
  1422. if (err < 0) {
  1423. vpn_progress(vpninfo, PRG_ERR,
  1424. _("Error validating signature against certificate: %s\n"),
  1425. gnutls_strerror(err));
  1426. /* We'll probably fail shortly if we don't find it. */
  1427. gnutls_pubkey_deinit(pubkey);
  1428. continue;
  1429. }
  1430. err = verify_signed_data(pubkey, gci->pkey, *dig,
  1431. &fdata, &pkey_sig);
  1432. gnutls_pubkey_deinit(pubkey);
  1433. if (err >= 0) {
  1434. if (extra_certs) {
  1435. cert = extra_certs[j];
  1436. extra_certs[j] = NULL;
  1437. }
  1438. gnutls_free(pkey_sig.data);
  1439. pkey_sig.data = NULL;
  1440. goto got_key;
  1441. }
  1442. }
  1443. gnutls_free(pkey_sig.data);
  1444. pkey_sig.data = NULL;
  1445. }
  1446. }
  1447. #endif /* P11KIT || TROUSERS || TSS2 || SYSTEM_KEYS */
  1448. /* We shouldn't reach this. It means that we didn't find *any* matching cert */
  1449. vpn_progress(vpninfo, PRG_ERR,
  1450. certinfo_string(certinfo, _("No SSL certificate found to match private key\n"),
  1451. _("No secondary certificate found to match private key\n")));
  1452. ret = -EINVAL;
  1453. goto out;
  1454. /********************************************************************/
  1455. got_key:
  1456. /* Now we have a key in either 'x509key' or 'gci->pkey', a matching cert in 'cert',
  1457. and potentially a list of other certs in 'extra_certs[]'. If we loaded
  1458. a PKCS#12 file we may have a trust chain in 'gci->certs[]' too. */
  1459. if (!((!gci->pkey != !x509key) && cert))
  1460. vpn_progress(vpninfo, PRG_ERR,
  1461. _("got_key conditions not met!\n"));
  1462. /* Transform the x509key to abstract key */
  1463. if (!gci->pkey) {
  1464. err = gnutls_privkey_init(&gci->pkey);
  1465. if (err >= 0) {
  1466. gnutls_privkey_set_pin_function(gci->pkey, gnutls_pin_callback,
  1467. certinfo);
  1468. err = gnutls_privkey_import_x509(gci->pkey, x509key,
  1469. GNUTLS_PRIVKEY_IMPORT_AUTO_RELEASE);
  1470. }
  1471. if (err < 0) {
  1472. vpn_progress(vpninfo, PRG_ERR,
  1473. _("Error creating an abstract privkey from /x509_privkey: %s\n"),
  1474. gnutls_strerror(err));
  1475. /* gci->pkey will be freed in out */
  1476. ret = -EIO;
  1477. goto out;
  1478. }
  1479. }
  1480. /* pkey owns x509key */
  1481. x509key = NULL;
  1482. check_certificate_expiry(vpninfo, certinfo, cert);
  1483. get_cert_name(cert, name, sizeof(name));
  1484. vpn_progress(vpninfo, PRG_INFO,
  1485. certinfo_string(certinfo, _("Using client certificate '%s'\n"),
  1486. _("Using secondary certificate '%s'\n")),
  1487. name);
  1488. /* OpenSSL has problems with certificate chains — if there are
  1489. multiple certs with the same name, it doesn't necessarily
  1490. choose the _right_ one. (RT#1942)
  1491. Pick the right ones for ourselves and add them manually. */
  1492. /* We may have already got a bunch of certs from PKCS#12
  1493. file. Remember how many need to be freed when we're done,
  1494. since we'll expand the gci->certs array with more
  1495. from the cafile and extra_certs[] array if we can, and
  1496. those extra certs must not be freed (twice). */
  1497. if (!gci->nr_certs) {
  1498. gci->certs = gnutls_malloc(sizeof(*gci->certs));
  1499. if (!gci->certs) {
  1500. vpn_progress(vpninfo, PRG_ERR,
  1501. _("Failed to allocate memory for certificate\n"));
  1502. ret = -ENOMEM;
  1503. goto out;
  1504. }
  1505. gci->certs[0] = cert;
  1506. gci->nr_certs = 1;
  1507. }
  1508. last_cert = gci->certs[gci->nr_certs-1];
  1509. while (1) {
  1510. gnutls_x509_crt_t issuer = NULL;
  1511. gnutls_x509_crt_t *saved_cert_list = NULL;
  1512. for (i = 0; i < nr_extra_certs; i++) {
  1513. if (extra_certs[i] &&
  1514. gnutls_x509_crt_check_issuer(last_cert, extra_certs[i]))
  1515. break;
  1516. }
  1517. if (i < nr_extra_certs) {
  1518. /* We found the next cert in the chain in extra_certs[] */
  1519. issuer = extra_certs[i];
  1520. extra_certs[i] = NULL;
  1521. } else {
  1522. /* Look for it in the system trust cafile too. */
  1523. err = gnutls_certificate_get_issuer(vpninfo->https_cred,
  1524. last_cert, &issuer, 0);
  1525. /* GnuTLS 3.2.10 does not support flag GNUTLS_TL_GET_COPY,
  1526. * which makes a copy of the issuer. Therefore, we make
  1527. * an explicit copy of the certificate
  1528. */
  1529. if (err >= 0)
  1530. err = copy_cert(&issuer, issuer);
  1531. #ifdef HAVE_P11KIT
  1532. if (err == GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE && cert_is_p11) {
  1533. gnutls_datum_t t = {NULL, 0};
  1534. err = gnutls_pkcs11_get_raw_issuer(cert_url, last_cert, &t, GNUTLS_X509_FMT_DER, 0);
  1535. if (err >= 0) {
  1536. err = import_cert(&issuer, &t);
  1537. gnutls_free(t.data);
  1538. }
  1539. if (err < 0) {
  1540. vpn_progress(vpninfo, PRG_TRACE,
  1541. _("Got no issuer from PKCS#11\n"));
  1542. } else {
  1543. get_cert_name(issuer, name, sizeof(name));
  1544. vpn_progress(vpninfo, PRG_ERR,
  1545. _("Got next CA '%s' from PKCS#11\n"), name);
  1546. }
  1547. }
  1548. #endif
  1549. if (err)
  1550. break;
  1551. }
  1552. if (gnutls_x509_crt_check_issuer(issuer, issuer)) {
  1553. /* Don't actually include the root CA. If they don't already trust it,
  1554. then handing it to them isn't going to help. But don't omit the
  1555. original certificate if it's self-signed. */
  1556. gnutls_x509_crt_deinit(issuer);
  1557. break;
  1558. }
  1559. /* OK, we found a new cert to add to our chain. */
  1560. saved_cert_list = gci->certs;
  1561. gci->certs = gnutls_realloc(gci->certs,
  1562. sizeof(cert) * (gci->nr_certs+1));
  1563. if (!gci->certs) {
  1564. gci->certs = saved_cert_list;
  1565. vpn_progress(vpninfo, PRG_ERR,
  1566. _("Failed to allocate memory for supporting certificates\n"));
  1567. gnutls_x509_crt_deinit(issuer);
  1568. break;
  1569. }
  1570. /* Append the new one */
  1571. gci->certs[gci->nr_certs] = issuer;
  1572. gci->nr_certs++;
  1573. last_cert = issuer;
  1574. }
  1575. for (i = 1; i < gci->nr_certs; i++) {
  1576. get_cert_name(gci->certs[i], name, sizeof(name));
  1577. vpn_progress(vpninfo, PRG_DEBUG,
  1578. _("Adding supporting CA '%s'\n"), name);
  1579. }
  1580. ret = 0;
  1581. if ((flags & MULTICERT_COMPAT))
  1582. (void) check_multicert_compat(vpninfo, certinfo);
  1583. /* OK, now we've checked the cert expiry and warned the user if it's
  1584. going to expire soon, and we've built up as much of a trust chain
  1585. in gci->certs[] as we can find, to help the server work around
  1586. OpenSSL RT#1942. Set up the GnuTLS credentials with the appropriate
  1587. key and certs. GnuTLS makes us do this differently for X509 privkeys
  1588. vs. TPM/PKCS#11 "generic" privkeys, and the latter is particularly
  1589. 'fun' for GnuTLS 2.12... */
  1590. out:
  1591. gnutls_x509_privkey_deinit(x509key);
  1592. if (cert && !gci->certs) {
  1593. /* Not if gci->certs. It's gci->certs[0] then and will be
  1594. * freed as such. This is only for the error path. */
  1595. gnutls_x509_crt_deinit(cert);
  1596. }
  1597. for (i = 0; i < nr_extra_certs; i++) {
  1598. if (extra_certs[i])
  1599. gnutls_x509_crt_deinit(extra_certs[i]);
  1600. }
  1601. gnutls_free(extra_certs);
  1602. #if defined(HAVE_P11KIT) || defined(HAVE_TROUSERS) || defined(HAVE_TSS2) || defined(HAVE_GNUTLS_SYSTEM_KEYS)
  1603. /* If we support arbitrary privkeys, we might have abused fdata.data
  1604. just to point to something to hash. Don't free it in that case! */
  1605. if (fdata.data != dummy_hash_data)
  1606. #endif
  1607. gnutls_free(fdata.data);
  1608. #ifdef HAVE_P11KIT
  1609. /* This exists in the HAVE_GNUTLS_SYSTEM_KEYS case but will never
  1610. change so it's OK not to add to the #ifdef mess here. */
  1611. if (cert_url != certinfo->cert)
  1612. free(cert_url);
  1613. if (key_url != certinfo->key)
  1614. free(key_url);
  1615. #endif
  1616. if (ret)
  1617. unload_certificate(certinfo, 1);
  1618. return ret;
  1619. }
  1620. /* We have to convert the array of X509 certificates to gnutls_pcert_st
  1621. for ourselves. There's no function that takes a gnutls_privkey_t as
  1622. the key and gnutls_x509_crt_t certificates. */
  1623. static int assign_privkey(struct openconnect_info *vpninfo, struct gtls_cert_info *gci)
  1624. {
  1625. gnutls_pcert_st *pcerts = gnutls_calloc(gci->nr_certs, sizeof(*pcerts));
  1626. unsigned int i;
  1627. int err;
  1628. if (!pcerts)
  1629. return GNUTLS_E_MEMORY_ERROR;
  1630. for (i = 0 ; i < gci->nr_certs; i++) {
  1631. err = gnutls_pcert_import_x509(pcerts + i, gci->certs[i], 0);
  1632. if (err) {
  1633. vpn_progress(vpninfo, PRG_ERR,
  1634. _("Importing X509 certificate failed: %s\n"),
  1635. gnutls_strerror(err));
  1636. goto free_pcerts;
  1637. }
  1638. }
  1639. err = gnutls_certificate_set_key(vpninfo->https_cred, NULL, 0,
  1640. pcerts, gci->nr_certs, gci->pkey);
  1641. if (err) {
  1642. vpn_progress(vpninfo, PRG_ERR,
  1643. _("Setting PKCS#11 certificate failed: %s\n"),
  1644. gnutls_strerror(err));
  1645. free_pcerts:
  1646. for (i = 0 ; i < gci->nr_certs; i++)
  1647. gnutls_pcert_deinit(pcerts + i);
  1648. } else
  1649. gci->pkey = NULL; /* We gave it away */
  1650. free(pcerts);
  1651. return err;
  1652. }
  1653. static int load_primary_certificate(struct openconnect_info *vpninfo)
  1654. {
  1655. struct cert_info *certinfo = &vpninfo->certinfo[0];
  1656. int ret, err;
  1657. ret = load_certificate(vpninfo, certinfo, 0);
  1658. if (ret)
  1659. return ret;
  1660. struct gtls_cert_info *gci = certinfo->priv_info;
  1661. gnutls_x509_crt_t cert = gci->certs[0];
  1662. get_cert_md5_fingerprint(vpninfo, cert, vpninfo->local_cert_md5);
  1663. if (gci->crl) {
  1664. err = gnutls_certificate_set_x509_crl(vpninfo->https_cred, &gci->crl, 1);
  1665. if (err) {
  1666. vpn_progress(vpninfo, PRG_ERR,
  1667. _("Setting certificate revocation list failed: %s\n"),
  1668. gnutls_strerror(err));
  1669. ret = -EINVAL;
  1670. goto out;
  1671. }
  1672. }
  1673. #if GNUTLS_VERSION_NUMBER >= 0x030600
  1674. if (gnutls_privkey_get_pk_algorithm(gci->pkey, NULL) == GNUTLS_PK_RSA) {
  1675. /*
  1676. * For hardware RSA keys, we need to check if they can cope with PSS.
  1677. * If not, disable TLSv1.3 which would make PSS mandatory.
  1678. * https://bugzilla.redhat.com/show_bug.cgi?id=1663058
  1679. */
  1680. gnutls_datum_t fdata= { (void *)gci, sizeof(*gci) };
  1681. gnutls_datum_t pkey_sig = { NULL, 0 };
  1682. err = gnutls_privkey_sign_data2(gci->pkey, GNUTLS_SIGN_RSA_PSS_RSAE_SHA256, 0, &fdata, &pkey_sig);
  1683. if (err) {
  1684. vpn_progress(vpninfo, PRG_INFO,
  1685. _("Private key appears not to support RSA-PSS. Disabling TLSv1.3\n"));
  1686. vpninfo->no_tls13 = 1;
  1687. }
  1688. free(pkey_sig.data);
  1689. }
  1690. #endif
  1691. err = assign_privkey(vpninfo, gci);
  1692. if (err) {
  1693. vpn_progress(vpninfo, PRG_ERR,
  1694. _("Setting certificate failed: %s\n"),
  1695. gnutls_strerror(err));
  1696. ret = -EIO;
  1697. } else
  1698. ret = 0;
  1699. out:
  1700. unload_certificate(certinfo, ret < 0);
  1701. return ret;
  1702. }
  1703. static int get_cert_fingerprint(struct openconnect_info *vpninfo,
  1704. gnutls_x509_crt_t cert,
  1705. gnutls_digest_algorithm_t algo,
  1706. char *buf)
  1707. {
  1708. unsigned char md[256];
  1709. size_t md_size = sizeof(md);
  1710. unsigned int i;
  1711. if (gnutls_x509_crt_get_fingerprint(cert, algo, md, &md_size))
  1712. return -EIO;
  1713. for (i = 0; i < md_size; i++)
  1714. sprintf(&buf[i*2], "%02X", md[i]);
  1715. return 0;
  1716. }
  1717. int get_cert_md5_fingerprint(struct openconnect_info *vpninfo,
  1718. void *cert, char *buf)
  1719. {
  1720. return get_cert_fingerprint(vpninfo, cert, GNUTLS_DIG_MD5, buf);
  1721. }
  1722. static int set_peer_cert_hash(struct openconnect_info *vpninfo)
  1723. {
  1724. size_t shalen;
  1725. gnutls_pubkey_t pkey;
  1726. gnutls_datum_t d;
  1727. int err;
  1728. err = gnutls_pubkey_init(&pkey);
  1729. if (err)
  1730. return err;
  1731. err = gnutls_pubkey_import_x509(pkey, vpninfo->peer_cert, 0);
  1732. if (!err)
  1733. err = gnutls_pubkey_export2(pkey, GNUTLS_X509_FMT_DER, &d);
  1734. gnutls_pubkey_deinit(pkey);
  1735. if (err)
  1736. return err;
  1737. shalen = sizeof(vpninfo->peer_cert_sha256_raw);
  1738. err = gnutls_fingerprint(GNUTLS_DIG_SHA256, &d, vpninfo->peer_cert_sha256_raw, &shalen);
  1739. if (err) {
  1740. gnutls_free(d.data);
  1741. return err;
  1742. }
  1743. shalen = sizeof(vpninfo->peer_cert_sha1_raw);
  1744. err = gnutls_fingerprint(GNUTLS_DIG_SHA1, &d, vpninfo->peer_cert_sha1_raw, &shalen);
  1745. if (err) {
  1746. gnutls_free(d.data);
  1747. return err;
  1748. }
  1749. gnutls_free(d.data);
  1750. return 0;
  1751. }
  1752. char *openconnect_get_peer_cert_details(struct openconnect_info *vpninfo)
  1753. {
  1754. gnutls_datum_t buf;
  1755. if (gnutls_x509_crt_print(vpninfo->peer_cert, GNUTLS_CRT_PRINT_FULL, &buf))
  1756. return NULL;
  1757. return (char *)buf.data;
  1758. }
  1759. int openconnect_get_peer_cert_DER(struct openconnect_info *vpninfo,
  1760. unsigned char **buf)
  1761. {
  1762. size_t l = 0;
  1763. unsigned char *ret = NULL;
  1764. if (gnutls_x509_crt_export(vpninfo->peer_cert, GNUTLS_X509_FMT_DER,
  1765. ret, &l) != GNUTLS_E_SHORT_MEMORY_BUFFER)
  1766. return -EIO;
  1767. ret = gnutls_malloc(l);
  1768. if (!ret)
  1769. return -ENOMEM;
  1770. if (gnutls_x509_crt_export(vpninfo->peer_cert, GNUTLS_X509_FMT_DER,
  1771. ret, &l)) {
  1772. gnutls_free(ret);
  1773. return -EIO;
  1774. }
  1775. *buf = ret;
  1776. return l;
  1777. }
  1778. void openconnect_free_cert_info(struct openconnect_info *vpninfo,
  1779. void *buf)
  1780. {
  1781. gnutls_free(buf);
  1782. }
  1783. int openconnect_get_peer_cert_chain(struct openconnect_info *vpninfo,
  1784. struct oc_cert **chainp)
  1785. {
  1786. struct oc_cert *chain, *p;
  1787. const gnutls_datum_t *cert_list = vpninfo->cert_list_handle;
  1788. int i, cert_list_size = vpninfo->cert_list_size;
  1789. if (!cert_list)
  1790. return -EINVAL;
  1791. if (cert_list_size <= 0)
  1792. return -EIO;
  1793. p = chain = calloc(cert_list_size, sizeof(struct oc_cert));
  1794. if (!chain)
  1795. return -ENOMEM;
  1796. for (i = 0; i < cert_list_size; i++, p++) {
  1797. p->der_data = (unsigned char *)cert_list[i].data;
  1798. p->der_len = cert_list[i].size;
  1799. }
  1800. *chainp = chain;
  1801. return cert_list_size;
  1802. }
  1803. void openconnect_free_peer_cert_chain(struct openconnect_info *vpninfo,
  1804. struct oc_cert *chain)
  1805. {
  1806. free(chain);
  1807. }
  1808. static int verify_peer(gnutls_session_t session)
  1809. {
  1810. struct openconnect_info *vpninfo = gnutls_session_get_ptr(session);
  1811. const gnutls_datum_t *cert_list;
  1812. gnutls_x509_crt_t cert;
  1813. unsigned int status, cert_list_size;
  1814. const char *reason = NULL;
  1815. int err = 0;
  1816. cert_list = gnutls_certificate_get_peers(session, &cert_list_size);
  1817. if (!cert_list) {
  1818. vpn_progress(vpninfo, PRG_ERR, _("Server presented no certificate\n"));
  1819. return GNUTLS_E_CERTIFICATE_ERROR;
  1820. }
  1821. if (vpninfo->peer_cert) {
  1822. unsigned char *prev_der = NULL;
  1823. int der_len = openconnect_get_peer_cert_DER(vpninfo, &prev_der);
  1824. if (der_len < 0) {
  1825. vpn_progress(vpninfo, PRG_ERR, _("Error comparing server's cert on rehandshake: %s\n"),
  1826. strerror(-der_len));
  1827. return GNUTLS_E_CERTIFICATE_ERROR;
  1828. }
  1829. if (cert_list[0].size != der_len || memcmp(cert_list[0].data, prev_der, der_len)) {
  1830. vpn_progress(vpninfo, PRG_ERR, _("Server presented different cert on rehandshake\n"));
  1831. gnutls_free(prev_der);
  1832. return GNUTLS_E_CERTIFICATE_ERROR;
  1833. }
  1834. gnutls_free(prev_der);
  1835. vpn_progress(vpninfo, PRG_TRACE, _("Server presented identical cert on rehandshake\n"));
  1836. return 0;
  1837. }
  1838. err = gnutls_x509_crt_init(&cert);
  1839. if (err) {
  1840. vpn_progress(vpninfo, PRG_ERR, _("Error initialising X509 cert structure\n"));
  1841. return GNUTLS_E_CERTIFICATE_ERROR;
  1842. }
  1843. err = gnutls_x509_crt_import(cert, &cert_list[0], GNUTLS_X509_FMT_DER);
  1844. if (err) {
  1845. vpn_progress(vpninfo, PRG_ERR, _("Error importing server's cert\n"));
  1846. gnutls_x509_crt_deinit(cert);
  1847. return GNUTLS_E_CERTIFICATE_ERROR;
  1848. }
  1849. vpninfo->peer_cert = cert;
  1850. err = set_peer_cert_hash(vpninfo);
  1851. if (err < 0) {
  1852. vpn_progress(vpninfo, PRG_ERR,
  1853. _("Could not calculate hash of server's certificate\n"));
  1854. }
  1855. err = gnutls_certificate_verify_peers2(session, &status);
  1856. if (err) {
  1857. vpn_progress(vpninfo, PRG_ERR, _("Error checking server cert status\n"));
  1858. return GNUTLS_E_CERTIFICATE_ERROR;
  1859. }
  1860. if (status & GNUTLS_CERT_REVOKED)
  1861. reason = _("certificate revoked");
  1862. else if (status & GNUTLS_CERT_SIGNER_NOT_FOUND)
  1863. reason = _("signer not found");
  1864. else if (status & GNUTLS_CERT_SIGNER_NOT_CA)
  1865. reason = _("signer not a CA certificate");
  1866. else if (status & GNUTLS_CERT_INSECURE_ALGORITHM)
  1867. reason = _("insecure algorithm");
  1868. else if (status & GNUTLS_CERT_NOT_ACTIVATED)
  1869. reason = _("certificate not yet activated");
  1870. else if (status & GNUTLS_CERT_EXPIRED)
  1871. reason = _("certificate expired");
  1872. else if (status & GNUTLS_CERT_INVALID)
  1873. /* If this is set and no other reason, it apparently means
  1874. that signature verification failed. Not entirely sure
  1875. why we don't just set a bit for that too. */
  1876. reason = _("signature verification failed");
  1877. if (reason)
  1878. goto done;
  1879. if (!gnutls_x509_crt_check_hostname(cert, vpninfo->hostname)) {
  1880. int i, ret;
  1881. unsigned char addrbuf[sizeof(struct in6_addr)];
  1882. unsigned char certaddr[sizeof(struct in6_addr)];
  1883. size_t addrlen = 0, certaddrlen;
  1884. /* gnutls_x509_crt_check_hostname() doesn't cope with IPv6 literals
  1885. in URI form with surrounding [] so we must check for ourselves. */
  1886. if (vpninfo->hostname[0] == '[' &&
  1887. vpninfo->hostname[strlen(vpninfo->hostname)-1] == ']') {
  1888. char *p = &vpninfo->hostname[strlen(vpninfo->hostname)-1];
  1889. *p = 0;
  1890. if (inet_pton(AF_INET6, vpninfo->hostname + 1, addrbuf) > 0)
  1891. addrlen = 16;
  1892. *p = ']';
  1893. }
  1894. #if GNUTLS_VERSION_NUMBER < 0x030306
  1895. /* And before 3.3.6 it didn't check IP addresses at all. */
  1896. else if (inet_pton(AF_INET, vpninfo->hostname, addrbuf) > 0)
  1897. addrlen = 4;
  1898. else if (inet_pton(AF_INET6, vpninfo->hostname, addrbuf) > 0)
  1899. addrlen = 16;
  1900. #endif
  1901. if (!addrlen) {
  1902. /* vpninfo->hostname was not a bare IP address. Nothing to do */
  1903. goto badhost;
  1904. }
  1905. for (i = 0; ; i++) {
  1906. certaddrlen = sizeof(certaddr);
  1907. ret = gnutls_x509_crt_get_subject_alt_name(cert, i, certaddr,
  1908. &certaddrlen, NULL);
  1909. /* If this happens, it wasn't an IP address. */
  1910. if (ret == GNUTLS_E_SHORT_MEMORY_BUFFER)
  1911. continue;
  1912. if (ret < 0)
  1913. break;
  1914. if (ret != GNUTLS_SAN_IPADDRESS)
  1915. continue;
  1916. if (certaddrlen == addrlen && !memcmp(addrbuf, certaddr, addrlen))
  1917. goto done;
  1918. }
  1919. badhost:
  1920. reason = _("certificate does not match hostname");
  1921. }
  1922. done:
  1923. if (reason) {
  1924. vpn_progress(vpninfo, PRG_INFO,
  1925. _("Server certificate verify failed: %s\n"),
  1926. reason);
  1927. if (vpninfo->validate_peer_cert) {
  1928. vpninfo->cert_list_handle = (void *)cert_list;
  1929. vpninfo->cert_list_size = cert_list_size;
  1930. err = vpninfo->validate_peer_cert(vpninfo->cbdata,
  1931. reason) ? GNUTLS_E_CERTIFICATE_ERROR : 0;
  1932. vpninfo->cert_list_handle = NULL;
  1933. } else
  1934. err = GNUTLS_E_CERTIFICATE_ERROR;
  1935. }
  1936. return err;
  1937. }
  1938. #ifdef HAVE_HPKE_SUPPORT
  1939. static int finished_fn(gnutls_session_t session, unsigned int htype, unsigned when,
  1940. unsigned int incoming, const gnutls_datum_t *msg)
  1941. {
  1942. struct openconnect_info *vpninfo = gnutls_session_get_ptr(session);
  1943. if (incoming)
  1944. return 0;
  1945. if (msg->size > sizeof(vpninfo->finished)) {
  1946. vpn_progress(vpninfo, PRG_ERR,
  1947. _("TLS Finished message larger than expected (%u bytes)\n"),
  1948. msg->size);
  1949. vpninfo->finished_len = sizeof(vpninfo->finished);
  1950. } else
  1951. vpninfo->finished_len = msg->size;
  1952. memcpy(vpninfo->finished, msg->data, vpninfo->finished_len);
  1953. return 0;
  1954. }
  1955. #endif
  1956. int openconnect_open_https(struct openconnect_info *vpninfo)
  1957. {
  1958. int ssl_sock = -1;
  1959. int err;
  1960. if (vpninfo->https_sess)
  1961. return 0;
  1962. if (vpninfo->peer_cert) {
  1963. gnutls_x509_crt_deinit(vpninfo->peer_cert);
  1964. vpninfo->peer_cert = NULL;
  1965. }
  1966. free(vpninfo->peer_cert_hash);
  1967. vpninfo->peer_cert_hash = NULL;
  1968. gnutls_free(vpninfo->cstp_cipher);
  1969. vpninfo->cstp_cipher = NULL;
  1970. ssl_sock = connect_https_socket(vpninfo);
  1971. if (ssl_sock < 0)
  1972. return ssl_sock;
  1973. if (!vpninfo->https_cred) {
  1974. gnutls_certificate_allocate_credentials(&vpninfo->https_cred);
  1975. if (!vpninfo->no_system_trust)
  1976. gnutls_certificate_set_x509_system_trust(vpninfo->https_cred);
  1977. gnutls_certificate_set_verify_function(vpninfo->https_cred,
  1978. verify_peer);
  1979. #ifdef ANDROID_KEYSTORE
  1980. if (vpninfo->cafile && !strncmp(vpninfo->cafile, "keystore:", 9)) {
  1981. gnutls_datum_t datum;
  1982. unsigned int nr_certs;
  1983. err = load_datum(vpninfo, &datum, vpninfo->cafile);
  1984. if (err < 0) {
  1985. gnutls_certificate_free_credentials(vpninfo->https_cred);
  1986. vpninfo->https_cred = NULL;
  1987. return err;
  1988. }
  1989. /* For GnuTLS 3.x We should use gnutls_x509_crt_list_import2() */
  1990. nr_certs = count_x509_certificates(&datum);
  1991. if (nr_certs) {
  1992. gnutls_x509_crt_t *certs;
  1993. int i;
  1994. certs = calloc(nr_certs, sizeof(*certs));
  1995. if (!certs) {
  1996. vpn_progress(vpninfo, PRG_ERR,
  1997. _("Failed to allocate memory for cafile certs\n"));
  1998. gnutls_free(datum.data);
  1999. gnutls_certificate_free_credentials(vpninfo->https_cred);
  2000. vpninfo->https_cred = NULL;
  2001. closesocket(ssl_sock);
  2002. return -ENOMEM;
  2003. }
  2004. err = gnutls_x509_crt_list_import(certs, &nr_certs, &datum,
  2005. GNUTLS_X509_FMT_PEM, 0);
  2006. gnutls_free(datum.data);
  2007. if (err >= 0) {
  2008. nr_certs = err;
  2009. err = gnutls_certificate_set_x509_trust(vpninfo->https_cred,
  2010. certs, nr_certs);
  2011. }
  2012. for (i = 0; i < nr_certs; i++)
  2013. gnutls_x509_crt_deinit(certs[i]);
  2014. free(certs);
  2015. if (err < 0) {
  2016. /* From crt_list_import or set_x509_trust */
  2017. vpn_progress(vpninfo, PRG_ERR,
  2018. _("Failed to read certs from cafile: %s\n"),
  2019. gnutls_strerror(err));
  2020. gnutls_certificate_free_credentials(vpninfo->https_cred);
  2021. vpninfo->https_cred = NULL;
  2022. closesocket(ssl_sock);
  2023. return -EINVAL;
  2024. }
  2025. }
  2026. } else
  2027. #endif
  2028. if (vpninfo->cafile) {
  2029. err = gnutls_certificate_set_x509_trust_file(vpninfo->https_cred,
  2030. vpninfo->cafile,
  2031. GNUTLS_X509_FMT_PEM);
  2032. if (err < 0) {
  2033. vpn_progress(vpninfo, PRG_ERR,
  2034. _("Failed to open CA file '%s': %s\n"),
  2035. vpninfo->cafile, gnutls_strerror(err));
  2036. gnutls_certificate_free_credentials(vpninfo->https_cred);
  2037. vpninfo->https_cred = NULL;
  2038. closesocket(ssl_sock);
  2039. return -EINVAL;
  2040. }
  2041. }
  2042. if (vpninfo->certinfo[0].cert) {
  2043. err = load_primary_certificate(vpninfo);
  2044. if (err) {
  2045. vpn_progress(vpninfo, PRG_ERR,
  2046. _("Loading certificate failed. Aborting.\n"));
  2047. gnutls_certificate_free_credentials(vpninfo->https_cred);
  2048. vpninfo->https_cred = NULL;
  2049. closesocket(ssl_sock);
  2050. return err;
  2051. }
  2052. }
  2053. }
  2054. gnutls_init(&vpninfo->https_sess, GNUTLS_CLIENT|GNUTLS_FORCE_CLIENT_CERT);
  2055. gnutls_session_set_ptr(vpninfo->https_sess, (void *) vpninfo);
  2056. /*
  2057. * For versions of GnuTLS older than 3.2.9, we try to avoid long
  2058. * packets by silently disabling extensions such as SNI.
  2059. *
  2060. * See comments above regarding COMPAT and DUMBFW.
  2061. */
  2062. if (string_is_hostname(vpninfo->hostname))
  2063. gnutls_server_name_set(vpninfo->https_sess, GNUTLS_NAME_DNS,
  2064. vpninfo->hostname,
  2065. strlen(vpninfo->hostname));
  2066. /*
  2067. * If a ClientHello is between 256 and 511 bytes, the
  2068. * server cannot distinguish between a SSLv2 formatted
  2069. * packet and a SSLv3 formatted packet.
  2070. *
  2071. * F5 BIG-IP reverse proxies in particular will
  2072. * silently drop an ambiguous ClientHello.
  2073. *
  2074. * GnuTLS fixes this in v3.2.9+ by padding ClientHello
  2075. * packets to at least 512 bytes if %COMPAT or %DUMBFW
  2076. * is specified.
  2077. *
  2078. * Discussion:
  2079. * https://www.ietf.org/mail-archive/web/tls/current/msg10423.html
  2080. *
  2081. * GnuTLS commits:
  2082. * b6d29bb1737f96ac44a8ef9cc9fe7f9837e20465
  2083. * a9bd8c4d3a639c40adb964349297f891f583a21b
  2084. * 531bec47037e882af32963f8461988f8c724919e
  2085. * 7c45ebbdd877cd994b6b938bd6faef19558a01e1
  2086. * 8d28901a3ebd2589d0fc9941475d50f04047f6fe
  2087. * 28065ce3896b1b0f87972d0bce9b17641ebb69b9
  2088. */
  2089. if (!vpninfo->ciphersuite_config) {
  2090. struct oc_text_buf *buf = buf_alloc();
  2091. #ifdef DEFAULT_PRIO
  2092. buf_append(buf, "%s", DEFAULT_PRIO ":%COMPAT");
  2093. #else
  2094. /* GnuTLS 3.5.19 and onward remove AES-CBC-HMAC-SHA256 from NORMAL,
  2095. * but some Cisco servers can't do anything better, so
  2096. * explicitly add '+SHA256' to allow it. Yay Cisco.
  2097. * - GnuTLS commit that removed: c433cdf92349afae66c703bdacedf987f423605e
  2098. * - Old server requiring SHA256: https://gitlab.com/openconnect/openconnect/-/issues/21
  2099. *
  2100. * Likewise, GnuTLS 3.6.0 and onward remove 3DES-CBC from NORMAL,
  2101. * but some ancient servers can't do anything better. This (and ARCFOUR-128)
  2102. * should not be re-enabled by default due to serious security flaws, so adding as
  2103. * an option, --allow-insecure-crypto. Yay ancient, unpatched servers.
  2104. * - GnuTLS commit that removed: 66f2a0a271bcc10e8fb68771f9349a3d3ecf6dda
  2105. * - Old server requiring 3DES-CBC: https://gitlab.com/openconnect/openconnect/-/issues/145
  2106. */
  2107. buf_append(buf, "NORMAL:-VERS-SSL3.0:+SHA256:%%COMPAT");
  2108. #endif
  2109. if (vpninfo->pfs)
  2110. buf_append(buf, ":-RSA");
  2111. if (vpninfo->no_tls13)
  2112. buf_append(buf, ":-VERS-TLS1.3");
  2113. if (vpninfo->allow_insecure_crypto) {
  2114. buf_append(buf, ":+3DES-CBC:+ARCFOUR-128:+SHA1");
  2115. if (gnutls_check_version_numeric(3,6,0))
  2116. buf_append(buf, ":%%VERIFY_ALLOW_SIGN_WITH_SHA1");
  2117. } else
  2118. buf_append(buf, ":-3DES-CBC:-ARCFOUR-128");
  2119. if (buf_error(buf)) {
  2120. vpn_progress(vpninfo, PRG_ERR,
  2121. _("Failed to construct GnuTLS priority string\n"));
  2122. return buf_free(buf);
  2123. }
  2124. vpninfo->ciphersuite_config = buf->data;
  2125. buf->data = NULL;
  2126. buf_free(buf);
  2127. }
  2128. err = gnutls_priority_set_direct(vpninfo->https_sess,
  2129. vpninfo->ciphersuite_config, NULL);
  2130. if (err) {
  2131. vpn_progress(vpninfo, PRG_ERR,
  2132. _("Failed to set GnuTLS priority string (\"%s\"): %s\n"),
  2133. vpninfo->ciphersuite_config, gnutls_strerror(err));
  2134. gnutls_deinit(vpninfo->https_sess);
  2135. vpninfo->https_sess = NULL;
  2136. closesocket(ssl_sock);
  2137. return -EIO;
  2138. }
  2139. gnutls_record_disable_padding(vpninfo->https_sess);
  2140. gnutls_credentials_set(vpninfo->https_sess, GNUTLS_CRD_CERTIFICATE, vpninfo->https_cred);
  2141. gnutls_transport_set_ptr(vpninfo->https_sess,(gnutls_transport_ptr_t)(intptr_t)ssl_sock);
  2142. vpn_progress(vpninfo, PRG_INFO, _("SSL negotiation with %s\n"),
  2143. vpninfo->hostname);
  2144. #ifdef GNUTLS_DEFAULT_HANDSHAKE_TIMEOUT
  2145. gnutls_handshake_set_timeout(vpninfo->https_sess,
  2146. GNUTLS_DEFAULT_HANDSHAKE_TIMEOUT);
  2147. #endif
  2148. #ifdef HAVE_HPKE_SUPPORT
  2149. /*
  2150. * The AnyConnect STRAP protocol needs the Finished message from the
  2151. * TLS connection. It isn't clear if this is a misguided attempt at
  2152. * MITM protection or just a convenient nonce known to both sides.
  2153. */
  2154. gnutls_handshake_set_hook_function(vpninfo->https_sess, GNUTLS_HANDSHAKE_FINISHED,
  2155. GNUTLS_HOOK_POST, finished_fn);
  2156. #endif
  2157. err = cstp_handshake(vpninfo, 1);
  2158. if (err)
  2159. return err;
  2160. vpninfo->ssl_fd = ssl_sock;
  2161. vpninfo->ssl_read = openconnect_gnutls_read;
  2162. vpninfo->ssl_write = openconnect_gnutls_write;
  2163. vpninfo->ssl_gets = openconnect_gnutls_gets;
  2164. return 0;
  2165. }
  2166. int cstp_handshake(struct openconnect_info *vpninfo, unsigned init)
  2167. {
  2168. int err;
  2169. int ssl_sock = -1;
  2170. ssl_sock = (intptr_t)gnutls_transport_get_ptr(vpninfo->https_sess);
  2171. while ((err = gnutls_handshake(vpninfo->https_sess))) {
  2172. if (err == GNUTLS_E_AGAIN || err == GNUTLS_E_INTERRUPTED) {
  2173. fd_set rd_set, wr_set;
  2174. int maxfd = ssl_sock;
  2175. FD_ZERO(&rd_set);
  2176. FD_ZERO(&wr_set);
  2177. if (gnutls_record_get_direction(vpninfo->https_sess))
  2178. FD_SET(ssl_sock, &wr_set);
  2179. else
  2180. FD_SET(ssl_sock, &rd_set);
  2181. cmd_fd_set(vpninfo, &rd_set, &maxfd);
  2182. if (select(maxfd + 1, &rd_set, &wr_set, NULL, NULL) < 0 &&
  2183. errno != EINTR) {
  2184. vpn_perror(vpninfo, _("Failed select() for TLS"));
  2185. return -EIO;
  2186. }
  2187. if (is_cancel_pending(vpninfo, &rd_set)) {
  2188. vpn_progress(vpninfo, PRG_ERR, _("SSL connection cancelled\n"));
  2189. gnutls_deinit(vpninfo->https_sess);
  2190. vpninfo->https_sess = NULL;
  2191. closesocket(ssl_sock);
  2192. return -EINTR;
  2193. }
  2194. } else if (gnutls_error_is_fatal(err)) {
  2195. vpn_progress(vpninfo, PRG_ERR, _("SSL connection failure: %s\n"),
  2196. gnutls_strerror(err));
  2197. gnutls_deinit(vpninfo->https_sess);
  2198. vpninfo->https_sess = NULL;
  2199. closesocket(ssl_sock);
  2200. return -EIO;
  2201. } else {
  2202. /* non-fatal error or warning. Ignore it and continue */
  2203. vpn_progress(vpninfo, PRG_DEBUG,
  2204. _("GnuTLS non-fatal return during handshake: %s\n"),
  2205. gnutls_strerror(err));
  2206. }
  2207. }
  2208. gnutls_free(vpninfo->cstp_cipher);
  2209. vpninfo->cstp_cipher = get_gnutls_cipher(vpninfo->https_sess);
  2210. if (init) {
  2211. vpn_progress(vpninfo, PRG_INFO, _("Connected to HTTPS on %s with ciphersuite %s\n"),
  2212. vpninfo->hostname, vpninfo->cstp_cipher);
  2213. } else {
  2214. vpn_progress(vpninfo, PRG_INFO, _("Renegotiated SSL on %s with ciphersuite %s\n"),
  2215. vpninfo->hostname, vpninfo->cstp_cipher);
  2216. }
  2217. return 0;
  2218. }
  2219. void openconnect_close_https(struct openconnect_info *vpninfo, int final)
  2220. {
  2221. if (vpninfo->https_sess) {
  2222. gnutls_deinit(vpninfo->https_sess);
  2223. vpninfo->https_sess = NULL;
  2224. }
  2225. if (vpninfo->ssl_fd != -1) {
  2226. unmonitor_fd(vpninfo, ssl);
  2227. closesocket(vpninfo->ssl_fd);
  2228. vpninfo->ssl_fd = -1;
  2229. }
  2230. if (final && vpninfo->https_cred) {
  2231. gnutls_certificate_free_credentials(vpninfo->https_cred);
  2232. vpninfo->https_cred = NULL;
  2233. unload_certificate(&vpninfo->certinfo[0], 1);
  2234. }
  2235. }
  2236. int openconnect_init_ssl(void)
  2237. {
  2238. #ifdef _WIN32
  2239. int ret = openconnect__win32_sock_init();
  2240. if (ret)
  2241. return ret;
  2242. #endif
  2243. if (gnutls_global_init())
  2244. return -EIO;
  2245. return 0;
  2246. }
  2247. char *get_gnutls_cipher(gnutls_session_t session)
  2248. {
  2249. return gnutls_session_get_desc(session);
  2250. }
  2251. int openconnect_sha1(unsigned char *result, void *data, int datalen)
  2252. {
  2253. const gnutls_datum_t d = { data, datalen };
  2254. size_t shalen = SHA1_SIZE;
  2255. if (gnutls_fingerprint(GNUTLS_DIG_SHA1, &d, result, &shalen))
  2256. return -1;
  2257. return 0;
  2258. }
  2259. int openconnect_sha256(unsigned char *result, void *data, int datalen)
  2260. {
  2261. const gnutls_datum_t d = { data, datalen };
  2262. size_t shalen = SHA256_SIZE;
  2263. if (gnutls_fingerprint(GNUTLS_DIG_SHA256, &d, result, &shalen))
  2264. return -1;
  2265. return 0;
  2266. }
  2267. int openconnect_md5(unsigned char *result, void *data, int datalen)
  2268. {
  2269. const gnutls_datum_t d = { data, datalen };
  2270. size_t md5len = MD5_SIZE;
  2271. if (gnutls_fingerprint(GNUTLS_DIG_MD5, &d, result, &md5len))
  2272. return -1;
  2273. return 0;
  2274. }
  2275. int openconnect_random(void *bytes, int len)
  2276. {
  2277. if (gnutls_rnd(GNUTLS_RND_RANDOM, bytes, len))
  2278. return -EIO;
  2279. return 0;
  2280. }
  2281. int openconnect_local_cert_md5(struct openconnect_info *vpninfo,
  2282. char *buf)
  2283. {
  2284. memcpy(buf, vpninfo->local_cert_md5, sizeof(vpninfo->local_cert_md5));
  2285. return 0;
  2286. }
  2287. #if defined(HAVE_P11KIT) || defined(HAVE_GNUTLS_SYSTEM_KEYS)
  2288. static int gnutls_pin_callback(void *priv, int attempt, const char *uri,
  2289. const char *token_label, unsigned int flags,
  2290. char *pin, size_t pin_max)
  2291. {
  2292. struct cert_info *certinfo = priv;
  2293. struct openconnect_info *vpninfo = certinfo->vpninfo;
  2294. struct pin_cache **cache = &vpninfo->pin_cache;
  2295. struct oc_auth_form f;
  2296. struct oc_form_opt o;
  2297. char message[1024];
  2298. int ret;
  2299. if (!vpninfo || !vpninfo->process_auth_form)
  2300. return -1;
  2301. while (*cache) {
  2302. if (!strcmp(uri, (*cache)->token)) {
  2303. if ((*cache)->pin) {
  2304. if (attempt == 0) {
  2305. snprintf(pin, pin_max, "%s", (*cache)->pin);
  2306. return 0;
  2307. }
  2308. memset((*cache)->pin, 0x5a, strlen((*cache)->pin));
  2309. free((*cache)->pin);
  2310. (*cache)->pin = NULL;
  2311. }
  2312. break;
  2313. }
  2314. cache = &(*cache)->next;
  2315. }
  2316. if (!*cache) {
  2317. *cache = calloc(1, sizeof(struct pin_cache));
  2318. if (!*cache)
  2319. return -1;
  2320. (*cache)->token = strdup(uri);
  2321. }
  2322. if (!attempt && certinfo->password) {
  2323. snprintf(pin, pin_max, "%s", certinfo->password);
  2324. (*cache)->pin = certinfo->password;
  2325. certinfo->password = NULL;
  2326. return 0;
  2327. }
  2328. memset(&f, 0, sizeof(f));
  2329. f.auth_id = (char *)certinfo_string(certinfo, "pkcs11_pin",
  2330. "secondary_pkcs11_pin");
  2331. f.opts = &o;
  2332. message[sizeof(message)-1] = 0;
  2333. snprintf(message, sizeof(message) - 1, _("PIN required for %s"), token_label);
  2334. f.message = message;
  2335. if (flags & GNUTLS_PIN_WRONG)
  2336. f.error = (char *)_("Wrong PIN");
  2337. if (flags & GNUTLS_PIN_FINAL_TRY)
  2338. f.banner = (char *)_("This is the final try before locking!");
  2339. else if (flags & GNUTLS_PIN_COUNT_LOW)
  2340. f.banner = (char *)_("Only a few tries left before locking!");
  2341. o.next = NULL;
  2342. o.type = OC_FORM_OPT_PASSWORD;
  2343. o.name = (char *)"pkcs11_pin";
  2344. o.label = (char *)_("Enter PIN:");
  2345. o._value = NULL;
  2346. ret = process_auth_form(vpninfo, &f);
  2347. if (ret || !o._value)
  2348. return -1;
  2349. snprintf(pin, pin_max, "%s", o._value);
  2350. (*cache)->pin = o._value;
  2351. return 0;
  2352. }
  2353. #endif /* HAVE_P11KIT || HAVE_GNUTLS_SYSTEM_KEYS */
  2354. #ifdef HAVE_LIBPCSCLITE
  2355. int openconnect_hash_yubikey_password(struct openconnect_info *vpninfo,
  2356. const char *password, int pwlen,
  2357. const void *ident, int id_len)
  2358. {
  2359. unsigned char U[SHA1_SIZE];
  2360. gnutls_hmac_hd_t dgst;
  2361. int ret = -EIO;
  2362. int i, j;
  2363. if (gnutls_hmac_init(&dgst, GNUTLS_MAC_SHA1, password, pwlen))
  2364. return -EIO;
  2365. if (gnutls_hmac(dgst, ident, id_len))
  2366. goto out;
  2367. /* This is a subset of full PBKDF2, where we know the outer loop is only
  2368. * run once because our output length (16) is less than the hash output
  2369. * size (20). So just hard-code the value. */
  2370. if (gnutls_hmac(dgst, "\x0\x0\x0\x1", 4))
  2371. goto out;
  2372. gnutls_hmac_output(dgst, U);
  2373. memcpy(vpninfo->yubikey_pwhash, U, 16);
  2374. for (i = 1; i < 1000; i++) {
  2375. if (gnutls_hmac(dgst, U, SHA1_SIZE))
  2376. goto out;
  2377. gnutls_hmac_output(dgst, U);
  2378. for (j = 0; j < 16; j++)
  2379. vpninfo->yubikey_pwhash[j] ^= U[j];
  2380. }
  2381. ret = 0;
  2382. out:
  2383. gnutls_hmac_deinit(dgst, NULL);
  2384. return ret;
  2385. }
  2386. int openconnect_yubikey_chalresp(struct openconnect_info *vpninfo,
  2387. const void *challenge, int chall_len, void *result)
  2388. {
  2389. if (gnutls_hmac_fast(GNUTLS_MAC_SHA1, vpninfo->yubikey_pwhash, 16, challenge, chall_len, result))
  2390. return -EIO;
  2391. return 0;
  2392. }
  2393. #endif
  2394. int hotp_hmac(struct openconnect_info *vpninfo, const void *challenge)
  2395. {
  2396. int ret;
  2397. int hpos;
  2398. unsigned char hash[64]; /* Enough for a SHA256 */
  2399. gnutls_mac_algorithm_t alg;
  2400. switch(vpninfo->oath_hmac_alg) {
  2401. case OATH_ALG_HMAC_SHA1:
  2402. alg = GNUTLS_MAC_SHA1;
  2403. hpos = 19;
  2404. break;
  2405. case OATH_ALG_HMAC_SHA256:
  2406. alg = GNUTLS_MAC_SHA256;
  2407. hpos = 31;
  2408. break;
  2409. case OATH_ALG_HMAC_SHA512:
  2410. alg = GNUTLS_MAC_SHA512;
  2411. hpos = 63;
  2412. break;
  2413. default:
  2414. vpn_progress(vpninfo, PRG_ERR,
  2415. _("Unsupported OATH HMAC algorithm\n"));
  2416. return -EINVAL;
  2417. }
  2418. ret = gnutls_hmac_fast(alg, vpninfo->oath_secret,
  2419. vpninfo->oath_secret_len,
  2420. challenge, 8, hash);
  2421. if (ret) {
  2422. vpninfo->progress(vpninfo, PRG_ERR,
  2423. _("Failed to calculate OATH HMAC: %s\n"),
  2424. gnutls_strerror(ret));
  2425. return -EINVAL;
  2426. }
  2427. hpos = hash[hpos] & 15;
  2428. return load_be32(&hash[hpos]) & 0x7fffffff;
  2429. }
  2430. static int ttls_pull_timeout_func(gnutls_transport_ptr_t t, unsigned int ms)
  2431. {
  2432. struct openconnect_info *vpninfo = t;
  2433. vpn_progress(vpninfo, PRG_TRACE, _("%s %dms\n"), __func__, ms);
  2434. return 0;
  2435. }
  2436. static ssize_t ttls_pull_func(gnutls_transport_ptr_t t, void *buf, size_t len)
  2437. {
  2438. int ret = pulse_eap_ttls_recv(t, buf, len);
  2439. if (ret >= 0)
  2440. return ret;
  2441. else
  2442. return GNUTLS_E_PULL_ERROR;
  2443. }
  2444. static ssize_t ttls_push_func(gnutls_transport_ptr_t t, const void *buf, size_t len)
  2445. {
  2446. int ret = pulse_eap_ttls_send(t, buf, len);
  2447. if (ret >= 0)
  2448. return ret;
  2449. else
  2450. return GNUTLS_E_PUSH_ERROR;
  2451. }
  2452. void *establish_eap_ttls(struct openconnect_info *vpninfo)
  2453. {
  2454. gnutls_session_t ttls_sess = NULL;
  2455. int err;
  2456. gnutls_init(&ttls_sess, GNUTLS_CLIENT);
  2457. gnutls_session_set_ptr(ttls_sess, (void *) vpninfo);
  2458. gnutls_transport_set_ptr(ttls_sess, (void *) vpninfo);
  2459. gnutls_transport_set_push_function(ttls_sess, ttls_push_func);
  2460. gnutls_transport_set_pull_function(ttls_sess, ttls_pull_func);
  2461. gnutls_transport_set_pull_timeout_function(ttls_sess, ttls_pull_timeout_func);
  2462. gnutls_credentials_set(ttls_sess, GNUTLS_CRD_CERTIFICATE, vpninfo->https_cred);
  2463. err = gnutls_priority_set_direct(ttls_sess,
  2464. vpninfo->ciphersuite_config, NULL);
  2465. if (err < 0) {
  2466. vpn_progress(vpninfo, PRG_TRACE,
  2467. _("Could not set ciphersuites: %s\n"), vpninfo->ciphersuite_config);
  2468. goto fail;
  2469. }
  2470. err = gnutls_handshake(ttls_sess);
  2471. if (!err) {
  2472. vpn_progress(vpninfo, PRG_TRACE,
  2473. _("Established EAP-TTLS session\n"));
  2474. return ttls_sess;
  2475. }
  2476. fail:
  2477. gnutls_deinit(ttls_sess);
  2478. return NULL;
  2479. }
  2480. void destroy_eap_ttls(struct openconnect_info *vpninfo, void *sess)
  2481. {
  2482. gnutls_deinit(sess);
  2483. }
  2484. static int generate_strap_key(gnutls_privkey_t *key, char **pubkey,
  2485. gnutls_datum_t *privder_in,
  2486. gnutls_datum_t *pubder)
  2487. {
  2488. int bits, pk, err;
  2489. gnutls_privkey_t lkey = NULL;
  2490. gnutls_pubkey_t pkey = NULL;
  2491. gnutls_datum_t pdata = { };
  2492. struct oc_text_buf *buf = NULL;
  2493. #if GNUTLS_VERSION_NUMBER >= 0x030500
  2494. pk = gnutls_ecc_curve_get_pk(GNUTLS_ECC_CURVE_SECP256R1);
  2495. #else
  2496. pk = GNUTLS_PK_EC;
  2497. #endif
  2498. bits = GNUTLS_CURVE_TO_BITS(GNUTLS_ECC_CURVE_SECP256R1);
  2499. err = gnutls_privkey_init(&lkey);
  2500. if (err)
  2501. goto out;
  2502. if (privder_in)
  2503. err = gnutls_privkey_import_x509_raw(lkey, privder_in, GNUTLS_X509_FMT_DER,
  2504. NULL, 0);
  2505. else
  2506. err = gnutls_privkey_generate(lkey, pk, bits, 0);
  2507. if (err)
  2508. goto out;
  2509. err = gnutls_pubkey_init(&pkey);
  2510. if (err)
  2511. goto out;
  2512. err = gnutls_pubkey_import_privkey(pkey, lkey,
  2513. GNUTLS_KEY_KEY_AGREEMENT, 0);
  2514. if (err)
  2515. goto out;
  2516. err = gnutls_pubkey_export2(pkey, GNUTLS_X509_FMT_DER, &pdata);
  2517. if (err)
  2518. goto out;
  2519. buf = buf_alloc();
  2520. buf_append_base64(buf, pdata.data, pdata.size, 0);
  2521. if (buf_error(buf)) {
  2522. err = GNUTLS_E_MEMORY_ERROR;
  2523. goto out;
  2524. }
  2525. gnutls_privkey_deinit(*key);
  2526. *key = lkey;
  2527. free(*pubkey);
  2528. *pubkey = buf->data;
  2529. buf->data = NULL;
  2530. out:
  2531. buf_free(buf);
  2532. gnutls_pubkey_deinit(pkey);
  2533. if (err) {
  2534. gnutls_privkey_deinit(lkey);
  2535. *key = NULL;
  2536. *pubkey = NULL;
  2537. pubder = NULL; /* So we don't return it... */
  2538. }
  2539. if (pubder)
  2540. *pubder = pdata;
  2541. else
  2542. gnutls_free(pdata.data);
  2543. return err;
  2544. }
  2545. int generate_strap_keys(struct openconnect_info *vpninfo)
  2546. {
  2547. int err;
  2548. err = generate_strap_key(&vpninfo->strap_key, &vpninfo->strap_pubkey, NULL, NULL);
  2549. if (err) {
  2550. vpn_progress(vpninfo, PRG_ERR,
  2551. _("Failed to generate STRAP key: %s\n"),
  2552. gnutls_strerror(err));
  2553. free_strap_keys(vpninfo);
  2554. return -EIO;
  2555. }
  2556. err = generate_strap_key(&vpninfo->strap_dh_key, &vpninfo->strap_dh_pubkey, NULL, NULL);
  2557. if (err) {
  2558. vpn_progress(vpninfo, PRG_ERR,
  2559. _("Failed to generate STRAP DH key: %s\n"),
  2560. gnutls_strerror(err));
  2561. free_strap_keys(vpninfo);
  2562. return -EIO;
  2563. }
  2564. return 0;
  2565. }
  2566. void free_strap_keys(struct openconnect_info *vpninfo)
  2567. {
  2568. if (vpninfo->strap_key)
  2569. gnutls_privkey_deinit(vpninfo->strap_key);
  2570. if (vpninfo->strap_dh_key)
  2571. gnutls_privkey_deinit(vpninfo->strap_dh_key);
  2572. vpninfo->strap_key = vpninfo->strap_dh_key = NULL;
  2573. }
  2574. #ifdef HAVE_HPKE_SUPPORT
  2575. #include <nettle/ecc.h>
  2576. #include <nettle/ecc-curve.h>
  2577. int ecdh_compute_secp256r1(struct openconnect_info *vpninfo, const unsigned char *pubkey_der,
  2578. int pubkey_len, unsigned char *secret)
  2579. {
  2580. int err, ret = -EIO;
  2581. gnutls_pubkey_t pubkey;
  2582. gnutls_datum_t d = { (void *)pubkey_der, pubkey_len };
  2583. if ((err = gnutls_pubkey_init(&pubkey)) ||
  2584. (err = gnutls_pubkey_import(pubkey, &d, GNUTLS_X509_FMT_DER))) {
  2585. vpn_progress(vpninfo, PRG_ERR,
  2586. _("Failed to decode server DH key: %s\n"),
  2587. gnutls_strerror(err));
  2588. goto out_pubkey;
  2589. }
  2590. /* Yay, we have to do ECDH for ourselves. */
  2591. gnutls_datum_t pub_x, pub_y, priv_k;
  2592. gnutls_ecc_curve_t pub_curve, priv_curve;
  2593. if ((err = gnutls_privkey_export_ecc_raw(vpninfo->strap_dh_key, &priv_curve,
  2594. NULL, NULL, &priv_k))) {
  2595. vpn_progress(vpninfo, PRG_ERR,
  2596. _("Failed to export DH private key parameters: %s\n"),
  2597. gnutls_strerror(err));
  2598. goto out_pubkey;
  2599. }
  2600. if ((err = gnutls_pubkey_export_ecc_raw(pubkey, &pub_curve, &pub_x, &pub_y))) {
  2601. vpn_progress(vpninfo, PRG_ERR,
  2602. _("Failed to export server DH key parameters: %s\n"),
  2603. gnutls_strerror(err));
  2604. goto out_priv_data;
  2605. }
  2606. if (pub_curve != GNUTLS_ECC_CURVE_SECP256R1 ||
  2607. priv_curve != GNUTLS_ECC_CURVE_SECP256R1) {
  2608. vpn_progress(vpninfo, PRG_ERR,
  2609. _("HPKE uses unsupported EC curve (%d, %d)\n"),
  2610. pub_curve, priv_curve);
  2611. goto out_pub_data;
  2612. }
  2613. mpz_t mx, my;
  2614. nettle_mpz_init_set_str_256_u(mx, pub_x.size, pub_x.data);
  2615. nettle_mpz_init_set_str_256_u(my, pub_y.size, pub_y.data);
  2616. struct ecc_point point;
  2617. ecc_point_init(&point, nettle_get_secp_256r1());
  2618. if (!ecc_point_set(&point, mx, my)) {
  2619. vpn_progress(vpninfo, PRG_ERR,
  2620. _("Failed to create ECC public point for ECDH\n"));
  2621. goto out_point;
  2622. }
  2623. mpz_t mk;
  2624. nettle_mpz_init_set_str_256_u(mk, priv_k.size, priv_k.data);
  2625. struct ecc_scalar priv;
  2626. ecc_scalar_init(&priv, nettle_get_secp_256r1());
  2627. ecc_scalar_set(&priv, mk);
  2628. ecc_point_mul(&point, &priv, &point);
  2629. ecc_point_get(&point, mx, my);
  2630. nettle_mpz_get_str_256(32, secret, mx);
  2631. ret = 0;
  2632. ecc_scalar_clear(&priv);
  2633. mpz_clear(mk);
  2634. out_point:
  2635. ecc_point_clear(&point);
  2636. mpz_clear(mx);
  2637. mpz_clear(my);
  2638. out_pub_data:
  2639. gnutls_free(pub_x.data);
  2640. gnutls_free(pub_y.data);
  2641. out_priv_data:
  2642. gnutls_free(priv_k.data);
  2643. out_pubkey:
  2644. gnutls_pubkey_deinit(pubkey);
  2645. return ret;
  2646. }
  2647. int hkdf_sha256_extract_expand(struct openconnect_info *vpninfo, unsigned char *buf,
  2648. const unsigned char *info, int infolen)
  2649. {
  2650. const gnutls_datum_t d = { buf, SHA256_SIZE };
  2651. int err = gnutls_hkdf_extract(GNUTLS_MAC_SHA256, &d, NULL, buf);
  2652. if (err) {
  2653. vpn_progress(vpninfo, PRG_ERR,
  2654. _("HKDF extract failed: %s\n"),
  2655. gnutls_strerror(err));
  2656. return -EIO;
  2657. }
  2658. const gnutls_datum_t info_d = { (unsigned char *)info, infolen };
  2659. err = gnutls_hkdf_expand(GNUTLS_MAC_SHA256, &d, &info_d, d.data, d.size);
  2660. if (err) {
  2661. vpn_progress(vpninfo, PRG_ERR,
  2662. _("HKDF expand failed: %s\n"),
  2663. gnutls_strerror(err));
  2664. return -EIO;
  2665. }
  2666. return 0;
  2667. }
  2668. int aes_256_gcm_decrypt(struct openconnect_info *vpninfo, unsigned char *key,
  2669. unsigned char *data, int len,
  2670. unsigned char *iv, unsigned char *tag)
  2671. {
  2672. gnutls_cipher_hd_t h = NULL;
  2673. const gnutls_datum_t d = { key, SHA256_SIZE };
  2674. const gnutls_datum_t iv_d = { iv, 12 };
  2675. int err = gnutls_cipher_init(&h, GNUTLS_CIPHER_AES_256_GCM, &d, &iv_d);
  2676. if (err) {
  2677. vpn_progress(vpninfo, PRG_ERR,
  2678. _("Failed to init AES-256-GCM cipher: %s\n"),
  2679. gnutls_strerror(err));
  2680. return -EIO;
  2681. }
  2682. err = gnutls_cipher_decrypt(h, data, len);
  2683. if (err) {
  2684. dec_fail:
  2685. vpn_progress(vpninfo, PRG_ERR,
  2686. _("SSO token decryption failed: %s\n"),
  2687. gnutls_strerror(err));
  2688. gnutls_cipher_deinit(h);
  2689. return -EIO;
  2690. }
  2691. /* Reusing the key buffer to fetch the auth tag */
  2692. err = gnutls_cipher_tag(h, d.data, 12);
  2693. if (err)
  2694. goto dec_fail;
  2695. if (memcmp(d.data, tag, 12)) {
  2696. err = GNUTLS_E_MAC_VERIFY_FAILED;
  2697. goto dec_fail;
  2698. }
  2699. gnutls_cipher_deinit(h);
  2700. return 0;
  2701. }
  2702. void append_strap_privkey(struct openconnect_info *vpninfo,
  2703. struct oc_text_buf *buf)
  2704. {
  2705. gnutls_x509_privkey_t xk = NULL;
  2706. gnutls_datum_t d = { NULL, 0 };
  2707. if (!gnutls_privkey_export_x509(vpninfo->strap_key, &xk) &&
  2708. !gnutls_x509_privkey_export2(xk, GNUTLS_X509_FMT_DER, &d)) {
  2709. buf_append_base64(buf, d.data, d.size, 0);
  2710. gnutls_free(d.data);
  2711. }
  2712. gnutls_x509_privkey_deinit(xk);
  2713. }
  2714. int ingest_strap_privkey(struct openconnect_info *vpninfo,
  2715. unsigned char *der, int len)
  2716. {
  2717. gnutls_datum_t d = { der, len };
  2718. int err = generate_strap_key(&vpninfo->strap_key, &vpninfo->strap_pubkey, &d, NULL);
  2719. if (err) {
  2720. vpn_progress(vpninfo, PRG_ERR,
  2721. _("Failed to decode STRAP key: %s\n"),
  2722. gnutls_strerror(err));
  2723. return -EIO;
  2724. }
  2725. return 0;
  2726. }
  2727. void append_strap_verify(struct openconnect_info *vpninfo,
  2728. struct oc_text_buf *buf, int rekey)
  2729. {
  2730. gnutls_privkey_t sign_key = vpninfo->strap_key;
  2731. int err;
  2732. /* Concatenate our Finished message with our pubkey to be signed */
  2733. struct oc_text_buf *nonce = buf_alloc();
  2734. buf_append_bytes(nonce, vpninfo->finished, vpninfo->finished_len);
  2735. if (rekey) {
  2736. /* We have a copy and we don't want it freed just yet */
  2737. vpninfo->strap_key = NULL;
  2738. gnutls_datum_t pubkey_der;
  2739. err = generate_strap_key(&vpninfo->strap_key, &vpninfo->strap_pubkey,
  2740. NULL, &pubkey_der);
  2741. if (err) {
  2742. vpn_progress(vpninfo, PRG_ERR,
  2743. _("Failed to regenerate STRAP key: %s\n"),
  2744. gnutls_strerror(err));
  2745. vpninfo->strap_key = sign_key;
  2746. if (!buf_error(buf))
  2747. buf->error = -EIO;
  2748. buf_free(nonce);
  2749. return;
  2750. }
  2751. buf_append_bytes(nonce, pubkey_der.data, pubkey_der.size);
  2752. gnutls_free(pubkey_der.data);
  2753. } else {
  2754. int len;
  2755. unsigned char *der = openconnect_base64_decode(&len, vpninfo->strap_pubkey);
  2756. if (!der) {
  2757. vpn_progress(vpninfo, PRG_ERR,
  2758. _("Failed to generate STRAP key DER\n"));
  2759. if (!buf_error(buf))
  2760. buf->error = -EIO;
  2761. buf_free(nonce);
  2762. return;
  2763. }
  2764. buf_append_bytes(nonce, der, len);
  2765. free(der);
  2766. }
  2767. err = GNUTLS_E_MEMORY_ERROR;
  2768. if (buf_error(nonce)) {
  2769. buf_free(nonce);
  2770. goto fail;
  2771. }
  2772. gnutls_datum_t nd = { (void *)nonce->data, nonce->pos };
  2773. gnutls_datum_t sig = { NULL, 0 };
  2774. err = gnutls_privkey_sign_data(sign_key, GNUTLS_DIG_SHA256,
  2775. 0, &nd, &sig);
  2776. if (rekey)
  2777. gnutls_privkey_deinit(sign_key);
  2778. buf_free(nonce);
  2779. if (err) {
  2780. fail:
  2781. vpn_progress(vpninfo, PRG_ERR, _("STRAP signature failed: %s\n"),
  2782. gnutls_strerror(err));
  2783. if (!buf_error(buf))
  2784. buf->error = -EIO;
  2785. return;
  2786. }
  2787. buf_append_base64(buf, sig.data, sig.size, 0);
  2788. gnutls_free(sig.data);
  2789. }
  2790. #endif /* HAVE_HPKE_SUPPORT */
  2791. /**
  2792. * multiple-client certificate authentication
  2793. */
  2794. static int app_error(int err)
  2795. {
  2796. if (err >= 0)
  2797. return 0;
  2798. switch (err) {
  2799. case GNUTLS_E_MEMORY_ERROR:
  2800. return -ENOMEM;
  2801. case GNUTLS_E_ILLEGAL_PARAMETER:
  2802. case GNUTLS_E_INVALID_REQUEST:
  2803. return -EINVAL;
  2804. case GNUTLS_E_CONSTRAINT_ERROR:
  2805. case GNUTLS_E_UNSUPPORTED_SIGNATURE_ALGORITHM:
  2806. default:
  2807. return -EIO;
  2808. }
  2809. }
  2810. static int to_text_buf(struct oc_text_buf **bufp,
  2811. const gnutls_datum_t *datum)
  2812. {
  2813. struct oc_text_buf *buf;
  2814. *bufp = NULL;
  2815. if (!(datum->size <= INT_MAX))
  2816. return GNUTLS_E_MEMORY_ERROR;
  2817. buf = buf_alloc();
  2818. if (!buf)
  2819. return GNUTLS_E_MEMORY_ERROR;
  2820. buf_append_bytes(buf, datum->data, (int) datum->size);
  2821. if (buf_error(buf) < 0)
  2822. goto fail;
  2823. *bufp = buf;
  2824. return 0;
  2825. fail:
  2826. buf_free(buf);
  2827. return GNUTLS_E_MEMORY_ERROR;
  2828. }
  2829. static int check_multicert_compat(struct openconnect_info *vpninfo,
  2830. struct cert_info *certinfo)
  2831. {
  2832. #ifndef GNUTLS_KP_ANY
  2833. # define GNUTLS_KP_ANY "2.5.29.37.0"
  2834. #endif
  2835. #ifndef GNUTLS_KP_TLS_WWW_CLIENT
  2836. # define GNUTLS_KP_TLS_WWW_CLIENT "1.3.6.1.5.5.7.3.2"
  2837. #endif
  2838. #ifndef GNUTLS_KP_MS_SMART_CARD_LOGON
  2839. # define GNUTLS_KP_MS_SMART_CARD_LOGON "1.3.6.1.4.1.311.20.2.2"
  2840. #endif
  2841. #define MAX_OID 128
  2842. char oid[MAX_OID];
  2843. struct gtls_cert_info *gci = certinfo->priv_info;
  2844. gnutls_x509_crt_t crt;
  2845. unsigned int usage = 0, critical;
  2846. gnutls_pk_algorithm_t pk;
  2847. size_t kp;
  2848. int err;
  2849. /**
  2850. * Multiple certificate authentication protocol parametrizes the
  2851. * digest independently of the pk algorithm. Warn if the signature
  2852. * algorithm doesn't operate this way. Warn if this isn't so.
  2853. */
  2854. crt = gci->certs[0];
  2855. pk = gnutls_x509_crt_get_pk_algorithm(crt, NULL);
  2856. switch (pk) {
  2857. default:
  2858. vpn_progress(vpninfo, PRG_INFO,
  2859. _("Certificate may be multiple certificate authentication incompatible.\n"));
  2860. break;
  2861. case GNUTLS_PK_RSA:
  2862. #if GNUTLS_VERSION_NUMBER >= 0x030600
  2863. case GNUTLS_PK_RSA_PSS:
  2864. #endif
  2865. case GNUTLS_PK_DSA:
  2866. #if GNUTLS_VERSION_NUMBER >= 0x030500
  2867. case GNUTLS_PK_ECDSA:
  2868. #else
  2869. case GNUTLS_PK_EC:
  2870. #endif
  2871. break;
  2872. }
  2873. /**
  2874. * Now check if the certificate supports client authentication.
  2875. *
  2876. * extendedKeyUsage of either any, clientAuth, or msSmartcardLogin
  2877. * satisfy authentication purposes.
  2878. */
  2879. for (kp = 0; ; kp++) {
  2880. size_t oid_size = sizeof(oid);
  2881. err = gnutls_x509_crt_get_key_purpose_oid(crt, kp,
  2882. oid, &oid_size,
  2883. &critical);
  2884. if (err == GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE) {
  2885. /* EOF */
  2886. break;
  2887. } else if (err == GNUTLS_E_SHORT_MEMORY_BUFFER) {
  2888. /**
  2889. * The oids we are concerned with have length less than
  2890. * MAX_OID
  2891. */
  2892. continue;
  2893. } else if (err < 0) {
  2894. vpn_progress(vpninfo, PRG_DEBUG,
  2895. _("gnutls_x509_crt_get_key_purpose_oid: %s.\n"),
  2896. gnutls_strerror(err));
  2897. return -1;
  2898. }
  2899. if (strcmp(oid, GNUTLS_KP_ANY) == 0 ||
  2900. strcmp(oid, GNUTLS_KP_TLS_WWW_CLIENT) == 0 ||
  2901. strcmp(oid, GNUTLS_KP_MS_SMART_CARD_LOGON) == 0)
  2902. return 1;
  2903. }
  2904. /**
  2905. * The certificate does not specify extendedKeyUsage; try
  2906. * keyUsage.
  2907. */
  2908. if (kp == 0) {
  2909. /**
  2910. * keyUsage of digitalSignature, nonRepudiation, or
  2911. * both satisfy authenticatio.n
  2912. */
  2913. err = gnutls_x509_crt_get_key_usage(crt, &usage, &critical);
  2914. if (err < 0 && err != GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE) {
  2915. vpn_progress(vpninfo, PRG_DEBUG,
  2916. _("gnutls_X509_crt_get_key_usage: %s.\n"),
  2917. gnutls_strerror(err));
  2918. }
  2919. if (err < 0)
  2920. usage = 0;
  2921. if (usage&
  2922. (GNUTLS_KEY_DIGITAL_SIGNATURE|GNUTLS_KEY_NON_REPUDIATION))
  2923. return 1;
  2924. }
  2925. /**
  2926. * extendedKeyUsage, keyUsage, or both are specified, but
  2927. * purposes are incompatible for authentication.
  2928. */
  2929. if (kp > 0 || usage != 0) {
  2930. vpn_progress(vpninfo, PRG_INFO,
  2931. _("The certificate specifies key usages incompatible with authentication.\n"));
  2932. return 0;
  2933. }
  2934. /**
  2935. * Found neither keyUsage nor extendedKeyUsage, defaults to any
  2936. * purpose.
  2937. */
  2938. vpn_progress(vpninfo, PRG_INFO,
  2939. _("Certificate doesn't specify key usage.\n"));
  2940. return 1;
  2941. }
  2942. int export_certificate_pkcs7(struct openconnect_info *vpninfo,
  2943. struct cert_info *certinfo,
  2944. cert_format_t format,
  2945. struct oc_text_buf **pp7b)
  2946. {
  2947. struct gtls_cert_info *gci = certinfo->priv_info;
  2948. gnutls_pkcs7_t pkcs7;
  2949. gnutls_datum_t data;
  2950. gnutls_x509_crt_fmt_t certform;
  2951. int err;
  2952. if (!gci) {
  2953. vpn_progress(vpninfo, PRG_ERR,
  2954. _("Precondition failed %s[%s]:%d\n"),
  2955. __FILE__, __func__, __LINE__);
  2956. return -EINVAL;
  2957. }
  2958. *pp7b = NULL;
  2959. /**
  2960. * Note! The PKCS7 structure produced by GnuTLS and this code is
  2961. * different from protocol captures in three ways:
  2962. *
  2963. * x: The root certificate is not added.
  2964. *
  2965. * x: The first object of the signedData is a OBJECT IDENTIFIER
  2966. * digestedData (1 2 840 113549 1 7 5), instead of OBJECT IDENTIFIER
  2967. * data (1 2 840 113549 1 7 1) and zero-length * OCTET STRING.
  2968. *
  2969. * x: Certificates are ordered in ASN.1 canonical order instead of the
  2970. * order added. The practical consequence is the server must identity
  2971. * the user certificate (a certificate for which basicConstraints
  2972. * CA:TRUE is false) and reconstruct the certificate path.
  2973. *
  2974. * Testing has not shown that these differences are meaningful, but the
  2975. * future will tell.
  2976. */
  2977. err = gnutls_pkcs7_init(&pkcs7);
  2978. if (err < 0)
  2979. goto error;
  2980. for (size_t i = 0, ncerts = gci->nr_certs; i < ncerts; i++) {
  2981. err = gnutls_pkcs7_set_crt(pkcs7, gci->certs[i]);
  2982. if (err < 0)
  2983. goto error;
  2984. }
  2985. if (format == CERT_FORMAT_ASN1) {
  2986. certform = GNUTLS_X509_FMT_DER;
  2987. } else if (format == CERT_FORMAT_PEM) {
  2988. certform = GNUTLS_X509_FMT_PEM;
  2989. } else {
  2990. err = GNUTLS_E_INVALID_REQUEST;
  2991. goto error;
  2992. }
  2993. err = gnutls_pkcs7_export2(pkcs7, certform, &data);
  2994. if (err < 0)
  2995. goto error;
  2996. err = to_text_buf(pp7b, &data);
  2997. gnutls_free(data.data);
  2998. if (err < 0) {
  2999. error:
  3000. vpn_progress(vpninfo, PRG_ERR,
  3001. _("Failed to generate the PKCS#7 structure: %s.\n"),
  3002. gnutls_strerror(err));
  3003. }
  3004. gnutls_pkcs7_deinit(pkcs7);
  3005. return app_error(err);
  3006. }
  3007. int multicert_sign_data(struct openconnect_info *vpninfo,
  3008. struct cert_info *certinfo,
  3009. unsigned int hashes,
  3010. const void *data, size_t len,
  3011. struct oc_text_buf **psig)
  3012. {
  3013. static const struct {
  3014. openconnect_hash_type id;
  3015. gnutls_digest_algorithm_t hash;
  3016. } hash_map[] = {
  3017. { OPENCONNECT_HASH_SHA512, GNUTLS_DIG_SHA512 },
  3018. { OPENCONNECT_HASH_SHA384, GNUTLS_DIG_SHA384 },
  3019. { OPENCONNECT_HASH_SHA256, GNUTLS_DIG_SHA256 },
  3020. { OPENCONNECT_HASH_UNKNOWN, GNUTLS_DIG_UNKNOWN },
  3021. };
  3022. gnutls_datum_t datum = { (void *) data, len };
  3023. gnutls_datum_t sign_data = { 0 };
  3024. struct gtls_cert_info *gci = certinfo->priv_info;
  3025. struct oc_text_buf *sig_buf = NULL;
  3026. openconnect_hash_type hash;
  3027. gnutls_pk_algorithm_t pk;
  3028. gnutls_sign_algorithm_t sign;
  3029. int ret, err;
  3030. if (!(gci && data && len && psig)) {
  3031. vpn_progress(vpninfo, PRG_ERR,
  3032. _("Precondition failed %s[%s]:%d.\n"),
  3033. __FILE__, __func__, __LINE__);
  3034. return -EINVAL;
  3035. }
  3036. err = gnutls_x509_crt_get_pk_algorithm(gci->certs[0], NULL);
  3037. if (err < 0)
  3038. goto error;
  3039. pk = err;
  3040. /**
  3041. * Sign data using hashes with decreasing hash size as
  3042. * Anyconnect prefers SHA2-512.
  3043. */
  3044. for (size_t i = 0;
  3045. (hash = hash_map[i].id) != OPENCONNECT_HASH_UNKNOWN;
  3046. i++) {
  3047. if ((hashes & MULTICERT_HASH_FLAG(hash)) == 0)
  3048. continue;
  3049. sign = gnutls_pk_to_sign(pk, hash_map[i].hash);
  3050. #if GNUTLS_VERSION_NUMBER >= 0x030600
  3051. err = gnutls_privkey_sign_data2(gci->pkey,
  3052. sign,
  3053. /* flag */ 0, &datum, &sign_data);
  3054. #else
  3055. err = gnutls_privkey_sign_data(gci->pkey,
  3056. gnutls_sign_get_hash_algorithm(sign),
  3057. /* flag */ 0, &datum, &sign_data);
  3058. #endif
  3059. if (err < 0) {
  3060. vpn_progress(vpninfo, PRG_DEBUG,
  3061. _("gnutls_privkey_sign_data: %s.\n"),
  3062. gnutls_strerror(err));
  3063. if (err == GNUTLS_E_INVALID_REQUEST || err == GNUTLS_E_CONSTRAINT_ERROR)
  3064. continue;
  3065. goto error;
  3066. }
  3067. err = to_text_buf(&sig_buf, &sign_data);
  3068. gnutls_free(sign_data.data);
  3069. break;
  3070. }
  3071. /**
  3072. * Since we tested for compatibility when we loaded the certificate,
  3073. * this condition is unlikely to happen.
  3074. */
  3075. if (hash == OPENCONNECT_HASH_UNKNOWN)
  3076. err = GNUTLS_E_UNSUPPORTED_SIGNATURE_ALGORITHM;
  3077. if (err < 0) {
  3078. error:
  3079. vpn_progress(vpninfo, PRG_ERR,
  3080. _("Failed to sign data with second certificate: %s.\n"),
  3081. gnutls_strerror(err));
  3082. ret = app_error(err);
  3083. goto done;
  3084. }
  3085. *psig = sig_buf;
  3086. ret = hash;
  3087. done:
  3088. return ret;
  3089. }