kssl.c 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184
  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. char
  711. *kstring(char *string)
  712. {
  713. static char *null = "[NULL]";
  714. return ((string == NULL)? null: string);
  715. }
  716. /* Given KRB5 enctype (basically DES or 3DES),
  717. ** return closest match openssl EVP_ encryption algorithm.
  718. ** Return NULL for unknown or problematic (krb5_dk_encrypt) enctypes.
  719. ** Assume ENCTYPE_*_RAW (krb5_raw_encrypt) are OK.
  720. */
  721. const EVP_CIPHER *
  722. kssl_map_enc(krb5_enctype enctype)
  723. {
  724. switch (enctype)
  725. {
  726. case ENCTYPE_DES_HMAC_SHA1: /* EVP_des_cbc(); */
  727. case ENCTYPE_DES_CBC_CRC:
  728. case ENCTYPE_DES_CBC_MD4:
  729. case ENCTYPE_DES_CBC_MD5:
  730. case ENCTYPE_DES_CBC_RAW:
  731. return EVP_des_cbc();
  732. break;
  733. case ENCTYPE_DES3_CBC_SHA1: /* EVP_des_ede3_cbc(); */
  734. case ENCTYPE_DES3_CBC_SHA:
  735. case ENCTYPE_DES3_CBC_RAW:
  736. return EVP_des_ede3_cbc();
  737. break;
  738. default: return NULL;
  739. break;
  740. }
  741. }
  742. /* Return true:1 if p "looks like" the start of the real authenticator
  743. ** described in kssl_skip_confound() below. The ASN.1 pattern is
  744. ** "62 xx 30 yy" (APPLICATION-2, SEQUENCE), where xx-yy =~ 2, and
  745. ** xx and yy are possibly multi-byte length fields.
  746. */
  747. int kssl_test_confound(unsigned char *p)
  748. {
  749. int len = 2;
  750. int xx = 0, yy = 0;
  751. if (*p++ != 0x62) return 0;
  752. if (*p > 0x82) return 0;
  753. switch(*p) {
  754. case 0x82: p++; xx = (*p++ << 8); xx += *p++; break;
  755. case 0x81: p++; xx = *p++; break;
  756. case 0x80: return 0;
  757. default: xx = *p++; break;
  758. }
  759. if (*p++ != 0x30) return 0;
  760. if (*p > 0x82) return 0;
  761. switch(*p) {
  762. case 0x82: p++; len+=2; yy = (*p++ << 8); yy += *p++; break;
  763. case 0x81: p++; len++; yy = *p++; break;
  764. case 0x80: return 0;
  765. default: yy = *p++; break;
  766. }
  767. return (xx - len == yy)? 1: 0;
  768. }
  769. /* Allocate, fill, and return cksumlens array of checksum lengths.
  770. ** This array holds just the unique elements from the krb5_cksumarray[].
  771. ** array[n] == 0 signals end of data.
  772. **
  773. ** The krb5_cksumarray[] was an internal variable that has since been
  774. ** replaced by a more general method for storing the data. It should
  775. ** not be used. Instead we use real API calls and make a guess for
  776. ** what the highest assigned CKSUMTYPE_ constant is. As of 1.2.2
  777. ** it is 0x000c (CKSUMTYPE_HMAC_SHA1_DES3). So we will use 0x0010.
  778. */
  779. size_t *populate_cksumlens(void)
  780. {
  781. int i, j, n;
  782. static size_t *cklens = NULL;
  783. #ifdef KRB5_MIT_OLD11
  784. n = krb5_max_cksum;
  785. #else
  786. n = 0x0010;
  787. #endif /* KRB5_MIT_OLD11 */
  788. #ifdef KRB5CHECKAUTH
  789. if (!cklens && !(cklens = (size_t *) calloc(sizeof(int),n+1))) return NULL;
  790. for (i=0; i < n; i++) {
  791. if (!valid_cksumtype(i)) continue; /* array has holes */
  792. for (j=0; j < n; j++) {
  793. if (cklens[j] == 0) {
  794. cklens[j] = krb5_checksum_size(NULL,i);
  795. break; /* krb5 elem was new: add */
  796. }
  797. if (cklens[j] == krb5_checksum_size(NULL,i)) {
  798. break; /* ignore duplicate elements */
  799. }
  800. }
  801. }
  802. #endif /* KRB5CHECKAUTH */
  803. return cklens;
  804. }
  805. /* Return pointer to start of real authenticator within authenticator, or
  806. ** return NULL on error.
  807. ** Decrypted authenticator looks like this:
  808. ** [0 or 8 byte confounder] [4-24 byte checksum] [real authent'r]
  809. ** This hackery wouldn't be necessary if MIT KRB5 1.0.6 had the
  810. ** krb5_auth_con_getcksumtype() function advertised in its krb5.h.
  811. */
  812. unsigned char *kssl_skip_confound(krb5_enctype etype, unsigned char *a)
  813. {
  814. int i, conlen;
  815. size_t cklen;
  816. static size_t *cksumlens = NULL;
  817. unsigned char *test_auth;
  818. conlen = (etype)? 8: 0;
  819. if (!cksumlens && !(cksumlens = populate_cksumlens())) return NULL;
  820. for (i=0; (cklen = cksumlens[i]) != 0; i++)
  821. {
  822. test_auth = a + conlen + cklen;
  823. if (kssl_test_confound(test_auth)) return test_auth;
  824. }
  825. return NULL;
  826. }
  827. /* Set kssl_err error info when reason text is a simple string
  828. ** kssl_err = struct { int reason; char text[KSSL_ERR_MAX+1]; }
  829. */
  830. void
  831. kssl_err_set(KSSL_ERR *kssl_err, int reason, char *text)
  832. {
  833. if (kssl_err == NULL) return;
  834. kssl_err->reason = reason;
  835. BIO_snprintf(kssl_err->text, KSSL_ERR_MAX, text);
  836. return;
  837. }
  838. /* Display contents of krb5_data struct, for debugging
  839. */
  840. void
  841. print_krb5_data(char *label, krb5_data *kdata)
  842. {
  843. int i;
  844. printf("%s[%d] ", label, kdata->length);
  845. for (i=0; i < (int)kdata->length; i++)
  846. {
  847. if (0 && isprint((int) kdata->data[i]))
  848. printf( "%c ", kdata->data[i]);
  849. else
  850. printf( "%02x ", (unsigned char) kdata->data[i]);
  851. }
  852. printf("\n");
  853. }
  854. /* Display contents of krb5_authdata struct, for debugging
  855. */
  856. void
  857. print_krb5_authdata(char *label, krb5_authdata **adata)
  858. {
  859. if (adata == NULL)
  860. {
  861. printf("%s, authdata==0\n", label);
  862. return;
  863. }
  864. printf("%s [%p]\n", label, (void *)adata);
  865. #if 0
  866. {
  867. int i;
  868. printf("%s[at%d:%d] ", label, adata->ad_type, adata->length);
  869. for (i=0; i < adata->length; i++)
  870. {
  871. printf((isprint(adata->contents[i]))? "%c ": "%02x",
  872. adata->contents[i]);
  873. }
  874. printf("\n");
  875. }
  876. #endif
  877. }
  878. /* Display contents of krb5_keyblock struct, for debugging
  879. */
  880. void
  881. print_krb5_keyblock(char *label, krb5_keyblock *keyblk)
  882. {
  883. int i;
  884. if (keyblk == NULL)
  885. {
  886. printf("%s, keyblk==0\n", label);
  887. return;
  888. }
  889. #ifdef KRB5_HEIMDAL
  890. printf("%s\n\t[et%d:%d]: ", label, keyblk->keytype,
  891. keyblk->keyvalue->length);
  892. for (i=0; i < (int)keyblk->keyvalue->length; i++)
  893. {
  894. printf("%02x",(unsigned char *)(keyblk->keyvalue->contents)[i]);
  895. }
  896. printf("\n");
  897. #else
  898. printf("%s\n\t[et%d:%d]: ", label, keyblk->enctype, keyblk->length);
  899. for (i=0; i < (int)keyblk->length; i++)
  900. {
  901. printf("%02x",keyblk->contents[i]);
  902. }
  903. printf("\n");
  904. #endif
  905. }
  906. /* Display contents of krb5_principal_data struct, for debugging
  907. ** (krb5_principal is typedef'd == krb5_principal_data *)
  908. */
  909. void
  910. print_krb5_princ(char *label, krb5_principal_data *princ)
  911. {
  912. int i, ui, uj;
  913. printf("%s principal Realm: ", label);
  914. if (princ == NULL) return;
  915. for (ui=0; ui < (int)princ->realm.length; ui++) putchar(princ->realm.data[ui]);
  916. printf(" (nametype %d) has %d strings:\n", princ->type,princ->length);
  917. for (i=0; i < (int)princ->length; i++)
  918. {
  919. printf("\t%d [%d]: ", i, princ->data[i].length);
  920. for (uj=0; uj < (int)princ->data[i].length; uj++) {
  921. putchar(princ->data[i].data[uj]);
  922. }
  923. printf("\n");
  924. }
  925. return;
  926. }
  927. /* Given krb5 service (typically "kssl") and hostname in kssl_ctx,
  928. ** Return encrypted Kerberos ticket for service @ hostname.
  929. ** If authenp is non-NULL, also return encrypted authenticator,
  930. ** whose data should be freed by caller.
  931. ** (Originally was: Create Kerberos AP_REQ message for SSL Client.)
  932. **
  933. ** 19990628 VRS Started; Returns Kerberos AP_REQ message.
  934. ** 20010409 VRS Modified for RFC2712; Returns enc tkt.
  935. ** 20010606 VRS May also return optional authenticator.
  936. */
  937. krb5_error_code
  938. kssl_cget_tkt( /* UPDATE */ KSSL_CTX *kssl_ctx,
  939. /* OUT */ krb5_data **enc_ticketp,
  940. /* UPDATE */ krb5_data *authenp,
  941. /* OUT */ KSSL_ERR *kssl_err)
  942. {
  943. krb5_error_code krb5rc = KRB5KRB_ERR_GENERIC;
  944. krb5_context krb5context = NULL;
  945. krb5_auth_context krb5auth_context = NULL;
  946. krb5_ccache krb5ccdef = NULL;
  947. krb5_creds krb5creds, *krb5credsp = NULL;
  948. krb5_data krb5_app_req;
  949. kssl_err_set(kssl_err, 0, "");
  950. memset((char *)&krb5creds, 0, sizeof(krb5creds));
  951. if (!kssl_ctx)
  952. {
  953. kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
  954. "No kssl_ctx defined.\n");
  955. goto err;
  956. }
  957. else if (!kssl_ctx->service_host)
  958. {
  959. kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
  960. "kssl_ctx service_host undefined.\n");
  961. goto err;
  962. }
  963. if ((krb5rc = krb5_init_context(&krb5context)) != 0)
  964. {
  965. BIO_snprintf(kssl_err->text,KSSL_ERR_MAX,
  966. "krb5_init_context() fails: %d\n", krb5rc);
  967. kssl_err->reason = SSL_R_KRB5_C_INIT;
  968. goto err;
  969. }
  970. if ((krb5rc = krb5_sname_to_principal(krb5context,
  971. kssl_ctx->service_host,
  972. (kssl_ctx->service_name)? kssl_ctx->service_name: KRB5SVC,
  973. KRB5_NT_SRV_HST, &krb5creds.server)) != 0)
  974. {
  975. BIO_snprintf(kssl_err->text,KSSL_ERR_MAX,
  976. "krb5_sname_to_principal() fails for %s/%s\n",
  977. kssl_ctx->service_host,
  978. (kssl_ctx->service_name)? kssl_ctx->service_name:
  979. KRB5SVC);
  980. kssl_err->reason = SSL_R_KRB5_C_INIT;
  981. goto err;
  982. }
  983. if ((krb5rc = krb5_cc_default(krb5context, &krb5ccdef)) != 0)
  984. {
  985. kssl_err_set(kssl_err, SSL_R_KRB5_C_CC_PRINC,
  986. "krb5_cc_default fails.\n");
  987. goto err;
  988. }
  989. if ((krb5rc = krb5_cc_get_principal(krb5context, krb5ccdef,
  990. &krb5creds.client)) != 0)
  991. {
  992. kssl_err_set(kssl_err, SSL_R_KRB5_C_CC_PRINC,
  993. "krb5_cc_get_principal() fails.\n");
  994. goto err;
  995. }
  996. if ((krb5rc = krb5_get_credentials(krb5context, 0, krb5ccdef,
  997. &krb5creds, &krb5credsp)) != 0)
  998. {
  999. kssl_err_set(kssl_err, SSL_R_KRB5_C_GET_CRED,
  1000. "krb5_get_credentials() fails.\n");
  1001. goto err;
  1002. }
  1003. *enc_ticketp = &krb5credsp->ticket;
  1004. #ifdef KRB5_HEIMDAL
  1005. kssl_ctx->enctype = krb5credsp->session.keytype;
  1006. #else
  1007. kssl_ctx->enctype = krb5credsp->keyblock.enctype;
  1008. #endif
  1009. krb5rc = KRB5KRB_ERR_GENERIC;
  1010. /* caller should free data of krb5_app_req */
  1011. /* 20010406 VRS deleted for real KerberosWrapper
  1012. ** 20010605 VRS reinstated to offer Authenticator to KerberosWrapper
  1013. */
  1014. krb5_app_req.length = 0;
  1015. if (authenp)
  1016. {
  1017. krb5_data krb5in_data;
  1018. const unsigned char *p;
  1019. long arlen;
  1020. KRB5_APREQBODY *ap_req;
  1021. authenp->length = 0;
  1022. krb5in_data.data = NULL;
  1023. krb5in_data.length = 0;
  1024. if ((krb5rc = krb5_mk_req_extended(krb5context,
  1025. &krb5auth_context, 0, &krb5in_data, krb5credsp,
  1026. &krb5_app_req)) != 0)
  1027. {
  1028. kssl_err_set(kssl_err, SSL_R_KRB5_C_MK_REQ,
  1029. "krb5_mk_req_extended() fails.\n");
  1030. goto err;
  1031. }
  1032. arlen = krb5_app_req.length;
  1033. p = (unsigned char *)krb5_app_req.data;
  1034. ap_req = (KRB5_APREQBODY *) d2i_KRB5_APREQ(NULL, &p, arlen);
  1035. if (ap_req)
  1036. {
  1037. authenp->length = i2d_KRB5_ENCDATA(
  1038. ap_req->authenticator, NULL);
  1039. if (authenp->length &&
  1040. (authenp->data = malloc(authenp->length)))
  1041. {
  1042. unsigned char *adp = (unsigned char *)authenp->data;
  1043. authenp->length = i2d_KRB5_ENCDATA(
  1044. ap_req->authenticator, &adp);
  1045. }
  1046. }
  1047. if (ap_req) KRB5_APREQ_free((KRB5_APREQ *) ap_req);
  1048. if (krb5_app_req.length)
  1049. kssl_krb5_free_data_contents(krb5context,&krb5_app_req);
  1050. }
  1051. #ifdef KRB5_HEIMDAL
  1052. if (kssl_ctx_setkey(kssl_ctx, &krb5credsp->session))
  1053. {
  1054. kssl_err_set(kssl_err, SSL_R_KRB5_C_INIT,
  1055. "kssl_ctx_setkey() fails.\n");
  1056. }
  1057. #else
  1058. if (kssl_ctx_setkey(kssl_ctx, &krb5credsp->keyblock))
  1059. {
  1060. kssl_err_set(kssl_err, SSL_R_KRB5_C_INIT,
  1061. "kssl_ctx_setkey() fails.\n");
  1062. }
  1063. #endif
  1064. else krb5rc = 0;
  1065. err:
  1066. #ifdef KSSL_DEBUG
  1067. kssl_ctx_show(kssl_ctx);
  1068. #endif /* KSSL_DEBUG */
  1069. if (krb5creds.client) krb5_free_principal(krb5context,
  1070. krb5creds.client);
  1071. if (krb5creds.server) krb5_free_principal(krb5context,
  1072. krb5creds.server);
  1073. if (krb5auth_context) krb5_auth_con_free(krb5context,
  1074. krb5auth_context);
  1075. if (krb5context) krb5_free_context(krb5context);
  1076. return (krb5rc);
  1077. }
  1078. /* Given d2i_-decoded asn1ticket, allocate and return a new krb5_ticket.
  1079. ** Return Kerberos error code and kssl_err struct on error.
  1080. ** Allocates krb5_ticket and krb5_principal; caller should free these.
  1081. **
  1082. ** 20010410 VRS Implemented krb5_decode_ticket() as
  1083. ** old_krb5_decode_ticket(). Missing from MIT1.0.6.
  1084. ** 20010615 VRS Re-cast as openssl/asn1 d2i_*() functions.
  1085. ** Re-used some of the old krb5_decode_ticket()
  1086. ** code here. This tkt should alloc/free just
  1087. ** like the real thing.
  1088. */
  1089. krb5_error_code
  1090. kssl_TKT2tkt( /* IN */ krb5_context krb5context,
  1091. /* IN */ KRB5_TKTBODY *asn1ticket,
  1092. /* OUT */ krb5_ticket **krb5ticket,
  1093. /* OUT */ KSSL_ERR *kssl_err )
  1094. {
  1095. krb5_error_code krb5rc = KRB5KRB_ERR_GENERIC;
  1096. krb5_ticket *new5ticket = NULL;
  1097. ASN1_GENERALSTRING *gstr_svc, *gstr_host;
  1098. *krb5ticket = NULL;
  1099. if (asn1ticket == NULL || asn1ticket->realm == NULL ||
  1100. asn1ticket->sname == NULL ||
  1101. sk_ASN1_GENERALSTRING_num(asn1ticket->sname->namestring) < 2)
  1102. {
  1103. BIO_snprintf(kssl_err->text, KSSL_ERR_MAX,
  1104. "Null field in asn1ticket.\n");
  1105. kssl_err->reason = SSL_R_KRB5_S_RD_REQ;
  1106. return KRB5KRB_ERR_GENERIC;
  1107. }
  1108. if ((new5ticket = (krb5_ticket *) calloc(1, sizeof(krb5_ticket)))==NULL)
  1109. {
  1110. BIO_snprintf(kssl_err->text, KSSL_ERR_MAX,
  1111. "Unable to allocate new krb5_ticket.\n");
  1112. kssl_err->reason = SSL_R_KRB5_S_RD_REQ;
  1113. return ENOMEM; /* or KRB5KRB_ERR_GENERIC; */
  1114. }
  1115. gstr_svc = sk_ASN1_GENERALSTRING_value(asn1ticket->sname->namestring, 0);
  1116. gstr_host = sk_ASN1_GENERALSTRING_value(asn1ticket->sname->namestring, 1);
  1117. if ((krb5rc = kssl_build_principal_2(krb5context,
  1118. &new5ticket->server,
  1119. asn1ticket->realm->length, (char *)asn1ticket->realm->data,
  1120. gstr_svc->length, (char *)gstr_svc->data,
  1121. gstr_host->length, (char *)gstr_host->data)) != 0)
  1122. {
  1123. free(new5ticket);
  1124. BIO_snprintf(kssl_err->text, KSSL_ERR_MAX,
  1125. "Error building ticket server principal.\n");
  1126. kssl_err->reason = SSL_R_KRB5_S_RD_REQ;
  1127. return krb5rc; /* or KRB5KRB_ERR_GENERIC; */
  1128. }
  1129. krb5_princ_type(krb5context, new5ticket->server) =
  1130. asn1ticket->sname->nametype->data[0];
  1131. new5ticket->enc_part.enctype = asn1ticket->encdata->etype->data[0];
  1132. new5ticket->enc_part.kvno = asn1ticket->encdata->kvno->data[0];
  1133. new5ticket->enc_part.ciphertext.length =
  1134. asn1ticket->encdata->cipher->length;
  1135. if ((new5ticket->enc_part.ciphertext.data =
  1136. calloc(1, asn1ticket->encdata->cipher->length)) == NULL)
  1137. {
  1138. free(new5ticket);
  1139. BIO_snprintf(kssl_err->text, KSSL_ERR_MAX,
  1140. "Error allocating cipher in krb5ticket.\n");
  1141. kssl_err->reason = SSL_R_KRB5_S_RD_REQ;
  1142. return KRB5KRB_ERR_GENERIC;
  1143. }
  1144. else
  1145. {
  1146. memcpy(new5ticket->enc_part.ciphertext.data,
  1147. asn1ticket->encdata->cipher->data,
  1148. asn1ticket->encdata->cipher->length);
  1149. }
  1150. *krb5ticket = new5ticket;
  1151. return 0;
  1152. }
  1153. /* Given krb5 service name in KSSL_CTX *kssl_ctx (typically "kssl"),
  1154. ** and krb5 AP_REQ message & message length,
  1155. ** Return Kerberos session key and client principle
  1156. ** to SSL Server in KSSL_CTX *kssl_ctx.
  1157. **
  1158. ** 19990702 VRS Started.
  1159. */
  1160. krb5_error_code
  1161. kssl_sget_tkt( /* UPDATE */ KSSL_CTX *kssl_ctx,
  1162. /* IN */ krb5_data *indata,
  1163. /* OUT */ krb5_ticket_times *ttimes,
  1164. /* OUT */ KSSL_ERR *kssl_err )
  1165. {
  1166. krb5_error_code krb5rc = KRB5KRB_ERR_GENERIC;
  1167. static krb5_context krb5context = NULL;
  1168. static krb5_auth_context krb5auth_context = NULL;
  1169. krb5_ticket *krb5ticket = NULL;
  1170. KRB5_TKTBODY *asn1ticket = NULL;
  1171. const unsigned char *p;
  1172. krb5_keytab krb5keytab = NULL;
  1173. krb5_keytab_entry kt_entry;
  1174. krb5_principal krb5server;
  1175. krb5_rcache rcache = NULL;
  1176. kssl_err_set(kssl_err, 0, "");
  1177. if (!kssl_ctx)
  1178. {
  1179. kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
  1180. "No kssl_ctx defined.\n");
  1181. goto err;
  1182. }
  1183. #ifdef KSSL_DEBUG
  1184. printf("in kssl_sget_tkt(%s)\n", kstring(kssl_ctx->service_name));
  1185. #endif /* KSSL_DEBUG */
  1186. if (!krb5context && (krb5rc = krb5_init_context(&krb5context)))
  1187. {
  1188. kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
  1189. "krb5_init_context() fails.\n");
  1190. goto err;
  1191. }
  1192. if (krb5auth_context &&
  1193. (krb5rc = krb5_auth_con_free(krb5context, krb5auth_context)))
  1194. {
  1195. kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
  1196. "krb5_auth_con_free() fails.\n");
  1197. goto err;
  1198. }
  1199. else krb5auth_context = NULL;
  1200. if (!krb5auth_context &&
  1201. (krb5rc = krb5_auth_con_init(krb5context, &krb5auth_context)))
  1202. {
  1203. kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
  1204. "krb5_auth_con_init() fails.\n");
  1205. goto err;
  1206. }
  1207. if ((krb5rc = krb5_auth_con_getrcache(krb5context, krb5auth_context,
  1208. &rcache)))
  1209. {
  1210. kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
  1211. "krb5_auth_con_getrcache() fails.\n");
  1212. goto err;
  1213. }
  1214. if ((krb5rc = krb5_sname_to_principal(krb5context, NULL,
  1215. (kssl_ctx->service_name)? kssl_ctx->service_name: KRB5SVC,
  1216. KRB5_NT_SRV_HST, &krb5server)) != 0)
  1217. {
  1218. kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
  1219. "krb5_sname_to_principal() fails.\n");
  1220. goto err;
  1221. }
  1222. if (rcache == NULL)
  1223. {
  1224. if ((krb5rc = krb5_get_server_rcache(krb5context,
  1225. krb5_princ_component(krb5context, krb5server, 0),
  1226. &rcache)))
  1227. {
  1228. kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
  1229. "krb5_get_server_rcache() fails.\n");
  1230. goto err;
  1231. }
  1232. }
  1233. if ((krb5rc = krb5_auth_con_setrcache(krb5context, krb5auth_context, rcache)))
  1234. {
  1235. kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
  1236. "krb5_auth_con_setrcache() fails.\n");
  1237. goto err;
  1238. }
  1239. /* kssl_ctx->keytab_file == NULL ==> use Kerberos default
  1240. */
  1241. if (kssl_ctx->keytab_file)
  1242. {
  1243. krb5rc = krb5_kt_resolve(krb5context, kssl_ctx->keytab_file,
  1244. &krb5keytab);
  1245. if (krb5rc)
  1246. {
  1247. kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
  1248. "krb5_kt_resolve() fails.\n");
  1249. goto err;
  1250. }
  1251. }
  1252. else
  1253. {
  1254. krb5rc = krb5_kt_default(krb5context,&krb5keytab);
  1255. if (krb5rc)
  1256. {
  1257. kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
  1258. "krb5_kt_default() fails.\n");
  1259. goto err;
  1260. }
  1261. }
  1262. /* Actual Kerberos5 krb5_recvauth() has initial conversation here
  1263. ** o check KRB5_SENDAUTH_BADAUTHVERS
  1264. ** unless KRB5_RECVAUTH_SKIP_VERSION
  1265. ** o check KRB5_SENDAUTH_BADAPPLVERS
  1266. ** o send "0" msg if all OK
  1267. */
  1268. /* 20010411 was using AP_REQ instead of true KerberosWrapper
  1269. **
  1270. ** if ((krb5rc = krb5_rd_req(krb5context, &krb5auth_context,
  1271. ** &krb5in_data, krb5server, krb5keytab,
  1272. ** &ap_option, &krb5ticket)) != 0) { Error }
  1273. */
  1274. p = (unsigned char *)indata->data;
  1275. if ((asn1ticket = (KRB5_TKTBODY *) d2i_KRB5_TICKET(NULL, &p,
  1276. (long) indata->length)) == NULL)
  1277. {
  1278. BIO_snprintf(kssl_err->text, KSSL_ERR_MAX,
  1279. "d2i_KRB5_TICKET() ASN.1 decode failure.\n");
  1280. kssl_err->reason = SSL_R_KRB5_S_RD_REQ;
  1281. goto err;
  1282. }
  1283. /* Was: krb5rc = krb5_decode_ticket(krb5in_data,&krb5ticket)) != 0) */
  1284. if ((krb5rc = kssl_TKT2tkt(krb5context, asn1ticket, &krb5ticket,
  1285. kssl_err)) != 0)
  1286. {
  1287. BIO_snprintf(kssl_err->text, KSSL_ERR_MAX,
  1288. "Error converting ASN.1 ticket to krb5_ticket.\n");
  1289. kssl_err->reason = SSL_R_KRB5_S_RD_REQ;
  1290. goto err;
  1291. }
  1292. if (! krb5_principal_compare(krb5context, krb5server,
  1293. krb5ticket->server)) {
  1294. krb5rc = KRB5_PRINC_NOMATCH;
  1295. BIO_snprintf(kssl_err->text, KSSL_ERR_MAX,
  1296. "server principal != ticket principal\n");
  1297. kssl_err->reason = SSL_R_KRB5_S_RD_REQ;
  1298. goto err;
  1299. }
  1300. if ((krb5rc = krb5_kt_get_entry(krb5context, krb5keytab,
  1301. krb5ticket->server, krb5ticket->enc_part.kvno,
  1302. krb5ticket->enc_part.enctype, &kt_entry)) != 0) {
  1303. BIO_snprintf(kssl_err->text, KSSL_ERR_MAX,
  1304. "krb5_kt_get_entry() fails with %x.\n", krb5rc);
  1305. kssl_err->reason = SSL_R_KRB5_S_RD_REQ;
  1306. goto err;
  1307. }
  1308. if ((krb5rc = krb5_decrypt_tkt_part(krb5context, &kt_entry.key,
  1309. krb5ticket)) != 0) {
  1310. BIO_snprintf(kssl_err->text, KSSL_ERR_MAX,
  1311. "krb5_decrypt_tkt_part() failed.\n");
  1312. kssl_err->reason = SSL_R_KRB5_S_RD_REQ;
  1313. goto err;
  1314. }
  1315. else {
  1316. krb5_kt_free_entry(krb5context, &kt_entry);
  1317. #ifdef KSSL_DEBUG
  1318. {
  1319. int i; krb5_address **paddr = krb5ticket->enc_part2->caddrs;
  1320. printf("Decrypted ticket fields:\n");
  1321. printf("\tflags: %X, transit-type: %X",
  1322. krb5ticket->enc_part2->flags,
  1323. krb5ticket->enc_part2->transited.tr_type);
  1324. print_krb5_data("\ttransit-data: ",
  1325. &(krb5ticket->enc_part2->transited.tr_contents));
  1326. printf("\tcaddrs: %p, authdata: %p\n",
  1327. krb5ticket->enc_part2->caddrs,
  1328. krb5ticket->enc_part2->authorization_data);
  1329. if (paddr)
  1330. {
  1331. printf("\tcaddrs:\n");
  1332. for (i=0; paddr[i] != NULL; i++)
  1333. {
  1334. krb5_data d;
  1335. d.length=paddr[i]->length;
  1336. d.data=paddr[i]->contents;
  1337. print_krb5_data("\t\tIP: ", &d);
  1338. }
  1339. }
  1340. printf("\tstart/auth/end times: %d / %d / %d\n",
  1341. krb5ticket->enc_part2->times.starttime,
  1342. krb5ticket->enc_part2->times.authtime,
  1343. krb5ticket->enc_part2->times.endtime);
  1344. }
  1345. #endif /* KSSL_DEBUG */
  1346. }
  1347. krb5rc = KRB5_NO_TKT_SUPPLIED;
  1348. if (!krb5ticket || !krb5ticket->enc_part2 ||
  1349. !krb5ticket->enc_part2->client ||
  1350. !krb5ticket->enc_part2->client->data ||
  1351. !krb5ticket->enc_part2->session)
  1352. {
  1353. kssl_err_set(kssl_err, SSL_R_KRB5_S_BAD_TICKET,
  1354. "bad ticket from krb5_rd_req.\n");
  1355. }
  1356. else if (kssl_ctx_setprinc(kssl_ctx, KSSL_CLIENT,
  1357. &krb5ticket->enc_part2->client->realm,
  1358. krb5ticket->enc_part2->client->data,
  1359. krb5ticket->enc_part2->client->length))
  1360. {
  1361. kssl_err_set(kssl_err, SSL_R_KRB5_S_BAD_TICKET,
  1362. "kssl_ctx_setprinc() fails.\n");
  1363. }
  1364. else if (kssl_ctx_setkey(kssl_ctx, krb5ticket->enc_part2->session))
  1365. {
  1366. kssl_err_set(kssl_err, SSL_R_KRB5_S_BAD_TICKET,
  1367. "kssl_ctx_setkey() fails.\n");
  1368. }
  1369. else if (krb5ticket->enc_part2->flags & TKT_FLG_INVALID)
  1370. {
  1371. krb5rc = KRB5KRB_AP_ERR_TKT_INVALID;
  1372. kssl_err_set(kssl_err, SSL_R_KRB5_S_BAD_TICKET,
  1373. "invalid ticket from krb5_rd_req.\n");
  1374. }
  1375. else krb5rc = 0;
  1376. kssl_ctx->enctype = krb5ticket->enc_part.enctype;
  1377. ttimes->authtime = krb5ticket->enc_part2->times.authtime;
  1378. ttimes->starttime = krb5ticket->enc_part2->times.starttime;
  1379. ttimes->endtime = krb5ticket->enc_part2->times.endtime;
  1380. ttimes->renew_till = krb5ticket->enc_part2->times.renew_till;
  1381. err:
  1382. #ifdef KSSL_DEBUG
  1383. kssl_ctx_show(kssl_ctx);
  1384. #endif /* KSSL_DEBUG */
  1385. if (asn1ticket) KRB5_TICKET_free((KRB5_TICKET *) asn1ticket);
  1386. if (krb5keytab) krb5_kt_close(krb5context, krb5keytab);
  1387. if (krb5ticket) krb5_free_ticket(krb5context, krb5ticket);
  1388. if (krb5server) krb5_free_principal(krb5context, krb5server);
  1389. return (krb5rc);
  1390. }
  1391. /* Allocate & return a new kssl_ctx struct.
  1392. */
  1393. KSSL_CTX *
  1394. kssl_ctx_new(void)
  1395. {
  1396. return ((KSSL_CTX *) calloc(1, sizeof(KSSL_CTX)));
  1397. }
  1398. /* Frees a kssl_ctx struct and any allocated memory it holds.
  1399. ** Returns NULL.
  1400. */
  1401. KSSL_CTX *
  1402. kssl_ctx_free(KSSL_CTX *kssl_ctx)
  1403. {
  1404. if (kssl_ctx == NULL) return kssl_ctx;
  1405. if (kssl_ctx->key) OPENSSL_cleanse(kssl_ctx->key,
  1406. kssl_ctx->length);
  1407. if (kssl_ctx->key) free(kssl_ctx->key);
  1408. if (kssl_ctx->client_princ) free(kssl_ctx->client_princ);
  1409. if (kssl_ctx->service_host) free(kssl_ctx->service_host);
  1410. if (kssl_ctx->service_name) free(kssl_ctx->service_name);
  1411. if (kssl_ctx->keytab_file) free(kssl_ctx->keytab_file);
  1412. free(kssl_ctx);
  1413. return (KSSL_CTX *) NULL;
  1414. }
  1415. /* Given an array of (krb5_data *) entity (and optional realm),
  1416. ** set the plain (char *) client_princ or service_host member
  1417. ** of the kssl_ctx struct.
  1418. */
  1419. krb5_error_code
  1420. kssl_ctx_setprinc(KSSL_CTX *kssl_ctx, int which,
  1421. krb5_data *realm, krb5_data *entity, int nentities)
  1422. {
  1423. char **princ;
  1424. int length;
  1425. int i;
  1426. if (kssl_ctx == NULL || entity == NULL) return KSSL_CTX_ERR;
  1427. switch (which)
  1428. {
  1429. case KSSL_CLIENT: princ = &kssl_ctx->client_princ; break;
  1430. case KSSL_SERVER: princ = &kssl_ctx->service_host; break;
  1431. default: return KSSL_CTX_ERR; break;
  1432. }
  1433. if (*princ) free(*princ);
  1434. /* Add up all the entity->lengths */
  1435. length = 0;
  1436. for (i=0; i < nentities; i++)
  1437. {
  1438. length += entity[i].length;
  1439. }
  1440. /* Add in space for the '/' character(s) (if any) */
  1441. length += nentities-1;
  1442. /* Space for the ('@'+realm+NULL | NULL) */
  1443. length += ((realm)? realm->length + 2: 1);
  1444. if ((*princ = calloc(1, length)) == NULL)
  1445. return KSSL_CTX_ERR;
  1446. else
  1447. {
  1448. for (i = 0; i < nentities; i++)
  1449. {
  1450. strncat(*princ, entity[i].data, entity[i].length);
  1451. if (i < nentities-1)
  1452. {
  1453. strcat (*princ, "/");
  1454. }
  1455. }
  1456. if (realm)
  1457. {
  1458. strcat (*princ, "@");
  1459. (void) strncat(*princ, realm->data, realm->length);
  1460. }
  1461. }
  1462. return KSSL_CTX_OK;
  1463. }
  1464. /* Set one of the plain (char *) string members of the kssl_ctx struct.
  1465. ** Default values should be:
  1466. ** which == KSSL_SERVICE => "khost" (KRB5SVC)
  1467. ** which == KSSL_KEYTAB => "/etc/krb5.keytab" (KRB5KEYTAB)
  1468. */
  1469. krb5_error_code
  1470. kssl_ctx_setstring(KSSL_CTX *kssl_ctx, int which, char *text)
  1471. {
  1472. char **string;
  1473. if (!kssl_ctx) return KSSL_CTX_ERR;
  1474. switch (which)
  1475. {
  1476. case KSSL_SERVICE: string = &kssl_ctx->service_name; break;
  1477. case KSSL_SERVER: string = &kssl_ctx->service_host; break;
  1478. case KSSL_CLIENT: string = &kssl_ctx->client_princ; break;
  1479. case KSSL_KEYTAB: string = &kssl_ctx->keytab_file; break;
  1480. default: return KSSL_CTX_ERR; break;
  1481. }
  1482. if (*string) free(*string);
  1483. if (!text)
  1484. {
  1485. *string = '\0';
  1486. return KSSL_CTX_OK;
  1487. }
  1488. if ((*string = calloc(1, strlen(text) + 1)) == NULL)
  1489. return KSSL_CTX_ERR;
  1490. else
  1491. strcpy(*string, text);
  1492. return KSSL_CTX_OK;
  1493. }
  1494. /* Copy the Kerberos session key from a (krb5_keyblock *) to a kssl_ctx
  1495. ** struct. Clear kssl_ctx->key if Kerberos session key is NULL.
  1496. */
  1497. krb5_error_code
  1498. kssl_ctx_setkey(KSSL_CTX *kssl_ctx, krb5_keyblock *session)
  1499. {
  1500. int length;
  1501. krb5_enctype enctype;
  1502. krb5_octet FAR *contents = NULL;
  1503. if (!kssl_ctx) return KSSL_CTX_ERR;
  1504. if (kssl_ctx->key)
  1505. {
  1506. OPENSSL_cleanse(kssl_ctx->key, kssl_ctx->length);
  1507. free(kssl_ctx->key);
  1508. }
  1509. if (session)
  1510. {
  1511. #ifdef KRB5_HEIMDAL
  1512. length = session->keyvalue->length;
  1513. enctype = session->keytype;
  1514. contents = session->keyvalue->contents;
  1515. #else
  1516. length = session->length;
  1517. enctype = session->enctype;
  1518. contents = session->contents;
  1519. #endif
  1520. kssl_ctx->enctype = enctype;
  1521. kssl_ctx->length = length;
  1522. }
  1523. else
  1524. {
  1525. kssl_ctx->enctype = ENCTYPE_UNKNOWN;
  1526. kssl_ctx->length = 0;
  1527. return KSSL_CTX_OK;
  1528. }
  1529. if ((kssl_ctx->key =
  1530. (krb5_octet FAR *) calloc(1, kssl_ctx->length)) == NULL)
  1531. {
  1532. kssl_ctx->length = 0;
  1533. return KSSL_CTX_ERR;
  1534. }
  1535. else
  1536. memcpy(kssl_ctx->key, contents, length);
  1537. return KSSL_CTX_OK;
  1538. }
  1539. /* Display contents of kssl_ctx struct
  1540. */
  1541. void
  1542. kssl_ctx_show(KSSL_CTX *kssl_ctx)
  1543. {
  1544. int i;
  1545. printf("kssl_ctx: ");
  1546. if (kssl_ctx == NULL)
  1547. {
  1548. printf("NULL\n");
  1549. return;
  1550. }
  1551. else
  1552. printf("%p\n", (void *)kssl_ctx);
  1553. printf("\tservice:\t%s\n",
  1554. (kssl_ctx->service_name)? kssl_ctx->service_name: "NULL");
  1555. printf("\tclient:\t%s\n",
  1556. (kssl_ctx->client_princ)? kssl_ctx->client_princ: "NULL");
  1557. printf("\tserver:\t%s\n",
  1558. (kssl_ctx->service_host)? kssl_ctx->service_host: "NULL");
  1559. printf("\tkeytab:\t%s\n",
  1560. (kssl_ctx->keytab_file)? kssl_ctx->keytab_file: "NULL");
  1561. printf("\tkey [%d:%d]:\t",
  1562. kssl_ctx->enctype, kssl_ctx->length);
  1563. for (i=0; i < kssl_ctx->length && kssl_ctx->key; i++)
  1564. {
  1565. printf("%02x", kssl_ctx->key[i]);
  1566. }
  1567. printf("\n");
  1568. return;
  1569. }
  1570. int
  1571. kssl_keytab_is_available(KSSL_CTX *kssl_ctx)
  1572. {
  1573. krb5_context krb5context = NULL;
  1574. krb5_keytab krb5keytab = NULL;
  1575. krb5_keytab_entry entry;
  1576. krb5_principal princ = NULL;
  1577. krb5_error_code krb5rc = KRB5KRB_ERR_GENERIC;
  1578. int rc = 0;
  1579. if ((krb5rc = krb5_init_context(&krb5context)))
  1580. return(0);
  1581. /* kssl_ctx->keytab_file == NULL ==> use Kerberos default
  1582. */
  1583. if (kssl_ctx->keytab_file)
  1584. {
  1585. krb5rc = krb5_kt_resolve(krb5context, kssl_ctx->keytab_file,
  1586. &krb5keytab);
  1587. if (krb5rc)
  1588. goto exit;
  1589. }
  1590. else
  1591. {
  1592. krb5rc = krb5_kt_default(krb5context,&krb5keytab);
  1593. if (krb5rc)
  1594. goto exit;
  1595. }
  1596. /* the host key we are looking for */
  1597. krb5rc = krb5_sname_to_principal(krb5context, NULL,
  1598. kssl_ctx->service_name ? kssl_ctx->service_name: KRB5SVC,
  1599. KRB5_NT_SRV_HST, &princ);
  1600. krb5rc = krb5_kt_get_entry(krb5context, krb5keytab,
  1601. princ,
  1602. 0 /* IGNORE_VNO */,
  1603. 0 /* IGNORE_ENCTYPE */,
  1604. &entry);
  1605. if ( krb5rc == KRB5_KT_NOTFOUND ) {
  1606. rc = 1;
  1607. goto exit;
  1608. } else if ( krb5rc )
  1609. goto exit;
  1610. krb5_kt_free_entry(krb5context, &entry);
  1611. rc = 1;
  1612. exit:
  1613. if (krb5keytab) krb5_kt_close(krb5context, krb5keytab);
  1614. if (princ) krb5_free_principal(krb5context, princ);
  1615. if (krb5context) krb5_free_context(krb5context);
  1616. return(rc);
  1617. }
  1618. int
  1619. kssl_tgt_is_available(KSSL_CTX *kssl_ctx)
  1620. {
  1621. krb5_error_code krb5rc = KRB5KRB_ERR_GENERIC;
  1622. krb5_context krb5context = NULL;
  1623. krb5_ccache krb5ccdef = NULL;
  1624. krb5_creds krb5creds, *krb5credsp = NULL;
  1625. int rc = 0;
  1626. memset((char *)&krb5creds, 0, sizeof(krb5creds));
  1627. if (!kssl_ctx)
  1628. return(0);
  1629. if (!kssl_ctx->service_host)
  1630. return(0);
  1631. if ((krb5rc = krb5_init_context(&krb5context)) != 0)
  1632. goto err;
  1633. if ((krb5rc = krb5_sname_to_principal(krb5context,
  1634. kssl_ctx->service_host,
  1635. (kssl_ctx->service_name)? kssl_ctx->service_name: KRB5SVC,
  1636. KRB5_NT_SRV_HST, &krb5creds.server)) != 0)
  1637. goto err;
  1638. if ((krb5rc = krb5_cc_default(krb5context, &krb5ccdef)) != 0)
  1639. goto err;
  1640. if ((krb5rc = krb5_cc_get_principal(krb5context, krb5ccdef,
  1641. &krb5creds.client)) != 0)
  1642. goto err;
  1643. if ((krb5rc = krb5_get_credentials(krb5context, 0, krb5ccdef,
  1644. &krb5creds, &krb5credsp)) != 0)
  1645. goto err;
  1646. rc = 1;
  1647. err:
  1648. #ifdef KSSL_DEBUG
  1649. kssl_ctx_show(kssl_ctx);
  1650. #endif /* KSSL_DEBUG */
  1651. if (krb5creds.client) krb5_free_principal(krb5context, krb5creds.client);
  1652. if (krb5creds.server) krb5_free_principal(krb5context, krb5creds.server);
  1653. if (krb5context) krb5_free_context(krb5context);
  1654. return(rc);
  1655. }
  1656. #if !defined(OPENSSL_SYS_WINDOWS) && !defined(OPENSSL_SYS_WIN32)
  1657. void kssl_krb5_free_data_contents(krb5_context context, krb5_data *data)
  1658. {
  1659. #ifdef KRB5_HEIMDAL
  1660. data->length = 0;
  1661. if (data->data)
  1662. free(data->data);
  1663. #elif defined(KRB5_MIT_OLD11)
  1664. if (data->data) {
  1665. krb5_xfree(data->data);
  1666. data->data = 0;
  1667. }
  1668. #else
  1669. krb5_free_data_contents(NULL, data);
  1670. #endif
  1671. }
  1672. #endif /* !OPENSSL_SYS_WINDOWS && !OPENSSL_SYS_WIN32 */
  1673. /* Given pointers to KerberosTime and struct tm structs, convert the
  1674. ** KerberosTime string to struct tm. Note that KerberosTime is a
  1675. ** ASN1_GENERALIZEDTIME value, constrained to GMT with no fractional
  1676. ** seconds as defined in RFC 1510.
  1677. ** Return pointer to the (partially) filled in struct tm on success,
  1678. ** return NULL on failure.
  1679. */
  1680. struct tm *k_gmtime(ASN1_GENERALIZEDTIME *gtime, struct tm *k_tm)
  1681. {
  1682. char c, *p;
  1683. if (!k_tm) return NULL;
  1684. if (gtime == NULL || gtime->length < 14) return NULL;
  1685. if (gtime->data == NULL) return NULL;
  1686. p = (char *)&gtime->data[14];
  1687. c = *p; *p = '\0'; p -= 2; k_tm->tm_sec = atoi(p); *(p+2) = c;
  1688. c = *p; *p = '\0'; p -= 2; k_tm->tm_min = atoi(p); *(p+2) = c;
  1689. c = *p; *p = '\0'; p -= 2; k_tm->tm_hour = atoi(p); *(p+2) = c;
  1690. c = *p; *p = '\0'; p -= 2; k_tm->tm_mday = atoi(p); *(p+2) = c;
  1691. c = *p; *p = '\0'; p -= 2; k_tm->tm_mon = atoi(p)-1; *(p+2) = c;
  1692. c = *p; *p = '\0'; p -= 4; k_tm->tm_year = atoi(p)-1900; *(p+4) = c;
  1693. return k_tm;
  1694. }
  1695. /* Helper function for kssl_validate_times().
  1696. ** We need context->clockskew, but krb5_context is an opaque struct.
  1697. ** So we try to sneek the clockskew out through the replay cache.
  1698. ** If that fails just return a likely default (300 seconds).
  1699. */
  1700. krb5_deltat get_rc_clockskew(krb5_context context)
  1701. {
  1702. krb5_rcache rc;
  1703. krb5_deltat clockskew;
  1704. if (krb5_rc_default(context, &rc)) return KSSL_CLOCKSKEW;
  1705. if (krb5_rc_initialize(context, rc, 0)) return KSSL_CLOCKSKEW;
  1706. if (krb5_rc_get_lifespan(context, rc, &clockskew)) {
  1707. clockskew = KSSL_CLOCKSKEW;
  1708. }
  1709. (void) krb5_rc_destroy(context, rc);
  1710. return clockskew;
  1711. }
  1712. /* kssl_validate_times() combines (and more importantly exposes)
  1713. ** the MIT KRB5 internal function krb5_validate_times() and the
  1714. ** in_clock_skew() macro. The authenticator client time is checked
  1715. ** to be within clockskew secs of the current time and the current
  1716. ** time is checked to be within the ticket start and expire times.
  1717. ** Either check may be omitted by supplying a NULL value.
  1718. ** Returns 0 for valid times, SSL_R_KRB5* error codes otherwise.
  1719. ** See Also: (Kerberos source)/krb5/lib/krb5/krb/valid_times.c
  1720. ** 20010420 VRS
  1721. */
  1722. krb5_error_code kssl_validate_times( krb5_timestamp atime,
  1723. krb5_ticket_times *ttimes)
  1724. {
  1725. krb5_deltat skew;
  1726. krb5_timestamp start, now;
  1727. krb5_error_code rc;
  1728. krb5_context context;
  1729. if ((rc = krb5_init_context(&context))) return SSL_R_KRB5_S_BAD_TICKET;
  1730. skew = get_rc_clockskew(context);
  1731. if ((rc = krb5_timeofday(context,&now))) return SSL_R_KRB5_S_BAD_TICKET;
  1732. krb5_free_context(context);
  1733. if (atime && labs(atime - now) >= skew) return SSL_R_KRB5_S_TKT_SKEW;
  1734. if (! ttimes) return 0;
  1735. start = (ttimes->starttime != 0)? ttimes->starttime: ttimes->authtime;
  1736. if (start - now > skew) return SSL_R_KRB5_S_TKT_NYV;
  1737. if ((now - ttimes->endtime) > skew) return SSL_R_KRB5_S_TKT_EXPIRED;
  1738. #ifdef KSSL_DEBUG
  1739. printf("kssl_validate_times: %d |<- | %d - %d | < %d ->| %d\n",
  1740. start, atime, now, skew, ttimes->endtime);
  1741. #endif /* KSSL_DEBUG */
  1742. return 0;
  1743. }
  1744. /* Decode and decrypt given DER-encoded authenticator, then pass
  1745. ** authenticator ctime back in *atimep (or 0 if time unavailable).
  1746. ** Returns krb5_error_code and kssl_err on error. A NULL
  1747. ** authenticator (authentp->length == 0) is not considered an error.
  1748. ** Note that kssl_check_authent() makes use of the KRB5 session key;
  1749. ** you must call kssl_sget_tkt() to get the key before calling this routine.
  1750. */
  1751. krb5_error_code kssl_check_authent(
  1752. /* IN */ KSSL_CTX *kssl_ctx,
  1753. /* IN */ krb5_data *authentp,
  1754. /* OUT */ krb5_timestamp *atimep,
  1755. /* OUT */ KSSL_ERR *kssl_err )
  1756. {
  1757. krb5_error_code krb5rc = 0;
  1758. KRB5_ENCDATA *dec_authent = NULL;
  1759. KRB5_AUTHENTBODY *auth = NULL;
  1760. krb5_enctype enctype;
  1761. EVP_CIPHER_CTX ciph_ctx;
  1762. const EVP_CIPHER *enc = NULL;
  1763. unsigned char iv[EVP_MAX_IV_LENGTH];
  1764. const unsigned char *p;
  1765. unsigned char *unenc_authent;
  1766. int outl, unencbufsize;
  1767. struct tm tm_time, *tm_l, *tm_g;
  1768. time_t now, tl, tg, tr, tz_offset;
  1769. EVP_CIPHER_CTX_init(&ciph_ctx);
  1770. *atimep = 0;
  1771. kssl_err_set(kssl_err, 0, "");
  1772. #ifndef KRB5CHECKAUTH
  1773. authentp = NULL;
  1774. #else
  1775. #if KRB5CHECKAUTH == 0
  1776. authentp = NULL;
  1777. #endif
  1778. #endif /* KRB5CHECKAUTH */
  1779. if (authentp == NULL || authentp->length == 0) return 0;
  1780. #ifdef KSSL_DEBUG
  1781. {
  1782. unsigned int ui;
  1783. printf("kssl_check_authent: authenticator[%d]:\n",authentp->length);
  1784. p = authentp->data;
  1785. for (ui=0; ui < authentp->length; ui++) printf("%02x ",p[ui]);
  1786. printf("\n");
  1787. }
  1788. #endif /* KSSL_DEBUG */
  1789. unencbufsize = 2 * authentp->length;
  1790. if ((unenc_authent = calloc(1, unencbufsize)) == NULL)
  1791. {
  1792. kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
  1793. "Unable to allocate authenticator buffer.\n");
  1794. krb5rc = KRB5KRB_ERR_GENERIC;
  1795. goto err;
  1796. }
  1797. p = (unsigned char *)authentp->data;
  1798. if ((dec_authent = d2i_KRB5_ENCDATA(NULL, &p,
  1799. (long) authentp->length)) == NULL)
  1800. {
  1801. kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
  1802. "Error decoding authenticator.\n");
  1803. krb5rc = KRB5KRB_AP_ERR_BAD_INTEGRITY;
  1804. goto err;
  1805. }
  1806. enctype = dec_authent->etype->data[0]; /* should = kssl_ctx->enctype */
  1807. #if !defined(KRB5_MIT_OLD11)
  1808. switch ( enctype ) {
  1809. case ENCTYPE_DES3_CBC_SHA1: /* EVP_des_ede3_cbc(); */
  1810. case ENCTYPE_DES3_CBC_SHA:
  1811. case ENCTYPE_DES3_CBC_RAW:
  1812. krb5rc = 0; /* Skip, can't handle derived keys */
  1813. goto err;
  1814. }
  1815. #endif
  1816. enc = kssl_map_enc(enctype);
  1817. memset(iv, 0, sizeof iv); /* per RFC 1510 */
  1818. if (enc == NULL)
  1819. {
  1820. /* Disable kssl_check_authent for ENCTYPE_DES3_CBC_SHA1.
  1821. ** This enctype indicates the authenticator was encrypted
  1822. ** using key-usage derived keys which openssl cannot decrypt.
  1823. */
  1824. goto err;
  1825. }
  1826. if (!EVP_CipherInit(&ciph_ctx,enc,kssl_ctx->key,iv,0))
  1827. {
  1828. kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
  1829. "EVP_CipherInit error decrypting authenticator.\n");
  1830. krb5rc = KRB5KRB_AP_ERR_BAD_INTEGRITY;
  1831. goto err;
  1832. }
  1833. outl = dec_authent->cipher->length;
  1834. if (!EVP_Cipher(&ciph_ctx,unenc_authent,dec_authent->cipher->data,outl))
  1835. {
  1836. kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
  1837. "EVP_Cipher error decrypting authenticator.\n");
  1838. krb5rc = KRB5KRB_AP_ERR_BAD_INTEGRITY;
  1839. goto err;
  1840. }
  1841. EVP_CIPHER_CTX_cleanup(&ciph_ctx);
  1842. #ifdef KSSL_DEBUG
  1843. printf("kssl_check_authent: decrypted authenticator[%d] =\n", outl);
  1844. for (padl=0; padl < outl; padl++) printf("%02x ",unenc_authent[padl]);
  1845. printf("\n");
  1846. #endif /* KSSL_DEBUG */
  1847. if ((p = kssl_skip_confound(enctype, unenc_authent)) == NULL)
  1848. {
  1849. kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
  1850. "confounded by authenticator.\n");
  1851. krb5rc = KRB5KRB_AP_ERR_BAD_INTEGRITY;
  1852. goto err;
  1853. }
  1854. outl -= p - unenc_authent;
  1855. if ((auth = (KRB5_AUTHENTBODY *) d2i_KRB5_AUTHENT(NULL, &p,
  1856. (long) outl))==NULL)
  1857. {
  1858. kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
  1859. "Error decoding authenticator body.\n");
  1860. krb5rc = KRB5KRB_AP_ERR_BAD_INTEGRITY;
  1861. goto err;
  1862. }
  1863. memset(&tm_time,0,sizeof(struct tm));
  1864. if (k_gmtime(auth->ctime, &tm_time) &&
  1865. ((tr = mktime(&tm_time)) != (time_t)(-1)))
  1866. {
  1867. now = time(&now);
  1868. tm_l = localtime(&now); tl = mktime(tm_l);
  1869. tm_g = gmtime(&now); tg = mktime(tm_g);
  1870. tz_offset = tg - tl;
  1871. *atimep = tr - tz_offset;
  1872. }
  1873. #ifdef KSSL_DEBUG
  1874. printf("kssl_check_authent: returns %d for client time ", *atimep);
  1875. if (auth && auth->ctime && auth->ctime->length && auth->ctime->data)
  1876. printf("%.*s\n", auth->ctime->length, auth->ctime->data);
  1877. else printf("NULL\n");
  1878. #endif /* KSSL_DEBUG */
  1879. err:
  1880. if (auth) KRB5_AUTHENT_free((KRB5_AUTHENT *) auth);
  1881. if (dec_authent) KRB5_ENCDATA_free(dec_authent);
  1882. if (unenc_authent) free(unenc_authent);
  1883. EVP_CIPHER_CTX_cleanup(&ciph_ctx);
  1884. return krb5rc;
  1885. }
  1886. /* Replaces krb5_build_principal_ext(), with varargs length == 2 (svc, host),
  1887. ** because I dont't know how to stub varargs.
  1888. ** Returns krb5_error_code == ENOMEM on alloc error, otherwise
  1889. ** passes back newly constructed principal, which should be freed by caller.
  1890. */
  1891. krb5_error_code kssl_build_principal_2(
  1892. /* UPDATE */ krb5_context context,
  1893. /* OUT */ krb5_principal *princ,
  1894. /* IN */ int rlen, const char *realm,
  1895. /* IN */ int slen, const char *svc,
  1896. /* IN */ int hlen, const char *host)
  1897. {
  1898. krb5_data *p_data = NULL;
  1899. krb5_principal new_p = NULL;
  1900. char *new_r = NULL;
  1901. if ((p_data = (krb5_data *) calloc(2, sizeof(krb5_data))) == NULL ||
  1902. (new_p = (krb5_principal) calloc(1, sizeof(krb5_principal_data)))
  1903. == NULL) goto err;
  1904. new_p->length = 2;
  1905. new_p->data = p_data;
  1906. if ((new_r = calloc(1, rlen + 1)) == NULL) goto err;
  1907. memcpy(new_r, realm, rlen);
  1908. krb5_princ_set_realm_length(context, new_p, rlen);
  1909. krb5_princ_set_realm_data(context, new_p, new_r);
  1910. if ((new_p->data[0].data = calloc(1, slen + 1)) == NULL) goto err;
  1911. memcpy(new_p->data[0].data, svc, slen);
  1912. new_p->data[0].length = slen;
  1913. if ((new_p->data[1].data = calloc(1, hlen + 1)) == NULL) goto err;
  1914. memcpy(new_p->data[1].data, host, hlen);
  1915. new_p->data[1].length = hlen;
  1916. krb5_princ_type(context, new_p) = KRB5_NT_UNKNOWN;
  1917. *princ = new_p;
  1918. return 0;
  1919. err:
  1920. if (new_p && new_p[0].data) free(new_p[0].data);
  1921. if (new_p && new_p[1].data) free(new_p[1].data);
  1922. if (new_p) free(new_p);
  1923. if (new_r) free(new_r);
  1924. return ENOMEM;
  1925. }
  1926. #else /* !OPENSSL_NO_KRB5 */
  1927. #if defined(PEDANTIC) || defined(OPENSSL_SYS_VMS)
  1928. static void *dummy=&dummy;
  1929. #endif
  1930. #endif /* !OPENSSL_NO_KRB5 */