kssl.c 75 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271
  1. /* ssl/kssl.c */
  2. /*
  3. * Written by Vern Staats <[email protected]> for the OpenSSL project
  4. * 2000.
  5. */
  6. /* ====================================================================
  7. * Copyright (c) 2000-2018 The OpenSSL Project. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. *
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in
  18. * the documentation and/or other materials provided with the
  19. * distribution.
  20. *
  21. * 3. All advertising materials mentioning features or use of this
  22. * software must display the following acknowledgment:
  23. * "This product includes software developed by the OpenSSL Project
  24. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  25. *
  26. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  27. * endorse or promote products derived from this software without
  28. * prior written permission. For written permission, please contact
  29. * [email protected].
  30. *
  31. * 5. Products derived from this software may not be called "OpenSSL"
  32. * nor may "OpenSSL" appear in their names without prior written
  33. * permission of the OpenSSL Project.
  34. *
  35. * 6. Redistributions of any form whatsoever must retain the following
  36. * acknowledgment:
  37. * "This product includes software developed by the OpenSSL Project
  38. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  41. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  43. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  44. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  45. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  46. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  47. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  49. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  50. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  51. * OF THE POSSIBILITY OF SUCH DAMAGE.
  52. * ====================================================================
  53. *
  54. * This product includes cryptographic software written by Eric Young
  55. * ([email protected]). This product includes software written by Tim
  56. * Hudson ([email protected]).
  57. *
  58. */
  59. /*-
  60. * ssl/kssl.c -- Routines to support (& debug) Kerberos5 auth for openssl
  61. *
  62. * 19990701 VRS Started.
  63. * 200011?? Jeffrey Altman, Richard Levitte
  64. * Generalized for Heimdal, Newer MIT, & Win32.
  65. * Integrated into main OpenSSL 0.9.7 snapshots.
  66. * 20010413 Simon Wilkinson, VRS
  67. * Real RFC2712 KerberosWrapper replaces AP_REQ.
  68. */
  69. #include <openssl/opensslconf.h>
  70. #include <string.h>
  71. #define KRB5_PRIVATE 1
  72. #include <openssl/ssl.h>
  73. #include <openssl/evp.h>
  74. #include <openssl/objects.h>
  75. #include <openssl/krb5_asn.h>
  76. #include "o_time.h"
  77. #include "kssl_lcl.h"
  78. #ifndef OPENSSL_NO_KRB5
  79. # ifndef ENOMEM
  80. # define ENOMEM KRB5KRB_ERR_GENERIC
  81. # endif
  82. /*
  83. * When OpenSSL is built on Windows, we do not want to require that
  84. * the Kerberos DLLs be available in order for the OpenSSL DLLs to
  85. * work. Therefore, all Kerberos routines are loaded at run time
  86. * and we do not link to a .LIB file.
  87. */
  88. # if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32)
  89. /*
  90. * The purpose of the following pre-processor statements is to provide
  91. * compatibility with different releases of MIT Kerberos for Windows.
  92. * All versions up to 1.2 used macros. But macros do not allow for
  93. * a binary compatible interface for DLLs. Therefore, all macros are
  94. * being replaced by function calls. The following code will allow
  95. * an OpenSSL DLL built on Windows to work whether or not the macro
  96. * or function form of the routines are utilized.
  97. */
  98. # ifdef krb5_cc_get_principal
  99. # define NO_DEF_KRB5_CCACHE
  100. # undef krb5_cc_get_principal
  101. # endif
  102. # define krb5_cc_get_principal kssl_krb5_cc_get_principal
  103. # define krb5_free_data_contents kssl_krb5_free_data_contents
  104. # define krb5_free_context kssl_krb5_free_context
  105. # define krb5_auth_con_free kssl_krb5_auth_con_free
  106. # define krb5_free_principal kssl_krb5_free_principal
  107. # define krb5_mk_req_extended kssl_krb5_mk_req_extended
  108. # define krb5_get_credentials kssl_krb5_get_credentials
  109. # define krb5_cc_default kssl_krb5_cc_default
  110. # define krb5_sname_to_principal kssl_krb5_sname_to_principal
  111. # define krb5_init_context kssl_krb5_init_context
  112. # define krb5_free_ticket kssl_krb5_free_ticket
  113. # define krb5_rd_req kssl_krb5_rd_req
  114. # define krb5_kt_default kssl_krb5_kt_default
  115. # define krb5_kt_resolve kssl_krb5_kt_resolve
  116. /* macros in mit 1.2.2 and earlier; functions in mit 1.2.3 and greater */
  117. # ifndef krb5_kt_close
  118. # define krb5_kt_close kssl_krb5_kt_close
  119. # endif /* krb5_kt_close */
  120. # ifndef krb5_kt_get_entry
  121. # define krb5_kt_get_entry kssl_krb5_kt_get_entry
  122. # endif /* krb5_kt_get_entry */
  123. # define krb5_auth_con_init kssl_krb5_auth_con_init
  124. # define krb5_principal_compare kssl_krb5_principal_compare
  125. # define krb5_decrypt_tkt_part kssl_krb5_decrypt_tkt_part
  126. # define krb5_timeofday kssl_krb5_timeofday
  127. # define krb5_rc_default kssl_krb5_rc_default
  128. # ifdef krb5_rc_initialize
  129. # undef krb5_rc_initialize
  130. # endif
  131. # define krb5_rc_initialize kssl_krb5_rc_initialize
  132. # ifdef krb5_rc_get_lifespan
  133. # undef krb5_rc_get_lifespan
  134. # endif
  135. # define krb5_rc_get_lifespan kssl_krb5_rc_get_lifespan
  136. # ifdef krb5_rc_destroy
  137. # undef krb5_rc_destroy
  138. # endif
  139. # define krb5_rc_destroy kssl_krb5_rc_destroy
  140. # define valid_cksumtype kssl_valid_cksumtype
  141. # define krb5_checksum_size kssl_krb5_checksum_size
  142. # define krb5_kt_free_entry kssl_krb5_kt_free_entry
  143. # define krb5_auth_con_setrcache kssl_krb5_auth_con_setrcache
  144. # define krb5_auth_con_getrcache kssl_krb5_auth_con_getrcache
  145. # define krb5_get_server_rcache kssl_krb5_get_server_rcache
  146. /* Prototypes for built in stubs */
  147. void kssl_krb5_free_data_contents(krb5_context, krb5_data *);
  148. void kssl_krb5_free_principal(krb5_context, krb5_principal);
  149. krb5_error_code kssl_krb5_kt_resolve(krb5_context,
  150. krb5_const char *, krb5_keytab *);
  151. krb5_error_code kssl_krb5_kt_default(krb5_context, krb5_keytab *);
  152. krb5_error_code kssl_krb5_free_ticket(krb5_context, krb5_ticket *);
  153. krb5_error_code kssl_krb5_rd_req(krb5_context, krb5_auth_context *,
  154. krb5_const krb5_data *,
  155. krb5_const_principal, krb5_keytab,
  156. krb5_flags *, krb5_ticket **);
  157. krb5_boolean kssl_krb5_principal_compare(krb5_context, krb5_const_principal,
  158. krb5_const_principal);
  159. krb5_error_code kssl_krb5_mk_req_extended(krb5_context,
  160. krb5_auth_context *,
  161. krb5_const krb5_flags,
  162. krb5_data *,
  163. krb5_creds *, krb5_data *);
  164. krb5_error_code kssl_krb5_init_context(krb5_context *);
  165. void kssl_krb5_free_context(krb5_context);
  166. krb5_error_code kssl_krb5_cc_default(krb5_context, krb5_ccache *);
  167. krb5_error_code kssl_krb5_sname_to_principal(krb5_context,
  168. krb5_const char *,
  169. krb5_const char *,
  170. krb5_int32, krb5_principal *);
  171. krb5_error_code kssl_krb5_get_credentials(krb5_context,
  172. krb5_const krb5_flags,
  173. krb5_ccache,
  174. krb5_creds *, krb5_creds * *);
  175. krb5_error_code kssl_krb5_auth_con_init(krb5_context, krb5_auth_context *);
  176. krb5_error_code kssl_krb5_cc_get_principal(krb5_context context,
  177. krb5_ccache cache,
  178. krb5_principal *principal);
  179. krb5_error_code kssl_krb5_auth_con_free(krb5_context, krb5_auth_context);
  180. size_t kssl_krb5_checksum_size(krb5_context context, krb5_cksumtype ctype);
  181. krb5_boolean kssl_valid_cksumtype(krb5_cksumtype ctype);
  182. krb5_error_code krb5_kt_free_entry(krb5_context, krb5_keytab_entry FAR *);
  183. krb5_error_code kssl_krb5_auth_con_setrcache(krb5_context,
  184. krb5_auth_context, krb5_rcache);
  185. krb5_error_code kssl_krb5_get_server_rcache(krb5_context,
  186. krb5_const krb5_data *,
  187. krb5_rcache *);
  188. krb5_error_code kssl_krb5_auth_con_getrcache(krb5_context,
  189. krb5_auth_context,
  190. krb5_rcache *);
  191. /* Function pointers (almost all Kerberos functions are _stdcall) */
  192. static void (_stdcall *p_krb5_free_data_contents) (krb5_context, krb5_data *)
  193. = NULL;
  194. static void (_stdcall *p_krb5_free_principal) (krb5_context, krb5_principal)
  195. = NULL;
  196. static krb5_error_code(_stdcall *p_krb5_kt_resolve)
  197. (krb5_context, krb5_const char *, krb5_keytab *) = NULL;
  198. static krb5_error_code(_stdcall *p_krb5_kt_default) (krb5_context,
  199. krb5_keytab *) = NULL;
  200. static krb5_error_code(_stdcall *p_krb5_free_ticket) (krb5_context,
  201. krb5_ticket *) = NULL;
  202. static krb5_error_code(_stdcall *p_krb5_rd_req) (krb5_context,
  203. krb5_auth_context *,
  204. krb5_const krb5_data *,
  205. krb5_const_principal,
  206. krb5_keytab, krb5_flags *,
  207. krb5_ticket **) = NULL;
  208. static krb5_error_code(_stdcall *p_krb5_mk_req_extended)
  209. (krb5_context, krb5_auth_context *,
  210. krb5_const krb5_flags, krb5_data *, krb5_creds *, krb5_data *) = NULL;
  211. static krb5_error_code(_stdcall *p_krb5_init_context) (krb5_context *) = NULL;
  212. static void (_stdcall *p_krb5_free_context) (krb5_context) = NULL;
  213. static krb5_error_code(_stdcall *p_krb5_cc_default) (krb5_context,
  214. krb5_ccache *) = NULL;
  215. static krb5_error_code(_stdcall *p_krb5_sname_to_principal)
  216. (krb5_context, krb5_const char *, krb5_const char *,
  217. krb5_int32, krb5_principal *) = NULL;
  218. static krb5_error_code(_stdcall *p_krb5_get_credentials)
  219. (krb5_context, krb5_const krb5_flags, krb5_ccache,
  220. krb5_creds *, krb5_creds **) = NULL;
  221. static krb5_error_code(_stdcall *p_krb5_auth_con_init)
  222. (krb5_context, krb5_auth_context *) = NULL;
  223. static krb5_error_code(_stdcall *p_krb5_cc_get_principal)
  224. (krb5_context context, krb5_ccache cache, krb5_principal *principal) = NULL;
  225. static krb5_error_code(_stdcall *p_krb5_auth_con_free)
  226. (krb5_context, krb5_auth_context) = NULL;
  227. static krb5_error_code(_stdcall *p_krb5_decrypt_tkt_part)
  228. (krb5_context, krb5_const krb5_keyblock *, krb5_ticket *) = NULL;
  229. static krb5_error_code(_stdcall *p_krb5_timeofday)
  230. (krb5_context context, krb5_int32 *timeret) = NULL;
  231. static krb5_error_code(_stdcall *p_krb5_rc_default)
  232. (krb5_context context, krb5_rcache *rc) = NULL;
  233. static krb5_error_code(_stdcall *p_krb5_rc_initialize)
  234. (krb5_context context, krb5_rcache rc, krb5_deltat lifespan) = NULL;
  235. static krb5_error_code(_stdcall *p_krb5_rc_get_lifespan)
  236. (krb5_context context, krb5_rcache rc, krb5_deltat *lifespan) = NULL;
  237. static krb5_error_code(_stdcall *p_krb5_rc_destroy)
  238. (krb5_context context, krb5_rcache rc) = NULL;
  239. static krb5_boolean(_stdcall *p_krb5_principal_compare)
  240. (krb5_context, krb5_const_principal, krb5_const_principal) = NULL;
  241. static size_t (_stdcall *p_krb5_checksum_size) (krb5_context context,
  242. krb5_cksumtype ctype) = NULL;
  243. static krb5_boolean(_stdcall *p_valid_cksumtype) (krb5_cksumtype ctype) =
  244. NULL;
  245. static krb5_error_code(_stdcall *p_krb5_kt_free_entry)
  246. (krb5_context, krb5_keytab_entry *) = NULL;
  247. static krb5_error_code(_stdcall *p_krb5_auth_con_setrcache) (krb5_context,
  248. krb5_auth_context,
  249. krb5_rcache) =
  250. NULL;
  251. static krb5_error_code(_stdcall *p_krb5_get_server_rcache) (krb5_context,
  252. krb5_const
  253. krb5_data *,
  254. krb5_rcache *) =
  255. NULL;
  256. static krb5_error_code(*p_krb5_auth_con_getrcache) (krb5_context,
  257. krb5_auth_context,
  258. krb5_rcache *) = NULL;
  259. static krb5_error_code(_stdcall *p_krb5_kt_close) (krb5_context context,
  260. krb5_keytab keytab) = NULL;
  261. static krb5_error_code(_stdcall *p_krb5_kt_get_entry) (krb5_context context,
  262. krb5_keytab keytab,
  263. krb5_const_principal
  264. principal,
  265. krb5_kvno vno,
  266. krb5_enctype enctype,
  267. krb5_keytab_entry
  268. *entry) = NULL;
  269. static int krb5_loaded = 0; /* only attempt to initialize func ptrs once */
  270. /* Function to Load the Kerberos 5 DLL and initialize function pointers */
  271. void load_krb5_dll(void)
  272. {
  273. HANDLE hKRB5_32;
  274. krb5_loaded++;
  275. hKRB5_32 = LoadLibrary(TEXT("KRB5_32"));
  276. if (!hKRB5_32)
  277. return;
  278. (FARPROC) p_krb5_free_data_contents =
  279. GetProcAddress(hKRB5_32, "krb5_free_data_contents");
  280. (FARPROC) p_krb5_free_context =
  281. GetProcAddress(hKRB5_32, "krb5_free_context");
  282. (FARPROC) p_krb5_auth_con_free =
  283. GetProcAddress(hKRB5_32, "krb5_auth_con_free");
  284. (FARPROC) p_krb5_free_principal =
  285. GetProcAddress(hKRB5_32, "krb5_free_principal");
  286. (FARPROC) p_krb5_mk_req_extended =
  287. GetProcAddress(hKRB5_32, "krb5_mk_req_extended");
  288. (FARPROC) p_krb5_get_credentials =
  289. GetProcAddress(hKRB5_32, "krb5_get_credentials");
  290. (FARPROC) p_krb5_cc_get_principal =
  291. GetProcAddress(hKRB5_32, "krb5_cc_get_principal");
  292. (FARPROC) p_krb5_cc_default = GetProcAddress(hKRB5_32, "krb5_cc_default");
  293. (FARPROC) p_krb5_sname_to_principal =
  294. GetProcAddress(hKRB5_32, "krb5_sname_to_principal");
  295. (FARPROC) p_krb5_init_context =
  296. GetProcAddress(hKRB5_32, "krb5_init_context");
  297. (FARPROC) p_krb5_free_ticket =
  298. GetProcAddress(hKRB5_32, "krb5_free_ticket");
  299. (FARPROC) p_krb5_rd_req = GetProcAddress(hKRB5_32, "krb5_rd_req");
  300. (FARPROC) p_krb5_principal_compare =
  301. GetProcAddress(hKRB5_32, "krb5_principal_compare");
  302. (FARPROC) p_krb5_decrypt_tkt_part =
  303. GetProcAddress(hKRB5_32, "krb5_decrypt_tkt_part");
  304. (FARPROC) p_krb5_timeofday = GetProcAddress(hKRB5_32, "krb5_timeofday");
  305. (FARPROC) p_krb5_rc_default = GetProcAddress(hKRB5_32, "krb5_rc_default");
  306. (FARPROC) p_krb5_rc_initialize =
  307. GetProcAddress(hKRB5_32, "krb5_rc_initialize");
  308. (FARPROC) p_krb5_rc_get_lifespan =
  309. GetProcAddress(hKRB5_32, "krb5_rc_get_lifespan");
  310. (FARPROC) p_krb5_rc_destroy = GetProcAddress(hKRB5_32, "krb5_rc_destroy");
  311. (FARPROC) p_krb5_kt_default = GetProcAddress(hKRB5_32, "krb5_kt_default");
  312. (FARPROC) p_krb5_kt_resolve = GetProcAddress(hKRB5_32, "krb5_kt_resolve");
  313. (FARPROC) p_krb5_auth_con_init =
  314. GetProcAddress(hKRB5_32, "krb5_auth_con_init");
  315. (FARPROC) p_valid_cksumtype = GetProcAddress(hKRB5_32, "valid_cksumtype");
  316. (FARPROC) p_krb5_checksum_size =
  317. GetProcAddress(hKRB5_32, "krb5_checksum_size");
  318. (FARPROC) p_krb5_kt_free_entry =
  319. GetProcAddress(hKRB5_32, "krb5_kt_free_entry");
  320. (FARPROC) p_krb5_auth_con_setrcache =
  321. GetProcAddress(hKRB5_32, "krb5_auth_con_setrcache");
  322. (FARPROC) p_krb5_get_server_rcache =
  323. GetProcAddress(hKRB5_32, "krb5_get_server_rcache");
  324. (FARPROC) p_krb5_auth_con_getrcache =
  325. GetProcAddress(hKRB5_32, "krb5_auth_con_getrcache");
  326. (FARPROC) p_krb5_kt_close = GetProcAddress(hKRB5_32, "krb5_kt_close");
  327. (FARPROC) p_krb5_kt_get_entry =
  328. GetProcAddress(hKRB5_32, "krb5_kt_get_entry");
  329. }
  330. /* Stubs for each function to be dynamicly loaded */
  331. void kssl_krb5_free_data_contents(krb5_context CO, krb5_data *data)
  332. {
  333. if (!krb5_loaded)
  334. load_krb5_dll();
  335. if (p_krb5_free_data_contents)
  336. p_krb5_free_data_contents(CO, data);
  337. }
  338. krb5_error_code
  339. kssl_krb5_mk_req_extended(krb5_context CO,
  340. krb5_auth_context *pACO,
  341. krb5_const krb5_flags F,
  342. krb5_data *pD1, krb5_creds *pC, krb5_data *pD2)
  343. {
  344. if (!krb5_loaded)
  345. load_krb5_dll();
  346. if (p_krb5_mk_req_extended)
  347. return (p_krb5_mk_req_extended(CO, pACO, F, pD1, pC, pD2));
  348. else
  349. return KRB5KRB_ERR_GENERIC;
  350. }
  351. krb5_error_code
  352. kssl_krb5_auth_con_init(krb5_context CO, krb5_auth_context *pACO)
  353. {
  354. if (!krb5_loaded)
  355. load_krb5_dll();
  356. if (p_krb5_auth_con_init)
  357. return (p_krb5_auth_con_init(CO, pACO));
  358. else
  359. return KRB5KRB_ERR_GENERIC;
  360. }
  361. krb5_error_code
  362. kssl_krb5_auth_con_free(krb5_context CO, krb5_auth_context ACO)
  363. {
  364. if (!krb5_loaded)
  365. load_krb5_dll();
  366. if (p_krb5_auth_con_free)
  367. return (p_krb5_auth_con_free(CO, ACO));
  368. else
  369. return KRB5KRB_ERR_GENERIC;
  370. }
  371. krb5_error_code
  372. kssl_krb5_get_credentials(krb5_context CO,
  373. krb5_const krb5_flags F,
  374. krb5_ccache CC, krb5_creds *pCR, krb5_creds **ppCR)
  375. {
  376. if (!krb5_loaded)
  377. load_krb5_dll();
  378. if (p_krb5_get_credentials)
  379. return (p_krb5_get_credentials(CO, F, CC, pCR, ppCR));
  380. else
  381. return KRB5KRB_ERR_GENERIC;
  382. }
  383. krb5_error_code
  384. kssl_krb5_sname_to_principal(krb5_context CO,
  385. krb5_const char *pC1,
  386. krb5_const char *pC2,
  387. krb5_int32 I, krb5_principal *pPR)
  388. {
  389. if (!krb5_loaded)
  390. load_krb5_dll();
  391. if (p_krb5_sname_to_principal)
  392. return (p_krb5_sname_to_principal(CO, pC1, pC2, I, pPR));
  393. else
  394. return KRB5KRB_ERR_GENERIC;
  395. }
  396. krb5_error_code kssl_krb5_cc_default(krb5_context CO, krb5_ccache *pCC)
  397. {
  398. if (!krb5_loaded)
  399. load_krb5_dll();
  400. if (p_krb5_cc_default)
  401. return (p_krb5_cc_default(CO, pCC));
  402. else
  403. return KRB5KRB_ERR_GENERIC;
  404. }
  405. krb5_error_code kssl_krb5_init_context(krb5_context *pCO)
  406. {
  407. if (!krb5_loaded)
  408. load_krb5_dll();
  409. if (p_krb5_init_context)
  410. return (p_krb5_init_context(pCO));
  411. else
  412. return KRB5KRB_ERR_GENERIC;
  413. }
  414. void kssl_krb5_free_context(krb5_context CO)
  415. {
  416. if (!krb5_loaded)
  417. load_krb5_dll();
  418. if (p_krb5_free_context)
  419. p_krb5_free_context(CO);
  420. }
  421. void kssl_krb5_free_principal(krb5_context c, krb5_principal p)
  422. {
  423. if (!krb5_loaded)
  424. load_krb5_dll();
  425. if (p_krb5_free_principal)
  426. p_krb5_free_principal(c, p);
  427. }
  428. krb5_error_code
  429. kssl_krb5_kt_resolve(krb5_context con, krb5_const char *sz, krb5_keytab *kt)
  430. {
  431. if (!krb5_loaded)
  432. load_krb5_dll();
  433. if (p_krb5_kt_resolve)
  434. return (p_krb5_kt_resolve(con, sz, kt));
  435. else
  436. return KRB5KRB_ERR_GENERIC;
  437. }
  438. krb5_error_code kssl_krb5_kt_default(krb5_context con, krb5_keytab *kt)
  439. {
  440. if (!krb5_loaded)
  441. load_krb5_dll();
  442. if (p_krb5_kt_default)
  443. return (p_krb5_kt_default(con, kt));
  444. else
  445. return KRB5KRB_ERR_GENERIC;
  446. }
  447. krb5_error_code kssl_krb5_free_ticket(krb5_context con, krb5_ticket *kt)
  448. {
  449. if (!krb5_loaded)
  450. load_krb5_dll();
  451. if (p_krb5_free_ticket)
  452. return (p_krb5_free_ticket(con, kt));
  453. else
  454. return KRB5KRB_ERR_GENERIC;
  455. }
  456. krb5_error_code
  457. kssl_krb5_rd_req(krb5_context con, krb5_auth_context *pacon,
  458. krb5_const krb5_data *data,
  459. krb5_const_principal princ, krb5_keytab keytab,
  460. krb5_flags *flags, krb5_ticket **pptkt)
  461. {
  462. if (!krb5_loaded)
  463. load_krb5_dll();
  464. if (p_krb5_rd_req)
  465. return (p_krb5_rd_req(con, pacon, data, princ, keytab, flags, pptkt));
  466. else
  467. return KRB5KRB_ERR_GENERIC;
  468. }
  469. krb5_boolean
  470. krb5_principal_compare(krb5_context con, krb5_const_principal princ1,
  471. krb5_const_principal princ2)
  472. {
  473. if (!krb5_loaded)
  474. load_krb5_dll();
  475. if (p_krb5_principal_compare)
  476. return (p_krb5_principal_compare(con, princ1, princ2));
  477. else
  478. return KRB5KRB_ERR_GENERIC;
  479. }
  480. krb5_error_code
  481. krb5_decrypt_tkt_part(krb5_context con, krb5_const krb5_keyblock *keys,
  482. krb5_ticket *ticket)
  483. {
  484. if (!krb5_loaded)
  485. load_krb5_dll();
  486. if (p_krb5_decrypt_tkt_part)
  487. return (p_krb5_decrypt_tkt_part(con, keys, ticket));
  488. else
  489. return KRB5KRB_ERR_GENERIC;
  490. }
  491. krb5_error_code krb5_timeofday(krb5_context con, krb5_int32 *timeret)
  492. {
  493. if (!krb5_loaded)
  494. load_krb5_dll();
  495. if (p_krb5_timeofday)
  496. return (p_krb5_timeofday(con, timeret));
  497. else
  498. return KRB5KRB_ERR_GENERIC;
  499. }
  500. krb5_error_code krb5_rc_default(krb5_context con, krb5_rcache *rc)
  501. {
  502. if (!krb5_loaded)
  503. load_krb5_dll();
  504. if (p_krb5_rc_default)
  505. return (p_krb5_rc_default(con, rc));
  506. else
  507. return KRB5KRB_ERR_GENERIC;
  508. }
  509. krb5_error_code
  510. krb5_rc_initialize(krb5_context con, krb5_rcache rc, krb5_deltat lifespan)
  511. {
  512. if (!krb5_loaded)
  513. load_krb5_dll();
  514. if (p_krb5_rc_initialize)
  515. return (p_krb5_rc_initialize(con, rc, lifespan));
  516. else
  517. return KRB5KRB_ERR_GENERIC;
  518. }
  519. krb5_error_code
  520. krb5_rc_get_lifespan(krb5_context con, krb5_rcache rc, krb5_deltat *lifespanp)
  521. {
  522. if (!krb5_loaded)
  523. load_krb5_dll();
  524. if (p_krb5_rc_get_lifespan)
  525. return (p_krb5_rc_get_lifespan(con, rc, lifespanp));
  526. else
  527. return KRB5KRB_ERR_GENERIC;
  528. }
  529. krb5_error_code krb5_rc_destroy(krb5_context con, krb5_rcache rc)
  530. {
  531. if (!krb5_loaded)
  532. load_krb5_dll();
  533. if (p_krb5_rc_destroy)
  534. return (p_krb5_rc_destroy(con, rc));
  535. else
  536. return KRB5KRB_ERR_GENERIC;
  537. }
  538. size_t krb5_checksum_size(krb5_context context, krb5_cksumtype ctype)
  539. {
  540. if (!krb5_loaded)
  541. load_krb5_dll();
  542. if (p_krb5_checksum_size)
  543. return (p_krb5_checksum_size(context, ctype));
  544. else
  545. return KRB5KRB_ERR_GENERIC;
  546. }
  547. krb5_boolean valid_cksumtype(krb5_cksumtype ctype)
  548. {
  549. if (!krb5_loaded)
  550. load_krb5_dll();
  551. if (p_valid_cksumtype)
  552. return (p_valid_cksumtype(ctype));
  553. else
  554. return KRB5KRB_ERR_GENERIC;
  555. }
  556. krb5_error_code krb5_kt_free_entry(krb5_context con, krb5_keytab_entry *entry)
  557. {
  558. if (!krb5_loaded)
  559. load_krb5_dll();
  560. if (p_krb5_kt_free_entry)
  561. return (p_krb5_kt_free_entry(con, entry));
  562. else
  563. return KRB5KRB_ERR_GENERIC;
  564. }
  565. /* Structure definitions */
  566. # ifndef NO_DEF_KRB5_CCACHE
  567. # ifndef krb5_x
  568. # define krb5_x(ptr,args) ((ptr)?((*(ptr)) args):(abort(),1))
  569. # define krb5_xc(ptr,args) ((ptr)?((*(ptr)) args):(abort(),(char*)0))
  570. # endif
  571. typedef krb5_pointer krb5_cc_cursor; /* cursor for sequential lookup */
  572. typedef struct _krb5_ccache {
  573. krb5_magic magic;
  574. struct _krb5_cc_ops FAR *ops;
  575. krb5_pointer data;
  576. } *krb5_ccache;
  577. typedef struct _krb5_cc_ops {
  578. krb5_magic magic;
  579. char *prefix;
  580. char *(KRB5_CALLCONV *get_name)
  581. (krb5_context, krb5_ccache);
  582. krb5_error_code(KRB5_CALLCONV *resolve)
  583. (krb5_context, krb5_ccache *, const char *);
  584. krb5_error_code(KRB5_CALLCONV *gen_new)
  585. (krb5_context, krb5_ccache *);
  586. krb5_error_code(KRB5_CALLCONV *init)
  587. (krb5_context, krb5_ccache, krb5_principal);
  588. krb5_error_code(KRB5_CALLCONV *destroy)
  589. (krb5_context, krb5_ccache);
  590. krb5_error_code(KRB5_CALLCONV *close)
  591. (krb5_context, krb5_ccache);
  592. krb5_error_code(KRB5_CALLCONV *store)
  593. (krb5_context, krb5_ccache, krb5_creds *);
  594. krb5_error_code(KRB5_CALLCONV *retrieve)
  595. (krb5_context, krb5_ccache, krb5_flags, krb5_creds *, krb5_creds *);
  596. krb5_error_code(KRB5_CALLCONV *get_princ)
  597. (krb5_context, krb5_ccache, krb5_principal *);
  598. krb5_error_code(KRB5_CALLCONV *get_first)
  599. (krb5_context, krb5_ccache, krb5_cc_cursor *);
  600. krb5_error_code(KRB5_CALLCONV *get_next)
  601. (krb5_context, krb5_ccache, krb5_cc_cursor *, krb5_creds *);
  602. krb5_error_code(KRB5_CALLCONV *end_get)
  603. (krb5_context, krb5_ccache, krb5_cc_cursor *);
  604. krb5_error_code(KRB5_CALLCONV *remove_cred)
  605. (krb5_context, krb5_ccache, krb5_flags, krb5_creds *);
  606. krb5_error_code(KRB5_CALLCONV *set_flags)
  607. (krb5_context, krb5_ccache, krb5_flags);
  608. } krb5_cc_ops;
  609. # endif /* NO_DEF_KRB5_CCACHE */
  610. krb5_error_code
  611. kssl_krb5_cc_get_principal
  612. (krb5_context context, krb5_ccache cache, krb5_principal *principal) {
  613. if (p_krb5_cc_get_principal)
  614. return (p_krb5_cc_get_principal(context, cache, principal));
  615. else
  616. return (krb5_x((cache)->ops->get_princ, (context, cache, principal)));
  617. }
  618. krb5_error_code
  619. kssl_krb5_auth_con_setrcache(krb5_context con, krb5_auth_context acon,
  620. krb5_rcache rcache)
  621. {
  622. if (p_krb5_auth_con_setrcache)
  623. return (p_krb5_auth_con_setrcache(con, acon, rcache));
  624. else
  625. return KRB5KRB_ERR_GENERIC;
  626. }
  627. krb5_error_code
  628. kssl_krb5_get_server_rcache(krb5_context con, krb5_const krb5_data *data,
  629. krb5_rcache *rcache)
  630. {
  631. if (p_krb5_get_server_rcache)
  632. return (p_krb5_get_server_rcache(con, data, rcache));
  633. else
  634. return KRB5KRB_ERR_GENERIC;
  635. }
  636. krb5_error_code
  637. kssl_krb5_auth_con_getrcache(krb5_context con, krb5_auth_context acon,
  638. krb5_rcache *prcache)
  639. {
  640. if (p_krb5_auth_con_getrcache)
  641. return (p_krb5_auth_con_getrcache(con, acon, prcache));
  642. else
  643. return KRB5KRB_ERR_GENERIC;
  644. }
  645. krb5_error_code kssl_krb5_kt_close(krb5_context context, krb5_keytab keytab)
  646. {
  647. if (p_krb5_kt_close)
  648. return (p_krb5_kt_close(context, keytab));
  649. else
  650. return KRB5KRB_ERR_GENERIC;
  651. }
  652. krb5_error_code
  653. kssl_krb5_kt_get_entry(krb5_context context, krb5_keytab keytab,
  654. krb5_const_principal principal, krb5_kvno vno,
  655. krb5_enctype enctype, krb5_keytab_entry *entry)
  656. {
  657. if (p_krb5_kt_get_entry)
  658. return (p_krb5_kt_get_entry
  659. (context, keytab, principal, vno, enctype, entry));
  660. else
  661. return KRB5KRB_ERR_GENERIC;
  662. }
  663. # endif /* OPENSSL_SYS_WINDOWS || OPENSSL_SYS_WIN32 */
  664. /*
  665. * memory allocation functions for non-temporary storage (e.g. stuff that
  666. * gets saved into the kssl context)
  667. */
  668. static void *kssl_calloc(size_t nmemb, size_t size)
  669. {
  670. void *p;
  671. p = OPENSSL_malloc(nmemb * size);
  672. if (p) {
  673. memset(p, 0, nmemb * size);
  674. }
  675. return p;
  676. }
  677. # define kssl_malloc(size) OPENSSL_malloc((size))
  678. # define kssl_realloc(ptr, size) OPENSSL_realloc(ptr, size)
  679. # define kssl_free(ptr) OPENSSL_free((ptr))
  680. char
  681. *kstring(char *string)
  682. {
  683. static char *null = "[NULL]";
  684. return ((string == NULL) ? null : string);
  685. }
  686. /*
  687. * Given KRB5 enctype (basically DES or 3DES), return closest match openssl
  688. * EVP_ encryption algorithm. Return NULL for unknown or problematic
  689. * (krb5_dk_encrypt) enctypes. Assume ENCTYPE_*_RAW (krb5_raw_encrypt) are
  690. * OK.
  691. */
  692. const EVP_CIPHER *kssl_map_enc(krb5_enctype enctype)
  693. {
  694. switch (enctype) {
  695. case ENCTYPE_DES_HMAC_SHA1: /* EVP_des_cbc(); */
  696. case ENCTYPE_DES_CBC_CRC:
  697. case ENCTYPE_DES_CBC_MD4:
  698. case ENCTYPE_DES_CBC_MD5:
  699. case ENCTYPE_DES_CBC_RAW:
  700. return EVP_des_cbc();
  701. break;
  702. case ENCTYPE_DES3_CBC_SHA1: /* EVP_des_ede3_cbc(); */
  703. case ENCTYPE_DES3_CBC_SHA:
  704. case ENCTYPE_DES3_CBC_RAW:
  705. return EVP_des_ede3_cbc();
  706. break;
  707. default:
  708. return NULL;
  709. break;
  710. }
  711. }
  712. /*
  713. * Return true:1 if p "looks like" the start of the real authenticator
  714. * described in kssl_skip_confound() below. The ASN.1 pattern is "62 xx 30
  715. * yy" (APPLICATION-2, SEQUENCE), where xx-yy =~ 2, and xx and yy are
  716. * possibly multi-byte length fields.
  717. */
  718. static int kssl_test_confound(unsigned char *p)
  719. {
  720. int len = 2;
  721. int xx = 0, yy = 0;
  722. if (*p++ != 0x62)
  723. return 0;
  724. if (*p > 0x82)
  725. return 0;
  726. switch (*p) {
  727. case 0x82:
  728. p++;
  729. xx = (*p++ << 8);
  730. xx += *p++;
  731. break;
  732. case 0x81:
  733. p++;
  734. xx = *p++;
  735. break;
  736. case 0x80:
  737. return 0;
  738. default:
  739. xx = *p++;
  740. break;
  741. }
  742. if (*p++ != 0x30)
  743. return 0;
  744. if (*p > 0x82)
  745. return 0;
  746. switch (*p) {
  747. case 0x82:
  748. p++;
  749. len += 2;
  750. yy = (*p++ << 8);
  751. yy += *p++;
  752. break;
  753. case 0x81:
  754. p++;
  755. len++;
  756. yy = *p++;
  757. break;
  758. case 0x80:
  759. return 0;
  760. default:
  761. yy = *p++;
  762. break;
  763. }
  764. return (xx - len == yy) ? 1 : 0;
  765. }
  766. /*
  767. * Allocate, fill, and return cksumlens array of checksum lengths. This
  768. * array holds just the unique elements from the krb5_cksumarray[]. array[n]
  769. * == 0 signals end of data. The krb5_cksumarray[] was an internal variable
  770. * that has since been replaced by a more general method for storing the
  771. * data. It should not be used. Instead we use real API calls and make a
  772. * guess for what the highest assigned CKSUMTYPE_ constant is. As of 1.2.2
  773. * it is 0x000c (CKSUMTYPE_HMAC_SHA1_DES3). So we will use 0x0010.
  774. */
  775. static size_t *populate_cksumlens(void)
  776. {
  777. int i, j, n;
  778. static size_t *cklens = NULL;
  779. # ifdef KRB5_MIT_OLD11
  780. n = krb5_max_cksum;
  781. # else
  782. n = 0x0010;
  783. # endif /* KRB5_MIT_OLD11 */
  784. # ifdef KRB5CHECKAUTH
  785. if (!cklens && !(cklens = (size_t *)calloc(sizeof(int), n + 1)))
  786. return NULL;
  787. for (i = 0; i < n; i++) {
  788. if (!valid_cksumtype(i))
  789. continue; /* array has holes */
  790. for (j = 0; j < n; j++) {
  791. if (cklens[j] == 0) {
  792. cklens[j] = krb5_checksum_size(NULL, i);
  793. break; /* krb5 elem was new: add */
  794. }
  795. if (cklens[j] == krb5_checksum_size(NULL, i)) {
  796. break; /* ignore duplicate elements */
  797. }
  798. }
  799. }
  800. # endif /* KRB5CHECKAUTH */
  801. return cklens;
  802. }
  803. /*-
  804. * Return pointer to start of real authenticator within authenticator, or
  805. * return NULL on error.
  806. * Decrypted authenticator looks like this:
  807. * [0 or 8 byte confounder] [4-24 byte checksum] [real authent'r]
  808. * This hackery wouldn't be necessary if MIT KRB5 1.0.6 had the
  809. * krb5_auth_con_getcksumtype() function advertised in its krb5.h.
  810. */
  811. unsigned char *kssl_skip_confound(krb5_enctype etype, unsigned char *a)
  812. {
  813. int i, conlen;
  814. size_t cklen;
  815. static size_t *cksumlens = NULL;
  816. unsigned char *test_auth;
  817. conlen = (etype) ? 8 : 0;
  818. if (!cksumlens && !(cksumlens = populate_cksumlens()))
  819. return NULL;
  820. for (i = 0; (cklen = cksumlens[i]) != 0; i++) {
  821. test_auth = a + conlen + cklen;
  822. if (kssl_test_confound(test_auth))
  823. return test_auth;
  824. }
  825. return NULL;
  826. }
  827. /*
  828. * Set kssl_err error info when reason text is a simple string kssl_err =
  829. * struct { int reason; char text[KSSL_ERR_MAX+1]; }
  830. */
  831. void kssl_err_set(KSSL_ERR *kssl_err, int reason, char *text)
  832. {
  833. if (kssl_err == NULL)
  834. return;
  835. kssl_err->reason = reason;
  836. BIO_snprintf(kssl_err->text, KSSL_ERR_MAX, "%s", text);
  837. return;
  838. }
  839. /*
  840. * Display contents of krb5_data struct, for debugging
  841. */
  842. void print_krb5_data(char *label, krb5_data *kdata)
  843. {
  844. int i;
  845. fprintf(stderr, "%s[%d] ", label, kdata->length);
  846. for (i = 0; i < (int)kdata->length; i++) {
  847. if (0 && isprint((int)kdata->data[i]))
  848. fprintf(stderr, "%c ", kdata->data[i]);
  849. else
  850. fprintf(stderr, "%02x ", (unsigned char)kdata->data[i]);
  851. }
  852. fprintf(stderr, "\n");
  853. }
  854. /*
  855. * Display contents of krb5_authdata struct, for debugging
  856. */
  857. void print_krb5_authdata(char *label, krb5_authdata **adata)
  858. {
  859. if (adata == NULL) {
  860. fprintf(stderr, "%s, authdata==0\n", label);
  861. return;
  862. }
  863. fprintf(stderr, "%s [%p]\n", label, (void *)adata);
  864. # if 0
  865. {
  866. int i;
  867. fprintf(stderr, "%s[at%d:%d] ", label, adata->ad_type, adata->length);
  868. for (i = 0; i < adata->length; i++) {
  869. fprintf(stderr, (isprint(adata->contents[i])) ? "%c " : "%02x",
  870. adata->contents[i]);
  871. }
  872. fprintf(stderr, "\n");
  873. }
  874. # endif
  875. }
  876. /*
  877. * Display contents of krb5_keyblock struct, for debugging
  878. */
  879. void print_krb5_keyblock(char *label, krb5_keyblock *keyblk)
  880. {
  881. int i;
  882. if (keyblk == NULL) {
  883. fprintf(stderr, "%s, keyblk==0\n", label);
  884. return;
  885. }
  886. # ifdef KRB5_HEIMDAL
  887. fprintf(stderr, "%s\n\t[et%d:%d]: ", label, keyblk->keytype,
  888. keyblk->keyvalue->length);
  889. for (i = 0; i < (int)keyblk->keyvalue->length; i++) {
  890. fprintf(stderr, "%02x",
  891. (unsigned char *)(keyblk->keyvalue->contents)[i]);
  892. }
  893. fprintf(stderr, "\n");
  894. # else
  895. fprintf(stderr, "%s\n\t[et%d:%d]: ", label, keyblk->enctype,
  896. keyblk->length);
  897. for (i = 0; i < (int)keyblk->length; i++) {
  898. fprintf(stderr, "%02x", keyblk->contents[i]);
  899. }
  900. fprintf(stderr, "\n");
  901. # endif
  902. }
  903. /*
  904. * Display contents of krb5_principal_data struct, for debugging
  905. * (krb5_principal is typedef'd == krb5_principal_data *)
  906. */
  907. static void print_krb5_princ(char *label, krb5_principal_data *princ)
  908. {
  909. int i, ui, uj;
  910. fprintf(stderr, "%s principal Realm: ", label);
  911. if (princ == NULL)
  912. return;
  913. for (ui = 0; ui < (int)princ->realm.length; ui++)
  914. putchar(princ->realm.data[ui]);
  915. fprintf(stderr, " (nametype %d) has %d strings:\n", princ->type,
  916. princ->length);
  917. for (i = 0; i < (int)princ->length; i++) {
  918. fprintf(stderr, "\t%d [%d]: ", i, princ->data[i].length);
  919. for (uj = 0; uj < (int)princ->data[i].length; uj++) {
  920. putchar(princ->data[i].data[uj]);
  921. }
  922. fprintf(stderr, "\n");
  923. }
  924. return;
  925. }
  926. /*- Given krb5 service (typically "kssl") and hostname in kssl_ctx,
  927. * Return encrypted Kerberos ticket for service @ hostname.
  928. * If authenp is non-NULL, also return encrypted authenticator,
  929. * whose data should be freed by caller.
  930. * (Originally was: Create Kerberos AP_REQ message for SSL Client.)
  931. *
  932. * 19990628 VRS Started; Returns Kerberos AP_REQ message.
  933. * 20010409 VRS Modified for RFC2712; Returns enc tkt.
  934. * 20010606 VRS May also return optional authenticator.
  935. */
  936. krb5_error_code kssl_cget_tkt( /* UPDATE */ KSSL_CTX *kssl_ctx,
  937. /*
  938. * OUT
  939. */ krb5_data **enc_ticketp,
  940. /*
  941. * UPDATE
  942. */ krb5_data *authenp,
  943. /*
  944. * OUT
  945. */ KSSL_ERR *kssl_err)
  946. {
  947. krb5_error_code krb5rc = KRB5KRB_ERR_GENERIC;
  948. krb5_context krb5context = NULL;
  949. krb5_auth_context krb5auth_context = NULL;
  950. krb5_ccache krb5ccdef = NULL;
  951. krb5_creds krb5creds, *krb5credsp = NULL;
  952. krb5_data krb5_app_req;
  953. kssl_err_set(kssl_err, 0, "");
  954. memset((char *)&krb5creds, 0, sizeof(krb5creds));
  955. if (!kssl_ctx) {
  956. kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT, "No kssl_ctx defined.\n");
  957. goto err;
  958. } else if (!kssl_ctx->service_host) {
  959. kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
  960. "kssl_ctx service_host undefined.\n");
  961. goto err;
  962. }
  963. if ((krb5rc = krb5_init_context(&krb5context)) != 0) {
  964. BIO_snprintf(kssl_err->text, KSSL_ERR_MAX,
  965. "krb5_init_context() fails: %d\n", krb5rc);
  966. kssl_err->reason = SSL_R_KRB5_C_INIT;
  967. goto err;
  968. }
  969. if ((krb5rc = krb5_sname_to_principal(krb5context,
  970. kssl_ctx->service_host,
  971. (kssl_ctx->service_name) ?
  972. kssl_ctx->service_name : KRB5SVC,
  973. KRB5_NT_SRV_HST,
  974. &krb5creds.server)) != 0) {
  975. BIO_snprintf(kssl_err->text, KSSL_ERR_MAX,
  976. "krb5_sname_to_principal() fails for %s/%s\n",
  977. kssl_ctx->service_host,
  978. (kssl_ctx->
  979. service_name) ? kssl_ctx->service_name : KRB5SVC);
  980. kssl_err->reason = SSL_R_KRB5_C_INIT;
  981. goto err;
  982. }
  983. if ((krb5rc = krb5_cc_default(krb5context, &krb5ccdef)) != 0) {
  984. kssl_err_set(kssl_err, SSL_R_KRB5_C_CC_PRINC,
  985. "krb5_cc_default fails.\n");
  986. goto err;
  987. }
  988. if ((krb5rc = krb5_cc_get_principal(krb5context, krb5ccdef,
  989. &krb5creds.client)) != 0) {
  990. kssl_err_set(kssl_err, SSL_R_KRB5_C_CC_PRINC,
  991. "krb5_cc_get_principal() fails.\n");
  992. goto err;
  993. }
  994. if ((krb5rc = krb5_get_credentials(krb5context, 0, krb5ccdef,
  995. &krb5creds, &krb5credsp)) != 0) {
  996. kssl_err_set(kssl_err, SSL_R_KRB5_C_GET_CRED,
  997. "krb5_get_credentials() fails.\n");
  998. goto err;
  999. }
  1000. *enc_ticketp = &krb5credsp->ticket;
  1001. # ifdef KRB5_HEIMDAL
  1002. kssl_ctx->enctype = krb5credsp->session.keytype;
  1003. # else
  1004. kssl_ctx->enctype = krb5credsp->keyblock.enctype;
  1005. # endif
  1006. krb5rc = KRB5KRB_ERR_GENERIC;
  1007. /* caller should free data of krb5_app_req */
  1008. /*
  1009. * 20010406 VRS deleted for real KerberosWrapper 20010605 VRS reinstated
  1010. * to offer Authenticator to KerberosWrapper
  1011. */
  1012. krb5_app_req.length = 0;
  1013. if (authenp) {
  1014. krb5_data krb5in_data;
  1015. const unsigned char *p;
  1016. long arlen;
  1017. KRB5_APREQBODY *ap_req;
  1018. authenp->length = 0;
  1019. krb5in_data.data = NULL;
  1020. krb5in_data.length = 0;
  1021. if ((krb5rc = krb5_mk_req_extended(krb5context,
  1022. &krb5auth_context, 0, &krb5in_data,
  1023. krb5credsp, &krb5_app_req)) != 0) {
  1024. kssl_err_set(kssl_err, SSL_R_KRB5_C_MK_REQ,
  1025. "krb5_mk_req_extended() fails.\n");
  1026. goto err;
  1027. }
  1028. arlen = krb5_app_req.length;
  1029. p = (unsigned char *)krb5_app_req.data;
  1030. ap_req = (KRB5_APREQBODY *)d2i_KRB5_APREQ(NULL, &p, arlen);
  1031. if (ap_req) {
  1032. authenp->length = i2d_KRB5_ENCDATA(ap_req->authenticator, NULL);
  1033. if (authenp->length && (authenp->data = malloc(authenp->length))) {
  1034. unsigned char *adp = (unsigned char *)authenp->data;
  1035. authenp->length =
  1036. i2d_KRB5_ENCDATA(ap_req->authenticator, &adp);
  1037. }
  1038. }
  1039. if (ap_req)
  1040. KRB5_APREQ_free((KRB5_APREQ *) ap_req);
  1041. if (krb5_app_req.length)
  1042. kssl_krb5_free_data_contents(krb5context, &krb5_app_req);
  1043. }
  1044. # ifdef KRB5_HEIMDAL
  1045. if (kssl_ctx_setkey(kssl_ctx, &krb5credsp->session)) {
  1046. kssl_err_set(kssl_err, SSL_R_KRB5_C_INIT,
  1047. "kssl_ctx_setkey() fails.\n");
  1048. }
  1049. # else
  1050. if (kssl_ctx_setkey(kssl_ctx, &krb5credsp->keyblock)) {
  1051. kssl_err_set(kssl_err, SSL_R_KRB5_C_INIT,
  1052. "kssl_ctx_setkey() fails.\n");
  1053. }
  1054. # endif
  1055. else
  1056. krb5rc = 0;
  1057. err:
  1058. # ifdef KSSL_DEBUG
  1059. kssl_ctx_show(kssl_ctx);
  1060. # endif /* KSSL_DEBUG */
  1061. if (krb5creds.client)
  1062. krb5_free_principal(krb5context, krb5creds.client);
  1063. if (krb5creds.server)
  1064. krb5_free_principal(krb5context, krb5creds.server);
  1065. if (krb5auth_context)
  1066. krb5_auth_con_free(krb5context, krb5auth_context);
  1067. if (krb5context)
  1068. krb5_free_context(krb5context);
  1069. return (krb5rc);
  1070. }
  1071. /*-
  1072. * Given d2i_-decoded asn1ticket, allocate and return a new krb5_ticket.
  1073. * Return Kerberos error code and kssl_err struct on error.
  1074. * Allocates krb5_ticket and krb5_principal; caller should free these.
  1075. *
  1076. * 20010410 VRS Implemented krb5_decode_ticket() as
  1077. * old_krb5_decode_ticket(). Missing from MIT1.0.6.
  1078. * 20010615 VRS Re-cast as openssl/asn1 d2i_*() functions.
  1079. * Re-used some of the old krb5_decode_ticket()
  1080. * code here. This tkt should alloc/free just
  1081. * like the real thing.
  1082. */
  1083. static krb5_error_code kssl_TKT2tkt( /* IN */ krb5_context krb5context,
  1084. /*
  1085. * IN
  1086. */ KRB5_TKTBODY *asn1ticket,
  1087. /*
  1088. * OUT
  1089. */ krb5_ticket **krb5ticket,
  1090. /*
  1091. * OUT
  1092. */ KSSL_ERR *kssl_err)
  1093. {
  1094. krb5_error_code krb5rc = KRB5KRB_ERR_GENERIC;
  1095. krb5_ticket *new5ticket = NULL;
  1096. ASN1_GENERALSTRING *gstr_svc, *gstr_host;
  1097. *krb5ticket = NULL;
  1098. if (asn1ticket == NULL || asn1ticket->realm == NULL ||
  1099. asn1ticket->sname == NULL ||
  1100. sk_ASN1_GENERALSTRING_num(asn1ticket->sname->namestring) < 2) {
  1101. BIO_snprintf(kssl_err->text, KSSL_ERR_MAX,
  1102. "Null field in asn1ticket.\n");
  1103. kssl_err->reason = SSL_R_KRB5_S_RD_REQ;
  1104. return KRB5KRB_ERR_GENERIC;
  1105. }
  1106. if ((new5ticket = (krb5_ticket *)calloc(1, sizeof(krb5_ticket))) == NULL) {
  1107. BIO_snprintf(kssl_err->text, KSSL_ERR_MAX,
  1108. "Unable to allocate new krb5_ticket.\n");
  1109. kssl_err->reason = SSL_R_KRB5_S_RD_REQ;
  1110. return ENOMEM; /* or KRB5KRB_ERR_GENERIC; */
  1111. }
  1112. gstr_svc = sk_ASN1_GENERALSTRING_value(asn1ticket->sname->namestring, 0);
  1113. gstr_host = sk_ASN1_GENERALSTRING_value(asn1ticket->sname->namestring, 1);
  1114. if ((krb5rc = kssl_build_principal_2(krb5context,
  1115. &new5ticket->server,
  1116. asn1ticket->realm->length,
  1117. (char *)asn1ticket->realm->data,
  1118. gstr_svc->length,
  1119. (char *)gstr_svc->data,
  1120. gstr_host->length,
  1121. (char *)gstr_host->data)) != 0) {
  1122. free(new5ticket);
  1123. BIO_snprintf(kssl_err->text, KSSL_ERR_MAX,
  1124. "Error building ticket server principal.\n");
  1125. kssl_err->reason = SSL_R_KRB5_S_RD_REQ;
  1126. return krb5rc; /* or KRB5KRB_ERR_GENERIC; */
  1127. }
  1128. krb5_princ_type(krb5context, new5ticket->server) =
  1129. asn1ticket->sname->nametype->data[0];
  1130. new5ticket->enc_part.enctype = asn1ticket->encdata->etype->data[0];
  1131. new5ticket->enc_part.kvno = asn1ticket->encdata->kvno->data[0];
  1132. new5ticket->enc_part.ciphertext.length =
  1133. asn1ticket->encdata->cipher->length;
  1134. if ((new5ticket->enc_part.ciphertext.data =
  1135. calloc(1, asn1ticket->encdata->cipher->length)) == NULL) {
  1136. free(new5ticket);
  1137. BIO_snprintf(kssl_err->text, KSSL_ERR_MAX,
  1138. "Error allocating cipher in krb5ticket.\n");
  1139. kssl_err->reason = SSL_R_KRB5_S_RD_REQ;
  1140. return KRB5KRB_ERR_GENERIC;
  1141. } else {
  1142. memcpy(new5ticket->enc_part.ciphertext.data,
  1143. asn1ticket->encdata->cipher->data,
  1144. asn1ticket->encdata->cipher->length);
  1145. }
  1146. *krb5ticket = new5ticket;
  1147. return 0;
  1148. }
  1149. /*-
  1150. * Given krb5 service name in KSSL_CTX *kssl_ctx (typically "kssl"),
  1151. * and krb5 AP_REQ message & message length,
  1152. * Return Kerberos session key and client principle
  1153. * to SSL Server in KSSL_CTX *kssl_ctx.
  1154. *
  1155. * 19990702 VRS Started.
  1156. */
  1157. krb5_error_code kssl_sget_tkt( /* UPDATE */ KSSL_CTX *kssl_ctx,
  1158. /*
  1159. * IN
  1160. */ krb5_data *indata,
  1161. /*
  1162. * OUT
  1163. */ krb5_ticket_times *ttimes,
  1164. /*
  1165. * OUT
  1166. */ KSSL_ERR *kssl_err)
  1167. {
  1168. krb5_error_code krb5rc = KRB5KRB_ERR_GENERIC;
  1169. static krb5_context krb5context = NULL;
  1170. static krb5_auth_context krb5auth_context = NULL;
  1171. krb5_ticket *krb5ticket = NULL;
  1172. KRB5_TKTBODY *asn1ticket = NULL;
  1173. const unsigned char *p;
  1174. krb5_keytab krb5keytab = NULL;
  1175. krb5_keytab_entry kt_entry;
  1176. krb5_principal krb5server;
  1177. krb5_rcache rcache = NULL;
  1178. kssl_err_set(kssl_err, 0, "");
  1179. if (!kssl_ctx) {
  1180. kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT, "No kssl_ctx defined.\n");
  1181. goto err;
  1182. }
  1183. # ifdef KSSL_DEBUG
  1184. fprintf(stderr, "in kssl_sget_tkt(%s)\n",
  1185. kstring(kssl_ctx->service_name));
  1186. # endif /* KSSL_DEBUG */
  1187. if (!krb5context && (krb5rc = krb5_init_context(&krb5context))) {
  1188. kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
  1189. "krb5_init_context() fails.\n");
  1190. goto err;
  1191. }
  1192. if (krb5auth_context &&
  1193. (krb5rc = krb5_auth_con_free(krb5context, krb5auth_context))) {
  1194. kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
  1195. "krb5_auth_con_free() fails.\n");
  1196. goto err;
  1197. } else
  1198. krb5auth_context = NULL;
  1199. if (!krb5auth_context &&
  1200. (krb5rc = krb5_auth_con_init(krb5context, &krb5auth_context))) {
  1201. kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
  1202. "krb5_auth_con_init() fails.\n");
  1203. goto err;
  1204. }
  1205. if ((krb5rc = krb5_auth_con_getrcache(krb5context, krb5auth_context,
  1206. &rcache))) {
  1207. kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
  1208. "krb5_auth_con_getrcache() fails.\n");
  1209. goto err;
  1210. }
  1211. if ((krb5rc = krb5_sname_to_principal(krb5context, NULL,
  1212. (kssl_ctx->service_name) ?
  1213. kssl_ctx->service_name : KRB5SVC,
  1214. KRB5_NT_SRV_HST,
  1215. &krb5server)) != 0) {
  1216. kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
  1217. "krb5_sname_to_principal() fails.\n");
  1218. goto err;
  1219. }
  1220. if (rcache == NULL) {
  1221. if ((krb5rc = krb5_get_server_rcache(krb5context,
  1222. krb5_princ_component(krb5context,
  1223. krb5server,
  1224. 0),
  1225. &rcache))) {
  1226. kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
  1227. "krb5_get_server_rcache() fails.\n");
  1228. goto err;
  1229. }
  1230. }
  1231. if ((krb5rc =
  1232. krb5_auth_con_setrcache(krb5context, krb5auth_context, rcache))) {
  1233. kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
  1234. "krb5_auth_con_setrcache() fails.\n");
  1235. goto err;
  1236. }
  1237. /*
  1238. * kssl_ctx->keytab_file == NULL ==> use Kerberos default
  1239. */
  1240. if (kssl_ctx->keytab_file) {
  1241. krb5rc = krb5_kt_resolve(krb5context, kssl_ctx->keytab_file,
  1242. &krb5keytab);
  1243. if (krb5rc) {
  1244. kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
  1245. "krb5_kt_resolve() fails.\n");
  1246. goto err;
  1247. }
  1248. } else {
  1249. krb5rc = krb5_kt_default(krb5context, &krb5keytab);
  1250. if (krb5rc) {
  1251. kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
  1252. "krb5_kt_default() fails.\n");
  1253. goto err;
  1254. }
  1255. }
  1256. /*- Actual Kerberos5 krb5_recvauth() has initial conversation here
  1257. * o check KRB5_SENDAUTH_BADAUTHVERS
  1258. * unless KRB5_RECVAUTH_SKIP_VERSION
  1259. * o check KRB5_SENDAUTH_BADAPPLVERS
  1260. * o send "0" msg if all OK
  1261. */
  1262. /*-
  1263. * 20010411 was using AP_REQ instead of true KerberosWrapper
  1264. *
  1265. * if ((krb5rc = krb5_rd_req(krb5context, &krb5auth_context,
  1266. * &krb5in_data, krb5server, krb5keytab,
  1267. * &ap_option, &krb5ticket)) != 0) { Error }
  1268. */
  1269. p = (unsigned char *)indata->data;
  1270. if ((asn1ticket = (KRB5_TKTBODY *)d2i_KRB5_TICKET(NULL, &p,
  1271. (long)indata->length))
  1272. == NULL) {
  1273. BIO_snprintf(kssl_err->text, KSSL_ERR_MAX,
  1274. "d2i_KRB5_TICKET() ASN.1 decode failure.\n");
  1275. kssl_err->reason = SSL_R_KRB5_S_RD_REQ;
  1276. goto err;
  1277. }
  1278. /*
  1279. * Was: krb5rc = krb5_decode_ticket(krb5in_data,&krb5ticket)) != 0)
  1280. */
  1281. if ((krb5rc = kssl_TKT2tkt(krb5context, asn1ticket, &krb5ticket,
  1282. kssl_err)) != 0) {
  1283. BIO_snprintf(kssl_err->text, KSSL_ERR_MAX,
  1284. "Error converting ASN.1 ticket to krb5_ticket.\n");
  1285. kssl_err->reason = SSL_R_KRB5_S_RD_REQ;
  1286. goto err;
  1287. }
  1288. if (!krb5_principal_compare(krb5context, krb5server, krb5ticket->server)) {
  1289. krb5rc = KRB5_PRINC_NOMATCH;
  1290. BIO_snprintf(kssl_err->text, KSSL_ERR_MAX,
  1291. "server principal != ticket principal\n");
  1292. kssl_err->reason = SSL_R_KRB5_S_RD_REQ;
  1293. goto err;
  1294. }
  1295. if ((krb5rc = krb5_kt_get_entry(krb5context, krb5keytab,
  1296. krb5ticket->server,
  1297. krb5ticket->enc_part.kvno,
  1298. krb5ticket->enc_part.enctype,
  1299. &kt_entry)) != 0) {
  1300. BIO_snprintf(kssl_err->text, KSSL_ERR_MAX,
  1301. "krb5_kt_get_entry() fails with %x.\n", krb5rc);
  1302. kssl_err->reason = SSL_R_KRB5_S_RD_REQ;
  1303. goto err;
  1304. }
  1305. if ((krb5rc = krb5_decrypt_tkt_part(krb5context, &kt_entry.key,
  1306. krb5ticket)) != 0) {
  1307. BIO_snprintf(kssl_err->text, KSSL_ERR_MAX,
  1308. "krb5_decrypt_tkt_part() failed.\n");
  1309. kssl_err->reason = SSL_R_KRB5_S_RD_REQ;
  1310. goto err;
  1311. } else {
  1312. krb5_kt_free_entry(krb5context, &kt_entry);
  1313. # ifdef KSSL_DEBUG
  1314. {
  1315. int i;
  1316. krb5_address **paddr = krb5ticket->enc_part2->caddrs;
  1317. fprintf(stderr, "Decrypted ticket fields:\n");
  1318. fprintf(stderr, "\tflags: %X, transit-type: %X",
  1319. krb5ticket->enc_part2->flags,
  1320. krb5ticket->enc_part2->transited.tr_type);
  1321. print_krb5_data("\ttransit-data: ",
  1322. &(krb5ticket->enc_part2->transited.tr_contents));
  1323. fprintf(stderr, "\tcaddrs: %p, authdata: %p\n",
  1324. krb5ticket->enc_part2->caddrs,
  1325. krb5ticket->enc_part2->authorization_data);
  1326. if (paddr) {
  1327. fprintf(stderr, "\tcaddrs:\n");
  1328. for (i = 0; paddr[i] != NULL; i++) {
  1329. krb5_data d;
  1330. d.length = paddr[i]->length;
  1331. d.data = paddr[i]->contents;
  1332. print_krb5_data("\t\tIP: ", &d);
  1333. }
  1334. }
  1335. fprintf(stderr, "\tstart/auth/end times: %d / %d / %d\n",
  1336. krb5ticket->enc_part2->times.starttime,
  1337. krb5ticket->enc_part2->times.authtime,
  1338. krb5ticket->enc_part2->times.endtime);
  1339. }
  1340. # endif /* KSSL_DEBUG */
  1341. }
  1342. krb5rc = KRB5_NO_TKT_SUPPLIED;
  1343. if (!krb5ticket || !krb5ticket->enc_part2 ||
  1344. !krb5ticket->enc_part2->client ||
  1345. !krb5ticket->enc_part2->client->data ||
  1346. !krb5ticket->enc_part2->session) {
  1347. kssl_err_set(kssl_err, SSL_R_KRB5_S_BAD_TICKET,
  1348. "bad ticket from krb5_rd_req.\n");
  1349. } else if (kssl_ctx_setprinc(kssl_ctx, KSSL_CLIENT,
  1350. &krb5ticket->enc_part2->client->realm,
  1351. krb5ticket->enc_part2->client->data,
  1352. krb5ticket->enc_part2->client->length)) {
  1353. kssl_err_set(kssl_err, SSL_R_KRB5_S_BAD_TICKET,
  1354. "kssl_ctx_setprinc() fails.\n");
  1355. } else if (kssl_ctx_setkey(kssl_ctx, krb5ticket->enc_part2->session)) {
  1356. kssl_err_set(kssl_err, SSL_R_KRB5_S_BAD_TICKET,
  1357. "kssl_ctx_setkey() fails.\n");
  1358. } else if (krb5ticket->enc_part2->flags & TKT_FLG_INVALID) {
  1359. krb5rc = KRB5KRB_AP_ERR_TKT_INVALID;
  1360. kssl_err_set(kssl_err, SSL_R_KRB5_S_BAD_TICKET,
  1361. "invalid ticket from krb5_rd_req.\n");
  1362. } else
  1363. krb5rc = 0;
  1364. kssl_ctx->enctype = krb5ticket->enc_part.enctype;
  1365. ttimes->authtime = krb5ticket->enc_part2->times.authtime;
  1366. ttimes->starttime = krb5ticket->enc_part2->times.starttime;
  1367. ttimes->endtime = krb5ticket->enc_part2->times.endtime;
  1368. ttimes->renew_till = krb5ticket->enc_part2->times.renew_till;
  1369. err:
  1370. # ifdef KSSL_DEBUG
  1371. kssl_ctx_show(kssl_ctx);
  1372. # endif /* KSSL_DEBUG */
  1373. if (asn1ticket)
  1374. KRB5_TICKET_free((KRB5_TICKET *) asn1ticket);
  1375. if (krb5keytab)
  1376. krb5_kt_close(krb5context, krb5keytab);
  1377. if (krb5ticket)
  1378. krb5_free_ticket(krb5context, krb5ticket);
  1379. if (krb5server)
  1380. krb5_free_principal(krb5context, krb5server);
  1381. return (krb5rc);
  1382. }
  1383. /*
  1384. * Allocate & return a new kssl_ctx struct.
  1385. */
  1386. KSSL_CTX *kssl_ctx_new(void)
  1387. {
  1388. return ((KSSL_CTX *)kssl_calloc(1, sizeof(KSSL_CTX)));
  1389. }
  1390. /*
  1391. * Frees a kssl_ctx struct and any allocated memory it holds. Returns NULL.
  1392. */
  1393. KSSL_CTX *kssl_ctx_free(KSSL_CTX *kssl_ctx)
  1394. {
  1395. if (kssl_ctx == NULL)
  1396. return kssl_ctx;
  1397. if (kssl_ctx->key)
  1398. OPENSSL_cleanse(kssl_ctx->key, kssl_ctx->length);
  1399. if (kssl_ctx->key)
  1400. kssl_free(kssl_ctx->key);
  1401. if (kssl_ctx->client_princ)
  1402. kssl_free(kssl_ctx->client_princ);
  1403. if (kssl_ctx->service_host)
  1404. kssl_free(kssl_ctx->service_host);
  1405. if (kssl_ctx->service_name)
  1406. kssl_free(kssl_ctx->service_name);
  1407. if (kssl_ctx->keytab_file)
  1408. kssl_free(kssl_ctx->keytab_file);
  1409. kssl_free(kssl_ctx);
  1410. return (KSSL_CTX *)NULL;
  1411. }
  1412. /*
  1413. * Given an array of (krb5_data *) entity (and optional realm), set the plain
  1414. * (char *) client_princ or service_host member of the kssl_ctx struct.
  1415. */
  1416. krb5_error_code
  1417. kssl_ctx_setprinc(KSSL_CTX *kssl_ctx, int which,
  1418. krb5_data *realm, krb5_data *entity, int nentities)
  1419. {
  1420. char **princ;
  1421. int length;
  1422. int i;
  1423. if (kssl_ctx == NULL || entity == NULL)
  1424. return KSSL_CTX_ERR;
  1425. switch (which) {
  1426. case KSSL_CLIENT:
  1427. princ = &kssl_ctx->client_princ;
  1428. break;
  1429. case KSSL_SERVER:
  1430. princ = &kssl_ctx->service_host;
  1431. break;
  1432. default:
  1433. return KSSL_CTX_ERR;
  1434. break;
  1435. }
  1436. if (*princ)
  1437. kssl_free(*princ);
  1438. /* Add up all the entity->lengths */
  1439. length = 0;
  1440. for (i = 0; i < nentities; i++) {
  1441. length += entity[i].length;
  1442. }
  1443. /* Add in space for the '/' character(s) (if any) */
  1444. length += nentities - 1;
  1445. /* Space for the ('@'+realm+NULL | NULL) */
  1446. length += ((realm) ? realm->length + 2 : 1);
  1447. if ((*princ = kssl_calloc(1, length)) == NULL)
  1448. return KSSL_CTX_ERR;
  1449. else {
  1450. for (i = 0; i < nentities; i++) {
  1451. strncat(*princ, entity[i].data, entity[i].length);
  1452. if (i < nentities - 1) {
  1453. strcat(*princ, "/");
  1454. }
  1455. }
  1456. if (realm) {
  1457. strcat(*princ, "@");
  1458. (void)strncat(*princ, realm->data, realm->length);
  1459. }
  1460. }
  1461. return KSSL_CTX_OK;
  1462. }
  1463. /*- Set one of the plain (char *) string members of the kssl_ctx struct.
  1464. * Default values should be:
  1465. * which == KSSL_SERVICE => "khost" (KRB5SVC)
  1466. * which == KSSL_KEYTAB => "/etc/krb5.keytab" (KRB5KEYTAB)
  1467. */
  1468. krb5_error_code kssl_ctx_setstring(KSSL_CTX *kssl_ctx, int which, char *text)
  1469. {
  1470. char **string;
  1471. if (!kssl_ctx)
  1472. return KSSL_CTX_ERR;
  1473. switch (which) {
  1474. case KSSL_SERVICE:
  1475. string = &kssl_ctx->service_name;
  1476. break;
  1477. case KSSL_SERVER:
  1478. string = &kssl_ctx->service_host;
  1479. break;
  1480. case KSSL_CLIENT:
  1481. string = &kssl_ctx->client_princ;
  1482. break;
  1483. case KSSL_KEYTAB:
  1484. string = &kssl_ctx->keytab_file;
  1485. break;
  1486. default:
  1487. return KSSL_CTX_ERR;
  1488. break;
  1489. }
  1490. if (*string)
  1491. kssl_free(*string);
  1492. if (!text) {
  1493. *string = '\0';
  1494. return KSSL_CTX_OK;
  1495. }
  1496. if ((*string = kssl_calloc(1, strlen(text) + 1)) == NULL)
  1497. return KSSL_CTX_ERR;
  1498. else
  1499. strcpy(*string, text);
  1500. return KSSL_CTX_OK;
  1501. }
  1502. /*
  1503. * Copy the Kerberos session key from a (krb5_keyblock *) to a kssl_ctx
  1504. * struct. Clear kssl_ctx->key if Kerberos session key is NULL.
  1505. */
  1506. krb5_error_code kssl_ctx_setkey(KSSL_CTX *kssl_ctx, krb5_keyblock *session)
  1507. {
  1508. int length;
  1509. krb5_enctype enctype;
  1510. krb5_octet FAR *contents = NULL;
  1511. if (!kssl_ctx)
  1512. return KSSL_CTX_ERR;
  1513. if (kssl_ctx->key) {
  1514. OPENSSL_cleanse(kssl_ctx->key, kssl_ctx->length);
  1515. kssl_free(kssl_ctx->key);
  1516. }
  1517. if (session) {
  1518. # ifdef KRB5_HEIMDAL
  1519. length = session->keyvalue->length;
  1520. enctype = session->keytype;
  1521. contents = session->keyvalue->contents;
  1522. # else
  1523. length = session->length;
  1524. enctype = session->enctype;
  1525. contents = session->contents;
  1526. # endif
  1527. kssl_ctx->enctype = enctype;
  1528. kssl_ctx->length = length;
  1529. } else {
  1530. kssl_ctx->enctype = ENCTYPE_UNKNOWN;
  1531. kssl_ctx->length = 0;
  1532. return KSSL_CTX_OK;
  1533. }
  1534. if ((kssl_ctx->key =
  1535. (krb5_octet FAR *)kssl_calloc(1, kssl_ctx->length)) == NULL) {
  1536. kssl_ctx->length = 0;
  1537. return KSSL_CTX_ERR;
  1538. } else
  1539. memcpy(kssl_ctx->key, contents, length);
  1540. return KSSL_CTX_OK;
  1541. }
  1542. /*
  1543. * Display contents of kssl_ctx struct
  1544. */
  1545. void kssl_ctx_show(KSSL_CTX *kssl_ctx)
  1546. {
  1547. int i;
  1548. printf("kssl_ctx: ");
  1549. if (kssl_ctx == NULL) {
  1550. printf("NULL\n");
  1551. return;
  1552. } else
  1553. printf("%p\n", (void *)kssl_ctx);
  1554. printf("\tservice:\t%s\n",
  1555. (kssl_ctx->service_name) ? kssl_ctx->service_name : "NULL");
  1556. printf("\tclient:\t%s\n",
  1557. (kssl_ctx->client_princ) ? kssl_ctx->client_princ : "NULL");
  1558. printf("\tserver:\t%s\n",
  1559. (kssl_ctx->service_host) ? kssl_ctx->service_host : "NULL");
  1560. printf("\tkeytab:\t%s\n",
  1561. (kssl_ctx->keytab_file) ? kssl_ctx->keytab_file : "NULL");
  1562. printf("\tkey [%d:%d]:\t", kssl_ctx->enctype, kssl_ctx->length);
  1563. for (i = 0; i < kssl_ctx->length && kssl_ctx->key; i++) {
  1564. printf("%02x", kssl_ctx->key[i]);
  1565. }
  1566. printf("\n");
  1567. return;
  1568. }
  1569. int kssl_keytab_is_available(KSSL_CTX *kssl_ctx)
  1570. {
  1571. krb5_context krb5context = NULL;
  1572. krb5_keytab krb5keytab = NULL;
  1573. krb5_keytab_entry entry;
  1574. krb5_principal princ = NULL;
  1575. krb5_error_code krb5rc = KRB5KRB_ERR_GENERIC;
  1576. int rc = 0;
  1577. if ((krb5rc = krb5_init_context(&krb5context)))
  1578. return (0);
  1579. /*
  1580. * kssl_ctx->keytab_file == NULL ==> use Kerberos default
  1581. */
  1582. if (kssl_ctx->keytab_file) {
  1583. krb5rc = krb5_kt_resolve(krb5context, kssl_ctx->keytab_file,
  1584. &krb5keytab);
  1585. if (krb5rc)
  1586. goto exit;
  1587. } else {
  1588. krb5rc = krb5_kt_default(krb5context, &krb5keytab);
  1589. if (krb5rc)
  1590. goto exit;
  1591. }
  1592. /* the host key we are looking for */
  1593. krb5rc = krb5_sname_to_principal(krb5context, NULL,
  1594. kssl_ctx->
  1595. service_name ? kssl_ctx->service_name :
  1596. KRB5SVC, KRB5_NT_SRV_HST, &princ);
  1597. if (krb5rc)
  1598. goto exit;
  1599. krb5rc = krb5_kt_get_entry(krb5context, krb5keytab, princ,
  1600. /* IGNORE_VNO */
  1601. 0,
  1602. /* IGNORE_ENCTYPE */
  1603. 0, &entry);
  1604. if (krb5rc == KRB5_KT_NOTFOUND) {
  1605. rc = 1;
  1606. goto exit;
  1607. } else if (krb5rc)
  1608. goto exit;
  1609. krb5_kt_free_entry(krb5context, &entry);
  1610. rc = 1;
  1611. exit:
  1612. if (krb5keytab)
  1613. krb5_kt_close(krb5context, krb5keytab);
  1614. if (princ)
  1615. krb5_free_principal(krb5context, princ);
  1616. if (krb5context)
  1617. krb5_free_context(krb5context);
  1618. return (rc);
  1619. }
  1620. int kssl_tgt_is_available(KSSL_CTX *kssl_ctx)
  1621. {
  1622. krb5_error_code krb5rc = KRB5KRB_ERR_GENERIC;
  1623. krb5_context krb5context = NULL;
  1624. krb5_ccache krb5ccdef = NULL;
  1625. krb5_creds krb5creds, *krb5credsp = NULL;
  1626. int rc = 0;
  1627. memset((char *)&krb5creds, 0, sizeof(krb5creds));
  1628. if (!kssl_ctx)
  1629. return (0);
  1630. if (!kssl_ctx->service_host)
  1631. return (0);
  1632. if ((krb5rc = krb5_init_context(&krb5context)) != 0)
  1633. goto err;
  1634. if ((krb5rc = krb5_sname_to_principal(krb5context,
  1635. kssl_ctx->service_host,
  1636. (kssl_ctx->service_name) ?
  1637. kssl_ctx->service_name : KRB5SVC,
  1638. KRB5_NT_SRV_HST,
  1639. &krb5creds.server)) != 0)
  1640. goto err;
  1641. if ((krb5rc = krb5_cc_default(krb5context, &krb5ccdef)) != 0)
  1642. goto err;
  1643. if ((krb5rc = krb5_cc_get_principal(krb5context, krb5ccdef,
  1644. &krb5creds.client)) != 0)
  1645. goto err;
  1646. if ((krb5rc = krb5_get_credentials(krb5context, 0, krb5ccdef,
  1647. &krb5creds, &krb5credsp)) != 0)
  1648. goto err;
  1649. rc = 1;
  1650. err:
  1651. # ifdef KSSL_DEBUG
  1652. kssl_ctx_show(kssl_ctx);
  1653. # endif /* KSSL_DEBUG */
  1654. if (krb5creds.client)
  1655. krb5_free_principal(krb5context, krb5creds.client);
  1656. if (krb5creds.server)
  1657. krb5_free_principal(krb5context, krb5creds.server);
  1658. if (krb5context)
  1659. krb5_free_context(krb5context);
  1660. return (rc);
  1661. }
  1662. # if !defined(OPENSSL_SYS_WINDOWS) && !defined(OPENSSL_SYS_WIN32)
  1663. void kssl_krb5_free_data_contents(krb5_context context, krb5_data *data)
  1664. {
  1665. # ifdef KRB5_HEIMDAL
  1666. data->length = 0;
  1667. if (data->data)
  1668. free(data->data);
  1669. # elif defined(KRB5_MIT_OLD11)
  1670. if (data->data) {
  1671. krb5_xfree(data->data);
  1672. data->data = 0;
  1673. }
  1674. # else
  1675. krb5_free_data_contents(NULL, data);
  1676. # endif
  1677. }
  1678. # endif
  1679. /* !OPENSSL_SYS_WINDOWS && !OPENSSL_SYS_WIN32 */
  1680. /*
  1681. * Given pointers to KerberosTime and struct tm structs, convert the
  1682. * KerberosTime string to struct tm. Note that KerberosTime is a
  1683. * ASN1_GENERALIZEDTIME value, constrained to GMT with no fractional seconds
  1684. * as defined in RFC 1510. Return pointer to the (partially) filled in
  1685. * struct tm on success, return NULL on failure.
  1686. */
  1687. static struct tm *k_gmtime(ASN1_GENERALIZEDTIME *gtime, struct tm *k_tm)
  1688. {
  1689. char c, *p;
  1690. if (!k_tm)
  1691. return NULL;
  1692. if (gtime == NULL || gtime->length < 14)
  1693. return NULL;
  1694. if (gtime->data == NULL)
  1695. return NULL;
  1696. p = (char *)&gtime->data[14];
  1697. c = *p;
  1698. *p = '\0';
  1699. p -= 2;
  1700. k_tm->tm_sec = atoi(p);
  1701. *(p + 2) = c;
  1702. c = *p;
  1703. *p = '\0';
  1704. p -= 2;
  1705. k_tm->tm_min = atoi(p);
  1706. *(p + 2) = c;
  1707. c = *p;
  1708. *p = '\0';
  1709. p -= 2;
  1710. k_tm->tm_hour = atoi(p);
  1711. *(p + 2) = c;
  1712. c = *p;
  1713. *p = '\0';
  1714. p -= 2;
  1715. k_tm->tm_mday = atoi(p);
  1716. *(p + 2) = c;
  1717. c = *p;
  1718. *p = '\0';
  1719. p -= 2;
  1720. k_tm->tm_mon = atoi(p) - 1;
  1721. *(p + 2) = c;
  1722. c = *p;
  1723. *p = '\0';
  1724. p -= 4;
  1725. k_tm->tm_year = atoi(p) - 1900;
  1726. *(p + 4) = c;
  1727. return k_tm;
  1728. }
  1729. /*
  1730. * Helper function for kssl_validate_times(). We need context->clockskew,
  1731. * but krb5_context is an opaque struct. So we try to sneek the clockskew
  1732. * out through the replay cache. If that fails just return a likely default
  1733. * (300 seconds).
  1734. */
  1735. static krb5_deltat get_rc_clockskew(krb5_context context)
  1736. {
  1737. krb5_rcache rc;
  1738. krb5_deltat clockskew;
  1739. if (krb5_rc_default(context, &rc))
  1740. return KSSL_CLOCKSKEW;
  1741. if (krb5_rc_initialize(context, rc, 0))
  1742. return KSSL_CLOCKSKEW;
  1743. if (krb5_rc_get_lifespan(context, rc, &clockskew)) {
  1744. clockskew = KSSL_CLOCKSKEW;
  1745. }
  1746. (void)krb5_rc_destroy(context, rc);
  1747. return clockskew;
  1748. }
  1749. /*
  1750. * kssl_validate_times() combines (and more importantly exposes) the MIT KRB5
  1751. * internal function krb5_validate_times() and the in_clock_skew() macro.
  1752. * The authenticator client time is checked to be within clockskew secs of
  1753. * the current time and the current time is checked to be within the ticket
  1754. * start and expire times. Either check may be omitted by supplying a NULL
  1755. * value. Returns 0 for valid times, SSL_R_KRB5* error codes otherwise. See
  1756. * Also: (Kerberos source)/krb5/lib/krb5/krb/valid_times.c 20010420 VRS
  1757. */
  1758. krb5_error_code kssl_validate_times(krb5_timestamp atime,
  1759. krb5_ticket_times *ttimes)
  1760. {
  1761. krb5_deltat skew;
  1762. krb5_timestamp start, now;
  1763. krb5_error_code rc;
  1764. krb5_context context;
  1765. if ((rc = krb5_init_context(&context)))
  1766. return SSL_R_KRB5_S_BAD_TICKET;
  1767. skew = get_rc_clockskew(context);
  1768. if ((rc = krb5_timeofday(context, &now)))
  1769. return SSL_R_KRB5_S_BAD_TICKET;
  1770. krb5_free_context(context);
  1771. if (atime && labs(atime - now) >= skew)
  1772. return SSL_R_KRB5_S_TKT_SKEW;
  1773. if (!ttimes)
  1774. return 0;
  1775. start = (ttimes->starttime != 0) ? ttimes->starttime : ttimes->authtime;
  1776. if (start - now > skew)
  1777. return SSL_R_KRB5_S_TKT_NYV;
  1778. if ((now - ttimes->endtime) > skew)
  1779. return SSL_R_KRB5_S_TKT_EXPIRED;
  1780. # ifdef KSSL_DEBUG
  1781. fprintf(stderr, "kssl_validate_times: %d |<- | %d - %d | < %d ->| %d\n",
  1782. start, atime, now, skew, ttimes->endtime);
  1783. # endif /* KSSL_DEBUG */
  1784. return 0;
  1785. }
  1786. /*
  1787. * Decode and decrypt given DER-encoded authenticator, then pass
  1788. * authenticator ctime back in *atimep (or 0 if time unavailable). Returns
  1789. * krb5_error_code and kssl_err on error. A NULL authenticator
  1790. * (authentp->length == 0) is not considered an error. Note that
  1791. * kssl_check_authent() makes use of the KRB5 session key; you must call
  1792. * kssl_sget_tkt() to get the key before calling this routine.
  1793. */
  1794. krb5_error_code kssl_check_authent(
  1795. /*
  1796. * IN
  1797. */ KSSL_CTX *kssl_ctx,
  1798. /*
  1799. * IN
  1800. */ krb5_data *authentp,
  1801. /*
  1802. * OUT
  1803. */ krb5_timestamp *atimep,
  1804. /*
  1805. * OUT
  1806. */ KSSL_ERR *kssl_err)
  1807. {
  1808. krb5_error_code krb5rc = 0;
  1809. KRB5_ENCDATA *dec_authent = NULL;
  1810. KRB5_AUTHENTBODY *auth = NULL;
  1811. krb5_enctype enctype;
  1812. EVP_CIPHER_CTX ciph_ctx;
  1813. const EVP_CIPHER *enc = NULL;
  1814. unsigned char iv[EVP_MAX_IV_LENGTH];
  1815. const unsigned char *p;
  1816. unsigned char *unenc_authent;
  1817. int outl, unencbufsize;
  1818. struct tm tm_time, *tm_l, *tm_g;
  1819. time_t now, tl, tg, tr, tz_offset;
  1820. struct tm gmt_result = {0};
  1821. struct tm lt_result = {0};
  1822. EVP_CIPHER_CTX_init(&ciph_ctx);
  1823. *atimep = 0;
  1824. kssl_err_set(kssl_err, 0, "");
  1825. # ifndef KRB5CHECKAUTH
  1826. authentp = NULL;
  1827. # else
  1828. # if KRB5CHECKAUTH == 0
  1829. authentp = NULL;
  1830. # endif
  1831. # endif /* KRB5CHECKAUTH */
  1832. if (authentp == NULL || authentp->length == 0)
  1833. return 0;
  1834. # ifdef KSSL_DEBUG
  1835. {
  1836. unsigned int ui;
  1837. fprintf(stderr, "kssl_check_authent: authenticator[%d]:\n",
  1838. authentp->length);
  1839. p = authentp->data;
  1840. for (ui = 0; ui < authentp->length; ui++)
  1841. fprintf(stderr, "%02x ", p[ui]);
  1842. fprintf(stderr, "\n");
  1843. }
  1844. # endif /* KSSL_DEBUG */
  1845. unencbufsize = 2 * authentp->length;
  1846. if ((unenc_authent = calloc(1, unencbufsize)) == NULL) {
  1847. kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
  1848. "Unable to allocate authenticator buffer.\n");
  1849. krb5rc = KRB5KRB_ERR_GENERIC;
  1850. goto err;
  1851. }
  1852. p = (unsigned char *)authentp->data;
  1853. if ((dec_authent = d2i_KRB5_ENCDATA(NULL, &p,
  1854. (long)authentp->length)) == NULL) {
  1855. kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
  1856. "Error decoding authenticator.\n");
  1857. krb5rc = KRB5KRB_AP_ERR_BAD_INTEGRITY;
  1858. goto err;
  1859. }
  1860. enctype = dec_authent->etype->data[0]; /* should = kssl_ctx->enctype */
  1861. # if !defined(KRB5_MIT_OLD11)
  1862. switch (enctype) {
  1863. case ENCTYPE_DES3_CBC_SHA1: /* EVP_des_ede3_cbc(); */
  1864. case ENCTYPE_DES3_CBC_SHA:
  1865. case ENCTYPE_DES3_CBC_RAW:
  1866. krb5rc = 0; /* Skip, can't handle derived keys */
  1867. goto err;
  1868. }
  1869. # endif
  1870. enc = kssl_map_enc(enctype);
  1871. memset(iv, 0, sizeof(iv)); /* per RFC 1510 */
  1872. if (enc == NULL) {
  1873. /*
  1874. * Disable kssl_check_authent for ENCTYPE_DES3_CBC_SHA1. This
  1875. * enctype indicates the authenticator was encrypted using key-usage
  1876. * derived keys which openssl cannot decrypt.
  1877. */
  1878. goto err;
  1879. }
  1880. if (!EVP_CipherInit(&ciph_ctx, enc, kssl_ctx->key, iv, 0)) {
  1881. kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
  1882. "EVP_CipherInit error decrypting authenticator.\n");
  1883. krb5rc = KRB5KRB_AP_ERR_BAD_INTEGRITY;
  1884. goto err;
  1885. }
  1886. outl = dec_authent->cipher->length;
  1887. if (!EVP_Cipher
  1888. (&ciph_ctx, unenc_authent, dec_authent->cipher->data, outl)) {
  1889. kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
  1890. "EVP_Cipher error decrypting authenticator.\n");
  1891. krb5rc = KRB5KRB_AP_ERR_BAD_INTEGRITY;
  1892. goto err;
  1893. }
  1894. EVP_CIPHER_CTX_cleanup(&ciph_ctx);
  1895. # ifdef KSSL_DEBUG
  1896. {
  1897. int padl;
  1898. fprintf(stderr, "kssl_check_authent: decrypted authenticator[%d] =\n",
  1899. outl);
  1900. for (padl = 0; padl < outl; padl++)
  1901. fprintf(stderr, "%02x ", unenc_authent[padl]);
  1902. fprintf(stderr, "\n");
  1903. }
  1904. # endif /* KSSL_DEBUG */
  1905. if ((p = kssl_skip_confound(enctype, unenc_authent)) == NULL) {
  1906. kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
  1907. "confounded by authenticator.\n");
  1908. krb5rc = KRB5KRB_AP_ERR_BAD_INTEGRITY;
  1909. goto err;
  1910. }
  1911. outl -= p - unenc_authent;
  1912. if ((auth = (KRB5_AUTHENTBODY *)d2i_KRB5_AUTHENT(NULL, &p,
  1913. (long)outl)) == NULL) {
  1914. kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
  1915. "Error decoding authenticator body.\n");
  1916. krb5rc = KRB5KRB_AP_ERR_BAD_INTEGRITY;
  1917. goto err;
  1918. }
  1919. memset(&tm_time, 0, sizeof(struct tm));
  1920. if (k_gmtime(auth->ctime, &tm_time) &&
  1921. ((tr = mktime(&tm_time)) != (time_t)(-1))) {
  1922. now = time(&now);
  1923. tm_g = OPENSSL_gmtime(&now, &gmt_result);
  1924. # if defined(OPENSSL_THREADS) && !defined(OPENSSL_SYS_WIN32) && \
  1925. !defined(OPENSSL_SYS_OS2) && !defined(OPENSSL_SYS_SUNOS) && \
  1926. (!defined(OPENSSL_SYS_VMS) || defined(localtime_r))
  1927. tm_l = localtime_r(&now, &lt_result);
  1928. # else
  1929. tm_l = localtime(&now);
  1930. # endif
  1931. tl = mktime(tm_l);
  1932. tg = mktime(tm_g);
  1933. tz_offset = tg - tl;
  1934. *atimep = (krb5_timestamp)(tr - tz_offset);
  1935. }
  1936. # ifdef KSSL_DEBUG
  1937. fprintf(stderr, "kssl_check_authent: returns %d for client time ",
  1938. *atimep);
  1939. if (auth && auth->ctime && auth->ctime->length && auth->ctime->data)
  1940. fprintf(stderr, "%.*s\n", auth->ctime->length, auth->ctime->data);
  1941. else
  1942. fprintf(stderr, "NULL\n");
  1943. # endif /* KSSL_DEBUG */
  1944. err:
  1945. if (auth)
  1946. KRB5_AUTHENT_free((KRB5_AUTHENT *) auth);
  1947. if (dec_authent)
  1948. KRB5_ENCDATA_free(dec_authent);
  1949. if (unenc_authent)
  1950. free(unenc_authent);
  1951. EVP_CIPHER_CTX_cleanup(&ciph_ctx);
  1952. return krb5rc;
  1953. }
  1954. /*
  1955. * Replaces krb5_build_principal_ext(), with varargs length == 2 (svc, host),
  1956. * because I don't know how to stub varargs. Returns krb5_error_code ==
  1957. * ENOMEM on alloc error, otherwise passes back newly constructed principal,
  1958. * which should be freed by caller.
  1959. */
  1960. krb5_error_code kssl_build_principal_2(
  1961. /*
  1962. * UPDATE
  1963. */ krb5_context context,
  1964. /*
  1965. * OUT
  1966. */ krb5_principal *princ,
  1967. /*
  1968. * IN
  1969. */ int rlen, const char *realm,
  1970. /*
  1971. * IN
  1972. */ int slen, const char *svc,
  1973. /*
  1974. * IN
  1975. */ int hlen, const char *host)
  1976. {
  1977. krb5_data *p_data = NULL;
  1978. krb5_principal new_p = NULL;
  1979. char *new_r = NULL;
  1980. if ((p_data = (krb5_data *)calloc(2, sizeof(krb5_data))) == NULL ||
  1981. (new_p = (krb5_principal)calloc(1, sizeof(krb5_principal_data)))
  1982. == NULL)
  1983. goto err;
  1984. new_p->length = 2;
  1985. new_p->data = p_data;
  1986. if ((new_r = calloc(1, rlen + 1)) == NULL)
  1987. goto err;
  1988. memcpy(new_r, realm, rlen);
  1989. krb5_princ_set_realm_length(context, new_p, rlen);
  1990. krb5_princ_set_realm_data(context, new_p, new_r);
  1991. if ((new_p->data[0].data = calloc(1, slen + 1)) == NULL)
  1992. goto err;
  1993. memcpy(new_p->data[0].data, svc, slen);
  1994. new_p->data[0].length = slen;
  1995. if ((new_p->data[1].data = calloc(1, hlen + 1)) == NULL)
  1996. goto err;
  1997. memcpy(new_p->data[1].data, host, hlen);
  1998. new_p->data[1].length = hlen;
  1999. krb5_princ_type(context, new_p) = KRB5_NT_UNKNOWN;
  2000. *princ = new_p;
  2001. return 0;
  2002. err:
  2003. if (new_p && new_p[0].data)
  2004. free(new_p[0].data);
  2005. if (new_p && new_p[1].data)
  2006. free(new_p[1].data);
  2007. if (new_p)
  2008. free(new_p);
  2009. if (new_r)
  2010. free(new_r);
  2011. return ENOMEM;
  2012. }
  2013. void SSL_set0_kssl_ctx(SSL *s, KSSL_CTX *kctx)
  2014. {
  2015. s->kssl_ctx = kctx;
  2016. }
  2017. KSSL_CTX *SSL_get0_kssl_ctx(SSL *s)
  2018. {
  2019. return s->kssl_ctx;
  2020. }
  2021. char *kssl_ctx_get0_client_princ(KSSL_CTX *kctx)
  2022. {
  2023. if (kctx)
  2024. return kctx->client_princ;
  2025. return NULL;
  2026. }
  2027. #else /* !OPENSSL_NO_KRB5 */
  2028. # if defined(PEDANTIC) || defined(OPENSSL_SYS_VMS)
  2029. static void *dummy = &dummy;
  2030. # endif
  2031. #endif /* !OPENSSL_NO_KRB5 */