1 /* ssl/kssl.c -*- mode: C; c-file-style: "eay" -*- */
2 /* Written by Vern Staats <staatsvr@asc.hpc.mil> for the OpenSSL project 2000.
4 /* ====================================================================
5 * Copyright (c) 2000 The OpenSSL Project. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
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
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/)"
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.
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.
33 * 6. Redistributions of any form whatsoever must retain the following
35 * "This product includes software developed by the OpenSSL Project
36 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
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 * ====================================================================
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).
59 /* ssl/kssl.c -- Routines to support (& debug) Kerberos5 auth for openssl
61 ** 19990701 VRS Started.
62 ** 200011?? Jeffrey Altman, Richard Levitte
63 ** Generalized for Heimdal, Newer MIT, & Win32.
64 ** Integrated into main OpenSSL 0.9.7 snapshots.
65 ** 20010413 Simon Wilkinson, VRS
66 ** Real RFC2712 KerberosWrapper replaces AP_REQ.
69 #include <openssl/opensslconf.h>
70 #ifndef OPENSSL_NO_KRB5
71 #define _XOPEN_SOURCE /* glibc2 needs this to declare strptime() */
75 #include <openssl/ssl.h>
76 #include <openssl/evp.h>
77 #include <openssl/objects.h>
78 #include <openssl/krb5_asn.h>
81 * When OpenSSL is built on Windows, we do not want to require that
82 * the Kerberos DLLs be available in order for the OpenSSL DLLs to
83 * work. Therefore, all Kerberos routines are loaded at run time
84 * and we do not link to a .LIB file.
87 #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32)
89 * The purpose of the following pre-processor statements is to provide
90 * compatibility with different releases of MIT Kerberos for Windows.
91 * All versions up to 1.2 used macros. But macros do not allow for
92 * a binary compatible interface for DLLs. Therefore, all macros are
93 * being replaced by function calls. The following code will allow
94 * an OpenSSL DLL built on Windows to work whether or not the macro
95 * or function form of the routines are utilized.
97 #ifdef krb5_cc_get_principal
98 #define NO_DEF_KRB5_CCACHE
99 #undef krb5_cc_get_principal
101 #define krb5_cc_get_principal kssl_krb5_cc_get_principal
103 #define krb5_free_data_contents kssl_krb5_free_data_contents
104 #define krb5_free_context kssl_krb5_free_context
105 #define krb5_auth_con_free kssl_krb5_auth_con_free
106 #define krb5_free_principal kssl_krb5_free_principal
107 #define krb5_mk_req_extended kssl_krb5_mk_req_extended
108 #define krb5_get_credentials kssl_krb5_get_credentials
109 #define krb5_cc_default kssl_krb5_cc_default
110 #define krb5_sname_to_principal kssl_krb5_sname_to_principal
111 #define krb5_init_context kssl_krb5_init_context
112 #define krb5_free_ticket kssl_krb5_free_ticket
113 #define krb5_rd_req kssl_krb5_rd_req
114 #define krb5_kt_default kssl_krb5_kt_default
115 #define krb5_kt_resolve kssl_krb5_kt_resolve
116 #define krb5_auth_con_init kssl_krb5_auth_con_init
118 #define krb5_principal_compare kssl_krb5_principal_compare
119 /* macro #define krb5_kt_get_entry kssl_krb5_kt_get_entry */
120 #define krb5_decrypt_tkt_part kssl_krb5_decrypt_tkt_part
121 #define krb5_timeofday kssl_krb5_timeofday
122 #define krb5_rc_default kssl_krb5_rc_default
123 #define krb5_rc_initialize kssl_krb5_rc_initialize
124 #define krb5_rc_get_lifespan kssl_krb5_rc_get_lifespan
125 #define krb5_rc_destroy kssl_krb5_rc_destroy
126 #define valid_cksumtype kssl_valid_cksumtype
127 #define krb5_checksum_size kssl_krb5_checksum_size
128 #define krb5_kt_free_entry kssl_krb5_kt_free_entry
129 #define krb5_auth_con_setrcache kssl_krb5_auth_con_setrcache
130 #define krb5_auth_con_getrcache kssl_krb5_auth_con_getrcache
131 #define krb5_get_server_rcache kssl_krb5_get_server_rcache
133 /* Prototypes for built in stubs */
134 void kssl_krb5_free_data_contents(krb5_context, krb5_data *);
135 void kssl_krb5_free_principal(krb5_context, krb5_principal );
136 krb5_error_code kssl_krb5_kt_resolve(krb5_context,
139 krb5_error_code kssl_krb5_kt_default(krb5_context,
141 krb5_error_code kssl_krb5_free_ticket(krb5_context, krb5_ticket *);
142 krb5_error_code kssl_krb5_rd_req(krb5_context, krb5_auth_context *,
143 krb5_const krb5_data *,
144 krb5_const_principal, krb5_keytab,
145 krb5_flags *,krb5_ticket **);
147 krb5_boolean kssl_krb5_principal_compare(krb5_context, krb5_const_principal,
148 krb5_const_principal);
149 krb5_error_code kssl_krb5_mk_req_extended(krb5_context,
151 krb5_const krb5_flags,
155 krb5_error_code kssl_krb5_init_context(krb5_context *);
156 void kssl_krb5_free_context(krb5_context);
157 krb5_error_code kssl_krb5_cc_default(krb5_context,krb5_ccache *);
158 krb5_error_code kssl_krb5_sname_to_principal(krb5_context,
163 krb5_error_code kssl_krb5_get_credentials(krb5_context,
164 krb5_const krb5_flags,
168 krb5_error_code kssl_krb5_auth_con_init(krb5_context,
169 krb5_auth_context *);
170 krb5_error_code kssl_krb5_cc_get_principal(krb5_context context,
172 krb5_principal *principal);
173 krb5_error_code kssl_krb5_auth_con_free(krb5_context,krb5_auth_context);
174 size_t kssl_krb5_checksum_size(krb5_context context,krb5_cksumtype ctype);
175 krb5_boolean kssl_valid_cksumtype(krb5_cksumtype ctype);
176 krb5_error_code krb5_kt_free_entry(krb5_context,krb5_keytab_entry FAR * );
177 krb5_error_code kssl_krb5_auth_con_setrcache(krb5_context,
180 krb5_error_code kssl_krb5_get_server_rcache(krb5_context,
181 krb5_const krb5_data *,
183 krb5_error_code kssl_krb5_auth_con_getrcache(krb5_context,
187 /* Function pointers (almost all Kerberos functions are _stdcall) */
188 static void (_stdcall *p_krb5_free_data_contents)(krb5_context, krb5_data *)
190 static void (_stdcall *p_krb5_free_principal)(krb5_context, krb5_principal )
192 static krb5_error_code(_stdcall *p_krb5_kt_resolve)
193 (krb5_context, krb5_const char *, krb5_keytab *)=NULL;
194 static krb5_error_code (_stdcall *p_krb5_kt_default)(krb5_context,
196 static krb5_error_code (_stdcall *p_krb5_free_ticket)(krb5_context,
198 static krb5_error_code (_stdcall *p_krb5_rd_req)(krb5_context,
200 krb5_const krb5_data *,
201 krb5_const_principal,
202 krb5_keytab, krb5_flags *,
203 krb5_ticket **)=NULL;
204 static krb5_error_code (_stdcall *p_krb5_mk_req_extended)
205 (krb5_context, krb5_auth_context *,
206 krb5_const krb5_flags, krb5_data *, krb5_creds *,
208 static krb5_error_code (_stdcall *p_krb5_init_context)(krb5_context *)=NULL;
209 static void (_stdcall *p_krb5_free_context)(krb5_context)=NULL;
210 static krb5_error_code (_stdcall *p_krb5_cc_default)(krb5_context,
212 static krb5_error_code (_stdcall *p_krb5_sname_to_principal)
213 (krb5_context, krb5_const char *, krb5_const char *,
214 krb5_int32, krb5_principal *)=NULL;
215 static krb5_error_code (_stdcall *p_krb5_get_credentials)
216 (krb5_context, krb5_const krb5_flags, krb5_ccache,
217 krb5_creds *, krb5_creds **)=NULL;
218 static krb5_error_code (_stdcall *p_krb5_auth_con_init)
219 (krb5_context, krb5_auth_context *)=NULL;
220 static krb5_error_code (_stdcall *p_krb5_cc_get_principal)
221 (krb5_context context, krb5_ccache cache,
222 krb5_principal *principal)=NULL;
223 static krb5_error_code (_stdcall *p_krb5_auth_con_free)
224 (krb5_context, krb5_auth_context)=NULL;
225 static krb5_error_code (_stdcall *p_krb5_decrypt_tkt_part)
226 (krb5_context, krb5_const krb5_keyblock *,
228 static krb5_error_code (_stdcall *p_krb5_timeofday)
229 (krb5_context context, krb5_int32 *timeret)=NULL;
230 static krb5_error_code (_stdcall *p_krb5_rc_default)
231 (krb5_context context, krb5_rcache *rc)=NULL;
232 static krb5_error_code (_stdcall *p_krb5_rc_initialize)
233 (krb5_context context, krb5_rcache rc,
234 krb5_deltat lifespan)=NULL;
235 static krb5_error_code (_stdcall *p_krb5_rc_get_lifespan)
236 (krb5_context context, krb5_rcache rc,
237 krb5_deltat *lifespan)=NULL;
238 static krb5_error_code (_stdcall *p_krb5_rc_destroy)
239 (krb5_context context, krb5_rcache rc)=NULL;
240 static krb5_boolean (_stdcall *p_krb5_principal_compare)
241 (krb5_context, krb5_const_principal, krb5_const_principal)=NULL;
242 static size_t (_stdcall *p_krb5_checksum_size)(krb5_context context,krb5_cksumtype ctype)=NULL;
243 static krb5_boolean (_stdcall *p_valid_cksumtype)(krb5_cksumtype ctype)=NULL;
244 static krb5_error_code (_stdcall *p_krb5_kt_free_entry)
245 (krb5_context,krb5_keytab_entry * )=NULL;
246 static krb5_error_code (_stdcall * p_krb5_auth_con_setrcache)(krb5_context,
249 static krb5_error_code (_stdcall * p_krb5_get_server_rcache)(krb5_context,
250 krb5_const krb5_data *,
252 static krb5_error_code (* p_krb5_auth_con_getrcache)(krb5_context,
255 static int krb5_loaded = 0; /* only attempt to initialize func ptrs once */
257 /* Function to Load the Kerberos 5 DLL and initialize function pointers */
264 hKRB5_32 = LoadLibrary("KRB5_32");
268 (FARPROC) p_krb5_free_data_contents =
269 GetProcAddress( hKRB5_32, "krb5_free_data_contents" );
270 (FARPROC) p_krb5_free_context =
271 GetProcAddress( hKRB5_32, "krb5_free_context" );
272 (FARPROC) p_krb5_auth_con_free =
273 GetProcAddress( hKRB5_32, "krb5_auth_con_free" );
274 (FARPROC) p_krb5_free_principal =
275 GetProcAddress( hKRB5_32, "krb5_free_principal" );
276 (FARPROC) p_krb5_mk_req_extended =
277 GetProcAddress( hKRB5_32, "krb5_mk_req_extended" );
278 (FARPROC) p_krb5_get_credentials =
279 GetProcAddress( hKRB5_32, "krb5_get_credentials" );
280 (FARPROC) p_krb5_cc_get_principal =
281 GetProcAddress( hKRB5_32, "krb5_cc_get_principal" );
282 (FARPROC) p_krb5_cc_default =
283 GetProcAddress( hKRB5_32, "krb5_cc_default" );
284 (FARPROC) p_krb5_sname_to_principal =
285 GetProcAddress( hKRB5_32, "krb5_sname_to_principal" );
286 (FARPROC) p_krb5_init_context =
287 GetProcAddress( hKRB5_32, "krb5_init_context" );
288 (FARPROC) p_krb5_free_ticket =
289 GetProcAddress( hKRB5_32, "krb5_free_ticket" );
290 (FARPROC) p_krb5_rd_req =
291 GetProcAddress( hKRB5_32, "krb5_rd_req" );
292 (FARPROC) p_krb5_principal_compare =
293 GetProcAddress( hKRB5_32, "krb5_principal_compare" );
294 (FARPROC) p_krb5_decrypt_tkt_part =
295 GetProcAddress( hKRB5_32, "krb5_decrypt_tkt_part" );
296 (FARPROC) p_krb5_timeofday =
297 GetProcAddress( hKRB5_32, "krb5_timeofday" );
298 (FARPROC) p_krb5_rc_default =
299 GetProcAddress( hKRB5_32, "krb5_rc_default" );
300 (FARPROC) p_krb5_rc_initialize =
301 GetProcAddress( hKRB5_32, "krb5_rc_initialize" );
302 (FARPROC) p_krb5_rc_get_lifespan =
303 GetProcAddress( hKRB5_32, "krb5_rc_get_lifespan" );
304 (FARPROC) p_krb5_rc_destroy =
305 GetProcAddress( hKRB5_32, "krb5_rc_destroy" );
306 (FARPROC) p_krb5_kt_default =
307 GetProcAddress( hKRB5_32, "krb5_kt_default" );
308 (FARPROC) p_krb5_kt_resolve =
309 GetProcAddress( hKRB5_32, "krb5_kt_resolve" );
310 (FARPROC) p_krb5_auth_con_init =
311 GetProcAddress( hKRB5_32, "krb5_auth_con_init" );
312 (FARPROC) p_valid_cksumtype =
313 GetProcAddress( hKRB5_32, "valid_cksumtype" );
314 (FARPROC) p_krb5_checksum_size =
315 GetProcAddress( hKRB5_32, "krb5_checksum_size" );
316 (FARPROC) p_krb5_kt_free_entry =
317 GetProcAddress( hKRB5_32, "krb5_kt_free_entry" );
318 (FARPROC) p_krb5_auth_con_setrcache =
319 GetProcAddress( hKRB5_32, "krb5_auth_con_setrcache" );
320 (FARPROC) p_krb5_get_server_rcache =
321 GetProcAddress( hKRB5_32, "krb5_get_server_rcache" );
322 (FARPROC) p_krb5_auth_con_getrcache =
323 GetProcAddress( hKRB5_32, "krb5_auth_con_getrcache" );
326 /* Stubs for each function to be dynamicly loaded */
328 kssl_krb5_free_data_contents(krb5_context CO, krb5_data * data)
333 if ( p_krb5_free_data_contents )
334 p_krb5_free_data_contents(CO,data);
338 kssl_krb5_mk_req_extended (krb5_context CO,
339 krb5_auth_context * pACO,
340 krb5_const krb5_flags F,
348 if ( p_krb5_mk_req_extended )
349 return(p_krb5_mk_req_extended(CO,pACO,F,pD1,pC,pD2));
351 return KRB5KRB_ERR_GENERIC;
354 kssl_krb5_auth_con_init(krb5_context CO,
355 krb5_auth_context * pACO)
360 if ( p_krb5_auth_con_init )
361 return(p_krb5_auth_con_init(CO,pACO));
363 return KRB5KRB_ERR_GENERIC;
366 kssl_krb5_auth_con_free (krb5_context CO,
367 krb5_auth_context ACO)
372 if ( p_krb5_auth_con_free )
373 return(p_krb5_auth_con_free(CO,ACO));
375 return KRB5KRB_ERR_GENERIC;
378 kssl_krb5_get_credentials(krb5_context CO,
379 krb5_const krb5_flags F,
387 if ( p_krb5_get_credentials )
388 return(p_krb5_get_credentials(CO,F,CC,pCR,ppCR));
390 return KRB5KRB_ERR_GENERIC;
393 kssl_krb5_sname_to_principal(krb5_context CO,
394 krb5_const char * pC1,
395 krb5_const char * pC2,
397 krb5_principal * pPR)
402 if ( p_krb5_sname_to_principal )
403 return(p_krb5_sname_to_principal(CO,pC1,pC2,I,pPR));
405 return KRB5KRB_ERR_GENERIC;
409 kssl_krb5_cc_default(krb5_context CO,
415 if ( p_krb5_cc_default )
416 return(p_krb5_cc_default(CO,pCC));
418 return KRB5KRB_ERR_GENERIC;
422 kssl_krb5_init_context(krb5_context * pCO)
427 if ( p_krb5_init_context )
428 return(p_krb5_init_context(pCO));
430 return KRB5KRB_ERR_GENERIC;
434 kssl_krb5_free_context(krb5_context CO)
439 if ( p_krb5_free_context )
440 p_krb5_free_context(CO);
444 kssl_krb5_free_principal(krb5_context c, krb5_principal p)
449 if ( p_krb5_free_principal )
450 p_krb5_free_principal(c,p);
454 kssl_krb5_kt_resolve(krb5_context con,
455 krb5_const char * sz,
461 if ( p_krb5_kt_resolve )
462 return(p_krb5_kt_resolve(con,sz,kt));
464 return KRB5KRB_ERR_GENERIC;
468 kssl_krb5_kt_default(krb5_context con,
474 if ( p_krb5_kt_default )
475 return(p_krb5_kt_default(con,kt));
477 return KRB5KRB_ERR_GENERIC;
481 kssl_krb5_free_ticket(krb5_context con,
487 if ( p_krb5_free_ticket )
488 return(p_krb5_free_ticket(con,kt));
490 return KRB5KRB_ERR_GENERIC;
494 kssl_krb5_rd_req(krb5_context con, krb5_auth_context * pacon,
495 krb5_const krb5_data * data,
496 krb5_const_principal princ, krb5_keytab keytab,
497 krb5_flags * flags, krb5_ticket ** pptkt)
503 return(p_krb5_rd_req(con,pacon,data,princ,keytab,flags,pptkt));
505 return KRB5KRB_ERR_GENERIC;
509 krb5_principal_compare(krb5_context con, krb5_const_principal princ1,
510 krb5_const_principal princ2)
515 if ( p_krb5_principal_compare )
516 return(p_krb5_principal_compare(con,princ1,princ2));
518 return KRB5KRB_ERR_GENERIC;
522 krb5_decrypt_tkt_part(krb5_context con, krb5_const krb5_keyblock *keys,
528 if ( p_krb5_decrypt_tkt_part )
529 return(p_krb5_decrypt_tkt_part(con,keys,ticket));
531 return KRB5KRB_ERR_GENERIC;
535 krb5_timeofday(krb5_context con, krb5_int32 *timeret)
540 if ( p_krb5_timeofday )
541 return(p_krb5_timeofday(con,timeret));
543 return KRB5KRB_ERR_GENERIC;
547 krb5_rc_default(krb5_context con, krb5_rcache *rc)
552 if ( p_krb5_rc_default )
553 return(p_krb5_rc_default(con,rc));
555 return KRB5KRB_ERR_GENERIC;
559 krb5_rc_initialize(krb5_context con, krb5_rcache rc, krb5_deltat lifespan)
564 if ( p_krb5_rc_initialize )
565 return(p_krb5_rc_initialize(con, rc, lifespan));
567 return KRB5KRB_ERR_GENERIC;
571 krb5_rc_get_lifespan(krb5_context con, krb5_rcache rc, krb5_deltat *lifespanp)
576 if ( p_krb5_rc_get_lifespan )
577 return(p_krb5_rc_get_lifespan(con, rc, lifespanp));
579 return KRB5KRB_ERR_GENERIC;
583 krb5_rc_destroy(krb5_context con, krb5_rcache rc)
588 if ( p_krb5_rc_destroy )
589 return(p_krb5_rc_destroy(con, rc));
591 return KRB5KRB_ERR_GENERIC;
595 krb5_checksum_size(krb5_context context,krb5_cksumtype ctype)
600 if ( p_krb5_checksum_size )
601 return(p_krb5_checksum_size(context, ctype));
603 return KRB5KRB_ERR_GENERIC;
607 valid_cksumtype(krb5_cksumtype ctype)
612 if ( p_valid_cksumtype )
613 return(p_valid_cksumtype(ctype));
615 return KRB5KRB_ERR_GENERIC;
619 krb5_kt_free_entry(krb5_context con,krb5_keytab_entry * entry)
624 if ( p_krb5_kt_free_entry )
625 return(p_krb5_kt_free_entry(con,entry));
627 return KRB5KRB_ERR_GENERIC;
630 /* Structure definitions */
631 #ifndef NO_DEF_KRB5_CCACHE
633 #define krb5_x(ptr,args) ((ptr)?((*(ptr)) args):(abort(),1))
634 #define krb5_xc(ptr,args) ((ptr)?((*(ptr)) args):(abort(),(char*)0))
637 typedef krb5_pointer krb5_cc_cursor; /* cursor for sequential lookup */
639 typedef struct _krb5_ccache
642 struct _krb5_cc_ops FAR *ops;
646 typedef struct _krb5_cc_ops
650 char * (KRB5_CALLCONV *get_name)
651 (krb5_context, krb5_ccache);
652 krb5_error_code (KRB5_CALLCONV *resolve)
653 (krb5_context, krb5_ccache *, const char *);
654 krb5_error_code (KRB5_CALLCONV *gen_new)
655 (krb5_context, krb5_ccache *);
656 krb5_error_code (KRB5_CALLCONV *init)
657 (krb5_context, krb5_ccache, krb5_principal);
658 krb5_error_code (KRB5_CALLCONV *destroy)
659 (krb5_context, krb5_ccache);
660 krb5_error_code (KRB5_CALLCONV *close)
661 (krb5_context, krb5_ccache);
662 krb5_error_code (KRB5_CALLCONV *store)
663 (krb5_context, krb5_ccache, krb5_creds *);
664 krb5_error_code (KRB5_CALLCONV *retrieve)
665 (krb5_context, krb5_ccache,
666 krb5_flags, krb5_creds *, krb5_creds *);
667 krb5_error_code (KRB5_CALLCONV *get_princ)
668 (krb5_context, krb5_ccache, krb5_principal *);
669 krb5_error_code (KRB5_CALLCONV *get_first)
670 (krb5_context, krb5_ccache, krb5_cc_cursor *);
671 krb5_error_code (KRB5_CALLCONV *get_next)
672 (krb5_context, krb5_ccache,
673 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,
678 krb5_flags, krb5_creds *);
679 krb5_error_code (KRB5_CALLCONV *set_flags)
680 (krb5_context, krb5_ccache, krb5_flags);
682 #endif /* NO_DEF_KRB5_CCACHE */
685 kssl_krb5_cc_get_principal
686 (krb5_context context, krb5_ccache cache,
687 krb5_principal *principal)
689 if ( p_krb5_cc_get_principal )
690 return(p_krb5_cc_get_principal(context,cache,principal));
693 ((cache)->ops->get_princ,(context, cache, principal)));
697 kssl_krb5_auth_con_setrcache(krb5_context con, krb5_auth_context acon,
700 if ( p_krb5_auth_con_setrcache )
701 return(p_krb5_auth_con_setrcache(con,acon,rcache));
703 return KRB5KRB_ERR_GENERIC;
707 kssl_krb5_get_server_rcache(krb5_context con, krb5_const krb5_data * data,
708 krb5_rcache * rcache)
710 if ( p_krb5_get_server_rcache )
711 return(p_krb5_get_server_rcache(con,data,rcache));
713 return KRB5KRB_ERR_GENERIC;
717 kssl_krb5_auth_con_getrcache(krb5_context con, krb5_auth_context acon,
718 krb5_rcache * prcache)
720 if ( p_krb5_auth_con_getrcache )
721 return(p_krb5_auth_con_getrcache(con,acon, prcache));
723 return KRB5KRB_ERR_GENERIC;
725 #endif /* OPENSSL_SYS_WINDOWS || OPENSSL_SYS_WIN32 */
728 *kstring(char *string)
730 static char *null = "[NULL]";
732 return ((string == NULL)? null: string);
737 *knumber(int len, krb5_octet *contents)
739 static char buf[MAXKNUM+1];
742 BIO_snprintf(buf, MAXKNUM, "[%d] ", len);
744 for (i=0; i < len && MAXKNUM > strlen(buf)+3; i++)
746 BIO_snprintf(&buf[strlen(buf)], 3, "%02x", contents[i]);
753 /* Given KRB5 enctype (basically DES or 3DES),
754 ** return closest match openssl EVP_ encryption algorithm.
755 ** Return NULL for unknown or problematic (krb5_dk_encrypt) enctypes.
756 ** Assume ENCTYPE_*_RAW (krb5_raw_encrypt) are OK.
759 kssl_map_enc(krb5_enctype enctype)
763 case ENCTYPE_DES_HMAC_SHA1: /* EVP_des_cbc(); */
764 case ENCTYPE_DES_CBC_CRC:
765 case ENCTYPE_DES_CBC_MD4:
766 case ENCTYPE_DES_CBC_MD5:
767 case ENCTYPE_DES_CBC_RAW:
768 return (EVP_CIPHER *) EVP_des_cbc();
770 case ENCTYPE_DES3_CBC_SHA1: /* EVP_des_ede3_cbc(); */
771 case ENCTYPE_DES3_CBC_SHA:
772 case ENCTYPE_DES3_CBC_RAW:
773 return (EVP_CIPHER *) EVP_des_ede3_cbc();
775 default: return (EVP_CIPHER *) NULL;
781 /* Return true:1 if p "looks like" the start of the real authenticator
782 ** described in kssl_skip_confound() below. The ASN.1 pattern is
783 ** "62 xx 30 yy" (APPLICATION-2, SEQUENCE), where xx-yy =~ 2, and
784 ** xx and yy are possibly multi-byte length fields.
786 int kssl_test_confound(unsigned char *p)
791 if (*p++ != 0x62) return 0;
792 if (*p > 0x82) return 0;
794 case 0x82: p++; xx = (*p++ << 8); xx += *p++; break;
795 case 0x81: p++; xx = *p++; break;
797 default: xx = *p++; break;
799 if (*p++ != 0x30) return 0;
800 if (*p > 0x82) return 0;
802 case 0x82: p++; len+=2; yy = (*p++ << 8); yy += *p++; break;
803 case 0x81: p++; len++; yy = *p++; break;
805 default: yy = *p++; break;
808 return (xx - len == yy)? 1: 0;
811 /* Allocate, fill, and return cksumlens array of checksum lengths.
812 ** This array holds just the unique elements from the krb5_cksumarray[].
813 ** array[n] == 0 signals end of data.
815 ** The krb5_cksumarray[] was an internal variable that has since been
816 ** replaced by a more general method for storing the data. It should
817 ** not be used. Instead we use real API calls and make a guess for
818 ** what the highest assigned CKSUMTYPE_ constant is. As of 1.2.2
819 ** it is 0x000c (CKSUMTYPE_HMAC_SHA1_DES3). So we will use 0x0010.
821 size_t *populate_cksumlens(void)
824 static size_t *cklens = NULL;
826 #ifdef KRB5_MIT_OLD11
830 #endif /* KRB5_MIT_OLD11 */
833 if (!cklens && !(cklens = (size_t *) calloc(sizeof(int),n+1))) return NULL;
835 for (i=0; i < n; i++) {
836 if (!valid_cksumtype(i)) continue; /* array has holes */
837 for (j=0; j < n; j++) {
838 if (cklens[j] == 0) {
839 cklens[j] = krb5_checksum_size(NULL,i);
840 break; /* krb5 elem was new: add */
842 if (cklens[j] == krb5_checksum_size(NULL,i)) {
843 break; /* ignore duplicate elements */
847 #endif /* KRB5CHECKAUTH */
852 /* Return pointer to start of real authenticator within authenticator, or
853 ** return NULL on error.
854 ** Decrypted authenticator looks like this:
855 ** [0 or 8 byte confounder] [4-24 byte checksum] [real authent'r]
856 ** This hackery wouldn't be necessary if MIT KRB5 1.0.6 had the
857 ** krb5_auth_con_getcksumtype() function advertised in its krb5.h.
859 unsigned char *kssl_skip_confound(krb5_enctype etype, unsigned char *a)
863 static size_t *cksumlens = NULL;
864 unsigned char *test_auth;
866 conlen = (etype)? 8: 0;
868 if (!cksumlens && !(cksumlens = populate_cksumlens())) return NULL;
869 for (i=0; (cklen = cksumlens[i]) != 0; i++)
871 test_auth = a + conlen + cklen;
872 if (kssl_test_confound(test_auth)) return test_auth;
879 /* Set kssl_err error info when reason text is a simple string
880 ** kssl_err = struct { int reason; char text[KSSL_ERR_MAX+1]; }
883 kssl_err_set(KSSL_ERR *kssl_err, int reason, char *text)
885 if (kssl_err == NULL) return;
887 kssl_err->reason = reason;
888 BIO_snprintf(kssl_err->text, KSSL_ERR_MAX, text);
893 /* Display contents of krb5_data struct, for debugging
896 print_krb5_data(char *label, krb5_data *kdata)
900 printf("%s[%d] ", label, kdata->length);
901 for (i=0; i < kdata->length; i++)
903 if (0 && isprint((int) kdata->data[i]))
904 printf( "%c ", kdata->data[i]);
906 printf( "%02x ", (unsigned char) kdata->data[i]);
912 /* Display contents of krb5_authdata struct, for debugging
915 print_krb5_authdata(char *label, krb5_authdata **adata)
919 printf("%s, authdata==0\n", label);
922 printf("%s [%p]\n", label, adata);
926 printf("%s[at%d:%d] ", label, adata->ad_type, adata->length);
927 for (i=0; i < adata->length; i++)
929 printf((isprint(adata->contents[i]))? "%c ": "%02x",
938 /* Display contents of krb5_keyblock struct, for debugging
941 print_krb5_keyblock(char *label, krb5_keyblock *keyblk)
947 printf("%s, keyblk==0\n", label);
951 printf("%s\n\t[et%d:%d]: ", label, keyblk->keytype,
952 keyblk->keyvalue->length);
953 for (i=0; i < keyblk->keyvalue->length; i++)
955 printf("%02x",(unsigned char *)(keyblk->keyvalue->contents)[i]);
959 printf("%s\n\t[et%d:%d]: ", label, keyblk->enctype, keyblk->length);
960 for (i=0; i < keyblk->length; i++)
962 printf("%02x",keyblk->contents[i]);
969 /* Display contents of krb5_principal_data struct, for debugging
970 ** (krb5_principal is typedef'd == krb5_principal_data *)
973 print_krb5_princ(char *label, krb5_principal_data *princ)
978 printf("%s principal Realm: ", label);
979 if (princ == NULL) return;
980 for (ui=0; ui < princ->realm.length; ui++) putchar(princ->realm.data[ui]);
981 printf(" (nametype %d) has %d strings:\n", princ->type,princ->length);
982 for (i=0; i < princ->length; i++)
984 printf("\t%d [%d]: ", i, princ->data[i].length);
985 for (uj=0; uj < princ->data[i].length; uj++) {
986 putchar(princ->data[i].data[uj]);
994 /* Given krb5 service (typically "kssl") and hostname in kssl_ctx,
995 ** Return encrypted Kerberos ticket for service @ hostname.
996 ** If authenp is non-NULL, also return encrypted authenticator,
997 ** whose data should be freed by caller.
998 ** (Originally was: Create Kerberos AP_REQ message for SSL Client.)
1000 ** 19990628 VRS Started; Returns Kerberos AP_REQ message.
1001 ** 20010409 VRS Modified for RFC2712; Returns enc tkt.
1002 ** 20010606 VRS May also return optional authenticator.
1005 kssl_cget_tkt( /* UPDATE */ KSSL_CTX *kssl_ctx,
1006 /* OUT */ krb5_data **enc_ticketp,
1007 /* UPDATE */ krb5_data *authenp,
1008 /* OUT */ KSSL_ERR *kssl_err)
1010 krb5_error_code krb5rc = KRB5KRB_ERR_GENERIC;
1011 krb5_context krb5context = NULL;
1012 krb5_auth_context krb5auth_context = NULL;
1013 krb5_ccache krb5ccdef = NULL;
1014 krb5_creds krb5creds, *krb5credsp = NULL;
1015 krb5_data krb5_app_req;
1017 kssl_err_set(kssl_err, 0, "");
1018 memset((char *)&krb5creds, 0, sizeof(krb5creds));
1022 kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
1023 "No kssl_ctx defined.\n");
1026 else if (!kssl_ctx->service_host)
1028 kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
1029 "kssl_ctx service_host undefined.\n");
1033 if ((krb5rc = krb5_init_context(&krb5context)) != 0)
1035 BIO_snprintf(kssl_err->text,KSSL_ERR_MAX,
1036 "krb5_init_context() fails: %d\n", krb5rc);
1037 kssl_err->reason = SSL_R_KRB5_C_INIT;
1041 if ((krb5rc = krb5_sname_to_principal(krb5context,
1042 kssl_ctx->service_host,
1043 (kssl_ctx->service_name)? kssl_ctx->service_name: KRB5SVC,
1044 KRB5_NT_SRV_HST, &krb5creds.server)) != 0)
1046 BIO_snprintf(kssl_err->text,KSSL_ERR_MAX,
1047 "krb5_sname_to_principal() fails for %s/%s\n",
1048 kssl_ctx->service_host,
1049 (kssl_ctx->service_name)? kssl_ctx->service_name:
1051 kssl_err->reason = SSL_R_KRB5_C_INIT;
1055 if ((krb5rc = krb5_cc_default(krb5context, &krb5ccdef)) != 0)
1057 kssl_err_set(kssl_err, SSL_R_KRB5_C_CC_PRINC,
1058 "krb5_cc_default fails.\n");
1062 if ((krb5rc = krb5_cc_get_principal(krb5context, krb5ccdef,
1063 &krb5creds.client)) != 0)
1065 kssl_err_set(kssl_err, SSL_R_KRB5_C_CC_PRINC,
1066 "krb5_cc_get_principal() fails.\n");
1070 if ((krb5rc = krb5_get_credentials(krb5context, 0, krb5ccdef,
1071 &krb5creds, &krb5credsp)) != 0)
1073 kssl_err_set(kssl_err, SSL_R_KRB5_C_GET_CRED,
1074 "krb5_get_credentials() fails.\n");
1078 *enc_ticketp = &krb5credsp->ticket;
1080 kssl_ctx->enctype = krb5credsp->session.keytype;
1082 kssl_ctx->enctype = krb5credsp->keyblock.enctype;
1085 krb5rc = KRB5KRB_ERR_GENERIC;
1086 /* caller should free data of krb5_app_req */
1087 /* 20010406 VRS deleted for real KerberosWrapper
1088 ** 20010605 VRS reinstated to offer Authenticator to KerberosWrapper
1090 krb5_app_req.length = 0;
1093 krb5_data krb5in_data;
1096 KRB5_APREQBODY *ap_req;
1098 authenp->length = 0;
1099 krb5in_data.data = NULL;
1100 krb5in_data.length = 0;
1101 if ((krb5rc = krb5_mk_req_extended(krb5context,
1102 &krb5auth_context, 0, &krb5in_data, krb5credsp,
1103 &krb5_app_req)) != 0)
1105 kssl_err_set(kssl_err, SSL_R_KRB5_C_MK_REQ,
1106 "krb5_mk_req_extended() fails.\n");
1110 arlen = krb5_app_req.length;
1111 p = (unsigned char *)krb5_app_req.data;
1112 ap_req = (KRB5_APREQBODY *) d2i_KRB5_APREQ(NULL, &p, arlen);
1115 authenp->length = i2d_KRB5_ENCDATA(
1116 ap_req->authenticator, NULL);
1117 if (authenp->length &&
1118 (authenp->data = malloc(authenp->length)))
1120 unsigned char *adp = (unsigned char *)authenp->data;
1121 authenp->length = i2d_KRB5_ENCDATA(
1122 ap_req->authenticator, &adp);
1126 if (ap_req) KRB5_APREQ_free((KRB5_APREQ *) ap_req);
1127 if (krb5_app_req.length)
1128 kssl_krb5_free_data_contents(krb5context,&krb5_app_req);
1131 if (kssl_ctx_setkey(kssl_ctx, &krb5credsp->session))
1133 kssl_err_set(kssl_err, SSL_R_KRB5_C_INIT,
1134 "kssl_ctx_setkey() fails.\n");
1137 if (kssl_ctx_setkey(kssl_ctx, &krb5credsp->keyblock))
1139 kssl_err_set(kssl_err, SSL_R_KRB5_C_INIT,
1140 "kssl_ctx_setkey() fails.\n");
1147 kssl_ctx_show(kssl_ctx);
1148 #endif /* KSSL_DEBUG */
1150 if (krb5creds.client) krb5_free_principal(krb5context,
1152 if (krb5creds.server) krb5_free_principal(krb5context,
1154 if (krb5auth_context) krb5_auth_con_free(krb5context,
1156 if (krb5context) krb5_free_context(krb5context);
1161 /* Given d2i_-decoded asn1ticket, allocate and return a new krb5_ticket.
1162 ** Return Kerberos error code and kssl_err struct on error.
1163 ** Allocates krb5_ticket and krb5_principal; caller should free these.
1165 ** 20010410 VRS Implemented krb5_decode_ticket() as
1166 ** old_krb5_decode_ticket(). Missing from MIT1.0.6.
1167 ** 20010615 VRS Re-cast as openssl/asn1 d2i_*() functions.
1168 ** Re-used some of the old krb5_decode_ticket()
1169 ** code here. This tkt should alloc/free just
1170 ** like the real thing.
1173 kssl_TKT2tkt( /* IN */ krb5_context krb5context,
1174 /* IN */ KRB5_TKTBODY *asn1ticket,
1175 /* OUT */ krb5_ticket **krb5ticket,
1176 /* OUT */ KSSL_ERR *kssl_err )
1178 krb5_error_code krb5rc = KRB5KRB_ERR_GENERIC;
1179 krb5_ticket *new5ticket = NULL;
1180 ASN1_GENERALSTRING *gstr_svc, *gstr_host;
1184 if (asn1ticket == NULL || asn1ticket->realm == NULL ||
1185 asn1ticket->sname == NULL ||
1186 asn1ticket->sname->namestring == NULL ||
1187 asn1ticket->sname->namestring->num < 2)
1189 BIO_snprintf(kssl_err->text, KSSL_ERR_MAX,
1190 "Null field in asn1ticket.\n");
1191 kssl_err->reason = SSL_R_KRB5_S_RD_REQ;
1192 return KRB5KRB_ERR_GENERIC;
1195 if ((new5ticket = (krb5_ticket *) calloc(1, sizeof(krb5_ticket)))==NULL)
1197 BIO_snprintf(kssl_err->text, KSSL_ERR_MAX,
1198 "Unable to allocate new krb5_ticket.\n");
1199 kssl_err->reason = SSL_R_KRB5_S_RD_REQ;
1200 return ENOMEM; /* or KRB5KRB_ERR_GENERIC; */
1203 gstr_svc = (ASN1_GENERALSTRING*)asn1ticket->sname->namestring->data[0];
1204 gstr_host = (ASN1_GENERALSTRING*)asn1ticket->sname->namestring->data[1];
1206 if ((krb5rc = kssl_build_principal_2(krb5context,
1207 &new5ticket->server,
1208 asn1ticket->realm->length, asn1ticket->realm->data,
1209 gstr_svc->length, gstr_svc->data,
1210 gstr_host->length, gstr_host->data)) != 0)
1213 BIO_snprintf(kssl_err->text, KSSL_ERR_MAX,
1214 "Error building ticket server principal.\n");
1215 kssl_err->reason = SSL_R_KRB5_S_RD_REQ;
1216 return krb5rc; /* or KRB5KRB_ERR_GENERIC; */
1219 krb5_princ_type(krb5context, new5ticket->server) =
1220 asn1ticket->sname->nametype->data[0];
1221 new5ticket->enc_part.enctype = asn1ticket->encdata->etype->data[0];
1222 new5ticket->enc_part.kvno = asn1ticket->encdata->kvno->data[0];
1223 new5ticket->enc_part.ciphertext.length =
1224 asn1ticket->encdata->cipher->length;
1225 if ((new5ticket->enc_part.ciphertext.data =
1226 calloc(1, asn1ticket->encdata->cipher->length)) == NULL)
1229 BIO_snprintf(kssl_err->text, KSSL_ERR_MAX,
1230 "Error allocating cipher in krb5ticket.\n");
1231 kssl_err->reason = SSL_R_KRB5_S_RD_REQ;
1232 return KRB5KRB_ERR_GENERIC;
1236 memcpy(new5ticket->enc_part.ciphertext.data,
1237 asn1ticket->encdata->cipher->data,
1238 asn1ticket->encdata->cipher->length);
1241 *krb5ticket = new5ticket;
1246 /* Given krb5 service name in KSSL_CTX *kssl_ctx (typically "kssl"),
1247 ** and krb5 AP_REQ message & message length,
1248 ** Return Kerberos session key and client principle
1249 ** to SSL Server in KSSL_CTX *kssl_ctx.
1251 ** 19990702 VRS Started.
1254 kssl_sget_tkt( /* UPDATE */ KSSL_CTX *kssl_ctx,
1255 /* IN */ krb5_data *indata,
1256 /* OUT */ krb5_ticket_times *ttimes,
1257 /* OUT */ KSSL_ERR *kssl_err )
1259 krb5_error_code krb5rc = KRB5KRB_ERR_GENERIC;
1260 static krb5_context krb5context = NULL;
1261 static krb5_auth_context krb5auth_context = NULL;
1262 krb5_ticket *krb5ticket = NULL;
1263 KRB5_TKTBODY *asn1ticket = NULL;
1265 krb5_keytab krb5keytab = NULL;
1266 krb5_keytab_entry kt_entry;
1267 krb5_principal krb5server;
1268 krb5_rcache rcache = NULL;
1270 kssl_err_set(kssl_err, 0, "");
1274 kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
1275 "No kssl_ctx defined.\n");
1280 printf("in kssl_sget_tkt(%s)\n", kstring(kssl_ctx->service_name));
1281 #endif /* KSSL_DEBUG */
1283 if (!krb5context && (krb5rc = krb5_init_context(&krb5context)))
1285 kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
1286 "krb5_init_context() fails.\n");
1289 if (krb5auth_context &&
1290 (krb5rc = krb5_auth_con_free(krb5context, krb5auth_context)))
1292 kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
1293 "krb5_auth_con_free() fails.\n");
1296 else krb5auth_context = NULL;
1297 if (!krb5auth_context &&
1298 (krb5rc = krb5_auth_con_init(krb5context, &krb5auth_context)))
1300 kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
1301 "krb5_auth_con_init() fails.\n");
1306 if ((krb5rc = krb5_auth_con_getrcache(krb5context, krb5auth_context,
1309 kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
1310 "krb5_auth_con_getrcache() fails.\n");
1314 if ((krb5rc = krb5_sname_to_principal(krb5context, NULL,
1315 (kssl_ctx->service_name)? kssl_ctx->service_name: KRB5SVC,
1316 KRB5_NT_SRV_HST, &krb5server)) != 0)
1318 kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
1319 "krb5_sname_to_principal() fails.\n");
1325 if ((krb5rc = krb5_get_server_rcache(krb5context,
1326 krb5_princ_component(krb5context, krb5server, 0),
1329 kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
1330 "krb5_get_server_rcache() fails.\n");
1335 if ((krb5rc = krb5_auth_con_setrcache(krb5context, krb5auth_context, rcache)))
1337 kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
1338 "krb5_auth_con_setrcache() fails.\n");
1343 /* kssl_ctx->keytab_file == NULL ==> use Kerberos default
1345 if (kssl_ctx->keytab_file)
1347 krb5rc = krb5_kt_resolve(krb5context, kssl_ctx->keytab_file,
1351 kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
1352 "krb5_kt_resolve() fails.\n");
1358 krb5rc = krb5_kt_default(krb5context,&krb5keytab);
1361 kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
1362 "krb5_kt_default() fails.\n");
1367 /* Actual Kerberos5 krb5_recvauth() has initial conversation here
1368 ** o check KRB5_SENDAUTH_BADAUTHVERS
1369 ** unless KRB5_RECVAUTH_SKIP_VERSION
1370 ** o check KRB5_SENDAUTH_BADAPPLVERS
1371 ** o send "0" msg if all OK
1374 /* 20010411 was using AP_REQ instead of true KerberosWrapper
1376 ** if ((krb5rc = krb5_rd_req(krb5context, &krb5auth_context,
1377 ** &krb5in_data, krb5server, krb5keytab,
1378 ** &ap_option, &krb5ticket)) != 0) { Error }
1381 p = (unsigned char *)indata->data;
1382 if ((asn1ticket = (KRB5_TKTBODY *) d2i_KRB5_TICKET(NULL, &p,
1383 (long) indata->length)) == NULL)
1385 BIO_snprintf(kssl_err->text, KSSL_ERR_MAX,
1386 "d2i_KRB5_TICKET() ASN.1 decode failure.\n");
1387 kssl_err->reason = SSL_R_KRB5_S_RD_REQ;
1391 /* Was: krb5rc = krb5_decode_ticket(krb5in_data,&krb5ticket)) != 0) */
1392 if ((krb5rc = kssl_TKT2tkt(krb5context, asn1ticket, &krb5ticket,
1395 BIO_snprintf(kssl_err->text, KSSL_ERR_MAX,
1396 "Error converting ASN.1 ticket to krb5_ticket.\n");
1397 kssl_err->reason = SSL_R_KRB5_S_RD_REQ;
1401 if (! krb5_principal_compare(krb5context, krb5server,
1402 krb5ticket->server)) {
1403 krb5rc = KRB5_PRINC_NOMATCH;
1404 BIO_snprintf(kssl_err->text, KSSL_ERR_MAX,
1405 "server principal != ticket principal\n");
1406 kssl_err->reason = SSL_R_KRB5_S_RD_REQ;
1409 if ((krb5rc = krb5_kt_get_entry(krb5context, krb5keytab,
1410 krb5ticket->server, krb5ticket->enc_part.kvno,
1411 krb5ticket->enc_part.enctype, &kt_entry)) != 0) {
1412 BIO_snprintf(kssl_err->text, KSSL_ERR_MAX,
1413 "krb5_kt_get_entry() fails with %x.\n", krb5rc);
1414 kssl_err->reason = SSL_R_KRB5_S_RD_REQ;
1417 if ((krb5rc = krb5_decrypt_tkt_part(krb5context, &kt_entry.key,
1418 krb5ticket)) != 0) {
1419 BIO_snprintf(kssl_err->text, KSSL_ERR_MAX,
1420 "krb5_decrypt_tkt_part() failed.\n");
1421 kssl_err->reason = SSL_R_KRB5_S_RD_REQ;
1425 krb5_kt_free_entry(krb5context, &kt_entry);
1428 int i; krb5_address **paddr = krb5ticket->enc_part2->caddrs;
1429 printf("Decrypted ticket fields:\n");
1430 printf("\tflags: %X, transit-type: %X",
1431 krb5ticket->enc_part2->flags,
1432 krb5ticket->enc_part2->transited.tr_type);
1433 print_krb5_data("\ttransit-data: ",
1434 &(krb5ticket->enc_part2->transited.tr_contents));
1435 printf("\tcaddrs: %p, authdata: %p\n",
1436 krb5ticket->enc_part2->caddrs,
1437 krb5ticket->enc_part2->authorization_data);
1440 printf("\tcaddrs:\n");
1441 for (i=0; paddr[i] != NULL; i++)
1444 d.length=paddr[i]->length;
1445 d.data=paddr[i]->contents;
1446 print_krb5_data("\t\tIP: ", &d);
1449 printf("\tstart/auth/end times: %d / %d / %d\n",
1450 krb5ticket->enc_part2->times.starttime,
1451 krb5ticket->enc_part2->times.authtime,
1452 krb5ticket->enc_part2->times.endtime);
1454 #endif /* KSSL_DEBUG */
1457 krb5rc = KRB5_NO_TKT_SUPPLIED;
1458 if (!krb5ticket || !krb5ticket->enc_part2 ||
1459 !krb5ticket->enc_part2->client ||
1460 !krb5ticket->enc_part2->client->data ||
1461 !krb5ticket->enc_part2->session)
1463 kssl_err_set(kssl_err, SSL_R_KRB5_S_BAD_TICKET,
1464 "bad ticket from krb5_rd_req.\n");
1466 else if (kssl_ctx_setprinc(kssl_ctx, KSSL_CLIENT,
1467 &krb5ticket->enc_part2->client->realm,
1468 krb5ticket->enc_part2->client->data))
1470 kssl_err_set(kssl_err, SSL_R_KRB5_S_BAD_TICKET,
1471 "kssl_ctx_setprinc() fails.\n");
1473 else if (kssl_ctx_setkey(kssl_ctx, krb5ticket->enc_part2->session))
1475 kssl_err_set(kssl_err, SSL_R_KRB5_S_BAD_TICKET,
1476 "kssl_ctx_setkey() fails.\n");
1478 else if (krb5ticket->enc_part2->flags & TKT_FLG_INVALID)
1480 krb5rc = KRB5KRB_AP_ERR_TKT_INVALID;
1481 kssl_err_set(kssl_err, SSL_R_KRB5_S_BAD_TICKET,
1482 "invalid ticket from krb5_rd_req.\n");
1486 kssl_ctx->enctype = krb5ticket->enc_part.enctype;
1487 ttimes->authtime = krb5ticket->enc_part2->times.authtime;
1488 ttimes->starttime = krb5ticket->enc_part2->times.starttime;
1489 ttimes->endtime = krb5ticket->enc_part2->times.endtime;
1490 ttimes->renew_till = krb5ticket->enc_part2->times.renew_till;
1494 kssl_ctx_show(kssl_ctx);
1495 #endif /* KSSL_DEBUG */
1497 if (asn1ticket) KRB5_TICKET_free((KRB5_TICKET *) asn1ticket);
1498 if (krb5keytab) krb5_kt_close(krb5context, krb5keytab);
1499 if (krb5ticket) krb5_free_ticket(krb5context, krb5ticket);
1500 if (krb5server) krb5_free_principal(krb5context, krb5server);
1505 /* Allocate & return a new kssl_ctx struct.
1510 return ((KSSL_CTX *) calloc(1, sizeof(KSSL_CTX)));
1514 /* Frees a kssl_ctx struct and any allocated memory it holds.
1518 kssl_ctx_free(KSSL_CTX *kssl_ctx)
1520 if (kssl_ctx == NULL) return kssl_ctx;
1522 if (kssl_ctx->key) memset(kssl_ctx->key, 0,
1524 if (kssl_ctx->key) free(kssl_ctx->key);
1525 if (kssl_ctx->client_princ) free(kssl_ctx->client_princ);
1526 if (kssl_ctx->service_host) free(kssl_ctx->service_host);
1527 if (kssl_ctx->service_name) free(kssl_ctx->service_name);
1528 if (kssl_ctx->keytab_file) free(kssl_ctx->keytab_file);
1531 return (KSSL_CTX *) NULL;
1535 /* Given a (krb5_data *) entity (and optional realm),
1536 ** set the plain (char *) client_princ or service_host member
1537 ** of the kssl_ctx struct.
1540 kssl_ctx_setprinc(KSSL_CTX *kssl_ctx, int which,
1541 krb5_data *realm, krb5_data *entity)
1546 if (kssl_ctx == NULL || entity == NULL) return KSSL_CTX_ERR;
1550 case KSSL_CLIENT: princ = &kssl_ctx->client_princ; break;
1551 case KSSL_SERVER: princ = &kssl_ctx->service_host; break;
1552 default: return KSSL_CTX_ERR; break;
1554 if (*princ) free(*princ);
1556 length = entity->length + ((realm)? realm->length + 2: 1);
1557 if ((*princ = calloc(1, length)) == NULL)
1558 return KSSL_CTX_ERR;
1561 strncpy(*princ, entity->data, entity->length);
1564 strcat (*princ, "@");
1565 (void) strncat(*princ, realm->data, realm->length);
1573 /* Set one of the plain (char *) string members of the kssl_ctx struct.
1574 ** Default values should be:
1575 ** which == KSSL_SERVICE => "khost" (KRB5SVC)
1576 ** which == KSSL_KEYTAB => "/etc/krb5.keytab" (KRB5KEYTAB)
1579 kssl_ctx_setstring(KSSL_CTX *kssl_ctx, int which, char *text)
1583 if (!kssl_ctx) return KSSL_CTX_ERR;
1587 case KSSL_SERVICE: string = &kssl_ctx->service_name; break;
1588 case KSSL_SERVER: string = &kssl_ctx->service_host; break;
1589 case KSSL_CLIENT: string = &kssl_ctx->client_princ; break;
1590 case KSSL_KEYTAB: string = &kssl_ctx->keytab_file; break;
1591 default: return KSSL_CTX_ERR; break;
1593 if (*string) free(*string);
1601 if ((*string = calloc(1, strlen(text) + 1)) == NULL)
1602 return KSSL_CTX_ERR;
1604 strcpy(*string, text);
1610 /* Copy the Kerberos session key from a (krb5_keyblock *) to a kssl_ctx
1611 ** struct. Clear kssl_ctx->key if Kerberos session key is NULL.
1614 kssl_ctx_setkey(KSSL_CTX *kssl_ctx, krb5_keyblock *session)
1617 krb5_enctype enctype;
1618 krb5_octet FAR *contents = NULL;
1620 if (!kssl_ctx) return KSSL_CTX_ERR;
1624 memset(kssl_ctx->key, 0, kssl_ctx->length);
1625 free(kssl_ctx->key);
1632 length = session->keyvalue->length;
1633 enctype = session->keytype;
1634 contents = session->keyvalue->contents;
1636 length = session->length;
1637 enctype = session->enctype;
1638 contents = session->contents;
1640 kssl_ctx->enctype = enctype;
1641 kssl_ctx->length = length;
1645 kssl_ctx->enctype = ENCTYPE_UNKNOWN;
1646 kssl_ctx->length = 0;
1650 if ((kssl_ctx->key =
1651 (krb5_octet FAR *) calloc(1, kssl_ctx->length)) == NULL)
1653 kssl_ctx->length = 0;
1654 return KSSL_CTX_ERR;
1657 memcpy(kssl_ctx->key, contents, length);
1663 /* Display contents of kssl_ctx struct
1666 kssl_ctx_show(KSSL_CTX *kssl_ctx)
1670 printf("kssl_ctx: ");
1671 if (kssl_ctx == NULL)
1677 printf("%p\n", kssl_ctx);
1679 printf("\tservice:\t%s\n",
1680 (kssl_ctx->service_name)? kssl_ctx->service_name: "NULL");
1681 printf("\tclient:\t%s\n",
1682 (kssl_ctx->client_princ)? kssl_ctx->client_princ: "NULL");
1683 printf("\tserver:\t%s\n",
1684 (kssl_ctx->service_host)? kssl_ctx->service_host: "NULL");
1685 printf("\tkeytab:\t%s\n",
1686 (kssl_ctx->keytab_file)? kssl_ctx->keytab_file: "NULL");
1687 printf("\tkey [%d:%d]:\t",
1688 kssl_ctx->enctype, kssl_ctx->length);
1690 for (i=0; i < kssl_ctx->length && kssl_ctx->key; i++)
1692 printf("%02x", kssl_ctx->key[i]);
1699 kssl_keytab_is_available(KSSL_CTX *kssl_ctx)
1701 krb5_context krb5context = NULL;
1702 krb5_keytab krb5keytab = NULL;
1703 krb5_keytab_entry entry;
1704 krb5_principal princ = NULL;
1705 krb5_error_code krb5rc = KRB5KRB_ERR_GENERIC;
1708 if ((krb5rc = krb5_init_context(&krb5context)))
1711 /* kssl_ctx->keytab_file == NULL ==> use Kerberos default
1713 if (kssl_ctx->keytab_file)
1715 krb5rc = krb5_kt_resolve(krb5context, kssl_ctx->keytab_file,
1722 krb5rc = krb5_kt_default(krb5context,&krb5keytab);
1727 /* the host key we are looking for */
1728 krb5rc = krb5_sname_to_principal(krb5context, NULL,
1729 kssl_ctx->service_name ? kssl_ctx->service_name: KRB5SVC,
1730 KRB5_NT_SRV_HST, &princ);
1732 krb5rc = krb5_kt_get_entry(krb5context, krb5keytab,
1735 0 /* IGNORE_ENCTYPE */,
1737 if ( krb5rc == KRB5_KT_NOTFOUND ) {
1740 } else if ( krb5rc )
1743 krb5_kt_free_entry(krb5context, &entry);
1747 if (krb5keytab) krb5_kt_close(krb5context, krb5keytab);
1748 if (princ) krb5_free_principal(krb5context, princ);
1749 if (krb5context) krb5_free_context(krb5context);
1754 kssl_tgt_is_available(KSSL_CTX *kssl_ctx)
1756 krb5_error_code krb5rc = KRB5KRB_ERR_GENERIC;
1757 krb5_context krb5context = NULL;
1758 krb5_ccache krb5ccdef = NULL;
1759 krb5_creds krb5creds, *krb5credsp = NULL;
1762 memset((char *)&krb5creds, 0, sizeof(krb5creds));
1767 if (!kssl_ctx->service_host)
1770 if ((krb5rc = krb5_init_context(&krb5context)) != 0)
1773 if ((krb5rc = krb5_sname_to_principal(krb5context,
1774 kssl_ctx->service_host,
1775 (kssl_ctx->service_name)? kssl_ctx->service_name: KRB5SVC,
1776 KRB5_NT_SRV_HST, &krb5creds.server)) != 0)
1779 if ((krb5rc = krb5_cc_default(krb5context, &krb5ccdef)) != 0)
1782 if ((krb5rc = krb5_cc_get_principal(krb5context, krb5ccdef,
1783 &krb5creds.client)) != 0)
1786 if ((krb5rc = krb5_get_credentials(krb5context, 0, krb5ccdef,
1787 &krb5creds, &krb5credsp)) != 0)
1794 kssl_ctx_show(kssl_ctx);
1795 #endif /* KSSL_DEBUG */
1797 if (krb5creds.client) krb5_free_principal(krb5context, krb5creds.client);
1798 if (krb5creds.server) krb5_free_principal(krb5context, krb5creds.server);
1799 if (krb5context) krb5_free_context(krb5context);
1803 #if !defined(OPENSSL_SYS_WINDOWS) && !defined(OPENSSL_SYS_WIN32)
1804 void kssl_krb5_free_data_contents(krb5_context context, krb5_data *data)
1810 #elif defined(KRB5_MIT_OLD11)
1812 krb5_xfree(data->data);
1816 krb5_free_data_contents(NULL, data);
1819 #endif /* !OPENSSL_SYS_WINDOWS && !OPENSSL_SYS_WIN32 */
1822 /* Given pointers to KerberosTime and struct tm structs, convert the
1823 ** KerberosTime string to struct tm. Note that KerberosTime is a
1824 ** ASN1_GENERALIZEDTIME value, constrained to GMT with no fractional
1825 ** seconds as defined in RFC 1510.
1826 ** Return pointer to the (partially) filled in struct tm on success,
1827 ** return NULL on failure.
1829 struct tm *k_gmtime(ASN1_GENERALIZEDTIME *gtime, struct tm *k_tm)
1833 if (!k_tm) return NULL;
1834 if (gtime == NULL || gtime->length < 14) return NULL;
1835 if (gtime->data == NULL) return NULL;
1837 p = (char *)>ime->data[14];
1839 c = *p; *p = '\0'; p -= 2; k_tm->tm_sec = atoi(p); *(p+2) = c;
1840 c = *p; *p = '\0'; p -= 2; k_tm->tm_min = atoi(p); *(p+2) = c;
1841 c = *p; *p = '\0'; p -= 2; k_tm->tm_hour = atoi(p); *(p+2) = c;
1842 c = *p; *p = '\0'; p -= 2; k_tm->tm_mday = atoi(p); *(p+2) = c;
1843 c = *p; *p = '\0'; p -= 2; k_tm->tm_mon = atoi(p)-1; *(p+2) = c;
1844 c = *p; *p = '\0'; p -= 4; k_tm->tm_year = atoi(p)-1900; *(p+4) = c;
1850 /* Helper function for kssl_validate_times().
1851 ** We need context->clockskew, but krb5_context is an opaque struct.
1852 ** So we try to sneek the clockskew out through the replay cache.
1853 ** If that fails just return a likely default (300 seconds).
1855 krb5_deltat get_rc_clockskew(krb5_context context)
1858 krb5_deltat clockskew;
1860 if (krb5_rc_default(context, &rc)) return KSSL_CLOCKSKEW;
1861 if (krb5_rc_initialize(context, rc, 0)) return KSSL_CLOCKSKEW;
1862 if (krb5_rc_get_lifespan(context, rc, &clockskew)) {
1863 clockskew = KSSL_CLOCKSKEW;
1865 (void) krb5_rc_destroy(context, rc);
1870 /* kssl_validate_times() combines (and more importantly exposes)
1871 ** the MIT KRB5 internal function krb5_validate_times() and the
1872 ** in_clock_skew() macro. The authenticator client time is checked
1873 ** to be within clockskew secs of the current time and the current
1874 ** time is checked to be within the ticket start and expire times.
1875 ** Either check may be omitted by supplying a NULL value.
1876 ** Returns 0 for valid times, SSL_R_KRB5* error codes otherwise.
1877 ** See Also: (Kerberos source)/krb5/lib/krb5/krb/valid_times.c
1880 krb5_error_code kssl_validate_times( krb5_timestamp atime,
1881 krb5_ticket_times *ttimes)
1884 krb5_timestamp start, now;
1886 krb5_context context;
1888 if ((rc = krb5_init_context(&context))) return SSL_R_KRB5_S_BAD_TICKET;
1889 skew = get_rc_clockskew(context);
1890 if ((rc = krb5_timeofday(context,&now))) return SSL_R_KRB5_S_BAD_TICKET;
1891 krb5_free_context(context);
1893 if (atime && labs(atime - now) >= skew) return SSL_R_KRB5_S_TKT_SKEW;
1895 if (! ttimes) return 0;
1897 start = (ttimes->starttime != 0)? ttimes->starttime: ttimes->authtime;
1898 if (start - now > skew) return SSL_R_KRB5_S_TKT_NYV;
1899 if ((now - ttimes->endtime) > skew) return SSL_R_KRB5_S_TKT_EXPIRED;
1902 printf("kssl_validate_times: %d |<- | %d - %d | < %d ->| %d\n",
1903 start, atime, now, skew, ttimes->endtime);
1904 #endif /* KSSL_DEBUG */
1910 /* Decode and decrypt given DER-encoded authenticator, then pass
1911 ** authenticator ctime back in *atimep (or 0 if time unavailable).
1912 ** Returns krb5_error_code and kssl_err on error. A NULL
1913 ** authenticator (authentp->length == 0) is not considered an error.
1914 ** Note that kssl_check_authent() makes use of the KRB5 session key;
1915 ** you must call kssl_sget_tkt() to get the key before calling this routine.
1917 krb5_error_code kssl_check_authent(
1918 /* IN */ KSSL_CTX *kssl_ctx,
1919 /* IN */ krb5_data *authentp,
1920 /* OUT */ krb5_timestamp *atimep,
1921 /* OUT */ KSSL_ERR *kssl_err )
1923 krb5_error_code krb5rc = 0;
1924 KRB5_ENCDATA *dec_authent = NULL;
1925 KRB5_AUTHENTBODY *auth = NULL;
1926 krb5_enctype enctype;
1927 EVP_CIPHER_CTX ciph_ctx;
1928 EVP_CIPHER *enc = NULL;
1929 unsigned char iv[EVP_MAX_IV_LENGTH];
1930 unsigned char *p, *unenc_authent;
1931 int padl, outl, unencbufsize;
1932 struct tm tm_time, *tm_l, *tm_g;
1933 time_t now, tl, tg, tr, tz_offset;
1936 kssl_err_set(kssl_err, 0, "");
1938 #ifndef KRB5CHECKAUTH
1941 #if KRB5CHECKAUTH == 0
1944 #endif /* KRB5CHECKAUTH */
1946 if (authentp == NULL || authentp->length == 0) return 0;
1951 printf("kssl_check_authent: authenticator[%d]:\n",authentp->length);
1953 for (ui=0; ui < authentp->length; ui++) printf("%02x ",p[ui]);
1956 #endif /* KSSL_DEBUG */
1958 unencbufsize = 2 * authentp->length;
1959 if ((unenc_authent = calloc(1, unencbufsize)) == NULL)
1961 kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
1962 "Unable to allocate authenticator buffer.\n");
1963 krb5rc = KRB5KRB_ERR_GENERIC;
1967 p = (unsigned char *)authentp->data;
1968 if ((dec_authent = d2i_KRB5_ENCDATA(NULL, &p,
1969 (long) authentp->length)) == NULL)
1971 kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
1972 "Error decoding authenticator.\n");
1973 krb5rc = KRB5KRB_AP_ERR_BAD_INTEGRITY;
1977 enctype = dec_authent->etype->data[0]; /* should = kssl_ctx->enctype */
1978 #if !defined(KRB5_MIT_OLD11)
1979 switch ( enctype ) {
1980 case ENCTYPE_DES3_CBC_SHA1: /* EVP_des_ede3_cbc(); */
1981 case ENCTYPE_DES3_CBC_SHA:
1982 case ENCTYPE_DES3_CBC_RAW:
1983 krb5rc = 0; /* Skip, can't handle derived keys */
1987 enc = kssl_map_enc(enctype);
1988 memset(iv, 0, EVP_MAX_IV_LENGTH); /* per RFC 1510 */
1992 /* Disable kssl_check_authent for ENCTYPE_DES3_CBC_SHA1.
1993 ** This enctype indicates the authenticator was encrypted
1994 ** using key-usage derived keys which openssl cannot decrypt.
1998 if (!EVP_DecryptInit(&ciph_ctx, enc, kssl_ctx->key, iv))
2000 kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
2001 "EVP_DecryptInit error decrypting authenticator.\n");
2002 krb5rc = KRB5KRB_AP_ERR_BAD_INTEGRITY;
2005 if (!EVP_DecryptUpdate(&ciph_ctx, unenc_authent, &outl,
2006 dec_authent->cipher->data, dec_authent->cipher->length))
2008 kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
2009 "EVP_DecryptUpdate error decrypting authenticator.\n");
2010 krb5rc = KRB5KRB_AP_ERR_BAD_INTEGRITY;
2013 if (outl > unencbufsize)
2015 kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
2016 "Buffer overflow decrypting authenticator.\n");
2017 krb5rc = KRB5KRB_AP_ERR_BAD_INTEGRITY;
2020 if (!EVP_DecryptFinal(&ciph_ctx, &(unenc_authent[outl]), &padl))
2022 kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
2023 "EVP_DecryptFinal error decrypting authenticator.\n");
2024 krb5rc = KRB5KRB_AP_ERR_BAD_INTEGRITY;
2028 if (outl > unencbufsize)
2030 kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
2031 "Buffer overflow decrypting authenticator.\n");
2032 krb5rc = KRB5KRB_AP_ERR_BAD_INTEGRITY;
2035 EVP_CIPHER_CTX_cleanup(&ciph_ctx);
2038 printf("kssl_check_authent: decrypted authenticator[%d] =\n", outl);
2039 for (padl=0; padl < outl; padl++) printf("%02x ",unenc_authent[padl]);
2041 #endif /* KSSL_DEBUG */
2043 if ((p = kssl_skip_confound(enctype, unenc_authent)) == NULL)
2045 kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
2046 "confounded by authenticator.\n");
2047 krb5rc = KRB5KRB_AP_ERR_BAD_INTEGRITY;
2050 outl -= p - unenc_authent;
2052 if ((auth = (KRB5_AUTHENTBODY *) d2i_KRB5_AUTHENT(NULL, &p,
2053 (long) outl))==NULL)
2055 kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
2056 "Error decoding authenticator body.\n");
2057 krb5rc = KRB5KRB_AP_ERR_BAD_INTEGRITY;
2061 memset(&tm_time,0,sizeof(struct tm));
2062 if (k_gmtime(auth->ctime, &tm_time) &&
2063 ((tr = mktime(&tm_time)) != (time_t)(-1)))
2066 tm_l = localtime(&now); tl = mktime(tm_l);
2067 tm_g = gmtime(&now); tg = mktime(tm_g);
2068 tz_offset = tg - tl;
2070 *atimep = tr - tz_offset;
2074 printf("kssl_check_authent: returns %d for client time ", *atimep);
2075 if (auth && auth->ctime && auth->ctime->length && auth->ctime->data)
2076 printf("%.*s\n", auth->ctime->length, auth->ctime->data);
2077 else printf("NULL\n");
2078 #endif /* KSSL_DEBUG */
2081 if (auth) KRB5_AUTHENT_free((KRB5_AUTHENT *) auth);
2082 if (dec_authent) KRB5_ENCDATA_free(dec_authent);
2083 if (unenc_authent) free(unenc_authent);
2088 /* Replaces krb5_build_principal_ext(), with varargs length == 2 (svc, host),
2089 ** because I dont't know how to stub varargs.
2090 ** Returns krb5_error_code == ENOMEM on alloc error, otherwise
2091 ** passes back newly constructed principal, which should be freed by caller.
2093 krb5_error_code kssl_build_principal_2(
2094 /* UPDATE */ krb5_context context,
2095 /* OUT */ krb5_principal *princ,
2096 /* IN */ int rlen, const char *realm,
2097 /* IN */ int slen, const char *svc,
2098 /* IN */ int hlen, const char *host)
2100 krb5_data *p_data = NULL;
2101 krb5_principal new_p = NULL;
2104 if ((p_data = (krb5_data *) calloc(2, sizeof(krb5_data))) == NULL ||
2105 (new_p = (krb5_principal) calloc(1, sizeof(krb5_principal_data)))
2108 new_p->data = p_data;
2110 if ((new_r = calloc(1, rlen + 1)) == NULL) goto err;
2111 memcpy(new_r, realm, rlen);
2112 krb5_princ_set_realm_length(context, new_p, rlen);
2113 krb5_princ_set_realm_data(context, new_p, new_r);
2115 if ((new_p->data[0].data = calloc(1, slen + 1)) == NULL) goto err;
2116 memcpy(new_p->data[0].data, svc, slen);
2117 new_p->data[0].length = slen;
2119 if ((new_p->data[1].data = calloc(1, hlen + 1)) == NULL) goto err;
2120 memcpy(new_p->data[1].data, host, hlen);
2121 new_p->data[1].length = hlen;
2123 krb5_princ_type(context, new_p) = KRB5_NT_UNKNOWN;
2128 if (new_p && new_p[0].data) free(new_p[0].data);
2129 if (new_p && new_p[1].data) free(new_p[1].data);
2130 if (new_p) free(new_p);
2131 if (new_r) free(new_r);
2136 #else /* !OPENSSL_NO_KRB5 */
2138 #if defined(PEDANTIC) || defined(OPENSSL_SYS_VMS)
2139 static int dummy=(int)&dummy;
2142 #endif /* !OPENSSL_NO_KRB5 */