kssl.c 68 KB

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