kssl.c 67 KB

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