kssl.c 67 KB

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