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 fprintf(stderr
, "%s[at%d:%d] ", label
, adata
->ad_type
, adata
->length
);
969 for (i
= 0; i
< adata
->length
; i
++) {
970 fprintf(stderr
, (isprint(adata
->contents
[i
])) ? "%c " : "%02x",
973 fprintf(stderr
, "\n");
979 * Display contents of krb5_keyblock struct, for debugging
981 void print_krb5_keyblock(char *label
, krb5_keyblock
*keyblk
)
985 if (keyblk
== NULL
) {
986 fprintf(stderr
, "%s, keyblk==0\n", label
);
990 fprintf(stderr
, "%s\n\t[et%d:%d]: ", label
, keyblk
->keytype
,
991 keyblk
->keyvalue
->length
);
992 for (i
= 0; i
< (int)keyblk
->keyvalue
->length
; i
++) {
993 fprintf(stderr
, "%02x",
994 (unsigned char *)(keyblk
->keyvalue
->contents
)[i
]);
996 fprintf(stderr
, "\n");
998 fprintf(stderr
, "%s\n\t[et%d:%d]: ", label
, keyblk
->enctype
,
1000 for (i
= 0; i
< (int)keyblk
->length
; i
++) {
1001 fprintf(stderr
, "%02x", keyblk
->contents
[i
]);
1003 fprintf(stderr
, "\n");
1008 * Display contents of krb5_principal_data struct, for debugging
1009 * (krb5_principal is typedef'd == krb5_principal_data *)
1011 static void print_krb5_princ(char *label
, krb5_principal_data
*princ
)
1015 fprintf(stderr
, "%s principal Realm: ", label
);
1018 for (ui
= 0; ui
< (int)princ
->realm
.length
; ui
++)
1019 putchar(princ
->realm
.data
[ui
]);
1020 fprintf(stderr
, " (nametype %d) has %d strings:\n", princ
->type
,
1022 for (i
= 0; i
< (int)princ
->length
; i
++) {
1023 fprintf(stderr
, "\t%d [%d]: ", i
, princ
->data
[i
].length
);
1024 for (uj
= 0; uj
< (int)princ
->data
[i
].length
; uj
++) {
1025 putchar(princ
->data
[i
].data
[uj
]);
1027 fprintf(stderr
, "\n");
1032 /*- Given krb5 service (typically "kssl") and hostname in kssl_ctx,
1033 * Return encrypted Kerberos ticket for service @ hostname.
1034 * If authenp is non-NULL, also return encrypted authenticator,
1035 * whose data should be freed by caller.
1036 * (Originally was: Create Kerberos AP_REQ message for SSL Client.)
1038 * 19990628 VRS Started; Returns Kerberos AP_REQ message.
1039 * 20010409 VRS Modified for RFC2712; Returns enc tkt.
1040 * 20010606 VRS May also return optional authenticator.
1042 krb5_error_code
kssl_cget_tkt( /* UPDATE */ KSSL_CTX
*kssl_ctx
,
1045 */ krb5_data
**enc_ticketp
,
1048 */ krb5_data
*authenp
,
1051 */ KSSL_ERR
*kssl_err
)
1053 krb5_error_code krb5rc
= KRB5KRB_ERR_GENERIC
;
1054 krb5_context krb5context
= NULL
;
1055 krb5_auth_context krb5auth_context
= NULL
;
1056 krb5_ccache krb5ccdef
= NULL
;
1057 krb5_creds krb5creds
, *krb5credsp
= NULL
;
1058 krb5_data krb5_app_req
;
1060 kssl_err_set(kssl_err
, 0, "");
1061 memset((char *)&krb5creds
, 0, sizeof(krb5creds
));
1064 kssl_err_set(kssl_err
, SSL_R_KRB5_S_INIT
, "No kssl_ctx defined.\n");
1066 } else if (!kssl_ctx
->service_host
) {
1067 kssl_err_set(kssl_err
, SSL_R_KRB5_S_INIT
,
1068 "kssl_ctx service_host undefined.\n");
1072 if ((krb5rc
= krb5_init_context(&krb5context
)) != 0) {
1073 BIO_snprintf(kssl_err
->text
, KSSL_ERR_MAX
,
1074 "krb5_init_context() fails: %d\n", krb5rc
);
1075 kssl_err
->reason
= SSL_R_KRB5_C_INIT
;
1079 if ((krb5rc
= krb5_sname_to_principal(krb5context
,
1080 kssl_ctx
->service_host
,
1081 (kssl_ctx
->service_name
) ?
1082 kssl_ctx
->service_name
: KRB5SVC
,
1084 &krb5creds
.server
)) != 0) {
1085 BIO_snprintf(kssl_err
->text
, KSSL_ERR_MAX
,
1086 "krb5_sname_to_principal() fails for %s/%s\n",
1087 kssl_ctx
->service_host
,
1089 service_name
) ? kssl_ctx
->service_name
: KRB5SVC
);
1090 kssl_err
->reason
= SSL_R_KRB5_C_INIT
;
1094 if ((krb5rc
= krb5_cc_default(krb5context
, &krb5ccdef
)) != 0) {
1095 kssl_err_set(kssl_err
, SSL_R_KRB5_C_CC_PRINC
,
1096 "krb5_cc_default fails.\n");
1100 if ((krb5rc
= krb5_cc_get_principal(krb5context
, krb5ccdef
,
1101 &krb5creds
.client
)) != 0) {
1102 kssl_err_set(kssl_err
, SSL_R_KRB5_C_CC_PRINC
,
1103 "krb5_cc_get_principal() fails.\n");
1107 if ((krb5rc
= krb5_get_credentials(krb5context
, 0, krb5ccdef
,
1108 &krb5creds
, &krb5credsp
)) != 0) {
1109 kssl_err_set(kssl_err
, SSL_R_KRB5_C_GET_CRED
,
1110 "krb5_get_credentials() fails.\n");
1114 *enc_ticketp
= &krb5credsp
->ticket
;
1115 # ifdef KRB5_HEIMDAL
1116 kssl_ctx
->enctype
= krb5credsp
->session
.keytype
;
1118 kssl_ctx
->enctype
= krb5credsp
->keyblock
.enctype
;
1121 krb5rc
= KRB5KRB_ERR_GENERIC
;
1122 /* caller should free data of krb5_app_req */
1124 * 20010406 VRS deleted for real KerberosWrapper 20010605 VRS reinstated
1125 * to offer Authenticator to KerberosWrapper
1127 krb5_app_req
.length
= 0;
1129 krb5_data krb5in_data
;
1130 const unsigned char *p
;
1132 KRB5_APREQBODY
*ap_req
;
1134 authenp
->length
= 0;
1135 krb5in_data
.data
= NULL
;
1136 krb5in_data
.length
= 0;
1137 if ((krb5rc
= krb5_mk_req_extended(krb5context
,
1138 &krb5auth_context
, 0, &krb5in_data
,
1139 krb5credsp
, &krb5_app_req
)) != 0) {
1140 kssl_err_set(kssl_err
, SSL_R_KRB5_C_MK_REQ
,
1141 "krb5_mk_req_extended() fails.\n");
1145 arlen
= krb5_app_req
.length
;
1146 p
= (unsigned char *)krb5_app_req
.data
;
1147 ap_req
= (KRB5_APREQBODY
*)d2i_KRB5_APREQ(NULL
, &p
, arlen
);
1149 authenp
->length
= i2d_KRB5_ENCDATA(ap_req
->authenticator
, NULL
);
1150 if (authenp
->length
&& (authenp
->data
= malloc(authenp
->length
))) {
1151 unsigned char *adp
= (unsigned char *)authenp
->data
;
1153 i2d_KRB5_ENCDATA(ap_req
->authenticator
, &adp
);
1158 KRB5_APREQ_free((KRB5_APREQ
*) ap_req
);
1159 if (krb5_app_req
.length
)
1160 kssl_krb5_free_data_contents(krb5context
, &krb5_app_req
);
1162 # ifdef KRB5_HEIMDAL
1163 if (kssl_ctx_setkey(kssl_ctx
, &krb5credsp
->session
)) {
1164 kssl_err_set(kssl_err
, SSL_R_KRB5_C_INIT
,
1165 "kssl_ctx_setkey() fails.\n");
1168 if (kssl_ctx_setkey(kssl_ctx
, &krb5credsp
->keyblock
)) {
1169 kssl_err_set(kssl_err
, SSL_R_KRB5_C_INIT
,
1170 "kssl_ctx_setkey() fails.\n");
1178 kssl_ctx_show(kssl_ctx
);
1179 # endif /* KSSL_DEBUG */
1181 if (krb5creds
.client
)
1182 krb5_free_principal(krb5context
, krb5creds
.client
);
1183 if (krb5creds
.server
)
1184 krb5_free_principal(krb5context
, krb5creds
.server
);
1185 if (krb5auth_context
)
1186 krb5_auth_con_free(krb5context
, krb5auth_context
);
1188 krb5_free_context(krb5context
);
1193 * Given d2i_-decoded asn1ticket, allocate and return a new krb5_ticket.
1194 * Return Kerberos error code and kssl_err struct on error.
1195 * Allocates krb5_ticket and krb5_principal; caller should free these.
1197 * 20010410 VRS Implemented krb5_decode_ticket() as
1198 * old_krb5_decode_ticket(). Missing from MIT1.0.6.
1199 * 20010615 VRS Re-cast as openssl/asn1 d2i_*() functions.
1200 * Re-used some of the old krb5_decode_ticket()
1201 * code here. This tkt should alloc/free just
1202 * like the real thing.
1204 static krb5_error_code
kssl_TKT2tkt( /* IN */ krb5_context krb5context
,
1207 */ KRB5_TKTBODY
*asn1ticket
,
1210 */ krb5_ticket
**krb5ticket
,
1213 */ KSSL_ERR
*kssl_err
)
1215 krb5_error_code krb5rc
= KRB5KRB_ERR_GENERIC
;
1216 krb5_ticket
*new5ticket
= NULL
;
1217 ASN1_GENERALSTRING
*gstr_svc
, *gstr_host
;
1221 if (asn1ticket
== NULL
|| asn1ticket
->realm
== NULL
||
1222 asn1ticket
->sname
== NULL
||
1223 sk_ASN1_GENERALSTRING_num(asn1ticket
->sname
->namestring
) < 2) {
1224 BIO_snprintf(kssl_err
->text
, KSSL_ERR_MAX
,
1225 "Null field in asn1ticket.\n");
1226 kssl_err
->reason
= SSL_R_KRB5_S_RD_REQ
;
1227 return KRB5KRB_ERR_GENERIC
;
1230 if ((new5ticket
= (krb5_ticket
*)calloc(1, sizeof(krb5_ticket
))) == NULL
) {
1231 BIO_snprintf(kssl_err
->text
, KSSL_ERR_MAX
,
1232 "Unable to allocate new krb5_ticket.\n");
1233 kssl_err
->reason
= SSL_R_KRB5_S_RD_REQ
;
1234 return ENOMEM
; /* or KRB5KRB_ERR_GENERIC; */
1237 gstr_svc
= sk_ASN1_GENERALSTRING_value(asn1ticket
->sname
->namestring
, 0);
1238 gstr_host
= sk_ASN1_GENERALSTRING_value(asn1ticket
->sname
->namestring
, 1);
1240 if ((krb5rc
= kssl_build_principal_2(krb5context
,
1241 &new5ticket
->server
,
1242 asn1ticket
->realm
->length
,
1243 (char *)asn1ticket
->realm
->data
,
1245 (char *)gstr_svc
->data
,
1247 (char *)gstr_host
->data
)) != 0) {
1249 BIO_snprintf(kssl_err
->text
, KSSL_ERR_MAX
,
1250 "Error building ticket server principal.\n");
1251 kssl_err
->reason
= SSL_R_KRB5_S_RD_REQ
;
1252 return krb5rc
; /* or KRB5KRB_ERR_GENERIC; */
1255 krb5_princ_type(krb5context
, new5ticket
->server
) =
1256 asn1ticket
->sname
->nametype
->data
[0];
1257 new5ticket
->enc_part
.enctype
= asn1ticket
->encdata
->etype
->data
[0];
1258 new5ticket
->enc_part
.kvno
= asn1ticket
->encdata
->kvno
->data
[0];
1259 new5ticket
->enc_part
.ciphertext
.length
=
1260 asn1ticket
->encdata
->cipher
->length
;
1261 if ((new5ticket
->enc_part
.ciphertext
.data
=
1262 calloc(1, asn1ticket
->encdata
->cipher
->length
)) == NULL
) {
1264 BIO_snprintf(kssl_err
->text
, KSSL_ERR_MAX
,
1265 "Error allocating cipher in krb5ticket.\n");
1266 kssl_err
->reason
= SSL_R_KRB5_S_RD_REQ
;
1267 return KRB5KRB_ERR_GENERIC
;
1269 memcpy(new5ticket
->enc_part
.ciphertext
.data
,
1270 asn1ticket
->encdata
->cipher
->data
,
1271 asn1ticket
->encdata
->cipher
->length
);
1274 *krb5ticket
= new5ticket
;
1279 * Given krb5 service name in KSSL_CTX *kssl_ctx (typically "kssl"),
1280 * and krb5 AP_REQ message & message length,
1281 * Return Kerberos session key and client principle
1282 * to SSL Server in KSSL_CTX *kssl_ctx.
1284 * 19990702 VRS Started.
1286 krb5_error_code
kssl_sget_tkt( /* UPDATE */ KSSL_CTX
*kssl_ctx
,
1289 */ krb5_data
*indata
,
1292 */ krb5_ticket_times
*ttimes
,
1295 */ KSSL_ERR
*kssl_err
)
1297 krb5_error_code krb5rc
= KRB5KRB_ERR_GENERIC
;
1298 static krb5_context krb5context
= NULL
;
1299 static krb5_auth_context krb5auth_context
= NULL
;
1300 krb5_ticket
*krb5ticket
= NULL
;
1301 KRB5_TKTBODY
*asn1ticket
= NULL
;
1302 const unsigned char *p
;
1303 krb5_keytab krb5keytab
= NULL
;
1304 krb5_keytab_entry kt_entry
;
1305 krb5_principal krb5server
;
1306 krb5_rcache rcache
= NULL
;
1308 kssl_err_set(kssl_err
, 0, "");
1311 kssl_err_set(kssl_err
, SSL_R_KRB5_S_INIT
, "No kssl_ctx defined.\n");
1315 fprintf(stderr
, "in kssl_sget_tkt(%s)\n",
1316 kstring(kssl_ctx
->service_name
));
1317 # endif /* KSSL_DEBUG */
1319 if (!krb5context
&& (krb5rc
= krb5_init_context(&krb5context
))) {
1320 kssl_err_set(kssl_err
, SSL_R_KRB5_S_INIT
,
1321 "krb5_init_context() fails.\n");
1324 if (krb5auth_context
&&
1325 (krb5rc
= krb5_auth_con_free(krb5context
, krb5auth_context
))) {
1326 kssl_err_set(kssl_err
, SSL_R_KRB5_S_INIT
,
1327 "krb5_auth_con_free() fails.\n");
1330 krb5auth_context
= NULL
;
1331 if (!krb5auth_context
&&
1332 (krb5rc
= krb5_auth_con_init(krb5context
, &krb5auth_context
))) {
1333 kssl_err_set(kssl_err
, SSL_R_KRB5_S_INIT
,
1334 "krb5_auth_con_init() fails.\n");
1338 if ((krb5rc
= krb5_auth_con_getrcache(krb5context
, krb5auth_context
,
1340 kssl_err_set(kssl_err
, SSL_R_KRB5_S_INIT
,
1341 "krb5_auth_con_getrcache() fails.\n");
1345 if ((krb5rc
= krb5_sname_to_principal(krb5context
, NULL
,
1346 (kssl_ctx
->service_name
) ?
1347 kssl_ctx
->service_name
: KRB5SVC
,
1349 &krb5server
)) != 0) {
1350 kssl_err_set(kssl_err
, SSL_R_KRB5_S_INIT
,
1351 "krb5_sname_to_principal() fails.\n");
1355 if (rcache
== NULL
) {
1356 if ((krb5rc
= krb5_get_server_rcache(krb5context
,
1357 krb5_princ_component(krb5context
,
1361 kssl_err_set(kssl_err
, SSL_R_KRB5_S_INIT
,
1362 "krb5_get_server_rcache() fails.\n");
1368 krb5_auth_con_setrcache(krb5context
, krb5auth_context
, rcache
))) {
1369 kssl_err_set(kssl_err
, SSL_R_KRB5_S_INIT
,
1370 "krb5_auth_con_setrcache() fails.\n");
1375 * kssl_ctx->keytab_file == NULL ==> use Kerberos default
1377 if (kssl_ctx
->keytab_file
) {
1378 krb5rc
= krb5_kt_resolve(krb5context
, kssl_ctx
->keytab_file
,
1381 kssl_err_set(kssl_err
, SSL_R_KRB5_S_INIT
,
1382 "krb5_kt_resolve() fails.\n");
1386 krb5rc
= krb5_kt_default(krb5context
, &krb5keytab
);
1388 kssl_err_set(kssl_err
, SSL_R_KRB5_S_INIT
,
1389 "krb5_kt_default() fails.\n");
1394 /*- Actual Kerberos5 krb5_recvauth() has initial conversation here
1395 * o check KRB5_SENDAUTH_BADAUTHVERS
1396 * unless KRB5_RECVAUTH_SKIP_VERSION
1397 * o check KRB5_SENDAUTH_BADAPPLVERS
1398 * o send "0" msg if all OK
1402 * 20010411 was using AP_REQ instead of true KerberosWrapper
1404 * if ((krb5rc = krb5_rd_req(krb5context, &krb5auth_context,
1405 * &krb5in_data, krb5server, krb5keytab,
1406 * &ap_option, &krb5ticket)) != 0) { Error }
1409 p
= (unsigned char *)indata
->data
;
1410 if ((asn1ticket
= (KRB5_TKTBODY
*)d2i_KRB5_TICKET(NULL
, &p
,
1411 (long)indata
->length
))
1413 BIO_snprintf(kssl_err
->text
, KSSL_ERR_MAX
,
1414 "d2i_KRB5_TICKET() ASN.1 decode failure.\n");
1415 kssl_err
->reason
= SSL_R_KRB5_S_RD_REQ
;
1420 * Was: krb5rc = krb5_decode_ticket(krb5in_data,&krb5ticket)) != 0)
1422 if ((krb5rc
= kssl_TKT2tkt(krb5context
, asn1ticket
, &krb5ticket
,
1424 BIO_snprintf(kssl_err
->text
, KSSL_ERR_MAX
,
1425 "Error converting ASN.1 ticket to krb5_ticket.\n");
1426 kssl_err
->reason
= SSL_R_KRB5_S_RD_REQ
;
1430 if (!krb5_principal_compare(krb5context
, krb5server
, krb5ticket
->server
)) {
1431 krb5rc
= KRB5_PRINC_NOMATCH
;
1432 BIO_snprintf(kssl_err
->text
, KSSL_ERR_MAX
,
1433 "server principal != ticket principal\n");
1434 kssl_err
->reason
= SSL_R_KRB5_S_RD_REQ
;
1437 if ((krb5rc
= krb5_kt_get_entry(krb5context
, krb5keytab
,
1439 krb5ticket
->enc_part
.kvno
,
1440 krb5ticket
->enc_part
.enctype
,
1442 BIO_snprintf(kssl_err
->text
, KSSL_ERR_MAX
,
1443 "krb5_kt_get_entry() fails with %x.\n", krb5rc
);
1444 kssl_err
->reason
= SSL_R_KRB5_S_RD_REQ
;
1447 if ((krb5rc
= krb5_decrypt_tkt_part(krb5context
, &kt_entry
.key
,
1448 krb5ticket
)) != 0) {
1449 BIO_snprintf(kssl_err
->text
, KSSL_ERR_MAX
,
1450 "krb5_decrypt_tkt_part() failed.\n");
1451 kssl_err
->reason
= SSL_R_KRB5_S_RD_REQ
;
1454 krb5_kt_free_entry(krb5context
, &kt_entry
);
1458 krb5_address
**paddr
= krb5ticket
->enc_part2
->caddrs
;
1459 fprintf(stderr
, "Decrypted ticket fields:\n");
1460 fprintf(stderr
, "\tflags: %X, transit-type: %X",
1461 krb5ticket
->enc_part2
->flags
,
1462 krb5ticket
->enc_part2
->transited
.tr_type
);
1463 print_krb5_data("\ttransit-data: ",
1464 &(krb5ticket
->enc_part2
->transited
.tr_contents
));
1465 fprintf(stderr
, "\tcaddrs: %p, authdata: %p\n",
1466 krb5ticket
->enc_part2
->caddrs
,
1467 krb5ticket
->enc_part2
->authorization_data
);
1469 fprintf(stderr
, "\tcaddrs:\n");
1470 for (i
= 0; paddr
[i
] != NULL
; i
++) {
1472 d
.length
= paddr
[i
]->length
;
1473 d
.data
= paddr
[i
]->contents
;
1474 print_krb5_data("\t\tIP: ", &d
);
1477 fprintf(stderr
, "\tstart/auth/end times: %d / %d / %d\n",
1478 krb5ticket
->enc_part2
->times
.starttime
,
1479 krb5ticket
->enc_part2
->times
.authtime
,
1480 krb5ticket
->enc_part2
->times
.endtime
);
1482 # endif /* KSSL_DEBUG */
1485 krb5rc
= KRB5_NO_TKT_SUPPLIED
;
1486 if (!krb5ticket
|| !krb5ticket
->enc_part2
||
1487 !krb5ticket
->enc_part2
->client
||
1488 !krb5ticket
->enc_part2
->client
->data
||
1489 !krb5ticket
->enc_part2
->session
) {
1490 kssl_err_set(kssl_err
, SSL_R_KRB5_S_BAD_TICKET
,
1491 "bad ticket from krb5_rd_req.\n");
1492 } else if (kssl_ctx_setprinc(kssl_ctx
, KSSL_CLIENT
,
1493 &krb5ticket
->enc_part2
->client
->realm
,
1494 krb5ticket
->enc_part2
->client
->data
,
1495 krb5ticket
->enc_part2
->client
->length
)) {
1496 kssl_err_set(kssl_err
, SSL_R_KRB5_S_BAD_TICKET
,
1497 "kssl_ctx_setprinc() fails.\n");
1498 } else if (kssl_ctx_setkey(kssl_ctx
, krb5ticket
->enc_part2
->session
)) {
1499 kssl_err_set(kssl_err
, SSL_R_KRB5_S_BAD_TICKET
,
1500 "kssl_ctx_setkey() fails.\n");
1501 } else if (krb5ticket
->enc_part2
->flags
& TKT_FLG_INVALID
) {
1502 krb5rc
= KRB5KRB_AP_ERR_TKT_INVALID
;
1503 kssl_err_set(kssl_err
, SSL_R_KRB5_S_BAD_TICKET
,
1504 "invalid ticket from krb5_rd_req.\n");
1508 kssl_ctx
->enctype
= krb5ticket
->enc_part
.enctype
;
1509 ttimes
->authtime
= krb5ticket
->enc_part2
->times
.authtime
;
1510 ttimes
->starttime
= krb5ticket
->enc_part2
->times
.starttime
;
1511 ttimes
->endtime
= krb5ticket
->enc_part2
->times
.endtime
;
1512 ttimes
->renew_till
= krb5ticket
->enc_part2
->times
.renew_till
;
1516 kssl_ctx_show(kssl_ctx
);
1517 # endif /* KSSL_DEBUG */
1520 KRB5_TICKET_free((KRB5_TICKET
*) asn1ticket
);
1522 krb5_kt_close(krb5context
, krb5keytab
);
1524 krb5_free_ticket(krb5context
, krb5ticket
);
1526 krb5_free_principal(krb5context
, krb5server
);
1531 * Allocate & return a new kssl_ctx struct.
1533 KSSL_CTX
*kssl_ctx_new(void)
1535 return ((KSSL_CTX
*)kssl_calloc(1, sizeof(KSSL_CTX
)));
1539 * Frees a kssl_ctx struct and any allocated memory it holds. Returns NULL.
1541 KSSL_CTX
*kssl_ctx_free(KSSL_CTX
*kssl_ctx
)
1543 if (kssl_ctx
== NULL
)
1547 OPENSSL_cleanse(kssl_ctx
->key
, kssl_ctx
->length
);
1549 kssl_free(kssl_ctx
->key
);
1550 if (kssl_ctx
->client_princ
)
1551 kssl_free(kssl_ctx
->client_princ
);
1552 if (kssl_ctx
->service_host
)
1553 kssl_free(kssl_ctx
->service_host
);
1554 if (kssl_ctx
->service_name
)
1555 kssl_free(kssl_ctx
->service_name
);
1556 if (kssl_ctx
->keytab_file
)
1557 kssl_free(kssl_ctx
->keytab_file
);
1559 kssl_free(kssl_ctx
);
1560 return (KSSL_CTX
*)NULL
;
1564 * Given an array of (krb5_data *) entity (and optional realm), set the plain
1565 * (char *) client_princ or service_host member of the kssl_ctx struct.
1568 kssl_ctx_setprinc(KSSL_CTX
*kssl_ctx
, int which
,
1569 krb5_data
*realm
, krb5_data
*entity
, int nentities
)
1575 if (kssl_ctx
== NULL
|| entity
== NULL
)
1576 return KSSL_CTX_ERR
;
1580 princ
= &kssl_ctx
->client_princ
;
1583 princ
= &kssl_ctx
->service_host
;
1586 return KSSL_CTX_ERR
;
1592 /* Add up all the entity->lengths */
1594 for (i
= 0; i
< nentities
; i
++) {
1595 length
+= entity
[i
].length
;
1597 /* Add in space for the '/' character(s) (if any) */
1598 length
+= nentities
- 1;
1599 /* Space for the ('@'+realm+NULL | NULL) */
1600 length
+= ((realm
) ? realm
->length
+ 2 : 1);
1602 if ((*princ
= kssl_calloc(1, length
)) == NULL
)
1603 return KSSL_CTX_ERR
;
1605 for (i
= 0; i
< nentities
; i
++) {
1606 strncat(*princ
, entity
[i
].data
, entity
[i
].length
);
1607 if (i
< nentities
- 1) {
1608 strcat(*princ
, "/");
1612 strcat(*princ
, "@");
1613 (void)strncat(*princ
, realm
->data
, realm
->length
);
1620 /*- Set one of the plain (char *) string members of the kssl_ctx struct.
1621 * Default values should be:
1622 * which == KSSL_SERVICE => "khost" (KRB5SVC)
1623 * which == KSSL_KEYTAB => "/etc/krb5.keytab" (KRB5KEYTAB)
1625 krb5_error_code
kssl_ctx_setstring(KSSL_CTX
*kssl_ctx
, int which
, char *text
)
1630 return KSSL_CTX_ERR
;
1634 string
= &kssl_ctx
->service_name
;
1637 string
= &kssl_ctx
->service_host
;
1640 string
= &kssl_ctx
->client_princ
;
1643 string
= &kssl_ctx
->keytab_file
;
1646 return KSSL_CTX_ERR
;
1657 if ((*string
= kssl_calloc(1, strlen(text
) + 1)) == NULL
)
1658 return KSSL_CTX_ERR
;
1660 strcpy(*string
, text
);
1666 * Copy the Kerberos session key from a (krb5_keyblock *) to a kssl_ctx
1667 * struct. Clear kssl_ctx->key if Kerberos session key is NULL.
1669 krb5_error_code
kssl_ctx_setkey(KSSL_CTX
*kssl_ctx
, krb5_keyblock
*session
)
1672 krb5_enctype enctype
;
1673 krb5_octet FAR
*contents
= NULL
;
1676 return KSSL_CTX_ERR
;
1678 if (kssl_ctx
->key
) {
1679 OPENSSL_cleanse(kssl_ctx
->key
, kssl_ctx
->length
);
1680 kssl_free(kssl_ctx
->key
);
1685 # ifdef KRB5_HEIMDAL
1686 length
= session
->keyvalue
->length
;
1687 enctype
= session
->keytype
;
1688 contents
= session
->keyvalue
->contents
;
1690 length
= session
->length
;
1691 enctype
= session
->enctype
;
1692 contents
= session
->contents
;
1694 kssl_ctx
->enctype
= enctype
;
1695 kssl_ctx
->length
= length
;
1697 kssl_ctx
->enctype
= ENCTYPE_UNKNOWN
;
1698 kssl_ctx
->length
= 0;
1702 if ((kssl_ctx
->key
=
1703 (krb5_octet FAR
*)kssl_calloc(1, kssl_ctx
->length
)) == NULL
) {
1704 kssl_ctx
->length
= 0;
1705 return KSSL_CTX_ERR
;
1707 memcpy(kssl_ctx
->key
, contents
, length
);
1713 * Display contents of kssl_ctx struct
1715 void kssl_ctx_show(KSSL_CTX
*kssl_ctx
)
1719 printf("kssl_ctx: ");
1720 if (kssl_ctx
== NULL
) {
1724 printf("%p\n", (void *)kssl_ctx
);
1726 printf("\tservice:\t%s\n",
1727 (kssl_ctx
->service_name
) ? kssl_ctx
->service_name
: "NULL");
1728 printf("\tclient:\t%s\n",
1729 (kssl_ctx
->client_princ
) ? kssl_ctx
->client_princ
: "NULL");
1730 printf("\tserver:\t%s\n",
1731 (kssl_ctx
->service_host
) ? kssl_ctx
->service_host
: "NULL");
1732 printf("\tkeytab:\t%s\n",
1733 (kssl_ctx
->keytab_file
) ? kssl_ctx
->keytab_file
: "NULL");
1734 printf("\tkey [%d:%d]:\t", kssl_ctx
->enctype
, kssl_ctx
->length
);
1736 for (i
= 0; i
< kssl_ctx
->length
&& kssl_ctx
->key
; i
++) {
1737 printf("%02x", kssl_ctx
->key
[i
]);
1743 int kssl_keytab_is_available(KSSL_CTX
*kssl_ctx
)
1745 krb5_context krb5context
= NULL
;
1746 krb5_keytab krb5keytab
= NULL
;
1747 krb5_keytab_entry entry
;
1748 krb5_principal princ
= NULL
;
1749 krb5_error_code krb5rc
= KRB5KRB_ERR_GENERIC
;
1752 if ((krb5rc
= krb5_init_context(&krb5context
)))
1756 * kssl_ctx->keytab_file == NULL ==> use Kerberos default
1758 if (kssl_ctx
->keytab_file
) {
1759 krb5rc
= krb5_kt_resolve(krb5context
, kssl_ctx
->keytab_file
,
1764 krb5rc
= krb5_kt_default(krb5context
, &krb5keytab
);
1769 /* the host key we are looking for */
1770 krb5rc
= krb5_sname_to_principal(krb5context
, NULL
,
1772 service_name
? kssl_ctx
->service_name
:
1773 KRB5SVC
, KRB5_NT_SRV_HST
, &princ
);
1778 krb5rc
= krb5_kt_get_entry(krb5context
, krb5keytab
, princ
,
1781 /* IGNORE_ENCTYPE */
1783 if (krb5rc
== KRB5_KT_NOTFOUND
) {
1789 krb5_kt_free_entry(krb5context
, &entry
);
1794 krb5_kt_close(krb5context
, krb5keytab
);
1796 krb5_free_principal(krb5context
, princ
);
1798 krb5_free_context(krb5context
);
1802 int kssl_tgt_is_available(KSSL_CTX
*kssl_ctx
)
1804 krb5_error_code krb5rc
= KRB5KRB_ERR_GENERIC
;
1805 krb5_context krb5context
= NULL
;
1806 krb5_ccache krb5ccdef
= NULL
;
1807 krb5_creds krb5creds
, *krb5credsp
= NULL
;
1810 memset((char *)&krb5creds
, 0, sizeof(krb5creds
));
1815 if (!kssl_ctx
->service_host
)
1818 if ((krb5rc
= krb5_init_context(&krb5context
)) != 0)
1821 if ((krb5rc
= krb5_sname_to_principal(krb5context
,
1822 kssl_ctx
->service_host
,
1823 (kssl_ctx
->service_name
) ?
1824 kssl_ctx
->service_name
: KRB5SVC
,
1826 &krb5creds
.server
)) != 0)
1829 if ((krb5rc
= krb5_cc_default(krb5context
, &krb5ccdef
)) != 0)
1832 if ((krb5rc
= krb5_cc_get_principal(krb5context
, krb5ccdef
,
1833 &krb5creds
.client
)) != 0)
1836 if ((krb5rc
= krb5_get_credentials(krb5context
, 0, krb5ccdef
,
1837 &krb5creds
, &krb5credsp
)) != 0)
1844 kssl_ctx_show(kssl_ctx
);
1845 # endif /* KSSL_DEBUG */
1847 if (krb5creds
.client
)
1848 krb5_free_principal(krb5context
, krb5creds
.client
);
1849 if (krb5creds
.server
)
1850 krb5_free_principal(krb5context
, krb5creds
.server
);
1852 krb5_free_context(krb5context
);
1856 # if !defined(OPENSSL_SYS_WINDOWS) && !defined(OPENSSL_SYS_WIN32)
1857 void kssl_krb5_free_data_contents(krb5_context context
, krb5_data
*data
)
1859 # ifdef KRB5_HEIMDAL
1863 # elif defined(KRB5_MIT_OLD11)
1865 krb5_xfree(data
->data
);
1869 krb5_free_data_contents(NULL
, data
);
1873 /* !OPENSSL_SYS_WINDOWS && !OPENSSL_SYS_WIN32 */
1876 * Given pointers to KerberosTime and struct tm structs, convert the
1877 * KerberosTime string to struct tm. Note that KerberosTime is a
1878 * ASN1_GENERALIZEDTIME value, constrained to GMT with no fractional seconds
1879 * as defined in RFC 1510. Return pointer to the (partially) filled in
1880 * struct tm on success, return NULL on failure.
1882 static struct tm
*k_gmtime(ASN1_GENERALIZEDTIME
*gtime
, struct tm
*k_tm
)
1888 if (gtime
== NULL
|| gtime
->length
< 14)
1890 if (gtime
->data
== NULL
)
1893 p
= (char *)>ime
->data
[14];
1898 k_tm
->tm_sec
= atoi(p
);
1903 k_tm
->tm_min
= atoi(p
);
1908 k_tm
->tm_hour
= atoi(p
);
1913 k_tm
->tm_mday
= atoi(p
);
1918 k_tm
->tm_mon
= atoi(p
) - 1;
1923 k_tm
->tm_year
= atoi(p
) - 1900;
1930 * Helper function for kssl_validate_times(). We need context->clockskew,
1931 * but krb5_context is an opaque struct. So we try to sneek the clockskew
1932 * out through the replay cache. If that fails just return a likely default
1935 static krb5_deltat
get_rc_clockskew(krb5_context context
)
1938 krb5_deltat clockskew
;
1940 if (krb5_rc_default(context
, &rc
))
1941 return KSSL_CLOCKSKEW
;
1942 if (krb5_rc_initialize(context
, rc
, 0))
1943 return KSSL_CLOCKSKEW
;
1944 if (krb5_rc_get_lifespan(context
, rc
, &clockskew
)) {
1945 clockskew
= KSSL_CLOCKSKEW
;
1947 (void)krb5_rc_destroy(context
, rc
);
1952 * kssl_validate_times() combines (and more importantly exposes) the MIT KRB5
1953 * internal function krb5_validate_times() and the in_clock_skew() macro.
1954 * The authenticator client time is checked to be within clockskew secs of
1955 * the current time and the current time is checked to be within the ticket
1956 * start and expire times. Either check may be omitted by supplying a NULL
1957 * value. Returns 0 for valid times, SSL_R_KRB5* error codes otherwise. See
1958 * Also: (Kerberos source)/krb5/lib/krb5/krb/valid_times.c 20010420 VRS
1960 krb5_error_code
kssl_validate_times(krb5_timestamp atime
,
1961 krb5_ticket_times
*ttimes
)
1964 krb5_timestamp start
, now
;
1966 krb5_context context
;
1968 if ((rc
= krb5_init_context(&context
)))
1969 return SSL_R_KRB5_S_BAD_TICKET
;
1970 skew
= get_rc_clockskew(context
);
1971 if ((rc
= krb5_timeofday(context
, &now
)))
1972 return SSL_R_KRB5_S_BAD_TICKET
;
1973 krb5_free_context(context
);
1975 if (atime
&& labs(atime
- now
) >= skew
)
1976 return SSL_R_KRB5_S_TKT_SKEW
;
1981 start
= (ttimes
->starttime
!= 0) ? ttimes
->starttime
: ttimes
->authtime
;
1982 if (start
- now
> skew
)
1983 return SSL_R_KRB5_S_TKT_NYV
;
1984 if ((now
- ttimes
->endtime
) > skew
)
1985 return SSL_R_KRB5_S_TKT_EXPIRED
;
1988 fprintf(stderr
, "kssl_validate_times: %d |<- | %d - %d | < %d ->| %d\n",
1989 start
, atime
, now
, skew
, ttimes
->endtime
);
1990 # endif /* KSSL_DEBUG */
1996 * Decode and decrypt given DER-encoded authenticator, then pass
1997 * authenticator ctime back in *atimep (or 0 if time unavailable). Returns
1998 * krb5_error_code and kssl_err on error. A NULL authenticator
1999 * (authentp->length == 0) is not considered an error. Note that
2000 * kssl_check_authent() makes use of the KRB5 session key; you must call
2001 * kssl_sget_tkt() to get the key before calling this routine.
2003 krb5_error_code
kssl_check_authent(
2006 */ KSSL_CTX
*kssl_ctx
,
2009 */ krb5_data
*authentp
,
2012 */ krb5_timestamp
*atimep
,
2015 */ KSSL_ERR
*kssl_err
)
2017 krb5_error_code krb5rc
= 0;
2018 KRB5_ENCDATA
*dec_authent
= NULL
;
2019 KRB5_AUTHENTBODY
*auth
= NULL
;
2020 krb5_enctype enctype
;
2021 EVP_CIPHER_CTX ciph_ctx
;
2022 const EVP_CIPHER
*enc
= NULL
;
2023 unsigned char iv
[EVP_MAX_IV_LENGTH
];
2024 const unsigned char *p
;
2025 unsigned char *unenc_authent
;
2026 int outl
, unencbufsize
;
2027 struct tm tm_time
, *tm_l
, *tm_g
;
2028 time_t now
, tl
, tg
, tr
, tz_offset
;
2030 EVP_CIPHER_CTX_init(&ciph_ctx
);
2032 kssl_err_set(kssl_err
, 0, "");
2034 # ifndef KRB5CHECKAUTH
2037 # if KRB5CHECKAUTH == 0
2040 # endif /* KRB5CHECKAUTH */
2042 if (authentp
== NULL
|| authentp
->length
== 0)
2048 fprintf(stderr
, "kssl_check_authent: authenticator[%d]:\n",
2051 for (ui
= 0; ui
< authentp
->length
; ui
++)
2052 fprintf(stderr
, "%02x ", p
[ui
]);
2053 fprintf(stderr
, "\n");
2055 # endif /* KSSL_DEBUG */
2057 unencbufsize
= 2 * authentp
->length
;
2058 if ((unenc_authent
= calloc(1, unencbufsize
)) == NULL
) {
2059 kssl_err_set(kssl_err
, SSL_R_KRB5_S_INIT
,
2060 "Unable to allocate authenticator buffer.\n");
2061 krb5rc
= KRB5KRB_ERR_GENERIC
;
2065 p
= (unsigned char *)authentp
->data
;
2066 if ((dec_authent
= d2i_KRB5_ENCDATA(NULL
, &p
,
2067 (long)authentp
->length
)) == NULL
) {
2068 kssl_err_set(kssl_err
, SSL_R_KRB5_S_INIT
,
2069 "Error decoding authenticator.\n");
2070 krb5rc
= KRB5KRB_AP_ERR_BAD_INTEGRITY
;
2074 enctype
= dec_authent
->etype
->data
[0]; /* should = kssl_ctx->enctype */
2075 # if !defined(KRB5_MIT_OLD11)
2077 case ENCTYPE_DES3_CBC_SHA1
: /* EVP_des_ede3_cbc(); */
2078 case ENCTYPE_DES3_CBC_SHA
:
2079 case ENCTYPE_DES3_CBC_RAW
:
2080 krb5rc
= 0; /* Skip, can't handle derived keys */
2084 enc
= kssl_map_enc(enctype
);
2085 memset(iv
, 0, sizeof iv
); /* per RFC 1510 */
2089 * Disable kssl_check_authent for ENCTYPE_DES3_CBC_SHA1. This
2090 * enctype indicates the authenticator was encrypted using key-usage
2091 * derived keys which openssl cannot decrypt.
2096 if (!EVP_CipherInit(&ciph_ctx
, enc
, kssl_ctx
->key
, iv
, 0)) {
2097 kssl_err_set(kssl_err
, SSL_R_KRB5_S_INIT
,
2098 "EVP_CipherInit error decrypting authenticator.\n");
2099 krb5rc
= KRB5KRB_AP_ERR_BAD_INTEGRITY
;
2102 outl
= dec_authent
->cipher
->length
;
2104 (&ciph_ctx
, unenc_authent
, dec_authent
->cipher
->data
, outl
)) {
2105 kssl_err_set(kssl_err
, SSL_R_KRB5_S_INIT
,
2106 "EVP_Cipher error decrypting authenticator.\n");
2107 krb5rc
= KRB5KRB_AP_ERR_BAD_INTEGRITY
;
2110 EVP_CIPHER_CTX_cleanup(&ciph_ctx
);
2115 fprintf(stderr
, "kssl_check_authent: decrypted authenticator[%d] =\n",
2117 for (padl
= 0; padl
< outl
; padl
++)
2118 fprintf(stderr
, "%02x ", unenc_authent
[padl
]);
2119 fprintf(stderr
, "\n");
2121 # endif /* KSSL_DEBUG */
2123 if ((p
= kssl_skip_confound(enctype
, unenc_authent
)) == NULL
) {
2124 kssl_err_set(kssl_err
, SSL_R_KRB5_S_INIT
,
2125 "confounded by authenticator.\n");
2126 krb5rc
= KRB5KRB_AP_ERR_BAD_INTEGRITY
;
2129 outl
-= p
- unenc_authent
;
2131 if ((auth
= (KRB5_AUTHENTBODY
*)d2i_KRB5_AUTHENT(NULL
, &p
,
2132 (long)outl
)) == NULL
) {
2133 kssl_err_set(kssl_err
, SSL_R_KRB5_S_INIT
,
2134 "Error decoding authenticator body.\n");
2135 krb5rc
= KRB5KRB_AP_ERR_BAD_INTEGRITY
;
2139 memset(&tm_time
, 0, sizeof(struct tm
));
2140 if (k_gmtime(auth
->ctime
, &tm_time
) &&
2141 ((tr
= mktime(&tm_time
)) != (time_t)(-1))) {
2143 tm_l
= localtime(&now
);
2145 tm_g
= gmtime(&now
);
2147 tz_offset
= tg
- tl
;
2149 *atimep
= (krb5_timestamp
)(tr
- tz_offset
);
2152 fprintf(stderr
, "kssl_check_authent: returns %d for client time ",
2154 if (auth
&& auth
->ctime
&& auth
->ctime
->length
&& auth
->ctime
->data
)
2155 fprintf(stderr
, "%.*s\n", auth
->ctime
->length
, auth
->ctime
->data
);
2157 fprintf(stderr
, "NULL\n");
2158 # endif /* KSSL_DEBUG */
2162 KRB5_AUTHENT_free((KRB5_AUTHENT
*) auth
);
2164 KRB5_ENCDATA_free(dec_authent
);
2166 free(unenc_authent
);
2167 EVP_CIPHER_CTX_cleanup(&ciph_ctx
);
2172 * Replaces krb5_build_principal_ext(), with varargs length == 2 (svc, host),
2173 * because I don't know how to stub varargs. Returns krb5_error_code ==
2174 * ENOMEM on alloc error, otherwise passes back newly constructed principal,
2175 * which should be freed by caller.
2177 krb5_error_code
kssl_build_principal_2(
2180 */ krb5_context context
,
2183 */ krb5_principal
*princ
,
2186 */ int rlen
, const char *realm
,
2189 */ int slen
, const char *svc
,
2192 */ int hlen
, const char *host
)
2194 krb5_data
*p_data
= NULL
;
2195 krb5_principal new_p
= NULL
;
2198 if ((p_data
= (krb5_data
*)calloc(2, sizeof(krb5_data
))) == NULL
||
2199 (new_p
= (krb5_principal
)calloc(1, sizeof(krb5_principal_data
)))
2203 new_p
->data
= p_data
;
2205 if ((new_r
= calloc(1, rlen
+ 1)) == NULL
)
2207 memcpy(new_r
, realm
, rlen
);
2208 krb5_princ_set_realm_length(context
, new_p
, rlen
);
2209 krb5_princ_set_realm_data(context
, new_p
, new_r
);
2211 if ((new_p
->data
[0].data
= calloc(1, slen
+ 1)) == NULL
)
2213 memcpy(new_p
->data
[0].data
, svc
, slen
);
2214 new_p
->data
[0].length
= slen
;
2216 if ((new_p
->data
[1].data
= calloc(1, hlen
+ 1)) == NULL
)
2218 memcpy(new_p
->data
[1].data
, host
, hlen
);
2219 new_p
->data
[1].length
= hlen
;
2221 krb5_princ_type(context
, new_p
) = KRB5_NT_UNKNOWN
;
2226 if (new_p
&& new_p
[0].data
)
2227 free(new_p
[0].data
);
2228 if (new_p
&& new_p
[1].data
)
2229 free(new_p
[1].data
);
2237 void SSL_set0_kssl_ctx(SSL
*s
, KSSL_CTX
*kctx
)
2242 KSSL_CTX
*SSL_get0_kssl_ctx(SSL
*s
)
2247 char *kssl_ctx_get0_client_princ(KSSL_CTX
*kctx
)
2250 return kctx
->client_princ
;
2254 #else /* !OPENSSL_NO_KRB5 */
2256 # if defined(PEDANTIC) || defined(OPENSSL_SYS_VMS)
2257 static void *dummy
= &dummy
;
2260 #endif /* !OPENSSL_NO_KRB5 */