1 /* ssl/kssl.c -*- mode: C; c-file-style: "eay" -*- */
3 * Written by Vern Staats <staatsvr@asc.hpc.mil> for the OpenSSL project
6 /* ====================================================================
7 * Copyright (c) 2000 The OpenSSL Project. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
21 * 3. All advertising materials mentioning features or use of this
22 * software must display the following acknowledgment:
23 * "This product includes software developed by the OpenSSL Project
24 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
26 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27 * endorse or promote products derived from this software without
28 * prior written permission. For written permission, please contact
29 * licensing@OpenSSL.org.
31 * 5. Products derived from this software may not be called "OpenSSL"
32 * nor may "OpenSSL" appear in their names without prior written
33 * permission of the OpenSSL Project.
35 * 6. Redistributions of any form whatsoever must retain the following
37 * "This product includes software developed by the OpenSSL Project
38 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
40 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
44 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51 * OF THE POSSIBILITY OF SUCH DAMAGE.
52 * ====================================================================
54 * This product includes cryptographic software written by Eric Young
55 * (eay@cryptsoft.com). This product includes software written by Tim
56 * Hudson (tjh@cryptsoft.com).
61 * ssl/kssl.c -- Routines to support (& debug) Kerberos5 auth for openssl
63 * 19990701 VRS Started.
64 * 200011?? Jeffrey Altman, Richard Levitte
65 * Generalized for Heimdal, Newer MIT, & Win32.
66 * Integrated into main OpenSSL 0.9.7 snapshots.
67 * 20010413 Simon Wilkinson, VRS
68 * Real RFC2712 KerberosWrapper replaces AP_REQ.
71 #include <openssl/opensslconf.h>
75 #define KRB5_PRIVATE 1
77 #include <openssl/ssl.h>
78 #include <openssl/evp.h>
79 #include <openssl/objects.h>
80 #include <openssl/krb5_asn.h>
83 #ifndef OPENSSL_NO_KRB5
86 # define ENOMEM KRB5KRB_ERR_GENERIC
90 * When OpenSSL is built on Windows, we do not want to require that
91 * the Kerberos DLLs be available in order for the OpenSSL DLLs to
92 * work. Therefore, all Kerberos routines are loaded at run time
93 * and we do not link to a .LIB file.
96 # if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32)
98 * The purpose of the following pre-processor statements is to provide
99 * compatibility with different releases of MIT Kerberos for Windows.
100 * All versions up to 1.2 used macros. But macros do not allow for
101 * a binary compatible interface for DLLs. Therefore, all macros are
102 * being replaced by function calls. The following code will allow
103 * an OpenSSL DLL built on Windows to work whether or not the macro
104 * or function form of the routines are utilized.
106 # ifdef krb5_cc_get_principal
107 # define NO_DEF_KRB5_CCACHE
108 # undef krb5_cc_get_principal
110 # define krb5_cc_get_principal kssl_krb5_cc_get_principal
112 # define krb5_free_data_contents kssl_krb5_free_data_contents
113 # define krb5_free_context kssl_krb5_free_context
114 # define krb5_auth_con_free kssl_krb5_auth_con_free
115 # define krb5_free_principal kssl_krb5_free_principal
116 # define krb5_mk_req_extended kssl_krb5_mk_req_extended
117 # define krb5_get_credentials kssl_krb5_get_credentials
118 # define krb5_cc_default kssl_krb5_cc_default
119 # define krb5_sname_to_principal kssl_krb5_sname_to_principal
120 # define krb5_init_context kssl_krb5_init_context
121 # define krb5_free_ticket kssl_krb5_free_ticket
122 # define krb5_rd_req kssl_krb5_rd_req
123 # define krb5_kt_default kssl_krb5_kt_default
124 # define krb5_kt_resolve kssl_krb5_kt_resolve
125 /* macros in mit 1.2.2 and earlier; functions in mit 1.2.3 and greater */
126 # ifndef krb5_kt_close
127 # define krb5_kt_close kssl_krb5_kt_close
128 # endif /* krb5_kt_close */
129 # ifndef krb5_kt_get_entry
130 # define krb5_kt_get_entry kssl_krb5_kt_get_entry
131 # endif /* krb5_kt_get_entry */
132 # define krb5_auth_con_init kssl_krb5_auth_con_init
134 # define krb5_principal_compare kssl_krb5_principal_compare
135 # define krb5_decrypt_tkt_part kssl_krb5_decrypt_tkt_part
136 # define krb5_timeofday kssl_krb5_timeofday
137 # define krb5_rc_default kssl_krb5_rc_default
139 # ifdef krb5_rc_initialize
140 # undef krb5_rc_initialize
142 # define krb5_rc_initialize kssl_krb5_rc_initialize
144 # ifdef krb5_rc_get_lifespan
145 # undef krb5_rc_get_lifespan
147 # define krb5_rc_get_lifespan kssl_krb5_rc_get_lifespan
149 # ifdef krb5_rc_destroy
150 # undef krb5_rc_destroy
152 # define krb5_rc_destroy kssl_krb5_rc_destroy
154 # define valid_cksumtype kssl_valid_cksumtype
155 # define krb5_checksum_size kssl_krb5_checksum_size
156 # define krb5_kt_free_entry kssl_krb5_kt_free_entry
157 # define krb5_auth_con_setrcache kssl_krb5_auth_con_setrcache
158 # define krb5_auth_con_getrcache kssl_krb5_auth_con_getrcache
159 # define krb5_get_server_rcache kssl_krb5_get_server_rcache
161 /* Prototypes for built in stubs */
162 void kssl_krb5_free_data_contents(krb5_context, krb5_data *);
163 void kssl_krb5_free_principal(krb5_context, krb5_principal);
164 krb5_error_code kssl_krb5_kt_resolve(krb5_context,
165 krb5_const char *, krb5_keytab *);
166 krb5_error_code kssl_krb5_kt_default(krb5_context, krb5_keytab *);
167 krb5_error_code kssl_krb5_free_ticket(krb5_context, krb5_ticket *);
168 krb5_error_code kssl_krb5_rd_req(krb5_context, krb5_auth_context *,
169 krb5_const krb5_data *,
170 krb5_const_principal, krb5_keytab,
171 krb5_flags *, krb5_ticket **);
173 krb5_boolean kssl_krb5_principal_compare(krb5_context, krb5_const_principal,
174 krb5_const_principal);
175 krb5_error_code kssl_krb5_mk_req_extended(krb5_context,
177 krb5_const krb5_flags,
179 krb5_creds *, krb5_data *);
180 krb5_error_code kssl_krb5_init_context(krb5_context *);
181 void kssl_krb5_free_context(krb5_context);
182 krb5_error_code kssl_krb5_cc_default(krb5_context, krb5_ccache *);
183 krb5_error_code kssl_krb5_sname_to_principal(krb5_context,
186 krb5_int32, krb5_principal *);
187 krb5_error_code kssl_krb5_get_credentials(krb5_context,
188 krb5_const krb5_flags,
190 krb5_creds *, krb5_creds * *);
191 krb5_error_code kssl_krb5_auth_con_init(krb5_context, krb5_auth_context *);
192 krb5_error_code kssl_krb5_cc_get_principal(krb5_context context,
194 krb5_principal *principal);
195 krb5_error_code kssl_krb5_auth_con_free(krb5_context, krb5_auth_context);
196 size_t kssl_krb5_checksum_size(krb5_context context, krb5_cksumtype ctype);
197 krb5_boolean kssl_valid_cksumtype(krb5_cksumtype ctype);
198 krb5_error_code krb5_kt_free_entry(krb5_context, krb5_keytab_entry FAR *);
199 krb5_error_code kssl_krb5_auth_con_setrcache(krb5_context,
200 krb5_auth_context, krb5_rcache);
201 krb5_error_code kssl_krb5_get_server_rcache(krb5_context,
202 krb5_const krb5_data *,
204 krb5_error_code kssl_krb5_auth_con_getrcache(krb5_context,
208 /* Function pointers (almost all Kerberos functions are _stdcall) */
209 static void (_stdcall *p_krb5_free_data_contents) (krb5_context, krb5_data *)
211 static void (_stdcall *p_krb5_free_principal) (krb5_context, krb5_principal)
213 static krb5_error_code(_stdcall *p_krb5_kt_resolve)
214 (krb5_context, krb5_const char *, krb5_keytab *) = NULL;
215 static krb5_error_code(_stdcall *p_krb5_kt_default) (krb5_context,
216 krb5_keytab *) = NULL;
217 static krb5_error_code(_stdcall *p_krb5_free_ticket) (krb5_context,
218 krb5_ticket *) = NULL;
219 static krb5_error_code(_stdcall *p_krb5_rd_req) (krb5_context,
221 krb5_const krb5_data *,
222 krb5_const_principal,
223 krb5_keytab, krb5_flags *,
224 krb5_ticket **) = NULL;
225 static krb5_error_code(_stdcall *p_krb5_mk_req_extended)
226 (krb5_context, krb5_auth_context *,
227 krb5_const krb5_flags, krb5_data *, krb5_creds *, krb5_data *) = NULL;
228 static krb5_error_code(_stdcall *p_krb5_init_context) (krb5_context *) = NULL;
229 static void (_stdcall *p_krb5_free_context) (krb5_context) = NULL;
230 static krb5_error_code(_stdcall *p_krb5_cc_default) (krb5_context,
231 krb5_ccache *) = NULL;
232 static krb5_error_code(_stdcall *p_krb5_sname_to_principal)
233 (krb5_context, krb5_const char *, krb5_const char *,
234 krb5_int32, krb5_principal *) = NULL;
235 static krb5_error_code(_stdcall *p_krb5_get_credentials)
236 (krb5_context, krb5_const krb5_flags, krb5_ccache,
237 krb5_creds *, krb5_creds **) = NULL;
238 static krb5_error_code(_stdcall *p_krb5_auth_con_init)
239 (krb5_context, krb5_auth_context *) = NULL;
240 static krb5_error_code(_stdcall *p_krb5_cc_get_principal)
241 (krb5_context context, krb5_ccache cache, krb5_principal *principal) = NULL;
242 static krb5_error_code(_stdcall *p_krb5_auth_con_free)
243 (krb5_context, krb5_auth_context) = NULL;
244 static krb5_error_code(_stdcall *p_krb5_decrypt_tkt_part)
245 (krb5_context, krb5_const krb5_keyblock *, krb5_ticket *) = NULL;
246 static krb5_error_code(_stdcall *p_krb5_timeofday)
247 (krb5_context context, krb5_int32 *timeret) = NULL;
248 static krb5_error_code(_stdcall *p_krb5_rc_default)
249 (krb5_context context, krb5_rcache *rc) = NULL;
250 static krb5_error_code(_stdcall *p_krb5_rc_initialize)
251 (krb5_context context, krb5_rcache rc, krb5_deltat lifespan) = NULL;
252 static krb5_error_code(_stdcall *p_krb5_rc_get_lifespan)
253 (krb5_context context, krb5_rcache rc, krb5_deltat *lifespan) = NULL;
254 static krb5_error_code(_stdcall *p_krb5_rc_destroy)
255 (krb5_context context, krb5_rcache rc) = NULL;
256 static krb5_boolean(_stdcall *p_krb5_principal_compare)
257 (krb5_context, krb5_const_principal, krb5_const_principal) = NULL;
258 static size_t (_stdcall *p_krb5_checksum_size) (krb5_context context,
259 krb5_cksumtype ctype) = NULL;
260 static krb5_boolean(_stdcall *p_valid_cksumtype) (krb5_cksumtype ctype) =
262 static krb5_error_code(_stdcall *p_krb5_kt_free_entry)
263 (krb5_context, krb5_keytab_entry *) = NULL;
264 static krb5_error_code(_stdcall *p_krb5_auth_con_setrcache) (krb5_context,
268 static krb5_error_code(_stdcall *p_krb5_get_server_rcache) (krb5_context,
273 static krb5_error_code(*p_krb5_auth_con_getrcache) (krb5_context,
275 krb5_rcache *) = NULL;
276 static krb5_error_code(_stdcall *p_krb5_kt_close) (krb5_context context,
277 krb5_keytab keytab) = NULL;
278 static krb5_error_code(_stdcall *p_krb5_kt_get_entry) (krb5_context context,
283 krb5_enctype enctype,
286 static int krb5_loaded = 0; /* only attempt to initialize func ptrs once */
288 /* Function to Load the Kerberos 5 DLL and initialize function pointers */
289 void load_krb5_dll(void)
294 hKRB5_32 = LoadLibrary(TEXT("KRB5_32"));
298 (FARPROC) p_krb5_free_data_contents =
299 GetProcAddress(hKRB5_32, "krb5_free_data_contents");
300 (FARPROC) p_krb5_free_context =
301 GetProcAddress(hKRB5_32, "krb5_free_context");
302 (FARPROC) p_krb5_auth_con_free =
303 GetProcAddress(hKRB5_32, "krb5_auth_con_free");
304 (FARPROC) p_krb5_free_principal =
305 GetProcAddress(hKRB5_32, "krb5_free_principal");
306 (FARPROC) p_krb5_mk_req_extended =
307 GetProcAddress(hKRB5_32, "krb5_mk_req_extended");
308 (FARPROC) p_krb5_get_credentials =
309 GetProcAddress(hKRB5_32, "krb5_get_credentials");
310 (FARPROC) p_krb5_cc_get_principal =
311 GetProcAddress(hKRB5_32, "krb5_cc_get_principal");
312 (FARPROC) p_krb5_cc_default = GetProcAddress(hKRB5_32, "krb5_cc_default");
313 (FARPROC) p_krb5_sname_to_principal =
314 GetProcAddress(hKRB5_32, "krb5_sname_to_principal");
315 (FARPROC) p_krb5_init_context =
316 GetProcAddress(hKRB5_32, "krb5_init_context");
317 (FARPROC) p_krb5_free_ticket =
318 GetProcAddress(hKRB5_32, "krb5_free_ticket");
319 (FARPROC) p_krb5_rd_req = GetProcAddress(hKRB5_32, "krb5_rd_req");
320 (FARPROC) p_krb5_principal_compare =
321 GetProcAddress(hKRB5_32, "krb5_principal_compare");
322 (FARPROC) p_krb5_decrypt_tkt_part =
323 GetProcAddress(hKRB5_32, "krb5_decrypt_tkt_part");
324 (FARPROC) p_krb5_timeofday = GetProcAddress(hKRB5_32, "krb5_timeofday");
325 (FARPROC) p_krb5_rc_default = GetProcAddress(hKRB5_32, "krb5_rc_default");
326 (FARPROC) p_krb5_rc_initialize =
327 GetProcAddress(hKRB5_32, "krb5_rc_initialize");
328 (FARPROC) p_krb5_rc_get_lifespan =
329 GetProcAddress(hKRB5_32, "krb5_rc_get_lifespan");
330 (FARPROC) p_krb5_rc_destroy = GetProcAddress(hKRB5_32, "krb5_rc_destroy");
331 (FARPROC) p_krb5_kt_default = GetProcAddress(hKRB5_32, "krb5_kt_default");
332 (FARPROC) p_krb5_kt_resolve = GetProcAddress(hKRB5_32, "krb5_kt_resolve");
333 (FARPROC) p_krb5_auth_con_init =
334 GetProcAddress(hKRB5_32, "krb5_auth_con_init");
335 (FARPROC) p_valid_cksumtype = GetProcAddress(hKRB5_32, "valid_cksumtype");
336 (FARPROC) p_krb5_checksum_size =
337 GetProcAddress(hKRB5_32, "krb5_checksum_size");
338 (FARPROC) p_krb5_kt_free_entry =
339 GetProcAddress(hKRB5_32, "krb5_kt_free_entry");
340 (FARPROC) p_krb5_auth_con_setrcache =
341 GetProcAddress(hKRB5_32, "krb5_auth_con_setrcache");
342 (FARPROC) p_krb5_get_server_rcache =
343 GetProcAddress(hKRB5_32, "krb5_get_server_rcache");
344 (FARPROC) p_krb5_auth_con_getrcache =
345 GetProcAddress(hKRB5_32, "krb5_auth_con_getrcache");
346 (FARPROC) p_krb5_kt_close = GetProcAddress(hKRB5_32, "krb5_kt_close");
347 (FARPROC) p_krb5_kt_get_entry =
348 GetProcAddress(hKRB5_32, "krb5_kt_get_entry");
351 /* Stubs for each function to be dynamicly loaded */
352 void kssl_krb5_free_data_contents(krb5_context CO, krb5_data *data)
357 if (p_krb5_free_data_contents)
358 p_krb5_free_data_contents(CO, data);
362 kssl_krb5_mk_req_extended(krb5_context CO,
363 krb5_auth_context *pACO,
364 krb5_const krb5_flags F,
365 krb5_data *pD1, krb5_creds *pC, krb5_data *pD2)
370 if (p_krb5_mk_req_extended)
371 return (p_krb5_mk_req_extended(CO, pACO, F, pD1, pC, pD2));
373 return KRB5KRB_ERR_GENERIC;
377 kssl_krb5_auth_con_init(krb5_context CO, krb5_auth_context *pACO)
382 if (p_krb5_auth_con_init)
383 return (p_krb5_auth_con_init(CO, pACO));
385 return KRB5KRB_ERR_GENERIC;
389 kssl_krb5_auth_con_free(krb5_context CO, krb5_auth_context ACO)
394 if (p_krb5_auth_con_free)
395 return (p_krb5_auth_con_free(CO, ACO));
397 return KRB5KRB_ERR_GENERIC;
401 kssl_krb5_get_credentials(krb5_context CO,
402 krb5_const krb5_flags F,
403 krb5_ccache CC, krb5_creds *pCR, krb5_creds **ppCR)
408 if (p_krb5_get_credentials)
409 return (p_krb5_get_credentials(CO, F, CC, pCR, ppCR));
411 return KRB5KRB_ERR_GENERIC;
415 kssl_krb5_sname_to_principal(krb5_context CO,
416 krb5_const char *pC1,
417 krb5_const char *pC2,
418 krb5_int32 I, krb5_principal *pPR)
423 if (p_krb5_sname_to_principal)
424 return (p_krb5_sname_to_principal(CO, pC1, pC2, I, pPR));
426 return KRB5KRB_ERR_GENERIC;
429 krb5_error_code kssl_krb5_cc_default(krb5_context CO, krb5_ccache *pCC)
434 if (p_krb5_cc_default)
435 return (p_krb5_cc_default(CO, pCC));
437 return KRB5KRB_ERR_GENERIC;
440 krb5_error_code kssl_krb5_init_context(krb5_context *pCO)
445 if (p_krb5_init_context)
446 return (p_krb5_init_context(pCO));
448 return KRB5KRB_ERR_GENERIC;
451 void kssl_krb5_free_context(krb5_context CO)
456 if (p_krb5_free_context)
457 p_krb5_free_context(CO);
460 void kssl_krb5_free_principal(krb5_context c, krb5_principal p)
465 if (p_krb5_free_principal)
466 p_krb5_free_principal(c, p);
470 kssl_krb5_kt_resolve(krb5_context con, krb5_const char *sz, krb5_keytab *kt)
475 if (p_krb5_kt_resolve)
476 return (p_krb5_kt_resolve(con, sz, kt));
478 return KRB5KRB_ERR_GENERIC;
481 krb5_error_code kssl_krb5_kt_default(krb5_context con, krb5_keytab *kt)
486 if (p_krb5_kt_default)
487 return (p_krb5_kt_default(con, kt));
489 return KRB5KRB_ERR_GENERIC;
492 krb5_error_code kssl_krb5_free_ticket(krb5_context con, krb5_ticket *kt)
497 if (p_krb5_free_ticket)
498 return (p_krb5_free_ticket(con, kt));
500 return KRB5KRB_ERR_GENERIC;
504 kssl_krb5_rd_req(krb5_context con, krb5_auth_context *pacon,
505 krb5_const krb5_data *data,
506 krb5_const_principal princ, krb5_keytab keytab,
507 krb5_flags *flags, krb5_ticket **pptkt)
513 return (p_krb5_rd_req(con, pacon, data, princ, keytab, flags, pptkt));
515 return KRB5KRB_ERR_GENERIC;
519 krb5_principal_compare(krb5_context con, krb5_const_principal princ1,
520 krb5_const_principal princ2)
525 if (p_krb5_principal_compare)
526 return (p_krb5_principal_compare(con, princ1, princ2));
528 return KRB5KRB_ERR_GENERIC;
532 krb5_decrypt_tkt_part(krb5_context con, krb5_const krb5_keyblock *keys,
538 if (p_krb5_decrypt_tkt_part)
539 return (p_krb5_decrypt_tkt_part(con, keys, ticket));
541 return KRB5KRB_ERR_GENERIC;
544 krb5_error_code krb5_timeofday(krb5_context con, krb5_int32 *timeret)
549 if (p_krb5_timeofday)
550 return (p_krb5_timeofday(con, timeret));
552 return KRB5KRB_ERR_GENERIC;
555 krb5_error_code krb5_rc_default(krb5_context con, krb5_rcache *rc)
560 if (p_krb5_rc_default)
561 return (p_krb5_rc_default(con, rc));
563 return KRB5KRB_ERR_GENERIC;
567 krb5_rc_initialize(krb5_context con, krb5_rcache rc, krb5_deltat lifespan)
572 if (p_krb5_rc_initialize)
573 return (p_krb5_rc_initialize(con, rc, lifespan));
575 return KRB5KRB_ERR_GENERIC;
579 krb5_rc_get_lifespan(krb5_context con, krb5_rcache rc, krb5_deltat *lifespanp)
584 if (p_krb5_rc_get_lifespan)
585 return (p_krb5_rc_get_lifespan(con, rc, lifespanp));
587 return KRB5KRB_ERR_GENERIC;
590 krb5_error_code krb5_rc_destroy(krb5_context con, krb5_rcache rc)
595 if (p_krb5_rc_destroy)
596 return (p_krb5_rc_destroy(con, rc));
598 return KRB5KRB_ERR_GENERIC;
601 size_t krb5_checksum_size(krb5_context context, krb5_cksumtype ctype)
606 if (p_krb5_checksum_size)
607 return (p_krb5_checksum_size(context, ctype));
609 return KRB5KRB_ERR_GENERIC;
612 krb5_boolean valid_cksumtype(krb5_cksumtype ctype)
617 if (p_valid_cksumtype)
618 return (p_valid_cksumtype(ctype));
620 return KRB5KRB_ERR_GENERIC;
623 krb5_error_code krb5_kt_free_entry(krb5_context con, krb5_keytab_entry *entry)
628 if (p_krb5_kt_free_entry)
629 return (p_krb5_kt_free_entry(con, entry));
631 return KRB5KRB_ERR_GENERIC;
634 /* Structure definitions */
635 # ifndef NO_DEF_KRB5_CCACHE
637 # define krb5_x(ptr,args) ((ptr)?((*(ptr)) args):(abort(),1))
638 # define krb5_xc(ptr,args) ((ptr)?((*(ptr)) args):(abort(),(char*)0))
641 typedef krb5_pointer krb5_cc_cursor; /* cursor for sequential lookup */
643 typedef struct _krb5_ccache {
645 struct _krb5_cc_ops FAR *ops;
649 typedef struct _krb5_cc_ops {
652 char *(KRB5_CALLCONV *get_name)
653 (krb5_context, krb5_ccache);
654 krb5_error_code(KRB5_CALLCONV *resolve)
655 (krb5_context, krb5_ccache *, const char *);
656 krb5_error_code(KRB5_CALLCONV *gen_new)
657 (krb5_context, krb5_ccache *);
658 krb5_error_code(KRB5_CALLCONV *init)
659 (krb5_context, krb5_ccache, krb5_principal);
660 krb5_error_code(KRB5_CALLCONV *destroy)
661 (krb5_context, krb5_ccache);
662 krb5_error_code(KRB5_CALLCONV *close)
663 (krb5_context, krb5_ccache);
664 krb5_error_code(KRB5_CALLCONV *store)
665 (krb5_context, krb5_ccache, krb5_creds *);
666 krb5_error_code(KRB5_CALLCONV *retrieve)
667 (krb5_context, krb5_ccache, krb5_flags, krb5_creds *, krb5_creds *);
668 krb5_error_code(KRB5_CALLCONV *get_princ)
669 (krb5_context, krb5_ccache, krb5_principal *);
670 krb5_error_code(KRB5_CALLCONV *get_first)
671 (krb5_context, krb5_ccache, krb5_cc_cursor *);
672 krb5_error_code(KRB5_CALLCONV *get_next)
673 (krb5_context, krb5_ccache, krb5_cc_cursor *, krb5_creds *);
674 krb5_error_code(KRB5_CALLCONV *end_get)
675 (krb5_context, krb5_ccache, krb5_cc_cursor *);
676 krb5_error_code(KRB5_CALLCONV *remove_cred)
677 (krb5_context, krb5_ccache, krb5_flags, krb5_creds *);
678 krb5_error_code(KRB5_CALLCONV *set_flags)
679 (krb5_context, krb5_ccache, krb5_flags);
681 # endif /* NO_DEF_KRB5_CCACHE */
684 kssl_krb5_cc_get_principal
685 (krb5_context context, krb5_ccache cache, krb5_principal *principal) {
686 if (p_krb5_cc_get_principal)
687 return (p_krb5_cc_get_principal(context, cache, principal));
689 return (krb5_x((cache)->ops->get_princ, (context, cache, principal)));
693 kssl_krb5_auth_con_setrcache(krb5_context con, krb5_auth_context acon,
696 if (p_krb5_auth_con_setrcache)
697 return (p_krb5_auth_con_setrcache(con, acon, rcache));
699 return KRB5KRB_ERR_GENERIC;
703 kssl_krb5_get_server_rcache(krb5_context con, krb5_const krb5_data *data,
706 if (p_krb5_get_server_rcache)
707 return (p_krb5_get_server_rcache(con, data, rcache));
709 return KRB5KRB_ERR_GENERIC;
713 kssl_krb5_auth_con_getrcache(krb5_context con, krb5_auth_context acon,
714 krb5_rcache *prcache)
716 if (p_krb5_auth_con_getrcache)
717 return (p_krb5_auth_con_getrcache(con, acon, prcache));
719 return KRB5KRB_ERR_GENERIC;
722 krb5_error_code kssl_krb5_kt_close(krb5_context context, krb5_keytab keytab)
725 return (p_krb5_kt_close(context, keytab));
727 return KRB5KRB_ERR_GENERIC;
731 kssl_krb5_kt_get_entry(krb5_context context, krb5_keytab keytab,
732 krb5_const_principal principal, krb5_kvno vno,
733 krb5_enctype enctype, krb5_keytab_entry *entry)
735 if (p_krb5_kt_get_entry)
736 return (p_krb5_kt_get_entry
737 (context, keytab, principal, vno, enctype, entry));
739 return KRB5KRB_ERR_GENERIC;
741 # endif /* OPENSSL_SYS_WINDOWS || OPENSSL_SYS_WIN32 */
744 * memory allocation functions for non-temporary storage (e.g. stuff that
745 * gets saved into the kssl context)
747 static void *kssl_calloc(size_t nmemb, size_t size)
751 p = OPENSSL_malloc(nmemb * size);
753 memset(p, 0, nmemb * size);
758 # define kssl_malloc(size) OPENSSL_malloc((size))
759 # define kssl_realloc(ptr, size) OPENSSL_realloc(ptr, size)
760 # define kssl_free(ptr) OPENSSL_free((ptr))
763 *kstring(char *string)
765 static char *null = "[NULL]";
767 return ((string == NULL) ? null : string);
771 * Given KRB5 enctype (basically DES or 3DES), return closest match openssl
772 * EVP_ encryption algorithm. Return NULL for unknown or problematic
773 * (krb5_dk_encrypt) enctypes. Assume ENCTYPE_*_RAW (krb5_raw_encrypt) are
776 const EVP_CIPHER *kssl_map_enc(krb5_enctype enctype)
779 case ENCTYPE_DES_HMAC_SHA1: /* EVP_des_cbc(); */
780 case ENCTYPE_DES_CBC_CRC:
781 case ENCTYPE_DES_CBC_MD4:
782 case ENCTYPE_DES_CBC_MD5:
783 case ENCTYPE_DES_CBC_RAW:
784 return EVP_des_cbc();
786 case ENCTYPE_DES3_CBC_SHA1: /* EVP_des_ede3_cbc(); */
787 case ENCTYPE_DES3_CBC_SHA:
788 case ENCTYPE_DES3_CBC_RAW:
789 return EVP_des_ede3_cbc();
798 * Return true:1 if p "looks like" the start of the real authenticator
799 * described in kssl_skip_confound() below. The ASN.1 pattern is "62 xx 30
800 * yy" (APPLICATION-2, SEQUENCE), where xx-yy =~ 2, and xx and yy are
801 * possibly multi-byte length fields.
803 static int kssl_test_confound(unsigned char *p)
851 return (xx - len == yy) ? 1 : 0;
855 * Allocate, fill, and return cksumlens array of checksum lengths. This
856 * array holds just the unique elements from the krb5_cksumarray[]. array[n]
857 * == 0 signals end of data. The krb5_cksumarray[] was an internal variable
858 * that has since been replaced by a more general method for storing the
859 * data. It should not be used. Instead we use real API calls and make a
860 * guess for what the highest assigned CKSUMTYPE_ constant is. As of 1.2.2
861 * it is 0x000c (CKSUMTYPE_HMAC_SHA1_DES3). So we will use 0x0010.
863 static size_t *populate_cksumlens(void)
866 static size_t *cklens = NULL;
868 # ifdef KRB5_MIT_OLD11
872 # endif /* KRB5_MIT_OLD11 */
874 # ifdef KRB5CHECKAUTH
875 if (!cklens && !(cklens = (size_t *)calloc(sizeof(int), n + 1)))
878 for (i = 0; i < n; i++) {
879 if (!valid_cksumtype(i))
880 continue; /* array has holes */
881 for (j = 0; j < n; j++) {
882 if (cklens[j] == 0) {
883 cklens[j] = krb5_checksum_size(NULL, i);
884 break; /* krb5 elem was new: add */
886 if (cklens[j] == krb5_checksum_size(NULL, i)) {
887 break; /* ignore duplicate elements */
891 # endif /* KRB5CHECKAUTH */
897 * Return pointer to start of real authenticator within authenticator, or
898 * return NULL on error.
899 * Decrypted authenticator looks like this:
900 * [0 or 8 byte confounder] [4-24 byte checksum] [real authent'r]
901 * This hackery wouldn't be necessary if MIT KRB5 1.0.6 had the
902 * krb5_auth_con_getcksumtype() function advertised in its krb5.h.
904 unsigned char *kssl_skip_confound(krb5_enctype etype, unsigned char *a)
908 static size_t *cksumlens = NULL;
909 unsigned char *test_auth;
911 conlen = (etype) ? 8 : 0;
913 if (!cksumlens && !(cksumlens = populate_cksumlens()))
915 for (i = 0; (cklen = cksumlens[i]) != 0; i++) {
916 test_auth = a + conlen + cklen;
917 if (kssl_test_confound(test_auth))
925 * Set kssl_err error info when reason text is a simple string kssl_err =
926 * struct { int reason; char text[KSSL_ERR_MAX+1]; }
928 void kssl_err_set(KSSL_ERR *kssl_err, int reason, char *text)
930 if (kssl_err == NULL)
933 kssl_err->reason = reason;
934 BIO_snprintf(kssl_err->text, KSSL_ERR_MAX, "%s", text);
939 * Display contents of krb5_data struct, for debugging
941 void print_krb5_data(char *label, krb5_data *kdata)
945 fprintf(stderr, "%s[%d] ", label, kdata->length);
946 for (i = 0; i < (int)kdata->length; i++) {
947 if (0 && isprint((int)kdata->data[i]))
948 fprintf(stderr, "%c ", kdata->data[i]);
950 fprintf(stderr, "%02x ", (unsigned char)kdata->data[i]);
952 fprintf(stderr, "\n");
956 * Display contents of krb5_authdata struct, for debugging
958 void print_krb5_authdata(char *label, krb5_authdata **adata)
961 fprintf(stderr, "%s, authdata==0\n", label);
964 fprintf(stderr, "%s [%p]\n", label, (void *)adata);
968 * Display contents of krb5_keyblock struct, for debugging
970 void print_krb5_keyblock(char *label, krb5_keyblock *keyblk)
974 if (keyblk == NULL) {
975 fprintf(stderr, "%s, keyblk==0\n", label);
979 fprintf(stderr, "%s\n\t[et%d:%d]: ", label, keyblk->keytype,
980 keyblk->keyvalue->length);
981 for (i = 0; i < (int)keyblk->keyvalue->length; i++) {
982 fprintf(stderr, "%02x",
983 (unsigned char *)(keyblk->keyvalue->contents)[i]);
985 fprintf(stderr, "\n");
987 fprintf(stderr, "%s\n\t[et%d:%d]: ", label, keyblk->enctype,
989 for (i = 0; i < (int)keyblk->length; i++) {
990 fprintf(stderr, "%02x", keyblk->contents[i]);
992 fprintf(stderr, "\n");
997 * Display contents of krb5_principal_data struct, for debugging
998 * (krb5_principal is typedef'd == krb5_principal_data *)
1000 static void print_krb5_princ(char *label, krb5_principal_data *princ)
1004 fprintf(stderr, "%s principal Realm: ", label);
1007 for (ui = 0; ui < (int)princ->realm.length; ui++)
1008 putchar(princ->realm.data[ui]);
1009 fprintf(stderr, " (nametype %d) has %d strings:\n", princ->type,
1011 for (i = 0; i < (int)princ->length; i++) {
1012 fprintf(stderr, "\t%d [%d]: ", i, princ->data[i].length);
1013 for (uj = 0; uj < (int)princ->data[i].length; uj++) {
1014 putchar(princ->data[i].data[uj]);
1016 fprintf(stderr, "\n");
1021 /*- Given krb5 service (typically "kssl") and hostname in kssl_ctx,
1022 * Return encrypted Kerberos ticket for service @ hostname.
1023 * If authenp is non-NULL, also return encrypted authenticator,
1024 * whose data should be freed by caller.
1025 * (Originally was: Create Kerberos AP_REQ message for SSL Client.)
1027 * 19990628 VRS Started; Returns Kerberos AP_REQ message.
1028 * 20010409 VRS Modified for RFC2712; Returns enc tkt.
1029 * 20010606 VRS May also return optional authenticator.
1031 krb5_error_code kssl_cget_tkt( /* UPDATE */ KSSL_CTX *kssl_ctx,
1034 */ krb5_data **enc_ticketp,
1037 */ krb5_data *authenp,
1040 */ KSSL_ERR *kssl_err)
1042 krb5_error_code krb5rc = KRB5KRB_ERR_GENERIC;
1043 krb5_context krb5context = NULL;
1044 krb5_auth_context krb5auth_context = NULL;
1045 krb5_ccache krb5ccdef = NULL;
1046 krb5_creds krb5creds, *krb5credsp = NULL;
1047 krb5_data krb5_app_req;
1049 kssl_err_set(kssl_err, 0, "");
1050 memset((char *)&krb5creds, 0, sizeof(krb5creds));
1053 kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT, "No kssl_ctx defined.\n");
1055 } else if (!kssl_ctx->service_host) {
1056 kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
1057 "kssl_ctx service_host undefined.\n");
1061 if ((krb5rc = krb5_init_context(&krb5context)) != 0) {
1062 BIO_snprintf(kssl_err->text, KSSL_ERR_MAX,
1063 "krb5_init_context() fails: %d\n", krb5rc);
1064 kssl_err->reason = SSL_R_KRB5_C_INIT;
1068 if ((krb5rc = krb5_sname_to_principal(krb5context,
1069 kssl_ctx->service_host,
1070 (kssl_ctx->service_name) ?
1071 kssl_ctx->service_name : KRB5SVC,
1073 &krb5creds.server)) != 0) {
1074 BIO_snprintf(kssl_err->text, KSSL_ERR_MAX,
1075 "krb5_sname_to_principal() fails for %s/%s\n",
1076 kssl_ctx->service_host,
1078 service_name) ? kssl_ctx->service_name : KRB5SVC);
1079 kssl_err->reason = SSL_R_KRB5_C_INIT;
1083 if ((krb5rc = krb5_cc_default(krb5context, &krb5ccdef)) != 0) {
1084 kssl_err_set(kssl_err, SSL_R_KRB5_C_CC_PRINC,
1085 "krb5_cc_default fails.\n");
1089 if ((krb5rc = krb5_cc_get_principal(krb5context, krb5ccdef,
1090 &krb5creds.client)) != 0) {
1091 kssl_err_set(kssl_err, SSL_R_KRB5_C_CC_PRINC,
1092 "krb5_cc_get_principal() fails.\n");
1096 if ((krb5rc = krb5_get_credentials(krb5context, 0, krb5ccdef,
1097 &krb5creds, &krb5credsp)) != 0) {
1098 kssl_err_set(kssl_err, SSL_R_KRB5_C_GET_CRED,
1099 "krb5_get_credentials() fails.\n");
1103 *enc_ticketp = &krb5credsp->ticket;
1104 # ifdef KRB5_HEIMDAL
1105 kssl_ctx->enctype = krb5credsp->session.keytype;
1107 kssl_ctx->enctype = krb5credsp->keyblock.enctype;
1110 krb5rc = KRB5KRB_ERR_GENERIC;
1111 /* caller should free data of krb5_app_req */
1113 * 20010406 VRS deleted for real KerberosWrapper 20010605 VRS reinstated
1114 * to offer Authenticator to KerberosWrapper
1116 krb5_app_req.length = 0;
1118 krb5_data krb5in_data;
1119 const unsigned char *p;
1121 KRB5_APREQBODY *ap_req;
1123 authenp->length = 0;
1124 krb5in_data.data = NULL;
1125 krb5in_data.length = 0;
1126 if ((krb5rc = krb5_mk_req_extended(krb5context,
1127 &krb5auth_context, 0, &krb5in_data,
1128 krb5credsp, &krb5_app_req)) != 0) {
1129 kssl_err_set(kssl_err, SSL_R_KRB5_C_MK_REQ,
1130 "krb5_mk_req_extended() fails.\n");
1134 arlen = krb5_app_req.length;
1135 p = (unsigned char *)krb5_app_req.data;
1136 ap_req = (KRB5_APREQBODY *)d2i_KRB5_APREQ(NULL, &p, arlen);
1138 authenp->length = i2d_KRB5_ENCDATA(ap_req->authenticator, NULL);
1139 if (authenp->length && (authenp->data = malloc(authenp->length))) {
1140 unsigned char *adp = (unsigned char *)authenp->data;
1142 i2d_KRB5_ENCDATA(ap_req->authenticator, &adp);
1147 KRB5_APREQ_free((KRB5_APREQ *) ap_req);
1148 if (krb5_app_req.length)
1149 kssl_krb5_free_data_contents(krb5context, &krb5_app_req);
1151 # ifdef KRB5_HEIMDAL
1152 if (kssl_ctx_setkey(kssl_ctx, &krb5credsp->session)) {
1153 kssl_err_set(kssl_err, SSL_R_KRB5_C_INIT,
1154 "kssl_ctx_setkey() fails.\n");
1157 if (kssl_ctx_setkey(kssl_ctx, &krb5credsp->keyblock)) {
1158 kssl_err_set(kssl_err, SSL_R_KRB5_C_INIT,
1159 "kssl_ctx_setkey() fails.\n");
1167 kssl_ctx_show(kssl_ctx);
1168 # endif /* KSSL_DEBUG */
1170 if (krb5creds.client)
1171 krb5_free_principal(krb5context, krb5creds.client);
1172 if (krb5creds.server)
1173 krb5_free_principal(krb5context, krb5creds.server);
1174 if (krb5auth_context)
1175 krb5_auth_con_free(krb5context, krb5auth_context);
1177 krb5_free_context(krb5context);
1182 * Given d2i_-decoded asn1ticket, allocate and return a new krb5_ticket.
1183 * Return Kerberos error code and kssl_err struct on error.
1184 * Allocates krb5_ticket and krb5_principal; caller should free these.
1186 * 20010410 VRS Implemented krb5_decode_ticket() as
1187 * old_krb5_decode_ticket(). Missing from MIT1.0.6.
1188 * 20010615 VRS Re-cast as openssl/asn1 d2i_*() functions.
1189 * Re-used some of the old krb5_decode_ticket()
1190 * code here. This tkt should alloc/free just
1191 * like the real thing.
1193 static krb5_error_code kssl_TKT2tkt( /* IN */ krb5_context krb5context,
1196 */ KRB5_TKTBODY *asn1ticket,
1199 */ krb5_ticket **krb5ticket,
1202 */ KSSL_ERR *kssl_err)
1204 krb5_error_code krb5rc = KRB5KRB_ERR_GENERIC;
1205 krb5_ticket *new5ticket = NULL;
1206 ASN1_GENERALSTRING *gstr_svc, *gstr_host;
1210 if (asn1ticket == NULL || asn1ticket->realm == NULL ||
1211 asn1ticket->sname == NULL ||
1212 sk_ASN1_GENERALSTRING_num(asn1ticket->sname->namestring) < 2) {
1213 BIO_snprintf(kssl_err->text, KSSL_ERR_MAX,
1214 "Null field in asn1ticket.\n");
1215 kssl_err->reason = SSL_R_KRB5_S_RD_REQ;
1216 return KRB5KRB_ERR_GENERIC;
1219 if ((new5ticket = (krb5_ticket *)calloc(1, sizeof(krb5_ticket))) == NULL) {
1220 BIO_snprintf(kssl_err->text, KSSL_ERR_MAX,
1221 "Unable to allocate new krb5_ticket.\n");
1222 kssl_err->reason = SSL_R_KRB5_S_RD_REQ;
1223 return ENOMEM; /* or KRB5KRB_ERR_GENERIC; */
1226 gstr_svc = sk_ASN1_GENERALSTRING_value(asn1ticket->sname->namestring, 0);
1227 gstr_host = sk_ASN1_GENERALSTRING_value(asn1ticket->sname->namestring, 1);
1229 if ((krb5rc = kssl_build_principal_2(krb5context,
1230 &new5ticket->server,
1231 asn1ticket->realm->length,
1232 (char *)asn1ticket->realm->data,
1234 (char *)gstr_svc->data,
1236 (char *)gstr_host->data)) != 0) {
1238 BIO_snprintf(kssl_err->text, KSSL_ERR_MAX,
1239 "Error building ticket server principal.\n");
1240 kssl_err->reason = SSL_R_KRB5_S_RD_REQ;
1241 return krb5rc; /* or KRB5KRB_ERR_GENERIC; */
1244 krb5_princ_type(krb5context, new5ticket->server) =
1245 asn1ticket->sname->nametype->data[0];
1246 new5ticket->enc_part.enctype = asn1ticket->encdata->etype->data[0];
1247 new5ticket->enc_part.kvno = asn1ticket->encdata->kvno->data[0];
1248 new5ticket->enc_part.ciphertext.length =
1249 asn1ticket->encdata->cipher->length;
1250 if ((new5ticket->enc_part.ciphertext.data =
1251 calloc(1, asn1ticket->encdata->cipher->length)) == NULL) {
1253 BIO_snprintf(kssl_err->text, KSSL_ERR_MAX,
1254 "Error allocating cipher in krb5ticket.\n");
1255 kssl_err->reason = SSL_R_KRB5_S_RD_REQ;
1256 return KRB5KRB_ERR_GENERIC;
1258 memcpy(new5ticket->enc_part.ciphertext.data,
1259 asn1ticket->encdata->cipher->data,
1260 asn1ticket->encdata->cipher->length);
1263 *krb5ticket = new5ticket;
1268 * Given krb5 service name in KSSL_CTX *kssl_ctx (typically "kssl"),
1269 * and krb5 AP_REQ message & message length,
1270 * Return Kerberos session key and client principle
1271 * to SSL Server in KSSL_CTX *kssl_ctx.
1273 * 19990702 VRS Started.
1275 krb5_error_code kssl_sget_tkt( /* UPDATE */ KSSL_CTX *kssl_ctx,
1278 */ krb5_data *indata,
1281 */ krb5_ticket_times *ttimes,
1284 */ KSSL_ERR *kssl_err)
1286 krb5_error_code krb5rc = KRB5KRB_ERR_GENERIC;
1287 static krb5_context krb5context = NULL;
1288 static krb5_auth_context krb5auth_context = NULL;
1289 krb5_ticket *krb5ticket = NULL;
1290 KRB5_TKTBODY *asn1ticket = NULL;
1291 const unsigned char *p;
1292 krb5_keytab krb5keytab = NULL;
1293 krb5_keytab_entry kt_entry;
1294 krb5_principal krb5server;
1295 krb5_rcache rcache = NULL;
1297 kssl_err_set(kssl_err, 0, "");
1300 kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT, "No kssl_ctx defined.\n");
1304 fprintf(stderr, "in kssl_sget_tkt(%s)\n",
1305 kstring(kssl_ctx->service_name));
1306 # endif /* KSSL_DEBUG */
1308 if (!krb5context && (krb5rc = krb5_init_context(&krb5context))) {
1309 kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
1310 "krb5_init_context() fails.\n");
1313 if (krb5auth_context &&
1314 (krb5rc = krb5_auth_con_free(krb5context, krb5auth_context))) {
1315 kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
1316 "krb5_auth_con_free() fails.\n");
1319 krb5auth_context = NULL;
1320 if (!krb5auth_context &&
1321 (krb5rc = krb5_auth_con_init(krb5context, &krb5auth_context))) {
1322 kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
1323 "krb5_auth_con_init() fails.\n");
1327 if ((krb5rc = krb5_auth_con_getrcache(krb5context, krb5auth_context,
1329 kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
1330 "krb5_auth_con_getrcache() fails.\n");
1334 if ((krb5rc = krb5_sname_to_principal(krb5context, NULL,
1335 (kssl_ctx->service_name) ?
1336 kssl_ctx->service_name : KRB5SVC,
1338 &krb5server)) != 0) {
1339 kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
1340 "krb5_sname_to_principal() fails.\n");
1344 if (rcache == NULL) {
1345 if ((krb5rc = krb5_get_server_rcache(krb5context,
1346 krb5_princ_component(krb5context,
1350 kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
1351 "krb5_get_server_rcache() fails.\n");
1357 krb5_auth_con_setrcache(krb5context, krb5auth_context, rcache))) {
1358 kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
1359 "krb5_auth_con_setrcache() fails.\n");
1364 * kssl_ctx->keytab_file == NULL ==> use Kerberos default
1366 if (kssl_ctx->keytab_file) {
1367 krb5rc = krb5_kt_resolve(krb5context, kssl_ctx->keytab_file,
1370 kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
1371 "krb5_kt_resolve() fails.\n");
1375 krb5rc = krb5_kt_default(krb5context, &krb5keytab);
1377 kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
1378 "krb5_kt_default() fails.\n");
1383 /*- Actual Kerberos5 krb5_recvauth() has initial conversation here
1384 * o check KRB5_SENDAUTH_BADAUTHVERS
1385 * unless KRB5_RECVAUTH_SKIP_VERSION
1386 * o check KRB5_SENDAUTH_BADAPPLVERS
1387 * o send "0" msg if all OK
1391 * 20010411 was using AP_REQ instead of true KerberosWrapper
1393 * if ((krb5rc = krb5_rd_req(krb5context, &krb5auth_context,
1394 * &krb5in_data, krb5server, krb5keytab,
1395 * &ap_option, &krb5ticket)) != 0) { Error }
1398 p = (unsigned char *)indata->data;
1399 if ((asn1ticket = (KRB5_TKTBODY *)d2i_KRB5_TICKET(NULL, &p,
1400 (long)indata->length))
1402 BIO_snprintf(kssl_err->text, KSSL_ERR_MAX,
1403 "d2i_KRB5_TICKET() ASN.1 decode failure.\n");
1404 kssl_err->reason = SSL_R_KRB5_S_RD_REQ;
1409 * Was: krb5rc = krb5_decode_ticket(krb5in_data,&krb5ticket)) != 0)
1411 if ((krb5rc = kssl_TKT2tkt(krb5context, asn1ticket, &krb5ticket,
1413 BIO_snprintf(kssl_err->text, KSSL_ERR_MAX,
1414 "Error converting ASN.1 ticket to krb5_ticket.\n");
1415 kssl_err->reason = SSL_R_KRB5_S_RD_REQ;
1419 if (!krb5_principal_compare(krb5context, krb5server, krb5ticket->server)) {
1420 krb5rc = KRB5_PRINC_NOMATCH;
1421 BIO_snprintf(kssl_err->text, KSSL_ERR_MAX,
1422 "server principal != ticket principal\n");
1423 kssl_err->reason = SSL_R_KRB5_S_RD_REQ;
1426 if ((krb5rc = krb5_kt_get_entry(krb5context, krb5keytab,
1428 krb5ticket->enc_part.kvno,
1429 krb5ticket->enc_part.enctype,
1431 BIO_snprintf(kssl_err->text, KSSL_ERR_MAX,
1432 "krb5_kt_get_entry() fails with %x.\n", krb5rc);
1433 kssl_err->reason = SSL_R_KRB5_S_RD_REQ;
1436 if ((krb5rc = krb5_decrypt_tkt_part(krb5context, &kt_entry.key,
1437 krb5ticket)) != 0) {
1438 BIO_snprintf(kssl_err->text, KSSL_ERR_MAX,
1439 "krb5_decrypt_tkt_part() failed.\n");
1440 kssl_err->reason = SSL_R_KRB5_S_RD_REQ;
1443 krb5_kt_free_entry(krb5context, &kt_entry);
1447 krb5_address **paddr = krb5ticket->enc_part2->caddrs;
1448 fprintf(stderr, "Decrypted ticket fields:\n");
1449 fprintf(stderr, "\tflags: %X, transit-type: %X",
1450 krb5ticket->enc_part2->flags,
1451 krb5ticket->enc_part2->transited.tr_type);
1452 print_krb5_data("\ttransit-data: ",
1453 &(krb5ticket->enc_part2->transited.tr_contents));
1454 fprintf(stderr, "\tcaddrs: %p, authdata: %p\n",
1455 krb5ticket->enc_part2->caddrs,
1456 krb5ticket->enc_part2->authorization_data);
1458 fprintf(stderr, "\tcaddrs:\n");
1459 for (i = 0; paddr[i] != NULL; i++) {
1461 d.length = paddr[i]->length;
1462 d.data = paddr[i]->contents;
1463 print_krb5_data("\t\tIP: ", &d);
1466 fprintf(stderr, "\tstart/auth/end times: %d / %d / %d\n",
1467 krb5ticket->enc_part2->times.starttime,
1468 krb5ticket->enc_part2->times.authtime,
1469 krb5ticket->enc_part2->times.endtime);
1471 # endif /* KSSL_DEBUG */
1474 krb5rc = KRB5_NO_TKT_SUPPLIED;
1475 if (!krb5ticket || !krb5ticket->enc_part2 ||
1476 !krb5ticket->enc_part2->client ||
1477 !krb5ticket->enc_part2->client->data ||
1478 !krb5ticket->enc_part2->session) {
1479 kssl_err_set(kssl_err, SSL_R_KRB5_S_BAD_TICKET,
1480 "bad ticket from krb5_rd_req.\n");
1481 } else if (kssl_ctx_setprinc(kssl_ctx, KSSL_CLIENT,
1482 &krb5ticket->enc_part2->client->realm,
1483 krb5ticket->enc_part2->client->data,
1484 krb5ticket->enc_part2->client->length)) {
1485 kssl_err_set(kssl_err, SSL_R_KRB5_S_BAD_TICKET,
1486 "kssl_ctx_setprinc() fails.\n");
1487 } else if (kssl_ctx_setkey(kssl_ctx, krb5ticket->enc_part2->session)) {
1488 kssl_err_set(kssl_err, SSL_R_KRB5_S_BAD_TICKET,
1489 "kssl_ctx_setkey() fails.\n");
1490 } else if (krb5ticket->enc_part2->flags & TKT_FLG_INVALID) {
1491 krb5rc = KRB5KRB_AP_ERR_TKT_INVALID;
1492 kssl_err_set(kssl_err, SSL_R_KRB5_S_BAD_TICKET,
1493 "invalid ticket from krb5_rd_req.\n");
1497 kssl_ctx->enctype = krb5ticket->enc_part.enctype;
1498 ttimes->authtime = krb5ticket->enc_part2->times.authtime;
1499 ttimes->starttime = krb5ticket->enc_part2->times.starttime;
1500 ttimes->endtime = krb5ticket->enc_part2->times.endtime;
1501 ttimes->renew_till = krb5ticket->enc_part2->times.renew_till;
1505 kssl_ctx_show(kssl_ctx);
1506 # endif /* KSSL_DEBUG */
1509 KRB5_TICKET_free((KRB5_TICKET *) asn1ticket);
1511 krb5_kt_close(krb5context, krb5keytab);
1513 krb5_free_ticket(krb5context, krb5ticket);
1515 krb5_free_principal(krb5context, krb5server);
1520 * Allocate & return a new kssl_ctx struct.
1522 KSSL_CTX *kssl_ctx_new(void)
1524 return ((KSSL_CTX *)kssl_calloc(1, sizeof(KSSL_CTX)));
1528 * Frees a kssl_ctx struct and any allocated memory it holds. Returns NULL.
1530 KSSL_CTX *kssl_ctx_free(KSSL_CTX *kssl_ctx)
1532 if (kssl_ctx == NULL)
1536 OPENSSL_cleanse(kssl_ctx->key, kssl_ctx->length);
1538 kssl_free(kssl_ctx->key);
1539 if (kssl_ctx->client_princ)
1540 kssl_free(kssl_ctx->client_princ);
1541 if (kssl_ctx->service_host)
1542 kssl_free(kssl_ctx->service_host);
1543 if (kssl_ctx->service_name)
1544 kssl_free(kssl_ctx->service_name);
1545 if (kssl_ctx->keytab_file)
1546 kssl_free(kssl_ctx->keytab_file);
1548 kssl_free(kssl_ctx);
1549 return (KSSL_CTX *)NULL;
1553 * Given an array of (krb5_data *) entity (and optional realm), set the plain
1554 * (char *) client_princ or service_host member of the kssl_ctx struct.
1557 kssl_ctx_setprinc(KSSL_CTX *kssl_ctx, int which,
1558 krb5_data *realm, krb5_data *entity, int nentities)
1564 if (kssl_ctx == NULL || entity == NULL)
1565 return KSSL_CTX_ERR;
1569 princ = &kssl_ctx->client_princ;
1572 princ = &kssl_ctx->service_host;
1575 return KSSL_CTX_ERR;
1581 /* Add up all the entity->lengths */
1583 for (i = 0; i < nentities; i++) {
1584 length += entity[i].length;
1586 /* Add in space for the '/' character(s) (if any) */
1587 length += nentities - 1;
1588 /* Space for the ('@'+realm+NULL | NULL) */
1589 length += ((realm) ? realm->length + 2 : 1);
1591 if ((*princ = kssl_calloc(1, length)) == NULL)
1592 return KSSL_CTX_ERR;
1594 for (i = 0; i < nentities; i++) {
1595 strncat(*princ, entity[i].data, entity[i].length);
1596 if (i < nentities - 1) {
1597 strcat(*princ, "/");
1601 strcat(*princ, "@");
1602 (void)strncat(*princ, realm->data, realm->length);
1609 /*- Set one of the plain (char *) string members of the kssl_ctx struct.
1610 * Default values should be:
1611 * which == KSSL_SERVICE => "khost" (KRB5SVC)
1612 * which == KSSL_KEYTAB => "/etc/krb5.keytab" (KRB5KEYTAB)
1614 krb5_error_code kssl_ctx_setstring(KSSL_CTX *kssl_ctx, int which, char *text)
1619 return KSSL_CTX_ERR;
1623 string = &kssl_ctx->service_name;
1626 string = &kssl_ctx->service_host;
1629 string = &kssl_ctx->client_princ;
1632 string = &kssl_ctx->keytab_file;
1635 return KSSL_CTX_ERR;
1646 if ((*string = kssl_calloc(1, strlen(text) + 1)) == NULL)
1647 return KSSL_CTX_ERR;
1649 strcpy(*string, text);
1655 * Copy the Kerberos session key from a (krb5_keyblock *) to a kssl_ctx
1656 * struct. Clear kssl_ctx->key if Kerberos session key is NULL.
1658 krb5_error_code kssl_ctx_setkey(KSSL_CTX *kssl_ctx, krb5_keyblock *session)
1661 krb5_enctype enctype;
1662 krb5_octet FAR *contents = NULL;
1665 return KSSL_CTX_ERR;
1667 if (kssl_ctx->key) {
1668 OPENSSL_cleanse(kssl_ctx->key, kssl_ctx->length);
1669 kssl_free(kssl_ctx->key);
1674 # ifdef KRB5_HEIMDAL
1675 length = session->keyvalue->length;
1676 enctype = session->keytype;
1677 contents = session->keyvalue->contents;
1679 length = session->length;
1680 enctype = session->enctype;
1681 contents = session->contents;
1683 kssl_ctx->enctype = enctype;
1684 kssl_ctx->length = length;
1686 kssl_ctx->enctype = ENCTYPE_UNKNOWN;
1687 kssl_ctx->length = 0;
1691 if ((kssl_ctx->key =
1692 (krb5_octet FAR *)kssl_calloc(1, kssl_ctx->length)) == NULL) {
1693 kssl_ctx->length = 0;
1694 return KSSL_CTX_ERR;
1696 memcpy(kssl_ctx->key, contents, length);
1702 * Display contents of kssl_ctx struct
1704 void kssl_ctx_show(KSSL_CTX *kssl_ctx)
1708 printf("kssl_ctx: ");
1709 if (kssl_ctx == NULL) {
1713 printf("%p\n", (void *)kssl_ctx);
1715 printf("\tservice:\t%s\n",
1716 (kssl_ctx->service_name) ? kssl_ctx->service_name : "NULL");
1717 printf("\tclient:\t%s\n",
1718 (kssl_ctx->client_princ) ? kssl_ctx->client_princ : "NULL");
1719 printf("\tserver:\t%s\n",
1720 (kssl_ctx->service_host) ? kssl_ctx->service_host : "NULL");
1721 printf("\tkeytab:\t%s\n",
1722 (kssl_ctx->keytab_file) ? kssl_ctx->keytab_file : "NULL");
1723 printf("\tkey [%d:%d]:\t", kssl_ctx->enctype, kssl_ctx->length);
1725 for (i = 0; i < kssl_ctx->length && kssl_ctx->key; i++) {
1726 printf("%02x", kssl_ctx->key[i]);
1732 int kssl_keytab_is_available(KSSL_CTX *kssl_ctx)
1734 krb5_context krb5context = NULL;
1735 krb5_keytab krb5keytab = NULL;
1736 krb5_keytab_entry entry;
1737 krb5_principal princ = NULL;
1738 krb5_error_code krb5rc = KRB5KRB_ERR_GENERIC;
1741 if ((krb5rc = krb5_init_context(&krb5context)))
1745 * kssl_ctx->keytab_file == NULL ==> use Kerberos default
1747 if (kssl_ctx->keytab_file) {
1748 krb5rc = krb5_kt_resolve(krb5context, kssl_ctx->keytab_file,
1753 krb5rc = krb5_kt_default(krb5context, &krb5keytab);
1758 /* the host key we are looking for */
1759 krb5rc = krb5_sname_to_principal(krb5context, NULL,
1761 service_name ? kssl_ctx->service_name :
1762 KRB5SVC, KRB5_NT_SRV_HST, &princ);
1767 krb5rc = krb5_kt_get_entry(krb5context, krb5keytab, princ,
1770 /* IGNORE_ENCTYPE */
1772 if (krb5rc == KRB5_KT_NOTFOUND) {
1778 krb5_kt_free_entry(krb5context, &entry);
1783 krb5_kt_close(krb5context, krb5keytab);
1785 krb5_free_principal(krb5context, princ);
1787 krb5_free_context(krb5context);
1791 int kssl_tgt_is_available(KSSL_CTX *kssl_ctx)
1793 krb5_error_code krb5rc = KRB5KRB_ERR_GENERIC;
1794 krb5_context krb5context = NULL;
1795 krb5_ccache krb5ccdef = NULL;
1796 krb5_creds krb5creds, *krb5credsp = NULL;
1799 memset((char *)&krb5creds, 0, sizeof(krb5creds));
1804 if (!kssl_ctx->service_host)
1807 if ((krb5rc = krb5_init_context(&krb5context)) != 0)
1810 if ((krb5rc = krb5_sname_to_principal(krb5context,
1811 kssl_ctx->service_host,
1812 (kssl_ctx->service_name) ?
1813 kssl_ctx->service_name : KRB5SVC,
1815 &krb5creds.server)) != 0)
1818 if ((krb5rc = krb5_cc_default(krb5context, &krb5ccdef)) != 0)
1821 if ((krb5rc = krb5_cc_get_principal(krb5context, krb5ccdef,
1822 &krb5creds.client)) != 0)
1825 if ((krb5rc = krb5_get_credentials(krb5context, 0, krb5ccdef,
1826 &krb5creds, &krb5credsp)) != 0)
1833 kssl_ctx_show(kssl_ctx);
1834 # endif /* KSSL_DEBUG */
1836 if (krb5creds.client)
1837 krb5_free_principal(krb5context, krb5creds.client);
1838 if (krb5creds.server)
1839 krb5_free_principal(krb5context, krb5creds.server);
1841 krb5_free_context(krb5context);
1845 # if !defined(OPENSSL_SYS_WINDOWS) && !defined(OPENSSL_SYS_WIN32)
1846 void kssl_krb5_free_data_contents(krb5_context context, krb5_data *data)
1848 # ifdef KRB5_HEIMDAL
1852 # elif defined(KRB5_MIT_OLD11)
1854 krb5_xfree(data->data);
1858 krb5_free_data_contents(NULL, data);
1862 /* !OPENSSL_SYS_WINDOWS && !OPENSSL_SYS_WIN32 */
1865 * Given pointers to KerberosTime and struct tm structs, convert the
1866 * KerberosTime string to struct tm. Note that KerberosTime is a
1867 * ASN1_GENERALIZEDTIME value, constrained to GMT with no fractional seconds
1868 * as defined in RFC 1510. Return pointer to the (partially) filled in
1869 * struct tm on success, return NULL on failure.
1871 static struct tm *k_gmtime(ASN1_GENERALIZEDTIME *gtime, struct tm *k_tm)
1877 if (gtime == NULL || gtime->length < 14)
1879 if (gtime->data == NULL)
1882 p = (char *)>ime->data[14];
1887 k_tm->tm_sec = atoi(p);
1892 k_tm->tm_min = atoi(p);
1897 k_tm->tm_hour = atoi(p);
1902 k_tm->tm_mday = atoi(p);
1907 k_tm->tm_mon = atoi(p) - 1;
1912 k_tm->tm_year = atoi(p) - 1900;
1919 * Helper function for kssl_validate_times(). We need context->clockskew,
1920 * but krb5_context is an opaque struct. So we try to sneek the clockskew
1921 * out through the replay cache. If that fails just return a likely default
1924 static krb5_deltat get_rc_clockskew(krb5_context context)
1927 krb5_deltat clockskew;
1929 if (krb5_rc_default(context, &rc))
1930 return KSSL_CLOCKSKEW;
1931 if (krb5_rc_initialize(context, rc, 0))
1932 return KSSL_CLOCKSKEW;
1933 if (krb5_rc_get_lifespan(context, rc, &clockskew)) {
1934 clockskew = KSSL_CLOCKSKEW;
1936 (void)krb5_rc_destroy(context, rc);
1941 * kssl_validate_times() combines (and more importantly exposes) the MIT KRB5
1942 * internal function krb5_validate_times() and the in_clock_skew() macro.
1943 * The authenticator client time is checked to be within clockskew secs of
1944 * the current time and the current time is checked to be within the ticket
1945 * start and expire times. Either check may be omitted by supplying a NULL
1946 * value. Returns 0 for valid times, SSL_R_KRB5* error codes otherwise. See
1947 * Also: (Kerberos source)/krb5/lib/krb5/krb/valid_times.c 20010420 VRS
1949 krb5_error_code kssl_validate_times(krb5_timestamp atime,
1950 krb5_ticket_times *ttimes)
1953 krb5_timestamp start, now;
1955 krb5_context context;
1957 if ((rc = krb5_init_context(&context)))
1958 return SSL_R_KRB5_S_BAD_TICKET;
1959 skew = get_rc_clockskew(context);
1960 if ((rc = krb5_timeofday(context, &now)))
1961 return SSL_R_KRB5_S_BAD_TICKET;
1962 krb5_free_context(context);
1964 if (atime && labs(atime - now) >= skew)
1965 return SSL_R_KRB5_S_TKT_SKEW;
1970 start = (ttimes->starttime != 0) ? ttimes->starttime : ttimes->authtime;
1971 if (start - now > skew)
1972 return SSL_R_KRB5_S_TKT_NYV;
1973 if ((now - ttimes->endtime) > skew)
1974 return SSL_R_KRB5_S_TKT_EXPIRED;
1977 fprintf(stderr, "kssl_validate_times: %d |<- | %d - %d | < %d ->| %d\n",
1978 start, atime, now, skew, ttimes->endtime);
1979 # endif /* KSSL_DEBUG */
1985 * Decode and decrypt given DER-encoded authenticator, then pass
1986 * authenticator ctime back in *atimep (or 0 if time unavailable). Returns
1987 * krb5_error_code and kssl_err on error. A NULL authenticator
1988 * (authentp->length == 0) is not considered an error. Note that
1989 * kssl_check_authent() makes use of the KRB5 session key; you must call
1990 * kssl_sget_tkt() to get the key before calling this routine.
1992 krb5_error_code kssl_check_authent(
1995 */ KSSL_CTX *kssl_ctx,
1998 */ krb5_data *authentp,
2001 */ krb5_timestamp *atimep,
2004 */ KSSL_ERR *kssl_err)
2006 krb5_error_code krb5rc = 0;
2007 KRB5_ENCDATA *dec_authent = NULL;
2008 KRB5_AUTHENTBODY *auth = NULL;
2009 krb5_enctype enctype;
2010 EVP_CIPHER_CTX ciph_ctx;
2011 const EVP_CIPHER *enc = NULL;
2012 unsigned char iv[EVP_MAX_IV_LENGTH];
2013 const unsigned char *p;
2014 unsigned char *unenc_authent;
2015 int outl, unencbufsize;
2016 struct tm tm_time, *tm_l, *tm_g;
2017 time_t now, tl, tg, tr, tz_offset;
2019 EVP_CIPHER_CTX_init(&ciph_ctx);
2021 kssl_err_set(kssl_err, 0, "");
2023 # ifndef KRB5CHECKAUTH
2026 # if KRB5CHECKAUTH == 0
2029 # endif /* KRB5CHECKAUTH */
2031 if (authentp == NULL || authentp->length == 0)
2037 fprintf(stderr, "kssl_check_authent: authenticator[%d]:\n",
2040 for (ui = 0; ui < authentp->length; ui++)
2041 fprintf(stderr, "%02x ", p[ui]);
2042 fprintf(stderr, "\n");
2044 # endif /* KSSL_DEBUG */
2046 unencbufsize = 2 * authentp->length;
2047 if ((unenc_authent = calloc(1, unencbufsize)) == NULL) {
2048 kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
2049 "Unable to allocate authenticator buffer.\n");
2050 krb5rc = KRB5KRB_ERR_GENERIC;
2054 p = (unsigned char *)authentp->data;
2055 if ((dec_authent = d2i_KRB5_ENCDATA(NULL, &p,
2056 (long)authentp->length)) == NULL) {
2057 kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
2058 "Error decoding authenticator.\n");
2059 krb5rc = KRB5KRB_AP_ERR_BAD_INTEGRITY;
2063 enctype = dec_authent->etype->data[0]; /* should = kssl_ctx->enctype */
2064 # if !defined(KRB5_MIT_OLD11)
2066 case ENCTYPE_DES3_CBC_SHA1: /* EVP_des_ede3_cbc(); */
2067 case ENCTYPE_DES3_CBC_SHA:
2068 case ENCTYPE_DES3_CBC_RAW:
2069 krb5rc = 0; /* Skip, can't handle derived keys */
2073 enc = kssl_map_enc(enctype);
2074 memset(iv, 0, sizeof iv); /* per RFC 1510 */
2078 * Disable kssl_check_authent for ENCTYPE_DES3_CBC_SHA1. This
2079 * enctype indicates the authenticator was encrypted using key-usage
2080 * derived keys which openssl cannot decrypt.
2085 if (!EVP_CipherInit(&ciph_ctx, enc, kssl_ctx->key, iv, 0)) {
2086 kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
2087 "EVP_CipherInit error decrypting authenticator.\n");
2088 krb5rc = KRB5KRB_AP_ERR_BAD_INTEGRITY;
2091 outl = dec_authent->cipher->length;
2093 (&ciph_ctx, unenc_authent, dec_authent->cipher->data, outl)) {
2094 kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
2095 "EVP_Cipher error decrypting authenticator.\n");
2096 krb5rc = KRB5KRB_AP_ERR_BAD_INTEGRITY;
2099 EVP_CIPHER_CTX_cleanup(&ciph_ctx);
2104 fprintf(stderr, "kssl_check_authent: decrypted authenticator[%d] =\n",
2106 for (padl = 0; padl < outl; padl++)
2107 fprintf(stderr, "%02x ", unenc_authent[padl]);
2108 fprintf(stderr, "\n");
2110 # endif /* KSSL_DEBUG */
2112 if ((p = kssl_skip_confound(enctype, unenc_authent)) == NULL) {
2113 kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
2114 "confounded by authenticator.\n");
2115 krb5rc = KRB5KRB_AP_ERR_BAD_INTEGRITY;
2118 outl -= p - unenc_authent;
2120 if ((auth = (KRB5_AUTHENTBODY *)d2i_KRB5_AUTHENT(NULL, &p,
2121 (long)outl)) == NULL) {
2122 kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
2123 "Error decoding authenticator body.\n");
2124 krb5rc = KRB5KRB_AP_ERR_BAD_INTEGRITY;
2128 memset(&tm_time, 0, sizeof(struct tm));
2129 if (k_gmtime(auth->ctime, &tm_time) &&
2130 ((tr = mktime(&tm_time)) != (time_t)(-1))) {
2132 tm_l = localtime(&now);
2134 tm_g = gmtime(&now);
2136 tz_offset = tg - tl;
2138 *atimep = (krb5_timestamp)(tr - tz_offset);
2141 fprintf(stderr, "kssl_check_authent: returns %d for client time ",
2143 if (auth->ctime && auth->ctime->length && auth->ctime->data)
2144 fprintf(stderr, "%.*s\n", auth->ctime->length, auth->ctime->data);
2146 fprintf(stderr, "NULL\n");
2147 # endif /* KSSL_DEBUG */
2151 KRB5_AUTHENT_free((KRB5_AUTHENT *) auth);
2153 KRB5_ENCDATA_free(dec_authent);
2155 free(unenc_authent);
2156 EVP_CIPHER_CTX_cleanup(&ciph_ctx);
2161 * Replaces krb5_build_principal_ext(), with varargs length == 2 (svc, host),
2162 * because I don't know how to stub varargs. Returns krb5_error_code ==
2163 * ENOMEM on alloc error, otherwise passes back newly constructed principal,
2164 * which should be freed by caller.
2166 krb5_error_code kssl_build_principal_2(
2169 */ krb5_context context,
2172 */ krb5_principal *princ,
2175 */ int rlen, const char *realm,
2178 */ int slen, const char *svc,
2181 */ int hlen, const char *host)
2183 krb5_data *p_data = NULL;
2184 krb5_principal new_p = NULL;
2187 if ((p_data = (krb5_data *)calloc(2, sizeof(krb5_data))) == NULL ||
2188 (new_p = (krb5_principal)calloc(1, sizeof(krb5_principal_data)))
2192 new_p->data = p_data;
2194 if ((new_r = calloc(1, rlen + 1)) == NULL)
2196 memcpy(new_r, realm, rlen);
2197 krb5_princ_set_realm_length(context, new_p, rlen);
2198 krb5_princ_set_realm_data(context, new_p, new_r);
2200 if ((new_p->data[0].data = calloc(1, slen + 1)) == NULL)
2202 memcpy(new_p->data[0].data, svc, slen);
2203 new_p->data[0].length = slen;
2205 if ((new_p->data[1].data = calloc(1, hlen + 1)) == NULL)
2207 memcpy(new_p->data[1].data, host, hlen);
2208 new_p->data[1].length = hlen;
2210 krb5_princ_type(context, new_p) = KRB5_NT_UNKNOWN;
2215 if (new_p && new_p[0].data)
2216 free(new_p[0].data);
2217 if (new_p && new_p[1].data)
2218 free(new_p[1].data);
2226 void SSL_set0_kssl_ctx(SSL *s, KSSL_CTX *kctx)
2231 KSSL_CTX *SSL_get0_kssl_ctx(SSL *s)
2236 char *kssl_ctx_get0_client_princ(KSSL_CTX *kctx)
2239 return kctx->client_princ;
2243 #else /* !OPENSSL_NO_KRB5 */
2245 # if defined(PEDANTIC) || defined(OPENSSL_SYS_VMS)
2246 static void *dummy = &dummy;
2249 #endif /* !OPENSSL_NO_KRB5 */