3 * Written by Vern Staats <staatsvr@asc.hpc.mil> for the OpenSSL project
6 /* ====================================================================
7 * Copyright (c) 2000-2018 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>
84 #ifndef OPENSSL_NO_KRB5
87 # define ENOMEM KRB5KRB_ERR_GENERIC
91 * When OpenSSL is built on Windows, we do not want to require that
92 * the Kerberos DLLs be available in order for the OpenSSL DLLs to
93 * work. Therefore, all Kerberos routines are loaded at run time
94 * and we do not link to a .LIB file.
97 # if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32)
99 * The purpose of the following pre-processor statements is to provide
100 * compatibility with different releases of MIT Kerberos for Windows.
101 * All versions up to 1.2 used macros. But macros do not allow for
102 * a binary compatible interface for DLLs. Therefore, all macros are
103 * being replaced by function calls. The following code will allow
104 * an OpenSSL DLL built on Windows to work whether or not the macro
105 * or function form of the routines are utilized.
107 # ifdef krb5_cc_get_principal
108 # define NO_DEF_KRB5_CCACHE
109 # undef krb5_cc_get_principal
111 # define krb5_cc_get_principal kssl_krb5_cc_get_principal
113 # define krb5_free_data_contents kssl_krb5_free_data_contents
114 # define krb5_free_context kssl_krb5_free_context
115 # define krb5_auth_con_free kssl_krb5_auth_con_free
116 # define krb5_free_principal kssl_krb5_free_principal
117 # define krb5_mk_req_extended kssl_krb5_mk_req_extended
118 # define krb5_get_credentials kssl_krb5_get_credentials
119 # define krb5_cc_default kssl_krb5_cc_default
120 # define krb5_sname_to_principal kssl_krb5_sname_to_principal
121 # define krb5_init_context kssl_krb5_init_context
122 # define krb5_free_ticket kssl_krb5_free_ticket
123 # define krb5_rd_req kssl_krb5_rd_req
124 # define krb5_kt_default kssl_krb5_kt_default
125 # define krb5_kt_resolve kssl_krb5_kt_resolve
126 /* macros in mit 1.2.2 and earlier; functions in mit 1.2.3 and greater */
127 # ifndef krb5_kt_close
128 # define krb5_kt_close kssl_krb5_kt_close
129 # endif /* krb5_kt_close */
130 # ifndef krb5_kt_get_entry
131 # define krb5_kt_get_entry kssl_krb5_kt_get_entry
132 # endif /* krb5_kt_get_entry */
133 # define krb5_auth_con_init kssl_krb5_auth_con_init
135 # define krb5_principal_compare kssl_krb5_principal_compare
136 # define krb5_decrypt_tkt_part kssl_krb5_decrypt_tkt_part
137 # define krb5_timeofday kssl_krb5_timeofday
138 # define krb5_rc_default kssl_krb5_rc_default
140 # ifdef krb5_rc_initialize
141 # undef krb5_rc_initialize
143 # define krb5_rc_initialize kssl_krb5_rc_initialize
145 # ifdef krb5_rc_get_lifespan
146 # undef krb5_rc_get_lifespan
148 # define krb5_rc_get_lifespan kssl_krb5_rc_get_lifespan
150 # ifdef krb5_rc_destroy
151 # undef krb5_rc_destroy
153 # define krb5_rc_destroy kssl_krb5_rc_destroy
155 # define valid_cksumtype kssl_valid_cksumtype
156 # define krb5_checksum_size kssl_krb5_checksum_size
157 # define krb5_kt_free_entry kssl_krb5_kt_free_entry
158 # define krb5_auth_con_setrcache kssl_krb5_auth_con_setrcache
159 # define krb5_auth_con_getrcache kssl_krb5_auth_con_getrcache
160 # define krb5_get_server_rcache kssl_krb5_get_server_rcache
162 /* Prototypes for built in stubs */
163 void kssl_krb5_free_data_contents(krb5_context
, krb5_data
*);
164 void kssl_krb5_free_principal(krb5_context
, krb5_principal
);
165 krb5_error_code
kssl_krb5_kt_resolve(krb5_context
,
166 krb5_const
char *, krb5_keytab
*);
167 krb5_error_code
kssl_krb5_kt_default(krb5_context
, krb5_keytab
*);
168 krb5_error_code
kssl_krb5_free_ticket(krb5_context
, krb5_ticket
*);
169 krb5_error_code
kssl_krb5_rd_req(krb5_context
, krb5_auth_context
*,
170 krb5_const krb5_data
*,
171 krb5_const_principal
, krb5_keytab
,
172 krb5_flags
*, krb5_ticket
**);
174 krb5_boolean
kssl_krb5_principal_compare(krb5_context
, krb5_const_principal
,
175 krb5_const_principal
);
176 krb5_error_code
kssl_krb5_mk_req_extended(krb5_context
,
178 krb5_const krb5_flags
,
180 krb5_creds
*, krb5_data
*);
181 krb5_error_code
kssl_krb5_init_context(krb5_context
*);
182 void kssl_krb5_free_context(krb5_context
);
183 krb5_error_code
kssl_krb5_cc_default(krb5_context
, krb5_ccache
*);
184 krb5_error_code
kssl_krb5_sname_to_principal(krb5_context
,
187 krb5_int32
, krb5_principal
*);
188 krb5_error_code
kssl_krb5_get_credentials(krb5_context
,
189 krb5_const krb5_flags
,
191 krb5_creds
*, krb5_creds
* *);
192 krb5_error_code
kssl_krb5_auth_con_init(krb5_context
, krb5_auth_context
*);
193 krb5_error_code
kssl_krb5_cc_get_principal(krb5_context context
,
195 krb5_principal
*principal
);
196 krb5_error_code
kssl_krb5_auth_con_free(krb5_context
, krb5_auth_context
);
197 size_t kssl_krb5_checksum_size(krb5_context context
, krb5_cksumtype ctype
);
198 krb5_boolean
kssl_valid_cksumtype(krb5_cksumtype ctype
);
199 krb5_error_code
krb5_kt_free_entry(krb5_context
, krb5_keytab_entry FAR
*);
200 krb5_error_code
kssl_krb5_auth_con_setrcache(krb5_context
,
201 krb5_auth_context
, krb5_rcache
);
202 krb5_error_code
kssl_krb5_get_server_rcache(krb5_context
,
203 krb5_const krb5_data
*,
205 krb5_error_code
kssl_krb5_auth_con_getrcache(krb5_context
,
209 /* Function pointers (almost all Kerberos functions are _stdcall) */
210 static void (_stdcall
*p_krb5_free_data_contents
) (krb5_context
, krb5_data
*)
212 static void (_stdcall
*p_krb5_free_principal
) (krb5_context
, krb5_principal
)
214 static krb5_error_code(_stdcall
*p_krb5_kt_resolve
)
215 (krb5_context
, krb5_const
char *, krb5_keytab
*) = NULL
;
216 static krb5_error_code(_stdcall
*p_krb5_kt_default
) (krb5_context
,
217 krb5_keytab
*) = NULL
;
218 static krb5_error_code(_stdcall
*p_krb5_free_ticket
) (krb5_context
,
219 krb5_ticket
*) = NULL
;
220 static krb5_error_code(_stdcall
*p_krb5_rd_req
) (krb5_context
,
222 krb5_const krb5_data
*,
223 krb5_const_principal
,
224 krb5_keytab
, krb5_flags
*,
225 krb5_ticket
**) = NULL
;
226 static krb5_error_code(_stdcall
*p_krb5_mk_req_extended
)
227 (krb5_context
, krb5_auth_context
*,
228 krb5_const krb5_flags
, krb5_data
*, krb5_creds
*, krb5_data
*) = NULL
;
229 static krb5_error_code(_stdcall
*p_krb5_init_context
) (krb5_context
*) = NULL
;
230 static void (_stdcall
*p_krb5_free_context
) (krb5_context
) = NULL
;
231 static krb5_error_code(_stdcall
*p_krb5_cc_default
) (krb5_context
,
232 krb5_ccache
*) = NULL
;
233 static krb5_error_code(_stdcall
*p_krb5_sname_to_principal
)
234 (krb5_context
, krb5_const
char *, krb5_const
char *,
235 krb5_int32
, krb5_principal
*) = NULL
;
236 static krb5_error_code(_stdcall
*p_krb5_get_credentials
)
237 (krb5_context
, krb5_const krb5_flags
, krb5_ccache
,
238 krb5_creds
*, krb5_creds
**) = NULL
;
239 static krb5_error_code(_stdcall
*p_krb5_auth_con_init
)
240 (krb5_context
, krb5_auth_context
*) = NULL
;
241 static krb5_error_code(_stdcall
*p_krb5_cc_get_principal
)
242 (krb5_context context
, krb5_ccache cache
, krb5_principal
*principal
) = NULL
;
243 static krb5_error_code(_stdcall
*p_krb5_auth_con_free
)
244 (krb5_context
, krb5_auth_context
) = NULL
;
245 static krb5_error_code(_stdcall
*p_krb5_decrypt_tkt_part
)
246 (krb5_context
, krb5_const krb5_keyblock
*, krb5_ticket
*) = NULL
;
247 static krb5_error_code(_stdcall
*p_krb5_timeofday
)
248 (krb5_context context
, krb5_int32
*timeret
) = NULL
;
249 static krb5_error_code(_stdcall
*p_krb5_rc_default
)
250 (krb5_context context
, krb5_rcache
*rc
) = NULL
;
251 static krb5_error_code(_stdcall
*p_krb5_rc_initialize
)
252 (krb5_context context
, krb5_rcache rc
, krb5_deltat lifespan
) = NULL
;
253 static krb5_error_code(_stdcall
*p_krb5_rc_get_lifespan
)
254 (krb5_context context
, krb5_rcache rc
, krb5_deltat
*lifespan
) = NULL
;
255 static krb5_error_code(_stdcall
*p_krb5_rc_destroy
)
256 (krb5_context context
, krb5_rcache rc
) = NULL
;
257 static krb5_boolean(_stdcall
*p_krb5_principal_compare
)
258 (krb5_context
, krb5_const_principal
, krb5_const_principal
) = NULL
;
259 static size_t (_stdcall
*p_krb5_checksum_size
) (krb5_context context
,
260 krb5_cksumtype ctype
) = NULL
;
261 static krb5_boolean(_stdcall
*p_valid_cksumtype
) (krb5_cksumtype ctype
) =
263 static krb5_error_code(_stdcall
*p_krb5_kt_free_entry
)
264 (krb5_context
, krb5_keytab_entry
*) = NULL
;
265 static krb5_error_code(_stdcall
*p_krb5_auth_con_setrcache
) (krb5_context
,
269 static krb5_error_code(_stdcall
*p_krb5_get_server_rcache
) (krb5_context
,
274 static krb5_error_code(*p_krb5_auth_con_getrcache
) (krb5_context
,
276 krb5_rcache
*) = NULL
;
277 static krb5_error_code(_stdcall
*p_krb5_kt_close
) (krb5_context context
,
278 krb5_keytab keytab
) = NULL
;
279 static krb5_error_code(_stdcall
*p_krb5_kt_get_entry
) (krb5_context context
,
284 krb5_enctype enctype
,
287 static int krb5_loaded
= 0; /* only attempt to initialize func ptrs once */
289 /* Function to Load the Kerberos 5 DLL and initialize function pointers */
290 void load_krb5_dll(void)
295 hKRB5_32
= LoadLibrary(TEXT("KRB5_32"));
299 (FARPROC
) p_krb5_free_data_contents
=
300 GetProcAddress(hKRB5_32
, "krb5_free_data_contents");
301 (FARPROC
) p_krb5_free_context
=
302 GetProcAddress(hKRB5_32
, "krb5_free_context");
303 (FARPROC
) p_krb5_auth_con_free
=
304 GetProcAddress(hKRB5_32
, "krb5_auth_con_free");
305 (FARPROC
) p_krb5_free_principal
=
306 GetProcAddress(hKRB5_32
, "krb5_free_principal");
307 (FARPROC
) p_krb5_mk_req_extended
=
308 GetProcAddress(hKRB5_32
, "krb5_mk_req_extended");
309 (FARPROC
) p_krb5_get_credentials
=
310 GetProcAddress(hKRB5_32
, "krb5_get_credentials");
311 (FARPROC
) p_krb5_cc_get_principal
=
312 GetProcAddress(hKRB5_32
, "krb5_cc_get_principal");
313 (FARPROC
) p_krb5_cc_default
= GetProcAddress(hKRB5_32
, "krb5_cc_default");
314 (FARPROC
) p_krb5_sname_to_principal
=
315 GetProcAddress(hKRB5_32
, "krb5_sname_to_principal");
316 (FARPROC
) p_krb5_init_context
=
317 GetProcAddress(hKRB5_32
, "krb5_init_context");
318 (FARPROC
) p_krb5_free_ticket
=
319 GetProcAddress(hKRB5_32
, "krb5_free_ticket");
320 (FARPROC
) p_krb5_rd_req
= GetProcAddress(hKRB5_32
, "krb5_rd_req");
321 (FARPROC
) p_krb5_principal_compare
=
322 GetProcAddress(hKRB5_32
, "krb5_principal_compare");
323 (FARPROC
) p_krb5_decrypt_tkt_part
=
324 GetProcAddress(hKRB5_32
, "krb5_decrypt_tkt_part");
325 (FARPROC
) p_krb5_timeofday
= GetProcAddress(hKRB5_32
, "krb5_timeofday");
326 (FARPROC
) p_krb5_rc_default
= GetProcAddress(hKRB5_32
, "krb5_rc_default");
327 (FARPROC
) p_krb5_rc_initialize
=
328 GetProcAddress(hKRB5_32
, "krb5_rc_initialize");
329 (FARPROC
) p_krb5_rc_get_lifespan
=
330 GetProcAddress(hKRB5_32
, "krb5_rc_get_lifespan");
331 (FARPROC
) p_krb5_rc_destroy
= GetProcAddress(hKRB5_32
, "krb5_rc_destroy");
332 (FARPROC
) p_krb5_kt_default
= GetProcAddress(hKRB5_32
, "krb5_kt_default");
333 (FARPROC
) p_krb5_kt_resolve
= GetProcAddress(hKRB5_32
, "krb5_kt_resolve");
334 (FARPROC
) p_krb5_auth_con_init
=
335 GetProcAddress(hKRB5_32
, "krb5_auth_con_init");
336 (FARPROC
) p_valid_cksumtype
= GetProcAddress(hKRB5_32
, "valid_cksumtype");
337 (FARPROC
) p_krb5_checksum_size
=
338 GetProcAddress(hKRB5_32
, "krb5_checksum_size");
339 (FARPROC
) p_krb5_kt_free_entry
=
340 GetProcAddress(hKRB5_32
, "krb5_kt_free_entry");
341 (FARPROC
) p_krb5_auth_con_setrcache
=
342 GetProcAddress(hKRB5_32
, "krb5_auth_con_setrcache");
343 (FARPROC
) p_krb5_get_server_rcache
=
344 GetProcAddress(hKRB5_32
, "krb5_get_server_rcache");
345 (FARPROC
) p_krb5_auth_con_getrcache
=
346 GetProcAddress(hKRB5_32
, "krb5_auth_con_getrcache");
347 (FARPROC
) p_krb5_kt_close
= GetProcAddress(hKRB5_32
, "krb5_kt_close");
348 (FARPROC
) p_krb5_kt_get_entry
=
349 GetProcAddress(hKRB5_32
, "krb5_kt_get_entry");
352 /* Stubs for each function to be dynamicly loaded */
353 void kssl_krb5_free_data_contents(krb5_context CO
, krb5_data
*data
)
358 if (p_krb5_free_data_contents
)
359 p_krb5_free_data_contents(CO
, data
);
363 kssl_krb5_mk_req_extended(krb5_context CO
,
364 krb5_auth_context
*pACO
,
365 krb5_const krb5_flags F
,
366 krb5_data
*pD1
, krb5_creds
*pC
, krb5_data
*pD2
)
371 if (p_krb5_mk_req_extended
)
372 return (p_krb5_mk_req_extended(CO
, pACO
, F
, pD1
, pC
, pD2
));
374 return KRB5KRB_ERR_GENERIC
;
378 kssl_krb5_auth_con_init(krb5_context CO
, krb5_auth_context
*pACO
)
383 if (p_krb5_auth_con_init
)
384 return (p_krb5_auth_con_init(CO
, pACO
));
386 return KRB5KRB_ERR_GENERIC
;
390 kssl_krb5_auth_con_free(krb5_context CO
, krb5_auth_context ACO
)
395 if (p_krb5_auth_con_free
)
396 return (p_krb5_auth_con_free(CO
, ACO
));
398 return KRB5KRB_ERR_GENERIC
;
402 kssl_krb5_get_credentials(krb5_context CO
,
403 krb5_const krb5_flags F
,
404 krb5_ccache CC
, krb5_creds
*pCR
, krb5_creds
**ppCR
)
409 if (p_krb5_get_credentials
)
410 return (p_krb5_get_credentials(CO
, F
, CC
, pCR
, ppCR
));
412 return KRB5KRB_ERR_GENERIC
;
416 kssl_krb5_sname_to_principal(krb5_context CO
,
417 krb5_const
char *pC1
,
418 krb5_const
char *pC2
,
419 krb5_int32 I
, krb5_principal
*pPR
)
424 if (p_krb5_sname_to_principal
)
425 return (p_krb5_sname_to_principal(CO
, pC1
, pC2
, I
, pPR
));
427 return KRB5KRB_ERR_GENERIC
;
430 krb5_error_code
kssl_krb5_cc_default(krb5_context CO
, krb5_ccache
*pCC
)
435 if (p_krb5_cc_default
)
436 return (p_krb5_cc_default(CO
, pCC
));
438 return KRB5KRB_ERR_GENERIC
;
441 krb5_error_code
kssl_krb5_init_context(krb5_context
*pCO
)
446 if (p_krb5_init_context
)
447 return (p_krb5_init_context(pCO
));
449 return KRB5KRB_ERR_GENERIC
;
452 void kssl_krb5_free_context(krb5_context CO
)
457 if (p_krb5_free_context
)
458 p_krb5_free_context(CO
);
461 void kssl_krb5_free_principal(krb5_context c
, krb5_principal p
)
466 if (p_krb5_free_principal
)
467 p_krb5_free_principal(c
, p
);
471 kssl_krb5_kt_resolve(krb5_context con
, krb5_const
char *sz
, krb5_keytab
*kt
)
476 if (p_krb5_kt_resolve
)
477 return (p_krb5_kt_resolve(con
, sz
, kt
));
479 return KRB5KRB_ERR_GENERIC
;
482 krb5_error_code
kssl_krb5_kt_default(krb5_context con
, krb5_keytab
*kt
)
487 if (p_krb5_kt_default
)
488 return (p_krb5_kt_default(con
, kt
));
490 return KRB5KRB_ERR_GENERIC
;
493 krb5_error_code
kssl_krb5_free_ticket(krb5_context con
, krb5_ticket
*kt
)
498 if (p_krb5_free_ticket
)
499 return (p_krb5_free_ticket(con
, kt
));
501 return KRB5KRB_ERR_GENERIC
;
505 kssl_krb5_rd_req(krb5_context con
, krb5_auth_context
*pacon
,
506 krb5_const krb5_data
*data
,
507 krb5_const_principal princ
, krb5_keytab keytab
,
508 krb5_flags
*flags
, krb5_ticket
**pptkt
)
514 return (p_krb5_rd_req(con
, pacon
, data
, princ
, keytab
, flags
, pptkt
));
516 return KRB5KRB_ERR_GENERIC
;
520 krb5_principal_compare(krb5_context con
, krb5_const_principal princ1
,
521 krb5_const_principal princ2
)
526 if (p_krb5_principal_compare
)
527 return (p_krb5_principal_compare(con
, princ1
, princ2
));
529 return KRB5KRB_ERR_GENERIC
;
533 krb5_decrypt_tkt_part(krb5_context con
, krb5_const krb5_keyblock
*keys
,
539 if (p_krb5_decrypt_tkt_part
)
540 return (p_krb5_decrypt_tkt_part(con
, keys
, ticket
));
542 return KRB5KRB_ERR_GENERIC
;
545 krb5_error_code
krb5_timeofday(krb5_context con
, krb5_int32
*timeret
)
550 if (p_krb5_timeofday
)
551 return (p_krb5_timeofday(con
, timeret
));
553 return KRB5KRB_ERR_GENERIC
;
556 krb5_error_code
krb5_rc_default(krb5_context con
, krb5_rcache
*rc
)
561 if (p_krb5_rc_default
)
562 return (p_krb5_rc_default(con
, rc
));
564 return KRB5KRB_ERR_GENERIC
;
568 krb5_rc_initialize(krb5_context con
, krb5_rcache rc
, krb5_deltat lifespan
)
573 if (p_krb5_rc_initialize
)
574 return (p_krb5_rc_initialize(con
, rc
, lifespan
));
576 return KRB5KRB_ERR_GENERIC
;
580 krb5_rc_get_lifespan(krb5_context con
, krb5_rcache rc
, krb5_deltat
*lifespanp
)
585 if (p_krb5_rc_get_lifespan
)
586 return (p_krb5_rc_get_lifespan(con
, rc
, lifespanp
));
588 return KRB5KRB_ERR_GENERIC
;
591 krb5_error_code
krb5_rc_destroy(krb5_context con
, krb5_rcache rc
)
596 if (p_krb5_rc_destroy
)
597 return (p_krb5_rc_destroy(con
, rc
));
599 return KRB5KRB_ERR_GENERIC
;
602 size_t krb5_checksum_size(krb5_context context
, krb5_cksumtype ctype
)
607 if (p_krb5_checksum_size
)
608 return (p_krb5_checksum_size(context
, ctype
));
610 return KRB5KRB_ERR_GENERIC
;
613 krb5_boolean
valid_cksumtype(krb5_cksumtype ctype
)
618 if (p_valid_cksumtype
)
619 return (p_valid_cksumtype(ctype
));
621 return KRB5KRB_ERR_GENERIC
;
624 krb5_error_code
krb5_kt_free_entry(krb5_context con
, krb5_keytab_entry
*entry
)
629 if (p_krb5_kt_free_entry
)
630 return (p_krb5_kt_free_entry(con
, entry
));
632 return KRB5KRB_ERR_GENERIC
;
635 /* Structure definitions */
636 # ifndef NO_DEF_KRB5_CCACHE
638 # define krb5_x(ptr,args) ((ptr)?((*(ptr)) args):(abort(),1))
639 # define krb5_xc(ptr,args) ((ptr)?((*(ptr)) args):(abort(),(char*)0))
642 typedef krb5_pointer krb5_cc_cursor
; /* cursor for sequential lookup */
644 typedef struct _krb5_ccache
{
646 struct _krb5_cc_ops FAR
*ops
;
650 typedef struct _krb5_cc_ops
{
653 char *(KRB5_CALLCONV
*get_name
)
654 (krb5_context
, krb5_ccache
);
655 krb5_error_code(KRB5_CALLCONV
*resolve
)
656 (krb5_context
, krb5_ccache
*, const char *);
657 krb5_error_code(KRB5_CALLCONV
*gen_new
)
658 (krb5_context
, krb5_ccache
*);
659 krb5_error_code(KRB5_CALLCONV
*init
)
660 (krb5_context
, krb5_ccache
, krb5_principal
);
661 krb5_error_code(KRB5_CALLCONV
*destroy
)
662 (krb5_context
, krb5_ccache
);
663 krb5_error_code(KRB5_CALLCONV
*close
)
664 (krb5_context
, krb5_ccache
);
665 krb5_error_code(KRB5_CALLCONV
*store
)
666 (krb5_context
, krb5_ccache
, krb5_creds
*);
667 krb5_error_code(KRB5_CALLCONV
*retrieve
)
668 (krb5_context
, krb5_ccache
, krb5_flags
, krb5_creds
*, krb5_creds
*);
669 krb5_error_code(KRB5_CALLCONV
*get_princ
)
670 (krb5_context
, krb5_ccache
, krb5_principal
*);
671 krb5_error_code(KRB5_CALLCONV
*get_first
)
672 (krb5_context
, krb5_ccache
, krb5_cc_cursor
*);
673 krb5_error_code(KRB5_CALLCONV
*get_next
)
674 (krb5_context
, krb5_ccache
, krb5_cc_cursor
*, krb5_creds
*);
675 krb5_error_code(KRB5_CALLCONV
*end_get
)
676 (krb5_context
, krb5_ccache
, krb5_cc_cursor
*);
677 krb5_error_code(KRB5_CALLCONV
*remove_cred
)
678 (krb5_context
, krb5_ccache
, 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
, krb5_principal
*principal
) {
687 if (p_krb5_cc_get_principal
)
688 return (p_krb5_cc_get_principal(context
, cache
, principal
));
690 return (krb5_x((cache
)->ops
->get_princ
, (context
, cache
, principal
)));
694 kssl_krb5_auth_con_setrcache(krb5_context con
, krb5_auth_context acon
,
697 if (p_krb5_auth_con_setrcache
)
698 return (p_krb5_auth_con_setrcache(con
, acon
, rcache
));
700 return KRB5KRB_ERR_GENERIC
;
704 kssl_krb5_get_server_rcache(krb5_context con
, krb5_const krb5_data
*data
,
707 if (p_krb5_get_server_rcache
)
708 return (p_krb5_get_server_rcache(con
, data
, rcache
));
710 return KRB5KRB_ERR_GENERIC
;
714 kssl_krb5_auth_con_getrcache(krb5_context con
, krb5_auth_context acon
,
715 krb5_rcache
*prcache
)
717 if (p_krb5_auth_con_getrcache
)
718 return (p_krb5_auth_con_getrcache(con
, acon
, prcache
));
720 return KRB5KRB_ERR_GENERIC
;
723 krb5_error_code
kssl_krb5_kt_close(krb5_context context
, krb5_keytab keytab
)
726 return (p_krb5_kt_close(context
, keytab
));
728 return KRB5KRB_ERR_GENERIC
;
732 kssl_krb5_kt_get_entry(krb5_context context
, krb5_keytab keytab
,
733 krb5_const_principal principal
, krb5_kvno vno
,
734 krb5_enctype enctype
, krb5_keytab_entry
*entry
)
736 if (p_krb5_kt_get_entry
)
737 return (p_krb5_kt_get_entry
738 (context
, keytab
, principal
, vno
, enctype
, entry
));
740 return KRB5KRB_ERR_GENERIC
;
742 # endif /* OPENSSL_SYS_WINDOWS || OPENSSL_SYS_WIN32 */
745 * memory allocation functions for non-temporary storage (e.g. stuff that
746 * gets saved into the kssl context)
748 static void *kssl_calloc(size_t nmemb
, size_t size
)
752 p
= OPENSSL_malloc(nmemb
* size
);
754 memset(p
, 0, nmemb
* size
);
759 # define kssl_malloc(size) OPENSSL_malloc((size))
760 # define kssl_realloc(ptr, size) OPENSSL_realloc(ptr, size)
761 # define kssl_free(ptr) OPENSSL_free((ptr))
764 *kstring(char *string
)
766 static char *null
= "[NULL]";
768 return ((string
== NULL
) ? null
: string
);
772 * Given KRB5 enctype (basically DES or 3DES), return closest match openssl
773 * EVP_ encryption algorithm. Return NULL for unknown or problematic
774 * (krb5_dk_encrypt) enctypes. Assume ENCTYPE_*_RAW (krb5_raw_encrypt) are
777 const EVP_CIPHER
*kssl_map_enc(krb5_enctype enctype
)
780 case ENCTYPE_DES_HMAC_SHA1
: /* EVP_des_cbc(); */
781 case ENCTYPE_DES_CBC_CRC
:
782 case ENCTYPE_DES_CBC_MD4
:
783 case ENCTYPE_DES_CBC_MD5
:
784 case ENCTYPE_DES_CBC_RAW
:
785 return EVP_des_cbc();
787 case ENCTYPE_DES3_CBC_SHA1
: /* EVP_des_ede3_cbc(); */
788 case ENCTYPE_DES3_CBC_SHA
:
789 case ENCTYPE_DES3_CBC_RAW
:
790 return EVP_des_ede3_cbc();
799 * Return true:1 if p "looks like" the start of the real authenticator
800 * described in kssl_skip_confound() below. The ASN.1 pattern is "62 xx 30
801 * yy" (APPLICATION-2, SEQUENCE), where xx-yy =~ 2, and xx and yy are
802 * possibly multi-byte length fields.
804 static int kssl_test_confound(unsigned char *p
)
852 return (xx
- len
== yy
) ? 1 : 0;
856 * Allocate, fill, and return cksumlens array of checksum lengths. This
857 * array holds just the unique elements from the krb5_cksumarray[]. array[n]
858 * == 0 signals end of data. The krb5_cksumarray[] was an internal variable
859 * that has since been replaced by a more general method for storing the
860 * data. It should not be used. Instead we use real API calls and make a
861 * guess for what the highest assigned CKSUMTYPE_ constant is. As of 1.2.2
862 * it is 0x000c (CKSUMTYPE_HMAC_SHA1_DES3). So we will use 0x0010.
864 static size_t *populate_cksumlens(void)
867 static size_t *cklens
= NULL
;
869 # ifdef KRB5_MIT_OLD11
873 # endif /* KRB5_MIT_OLD11 */
875 # ifdef KRB5CHECKAUTH
876 if (!cklens
&& !(cklens
= (size_t *)calloc(sizeof(int), n
+ 1)))
879 for (i
= 0; i
< n
; i
++) {
880 if (!valid_cksumtype(i
))
881 continue; /* array has holes */
882 for (j
= 0; j
< n
; j
++) {
883 if (cklens
[j
] == 0) {
884 cklens
[j
] = krb5_checksum_size(NULL
, i
);
885 break; /* krb5 elem was new: add */
887 if (cklens
[j
] == krb5_checksum_size(NULL
, i
)) {
888 break; /* ignore duplicate elements */
892 # endif /* KRB5CHECKAUTH */
898 * Return pointer to start of real authenticator within authenticator, or
899 * return NULL on error.
900 * Decrypted authenticator looks like this:
901 * [0 or 8 byte confounder] [4-24 byte checksum] [real authent'r]
902 * This hackery wouldn't be necessary if MIT KRB5 1.0.6 had the
903 * krb5_auth_con_getcksumtype() function advertised in its krb5.h.
905 unsigned char *kssl_skip_confound(krb5_enctype etype
, unsigned char *a
)
909 static size_t *cksumlens
= NULL
;
910 unsigned char *test_auth
;
912 conlen
= (etype
) ? 8 : 0;
914 if (!cksumlens
&& !(cksumlens
= populate_cksumlens()))
916 for (i
= 0; (cklen
= cksumlens
[i
]) != 0; i
++) {
917 test_auth
= a
+ conlen
+ cklen
;
918 if (kssl_test_confound(test_auth
))
926 * Set kssl_err error info when reason text is a simple string kssl_err =
927 * struct { int reason; char text[KSSL_ERR_MAX+1]; }
929 void kssl_err_set(KSSL_ERR
*kssl_err
, int reason
, char *text
)
931 if (kssl_err
== NULL
)
934 kssl_err
->reason
= reason
;
935 BIO_snprintf(kssl_err
->text
, KSSL_ERR_MAX
, "%s", text
);
940 * Display contents of krb5_data struct, for debugging
942 void print_krb5_data(char *label
, krb5_data
*kdata
)
946 fprintf(stderr
, "%s[%d] ", label
, kdata
->length
);
947 for (i
= 0; i
< (int)kdata
->length
; i
++) {
948 if (0 && isprint((int)kdata
->data
[i
]))
949 fprintf(stderr
, "%c ", kdata
->data
[i
]);
951 fprintf(stderr
, "%02x ", (unsigned char)kdata
->data
[i
]);
953 fprintf(stderr
, "\n");
957 * Display contents of krb5_authdata struct, for debugging
959 void print_krb5_authdata(char *label
, krb5_authdata
**adata
)
962 fprintf(stderr
, "%s, authdata==0\n", label
);
965 fprintf(stderr
, "%s [%p]\n", label
, (void *)adata
);
969 fprintf(stderr
, "%s[at%d:%d] ", label
, adata
->ad_type
, adata
->length
);
970 for (i
= 0; i
< adata
->length
; i
++) {
971 fprintf(stderr
, (isprint(adata
->contents
[i
])) ? "%c " : "%02x",
974 fprintf(stderr
, "\n");
980 * Display contents of krb5_keyblock struct, for debugging
982 void print_krb5_keyblock(char *label
, krb5_keyblock
*keyblk
)
986 if (keyblk
== NULL
) {
987 fprintf(stderr
, "%s, keyblk==0\n", label
);
991 fprintf(stderr
, "%s\n\t[et%d:%d]: ", label
, keyblk
->keytype
,
992 keyblk
->keyvalue
->length
);
993 for (i
= 0; i
< (int)keyblk
->keyvalue
->length
; i
++) {
994 fprintf(stderr
, "%02x",
995 (unsigned char *)(keyblk
->keyvalue
->contents
)[i
]);
997 fprintf(stderr
, "\n");
999 fprintf(stderr
, "%s\n\t[et%d:%d]: ", label
, keyblk
->enctype
,
1001 for (i
= 0; i
< (int)keyblk
->length
; i
++) {
1002 fprintf(stderr
, "%02x", keyblk
->contents
[i
]);
1004 fprintf(stderr
, "\n");
1009 * Display contents of krb5_principal_data struct, for debugging
1010 * (krb5_principal is typedef'd == krb5_principal_data *)
1012 static void print_krb5_princ(char *label
, krb5_principal_data
*princ
)
1016 fprintf(stderr
, "%s principal Realm: ", label
);
1019 for (ui
= 0; ui
< (int)princ
->realm
.length
; ui
++)
1020 putchar(princ
->realm
.data
[ui
]);
1021 fprintf(stderr
, " (nametype %d) has %d strings:\n", princ
->type
,
1023 for (i
= 0; i
< (int)princ
->length
; i
++) {
1024 fprintf(stderr
, "\t%d [%d]: ", i
, princ
->data
[i
].length
);
1025 for (uj
= 0; uj
< (int)princ
->data
[i
].length
; uj
++) {
1026 putchar(princ
->data
[i
].data
[uj
]);
1028 fprintf(stderr
, "\n");
1033 /*- Given krb5 service (typically "kssl") and hostname in kssl_ctx,
1034 * Return encrypted Kerberos ticket for service @ hostname.
1035 * If authenp is non-NULL, also return encrypted authenticator,
1036 * whose data should be freed by caller.
1037 * (Originally was: Create Kerberos AP_REQ message for SSL Client.)
1039 * 19990628 VRS Started; Returns Kerberos AP_REQ message.
1040 * 20010409 VRS Modified for RFC2712; Returns enc tkt.
1041 * 20010606 VRS May also return optional authenticator.
1043 krb5_error_code
kssl_cget_tkt( /* UPDATE */ KSSL_CTX
*kssl_ctx
,
1046 */ krb5_data
**enc_ticketp
,
1049 */ krb5_data
*authenp
,
1052 */ KSSL_ERR
*kssl_err
)
1054 krb5_error_code krb5rc
= KRB5KRB_ERR_GENERIC
;
1055 krb5_context krb5context
= NULL
;
1056 krb5_auth_context krb5auth_context
= NULL
;
1057 krb5_ccache krb5ccdef
= NULL
;
1058 krb5_creds krb5creds
, *krb5credsp
= NULL
;
1059 krb5_data krb5_app_req
;
1061 kssl_err_set(kssl_err
, 0, "");
1062 memset((char *)&krb5creds
, 0, sizeof(krb5creds
));
1065 kssl_err_set(kssl_err
, SSL_R_KRB5_S_INIT
, "No kssl_ctx defined.\n");
1067 } else if (!kssl_ctx
->service_host
) {
1068 kssl_err_set(kssl_err
, SSL_R_KRB5_S_INIT
,
1069 "kssl_ctx service_host undefined.\n");
1073 if ((krb5rc
= krb5_init_context(&krb5context
)) != 0) {
1074 BIO_snprintf(kssl_err
->text
, KSSL_ERR_MAX
,
1075 "krb5_init_context() fails: %d\n", krb5rc
);
1076 kssl_err
->reason
= SSL_R_KRB5_C_INIT
;
1080 if ((krb5rc
= krb5_sname_to_principal(krb5context
,
1081 kssl_ctx
->service_host
,
1082 (kssl_ctx
->service_name
) ?
1083 kssl_ctx
->service_name
: KRB5SVC
,
1085 &krb5creds
.server
)) != 0) {
1086 BIO_snprintf(kssl_err
->text
, KSSL_ERR_MAX
,
1087 "krb5_sname_to_principal() fails for %s/%s\n",
1088 kssl_ctx
->service_host
,
1090 service_name
) ? kssl_ctx
->service_name
: KRB5SVC
);
1091 kssl_err
->reason
= SSL_R_KRB5_C_INIT
;
1095 if ((krb5rc
= krb5_cc_default(krb5context
, &krb5ccdef
)) != 0) {
1096 kssl_err_set(kssl_err
, SSL_R_KRB5_C_CC_PRINC
,
1097 "krb5_cc_default fails.\n");
1101 if ((krb5rc
= krb5_cc_get_principal(krb5context
, krb5ccdef
,
1102 &krb5creds
.client
)) != 0) {
1103 kssl_err_set(kssl_err
, SSL_R_KRB5_C_CC_PRINC
,
1104 "krb5_cc_get_principal() fails.\n");
1108 if ((krb5rc
= krb5_get_credentials(krb5context
, 0, krb5ccdef
,
1109 &krb5creds
, &krb5credsp
)) != 0) {
1110 kssl_err_set(kssl_err
, SSL_R_KRB5_C_GET_CRED
,
1111 "krb5_get_credentials() fails.\n");
1115 *enc_ticketp
= &krb5credsp
->ticket
;
1116 # ifdef KRB5_HEIMDAL
1117 kssl_ctx
->enctype
= krb5credsp
->session
.keytype
;
1119 kssl_ctx
->enctype
= krb5credsp
->keyblock
.enctype
;
1122 krb5rc
= KRB5KRB_ERR_GENERIC
;
1123 /* caller should free data of krb5_app_req */
1125 * 20010406 VRS deleted for real KerberosWrapper 20010605 VRS reinstated
1126 * to offer Authenticator to KerberosWrapper
1128 krb5_app_req
.length
= 0;
1130 krb5_data krb5in_data
;
1131 const unsigned char *p
;
1133 KRB5_APREQBODY
*ap_req
;
1135 authenp
->length
= 0;
1136 krb5in_data
.data
= NULL
;
1137 krb5in_data
.length
= 0;
1138 if ((krb5rc
= krb5_mk_req_extended(krb5context
,
1139 &krb5auth_context
, 0, &krb5in_data
,
1140 krb5credsp
, &krb5_app_req
)) != 0) {
1141 kssl_err_set(kssl_err
, SSL_R_KRB5_C_MK_REQ
,
1142 "krb5_mk_req_extended() fails.\n");
1146 arlen
= krb5_app_req
.length
;
1147 p
= (unsigned char *)krb5_app_req
.data
;
1148 ap_req
= (KRB5_APREQBODY
*)d2i_KRB5_APREQ(NULL
, &p
, arlen
);
1150 authenp
->length
= i2d_KRB5_ENCDATA(ap_req
->authenticator
, NULL
);
1151 if (authenp
->length
&& (authenp
->data
= malloc(authenp
->length
))) {
1152 unsigned char *adp
= (unsigned char *)authenp
->data
;
1154 i2d_KRB5_ENCDATA(ap_req
->authenticator
, &adp
);
1159 KRB5_APREQ_free((KRB5_APREQ
*) ap_req
);
1160 if (krb5_app_req
.length
)
1161 kssl_krb5_free_data_contents(krb5context
, &krb5_app_req
);
1163 # ifdef KRB5_HEIMDAL
1164 if (kssl_ctx_setkey(kssl_ctx
, &krb5credsp
->session
)) {
1165 kssl_err_set(kssl_err
, SSL_R_KRB5_C_INIT
,
1166 "kssl_ctx_setkey() fails.\n");
1169 if (kssl_ctx_setkey(kssl_ctx
, &krb5credsp
->keyblock
)) {
1170 kssl_err_set(kssl_err
, SSL_R_KRB5_C_INIT
,
1171 "kssl_ctx_setkey() fails.\n");
1179 kssl_ctx_show(kssl_ctx
);
1180 # endif /* KSSL_DEBUG */
1182 if (krb5creds
.client
)
1183 krb5_free_principal(krb5context
, krb5creds
.client
);
1184 if (krb5creds
.server
)
1185 krb5_free_principal(krb5context
, krb5creds
.server
);
1186 if (krb5auth_context
)
1187 krb5_auth_con_free(krb5context
, krb5auth_context
);
1189 krb5_free_context(krb5context
);
1194 * Given d2i_-decoded asn1ticket, allocate and return a new krb5_ticket.
1195 * Return Kerberos error code and kssl_err struct on error.
1196 * Allocates krb5_ticket and krb5_principal; caller should free these.
1198 * 20010410 VRS Implemented krb5_decode_ticket() as
1199 * old_krb5_decode_ticket(). Missing from MIT1.0.6.
1200 * 20010615 VRS Re-cast as openssl/asn1 d2i_*() functions.
1201 * Re-used some of the old krb5_decode_ticket()
1202 * code here. This tkt should alloc/free just
1203 * like the real thing.
1205 static krb5_error_code
kssl_TKT2tkt( /* IN */ krb5_context krb5context
,
1208 */ KRB5_TKTBODY
*asn1ticket
,
1211 */ krb5_ticket
**krb5ticket
,
1214 */ KSSL_ERR
*kssl_err
)
1216 krb5_error_code krb5rc
= KRB5KRB_ERR_GENERIC
;
1217 krb5_ticket
*new5ticket
= NULL
;
1218 ASN1_GENERALSTRING
*gstr_svc
, *gstr_host
;
1222 if (asn1ticket
== NULL
|| asn1ticket
->realm
== NULL
||
1223 asn1ticket
->sname
== NULL
||
1224 sk_ASN1_GENERALSTRING_num(asn1ticket
->sname
->namestring
) < 2) {
1225 BIO_snprintf(kssl_err
->text
, KSSL_ERR_MAX
,
1226 "Null field in asn1ticket.\n");
1227 kssl_err
->reason
= SSL_R_KRB5_S_RD_REQ
;
1228 return KRB5KRB_ERR_GENERIC
;
1231 if ((new5ticket
= (krb5_ticket
*)calloc(1, sizeof(krb5_ticket
))) == NULL
) {
1232 BIO_snprintf(kssl_err
->text
, KSSL_ERR_MAX
,
1233 "Unable to allocate new krb5_ticket.\n");
1234 kssl_err
->reason
= SSL_R_KRB5_S_RD_REQ
;
1235 return ENOMEM
; /* or KRB5KRB_ERR_GENERIC; */
1238 gstr_svc
= sk_ASN1_GENERALSTRING_value(asn1ticket
->sname
->namestring
, 0);
1239 gstr_host
= sk_ASN1_GENERALSTRING_value(asn1ticket
->sname
->namestring
, 1);
1241 if ((krb5rc
= kssl_build_principal_2(krb5context
,
1242 &new5ticket
->server
,
1243 asn1ticket
->realm
->length
,
1244 (char *)asn1ticket
->realm
->data
,
1246 (char *)gstr_svc
->data
,
1248 (char *)gstr_host
->data
)) != 0) {
1250 BIO_snprintf(kssl_err
->text
, KSSL_ERR_MAX
,
1251 "Error building ticket server principal.\n");
1252 kssl_err
->reason
= SSL_R_KRB5_S_RD_REQ
;
1253 return krb5rc
; /* or KRB5KRB_ERR_GENERIC; */
1256 krb5_princ_type(krb5context
, new5ticket
->server
) =
1257 asn1ticket
->sname
->nametype
->data
[0];
1258 new5ticket
->enc_part
.enctype
= asn1ticket
->encdata
->etype
->data
[0];
1259 new5ticket
->enc_part
.kvno
= asn1ticket
->encdata
->kvno
->data
[0];
1260 new5ticket
->enc_part
.ciphertext
.length
=
1261 asn1ticket
->encdata
->cipher
->length
;
1262 if ((new5ticket
->enc_part
.ciphertext
.data
=
1263 calloc(1, asn1ticket
->encdata
->cipher
->length
)) == NULL
) {
1265 BIO_snprintf(kssl_err
->text
, KSSL_ERR_MAX
,
1266 "Error allocating cipher in krb5ticket.\n");
1267 kssl_err
->reason
= SSL_R_KRB5_S_RD_REQ
;
1268 return KRB5KRB_ERR_GENERIC
;
1270 memcpy(new5ticket
->enc_part
.ciphertext
.data
,
1271 asn1ticket
->encdata
->cipher
->data
,
1272 asn1ticket
->encdata
->cipher
->length
);
1275 *krb5ticket
= new5ticket
;
1280 * Given krb5 service name in KSSL_CTX *kssl_ctx (typically "kssl"),
1281 * and krb5 AP_REQ message & message length,
1282 * Return Kerberos session key and client principle
1283 * to SSL Server in KSSL_CTX *kssl_ctx.
1285 * 19990702 VRS Started.
1287 krb5_error_code
kssl_sget_tkt( /* UPDATE */ KSSL_CTX
*kssl_ctx
,
1290 */ krb5_data
*indata
,
1293 */ krb5_ticket_times
*ttimes
,
1296 */ KSSL_ERR
*kssl_err
)
1298 krb5_error_code krb5rc
= KRB5KRB_ERR_GENERIC
;
1299 static krb5_context krb5context
= NULL
;
1300 static krb5_auth_context krb5auth_context
= NULL
;
1301 krb5_ticket
*krb5ticket
= NULL
;
1302 KRB5_TKTBODY
*asn1ticket
= NULL
;
1303 const unsigned char *p
;
1304 krb5_keytab krb5keytab
= NULL
;
1305 krb5_keytab_entry kt_entry
;
1306 krb5_principal krb5server
;
1307 krb5_rcache rcache
= NULL
;
1309 kssl_err_set(kssl_err
, 0, "");
1312 kssl_err_set(kssl_err
, SSL_R_KRB5_S_INIT
, "No kssl_ctx defined.\n");
1316 fprintf(stderr
, "in kssl_sget_tkt(%s)\n",
1317 kstring(kssl_ctx
->service_name
));
1318 # endif /* KSSL_DEBUG */
1320 if (!krb5context
&& (krb5rc
= krb5_init_context(&krb5context
))) {
1321 kssl_err_set(kssl_err
, SSL_R_KRB5_S_INIT
,
1322 "krb5_init_context() fails.\n");
1325 if (krb5auth_context
&&
1326 (krb5rc
= krb5_auth_con_free(krb5context
, krb5auth_context
))) {
1327 kssl_err_set(kssl_err
, SSL_R_KRB5_S_INIT
,
1328 "krb5_auth_con_free() fails.\n");
1331 krb5auth_context
= NULL
;
1332 if (!krb5auth_context
&&
1333 (krb5rc
= krb5_auth_con_init(krb5context
, &krb5auth_context
))) {
1334 kssl_err_set(kssl_err
, SSL_R_KRB5_S_INIT
,
1335 "krb5_auth_con_init() fails.\n");
1339 if ((krb5rc
= krb5_auth_con_getrcache(krb5context
, krb5auth_context
,
1341 kssl_err_set(kssl_err
, SSL_R_KRB5_S_INIT
,
1342 "krb5_auth_con_getrcache() fails.\n");
1346 if ((krb5rc
= krb5_sname_to_principal(krb5context
, NULL
,
1347 (kssl_ctx
->service_name
) ?
1348 kssl_ctx
->service_name
: KRB5SVC
,
1350 &krb5server
)) != 0) {
1351 kssl_err_set(kssl_err
, SSL_R_KRB5_S_INIT
,
1352 "krb5_sname_to_principal() fails.\n");
1356 if (rcache
== NULL
) {
1357 if ((krb5rc
= krb5_get_server_rcache(krb5context
,
1358 krb5_princ_component(krb5context
,
1362 kssl_err_set(kssl_err
, SSL_R_KRB5_S_INIT
,
1363 "krb5_get_server_rcache() fails.\n");
1369 krb5_auth_con_setrcache(krb5context
, krb5auth_context
, rcache
))) {
1370 kssl_err_set(kssl_err
, SSL_R_KRB5_S_INIT
,
1371 "krb5_auth_con_setrcache() fails.\n");
1376 * kssl_ctx->keytab_file == NULL ==> use Kerberos default
1378 if (kssl_ctx
->keytab_file
) {
1379 krb5rc
= krb5_kt_resolve(krb5context
, kssl_ctx
->keytab_file
,
1382 kssl_err_set(kssl_err
, SSL_R_KRB5_S_INIT
,
1383 "krb5_kt_resolve() fails.\n");
1387 krb5rc
= krb5_kt_default(krb5context
, &krb5keytab
);
1389 kssl_err_set(kssl_err
, SSL_R_KRB5_S_INIT
,
1390 "krb5_kt_default() fails.\n");
1395 /*- Actual Kerberos5 krb5_recvauth() has initial conversation here
1396 * o check KRB5_SENDAUTH_BADAUTHVERS
1397 * unless KRB5_RECVAUTH_SKIP_VERSION
1398 * o check KRB5_SENDAUTH_BADAPPLVERS
1399 * o send "0" msg if all OK
1403 * 20010411 was using AP_REQ instead of true KerberosWrapper
1405 * if ((krb5rc = krb5_rd_req(krb5context, &krb5auth_context,
1406 * &krb5in_data, krb5server, krb5keytab,
1407 * &ap_option, &krb5ticket)) != 0) { Error }
1410 p
= (unsigned char *)indata
->data
;
1411 if ((asn1ticket
= (KRB5_TKTBODY
*)d2i_KRB5_TICKET(NULL
, &p
,
1412 (long)indata
->length
))
1414 BIO_snprintf(kssl_err
->text
, KSSL_ERR_MAX
,
1415 "d2i_KRB5_TICKET() ASN.1 decode failure.\n");
1416 kssl_err
->reason
= SSL_R_KRB5_S_RD_REQ
;
1421 * Was: krb5rc = krb5_decode_ticket(krb5in_data,&krb5ticket)) != 0)
1423 if ((krb5rc
= kssl_TKT2tkt(krb5context
, asn1ticket
, &krb5ticket
,
1425 BIO_snprintf(kssl_err
->text
, KSSL_ERR_MAX
,
1426 "Error converting ASN.1 ticket to krb5_ticket.\n");
1427 kssl_err
->reason
= SSL_R_KRB5_S_RD_REQ
;
1431 if (!krb5_principal_compare(krb5context
, krb5server
, krb5ticket
->server
)) {
1432 krb5rc
= KRB5_PRINC_NOMATCH
;
1433 BIO_snprintf(kssl_err
->text
, KSSL_ERR_MAX
,
1434 "server principal != ticket principal\n");
1435 kssl_err
->reason
= SSL_R_KRB5_S_RD_REQ
;
1438 if ((krb5rc
= krb5_kt_get_entry(krb5context
, krb5keytab
,
1440 krb5ticket
->enc_part
.kvno
,
1441 krb5ticket
->enc_part
.enctype
,
1443 BIO_snprintf(kssl_err
->text
, KSSL_ERR_MAX
,
1444 "krb5_kt_get_entry() fails with %x.\n", krb5rc
);
1445 kssl_err
->reason
= SSL_R_KRB5_S_RD_REQ
;
1448 if ((krb5rc
= krb5_decrypt_tkt_part(krb5context
, &kt_entry
.key
,
1449 krb5ticket
)) != 0) {
1450 BIO_snprintf(kssl_err
->text
, KSSL_ERR_MAX
,
1451 "krb5_decrypt_tkt_part() failed.\n");
1452 kssl_err
->reason
= SSL_R_KRB5_S_RD_REQ
;
1455 krb5_kt_free_entry(krb5context
, &kt_entry
);
1459 krb5_address
**paddr
= krb5ticket
->enc_part2
->caddrs
;
1460 fprintf(stderr
, "Decrypted ticket fields:\n");
1461 fprintf(stderr
, "\tflags: %X, transit-type: %X",
1462 krb5ticket
->enc_part2
->flags
,
1463 krb5ticket
->enc_part2
->transited
.tr_type
);
1464 print_krb5_data("\ttransit-data: ",
1465 &(krb5ticket
->enc_part2
->transited
.tr_contents
));
1466 fprintf(stderr
, "\tcaddrs: %p, authdata: %p\n",
1467 krb5ticket
->enc_part2
->caddrs
,
1468 krb5ticket
->enc_part2
->authorization_data
);
1470 fprintf(stderr
, "\tcaddrs:\n");
1471 for (i
= 0; paddr
[i
] != NULL
; i
++) {
1473 d
.length
= paddr
[i
]->length
;
1474 d
.data
= paddr
[i
]->contents
;
1475 print_krb5_data("\t\tIP: ", &d
);
1478 fprintf(stderr
, "\tstart/auth/end times: %d / %d / %d\n",
1479 krb5ticket
->enc_part2
->times
.starttime
,
1480 krb5ticket
->enc_part2
->times
.authtime
,
1481 krb5ticket
->enc_part2
->times
.endtime
);
1483 # endif /* KSSL_DEBUG */
1486 krb5rc
= KRB5_NO_TKT_SUPPLIED
;
1487 if (!krb5ticket
|| !krb5ticket
->enc_part2
||
1488 !krb5ticket
->enc_part2
->client
||
1489 !krb5ticket
->enc_part2
->client
->data
||
1490 !krb5ticket
->enc_part2
->session
) {
1491 kssl_err_set(kssl_err
, SSL_R_KRB5_S_BAD_TICKET
,
1492 "bad ticket from krb5_rd_req.\n");
1493 } else if (kssl_ctx_setprinc(kssl_ctx
, KSSL_CLIENT
,
1494 &krb5ticket
->enc_part2
->client
->realm
,
1495 krb5ticket
->enc_part2
->client
->data
,
1496 krb5ticket
->enc_part2
->client
->length
)) {
1497 kssl_err_set(kssl_err
, SSL_R_KRB5_S_BAD_TICKET
,
1498 "kssl_ctx_setprinc() fails.\n");
1499 } else if (kssl_ctx_setkey(kssl_ctx
, krb5ticket
->enc_part2
->session
)) {
1500 kssl_err_set(kssl_err
, SSL_R_KRB5_S_BAD_TICKET
,
1501 "kssl_ctx_setkey() fails.\n");
1502 } else if (krb5ticket
->enc_part2
->flags
& TKT_FLG_INVALID
) {
1503 krb5rc
= KRB5KRB_AP_ERR_TKT_INVALID
;
1504 kssl_err_set(kssl_err
, SSL_R_KRB5_S_BAD_TICKET
,
1505 "invalid ticket from krb5_rd_req.\n");
1509 kssl_ctx
->enctype
= krb5ticket
->enc_part
.enctype
;
1510 ttimes
->authtime
= krb5ticket
->enc_part2
->times
.authtime
;
1511 ttimes
->starttime
= krb5ticket
->enc_part2
->times
.starttime
;
1512 ttimes
->endtime
= krb5ticket
->enc_part2
->times
.endtime
;
1513 ttimes
->renew_till
= krb5ticket
->enc_part2
->times
.renew_till
;
1517 kssl_ctx_show(kssl_ctx
);
1518 # endif /* KSSL_DEBUG */
1521 KRB5_TICKET_free((KRB5_TICKET
*) asn1ticket
);
1523 krb5_kt_close(krb5context
, krb5keytab
);
1525 krb5_free_ticket(krb5context
, krb5ticket
);
1527 krb5_free_principal(krb5context
, krb5server
);
1532 * Allocate & return a new kssl_ctx struct.
1534 KSSL_CTX
*kssl_ctx_new(void)
1536 return ((KSSL_CTX
*)kssl_calloc(1, sizeof(KSSL_CTX
)));
1540 * Frees a kssl_ctx struct and any allocated memory it holds. Returns NULL.
1542 KSSL_CTX
*kssl_ctx_free(KSSL_CTX
*kssl_ctx
)
1544 if (kssl_ctx
== NULL
)
1548 OPENSSL_cleanse(kssl_ctx
->key
, kssl_ctx
->length
);
1550 kssl_free(kssl_ctx
->key
);
1551 if (kssl_ctx
->client_princ
)
1552 kssl_free(kssl_ctx
->client_princ
);
1553 if (kssl_ctx
->service_host
)
1554 kssl_free(kssl_ctx
->service_host
);
1555 if (kssl_ctx
->service_name
)
1556 kssl_free(kssl_ctx
->service_name
);
1557 if (kssl_ctx
->keytab_file
)
1558 kssl_free(kssl_ctx
->keytab_file
);
1560 kssl_free(kssl_ctx
);
1561 return (KSSL_CTX
*)NULL
;
1565 * Given an array of (krb5_data *) entity (and optional realm), set the plain
1566 * (char *) client_princ or service_host member of the kssl_ctx struct.
1569 kssl_ctx_setprinc(KSSL_CTX
*kssl_ctx
, int which
,
1570 krb5_data
*realm
, krb5_data
*entity
, int nentities
)
1576 if (kssl_ctx
== NULL
|| entity
== NULL
)
1577 return KSSL_CTX_ERR
;
1581 princ
= &kssl_ctx
->client_princ
;
1584 princ
= &kssl_ctx
->service_host
;
1587 return KSSL_CTX_ERR
;
1593 /* Add up all the entity->lengths */
1595 for (i
= 0; i
< nentities
; i
++) {
1596 length
+= entity
[i
].length
;
1598 /* Add in space for the '/' character(s) (if any) */
1599 length
+= nentities
- 1;
1600 /* Space for the ('@'+realm+NULL | NULL) */
1601 length
+= ((realm
) ? realm
->length
+ 2 : 1);
1603 if ((*princ
= kssl_calloc(1, length
)) == NULL
)
1604 return KSSL_CTX_ERR
;
1606 for (i
= 0; i
< nentities
; i
++) {
1607 strncat(*princ
, entity
[i
].data
, entity
[i
].length
);
1608 if (i
< nentities
- 1) {
1609 strcat(*princ
, "/");
1613 strcat(*princ
, "@");
1614 (void)strncat(*princ
, realm
->data
, realm
->length
);
1621 /*- Set one of the plain (char *) string members of the kssl_ctx struct.
1622 * Default values should be:
1623 * which == KSSL_SERVICE => "khost" (KRB5SVC)
1624 * which == KSSL_KEYTAB => "/etc/krb5.keytab" (KRB5KEYTAB)
1626 krb5_error_code
kssl_ctx_setstring(KSSL_CTX
*kssl_ctx
, int which
, char *text
)
1631 return KSSL_CTX_ERR
;
1635 string
= &kssl_ctx
->service_name
;
1638 string
= &kssl_ctx
->service_host
;
1641 string
= &kssl_ctx
->client_princ
;
1644 string
= &kssl_ctx
->keytab_file
;
1647 return KSSL_CTX_ERR
;
1658 if ((*string
= kssl_calloc(1, strlen(text
) + 1)) == NULL
)
1659 return KSSL_CTX_ERR
;
1661 strcpy(*string
, text
);
1667 * Copy the Kerberos session key from a (krb5_keyblock *) to a kssl_ctx
1668 * struct. Clear kssl_ctx->key if Kerberos session key is NULL.
1670 krb5_error_code
kssl_ctx_setkey(KSSL_CTX
*kssl_ctx
, krb5_keyblock
*session
)
1673 krb5_enctype enctype
;
1674 krb5_octet FAR
*contents
= NULL
;
1677 return KSSL_CTX_ERR
;
1679 if (kssl_ctx
->key
) {
1680 OPENSSL_cleanse(kssl_ctx
->key
, kssl_ctx
->length
);
1681 kssl_free(kssl_ctx
->key
);
1686 # ifdef KRB5_HEIMDAL
1687 length
= session
->keyvalue
->length
;
1688 enctype
= session
->keytype
;
1689 contents
= session
->keyvalue
->contents
;
1691 length
= session
->length
;
1692 enctype
= session
->enctype
;
1693 contents
= session
->contents
;
1695 kssl_ctx
->enctype
= enctype
;
1696 kssl_ctx
->length
= length
;
1698 kssl_ctx
->enctype
= ENCTYPE_UNKNOWN
;
1699 kssl_ctx
->length
= 0;
1703 if ((kssl_ctx
->key
=
1704 (krb5_octet FAR
*)kssl_calloc(1, kssl_ctx
->length
)) == NULL
) {
1705 kssl_ctx
->length
= 0;
1706 return KSSL_CTX_ERR
;
1708 memcpy(kssl_ctx
->key
, contents
, length
);
1714 * Display contents of kssl_ctx struct
1716 void kssl_ctx_show(KSSL_CTX
*kssl_ctx
)
1720 printf("kssl_ctx: ");
1721 if (kssl_ctx
== NULL
) {
1725 printf("%p\n", (void *)kssl_ctx
);
1727 printf("\tservice:\t%s\n",
1728 (kssl_ctx
->service_name
) ? kssl_ctx
->service_name
: "NULL");
1729 printf("\tclient:\t%s\n",
1730 (kssl_ctx
->client_princ
) ? kssl_ctx
->client_princ
: "NULL");
1731 printf("\tserver:\t%s\n",
1732 (kssl_ctx
->service_host
) ? kssl_ctx
->service_host
: "NULL");
1733 printf("\tkeytab:\t%s\n",
1734 (kssl_ctx
->keytab_file
) ? kssl_ctx
->keytab_file
: "NULL");
1735 printf("\tkey [%d:%d]:\t", kssl_ctx
->enctype
, kssl_ctx
->length
);
1737 for (i
= 0; i
< kssl_ctx
->length
&& kssl_ctx
->key
; i
++) {
1738 printf("%02x", kssl_ctx
->key
[i
]);
1744 int kssl_keytab_is_available(KSSL_CTX
*kssl_ctx
)
1746 krb5_context krb5context
= NULL
;
1747 krb5_keytab krb5keytab
= NULL
;
1748 krb5_keytab_entry entry
;
1749 krb5_principal princ
= NULL
;
1750 krb5_error_code krb5rc
= KRB5KRB_ERR_GENERIC
;
1753 if ((krb5rc
= krb5_init_context(&krb5context
)))
1757 * kssl_ctx->keytab_file == NULL ==> use Kerberos default
1759 if (kssl_ctx
->keytab_file
) {
1760 krb5rc
= krb5_kt_resolve(krb5context
, kssl_ctx
->keytab_file
,
1765 krb5rc
= krb5_kt_default(krb5context
, &krb5keytab
);
1770 /* the host key we are looking for */
1771 krb5rc
= krb5_sname_to_principal(krb5context
, NULL
,
1773 service_name
? kssl_ctx
->service_name
:
1774 KRB5SVC
, KRB5_NT_SRV_HST
, &princ
);
1779 krb5rc
= krb5_kt_get_entry(krb5context
, krb5keytab
, princ
,
1782 /* IGNORE_ENCTYPE */
1784 if (krb5rc
== KRB5_KT_NOTFOUND
) {
1790 krb5_kt_free_entry(krb5context
, &entry
);
1795 krb5_kt_close(krb5context
, krb5keytab
);
1797 krb5_free_principal(krb5context
, princ
);
1799 krb5_free_context(krb5context
);
1803 int kssl_tgt_is_available(KSSL_CTX
*kssl_ctx
)
1805 krb5_error_code krb5rc
= KRB5KRB_ERR_GENERIC
;
1806 krb5_context krb5context
= NULL
;
1807 krb5_ccache krb5ccdef
= NULL
;
1808 krb5_creds krb5creds
, *krb5credsp
= NULL
;
1811 memset((char *)&krb5creds
, 0, sizeof(krb5creds
));
1816 if (!kssl_ctx
->service_host
)
1819 if ((krb5rc
= krb5_init_context(&krb5context
)) != 0)
1822 if ((krb5rc
= krb5_sname_to_principal(krb5context
,
1823 kssl_ctx
->service_host
,
1824 (kssl_ctx
->service_name
) ?
1825 kssl_ctx
->service_name
: KRB5SVC
,
1827 &krb5creds
.server
)) != 0)
1830 if ((krb5rc
= krb5_cc_default(krb5context
, &krb5ccdef
)) != 0)
1833 if ((krb5rc
= krb5_cc_get_principal(krb5context
, krb5ccdef
,
1834 &krb5creds
.client
)) != 0)
1837 if ((krb5rc
= krb5_get_credentials(krb5context
, 0, krb5ccdef
,
1838 &krb5creds
, &krb5credsp
)) != 0)
1845 kssl_ctx_show(kssl_ctx
);
1846 # endif /* KSSL_DEBUG */
1848 if (krb5creds
.client
)
1849 krb5_free_principal(krb5context
, krb5creds
.client
);
1850 if (krb5creds
.server
)
1851 krb5_free_principal(krb5context
, krb5creds
.server
);
1853 krb5_free_context(krb5context
);
1857 # if !defined(OPENSSL_SYS_WINDOWS) && !defined(OPENSSL_SYS_WIN32)
1858 void kssl_krb5_free_data_contents(krb5_context context
, krb5_data
*data
)
1860 # ifdef KRB5_HEIMDAL
1864 # elif defined(KRB5_MIT_OLD11)
1866 krb5_xfree(data
->data
);
1870 krb5_free_data_contents(NULL
, data
);
1874 /* !OPENSSL_SYS_WINDOWS && !OPENSSL_SYS_WIN32 */
1877 * Given pointers to KerberosTime and struct tm structs, convert the
1878 * KerberosTime string to struct tm. Note that KerberosTime is a
1879 * ASN1_GENERALIZEDTIME value, constrained to GMT with no fractional seconds
1880 * as defined in RFC 1510. Return pointer to the (partially) filled in
1881 * struct tm on success, return NULL on failure.
1883 static struct tm
*k_gmtime(ASN1_GENERALIZEDTIME
*gtime
, struct tm
*k_tm
)
1889 if (gtime
== NULL
|| gtime
->length
< 14)
1891 if (gtime
->data
== NULL
)
1894 p
= (char *)>ime
->data
[14];
1899 k_tm
->tm_sec
= atoi(p
);
1904 k_tm
->tm_min
= atoi(p
);
1909 k_tm
->tm_hour
= atoi(p
);
1914 k_tm
->tm_mday
= atoi(p
);
1919 k_tm
->tm_mon
= atoi(p
) - 1;
1924 k_tm
->tm_year
= atoi(p
) - 1900;
1931 * Helper function for kssl_validate_times(). We need context->clockskew,
1932 * but krb5_context is an opaque struct. So we try to sneek the clockskew
1933 * out through the replay cache. If that fails just return a likely default
1936 static krb5_deltat
get_rc_clockskew(krb5_context context
)
1939 krb5_deltat clockskew
;
1941 if (krb5_rc_default(context
, &rc
))
1942 return KSSL_CLOCKSKEW
;
1943 if (krb5_rc_initialize(context
, rc
, 0))
1944 return KSSL_CLOCKSKEW
;
1945 if (krb5_rc_get_lifespan(context
, rc
, &clockskew
)) {
1946 clockskew
= KSSL_CLOCKSKEW
;
1948 (void)krb5_rc_destroy(context
, rc
);
1953 * kssl_validate_times() combines (and more importantly exposes) the MIT KRB5
1954 * internal function krb5_validate_times() and the in_clock_skew() macro.
1955 * The authenticator client time is checked to be within clockskew secs of
1956 * the current time and the current time is checked to be within the ticket
1957 * start and expire times. Either check may be omitted by supplying a NULL
1958 * value. Returns 0 for valid times, SSL_R_KRB5* error codes otherwise. See
1959 * Also: (Kerberos source)/krb5/lib/krb5/krb/valid_times.c 20010420 VRS
1961 krb5_error_code
kssl_validate_times(krb5_timestamp atime
,
1962 krb5_ticket_times
*ttimes
)
1965 krb5_timestamp start
, now
;
1967 krb5_context context
;
1969 if ((rc
= krb5_init_context(&context
)))
1970 return SSL_R_KRB5_S_BAD_TICKET
;
1971 skew
= get_rc_clockskew(context
);
1972 if ((rc
= krb5_timeofday(context
, &now
)))
1973 return SSL_R_KRB5_S_BAD_TICKET
;
1974 krb5_free_context(context
);
1976 if (atime
&& labs(atime
- now
) >= skew
)
1977 return SSL_R_KRB5_S_TKT_SKEW
;
1982 start
= (ttimes
->starttime
!= 0) ? ttimes
->starttime
: ttimes
->authtime
;
1983 if (start
- now
> skew
)
1984 return SSL_R_KRB5_S_TKT_NYV
;
1985 if ((now
- ttimes
->endtime
) > skew
)
1986 return SSL_R_KRB5_S_TKT_EXPIRED
;
1989 fprintf(stderr
, "kssl_validate_times: %d |<- | %d - %d | < %d ->| %d\n",
1990 start
, atime
, now
, skew
, ttimes
->endtime
);
1991 # endif /* KSSL_DEBUG */
1997 * Decode and decrypt given DER-encoded authenticator, then pass
1998 * authenticator ctime back in *atimep (or 0 if time unavailable). Returns
1999 * krb5_error_code and kssl_err on error. A NULL authenticator
2000 * (authentp->length == 0) is not considered an error. Note that
2001 * kssl_check_authent() makes use of the KRB5 session key; you must call
2002 * kssl_sget_tkt() to get the key before calling this routine.
2004 krb5_error_code
kssl_check_authent(
2007 */ KSSL_CTX
*kssl_ctx
,
2010 */ krb5_data
*authentp
,
2013 */ krb5_timestamp
*atimep
,
2016 */ KSSL_ERR
*kssl_err
)
2018 krb5_error_code krb5rc
= 0;
2019 KRB5_ENCDATA
*dec_authent
= NULL
;
2020 KRB5_AUTHENTBODY
*auth
= NULL
;
2021 krb5_enctype enctype
;
2022 EVP_CIPHER_CTX ciph_ctx
;
2023 const EVP_CIPHER
*enc
= NULL
;
2024 unsigned char iv
[EVP_MAX_IV_LENGTH
];
2025 const unsigned char *p
;
2026 unsigned char *unenc_authent
;
2027 int outl
, unencbufsize
;
2028 struct tm tm_time
, *tm_l
, *tm_g
;
2029 time_t now
, tl
, tg
, tr
, tz_offset
;
2030 struct tm gmt_result
= {0};
2031 struct tm lt_result
= {0};
2033 EVP_CIPHER_CTX_init(&ciph_ctx
);
2035 kssl_err_set(kssl_err
, 0, "");
2037 # ifndef KRB5CHECKAUTH
2040 # if KRB5CHECKAUTH == 0
2043 # endif /* KRB5CHECKAUTH */
2045 if (authentp
== NULL
|| authentp
->length
== 0)
2051 fprintf(stderr
, "kssl_check_authent: authenticator[%d]:\n",
2054 for (ui
= 0; ui
< authentp
->length
; ui
++)
2055 fprintf(stderr
, "%02x ", p
[ui
]);
2056 fprintf(stderr
, "\n");
2058 # endif /* KSSL_DEBUG */
2060 unencbufsize
= 2 * authentp
->length
;
2061 if ((unenc_authent
= calloc(1, unencbufsize
)) == NULL
) {
2062 kssl_err_set(kssl_err
, SSL_R_KRB5_S_INIT
,
2063 "Unable to allocate authenticator buffer.\n");
2064 krb5rc
= KRB5KRB_ERR_GENERIC
;
2068 p
= (unsigned char *)authentp
->data
;
2069 if ((dec_authent
= d2i_KRB5_ENCDATA(NULL
, &p
,
2070 (long)authentp
->length
)) == NULL
) {
2071 kssl_err_set(kssl_err
, SSL_R_KRB5_S_INIT
,
2072 "Error decoding authenticator.\n");
2073 krb5rc
= KRB5KRB_AP_ERR_BAD_INTEGRITY
;
2077 enctype
= dec_authent
->etype
->data
[0]; /* should = kssl_ctx->enctype */
2078 # if !defined(KRB5_MIT_OLD11)
2080 case ENCTYPE_DES3_CBC_SHA1
: /* EVP_des_ede3_cbc(); */
2081 case ENCTYPE_DES3_CBC_SHA
:
2082 case ENCTYPE_DES3_CBC_RAW
:
2083 krb5rc
= 0; /* Skip, can't handle derived keys */
2087 enc
= kssl_map_enc(enctype
);
2088 memset(iv
, 0, sizeof(iv
)); /* per RFC 1510 */
2092 * Disable kssl_check_authent for ENCTYPE_DES3_CBC_SHA1. This
2093 * enctype indicates the authenticator was encrypted using key-usage
2094 * derived keys which openssl cannot decrypt.
2099 if (!EVP_CipherInit(&ciph_ctx
, enc
, kssl_ctx
->key
, iv
, 0)) {
2100 kssl_err_set(kssl_err
, SSL_R_KRB5_S_INIT
,
2101 "EVP_CipherInit error decrypting authenticator.\n");
2102 krb5rc
= KRB5KRB_AP_ERR_BAD_INTEGRITY
;
2105 outl
= dec_authent
->cipher
->length
;
2107 (&ciph_ctx
, unenc_authent
, dec_authent
->cipher
->data
, outl
)) {
2108 kssl_err_set(kssl_err
, SSL_R_KRB5_S_INIT
,
2109 "EVP_Cipher error decrypting authenticator.\n");
2110 krb5rc
= KRB5KRB_AP_ERR_BAD_INTEGRITY
;
2113 EVP_CIPHER_CTX_cleanup(&ciph_ctx
);
2118 fprintf(stderr
, "kssl_check_authent: decrypted authenticator[%d] =\n",
2120 for (padl
= 0; padl
< outl
; padl
++)
2121 fprintf(stderr
, "%02x ", unenc_authent
[padl
]);
2122 fprintf(stderr
, "\n");
2124 # endif /* KSSL_DEBUG */
2126 if ((p
= kssl_skip_confound(enctype
, unenc_authent
)) == NULL
) {
2127 kssl_err_set(kssl_err
, SSL_R_KRB5_S_INIT
,
2128 "confounded by authenticator.\n");
2129 krb5rc
= KRB5KRB_AP_ERR_BAD_INTEGRITY
;
2132 outl
-= p
- unenc_authent
;
2134 if ((auth
= (KRB5_AUTHENTBODY
*)d2i_KRB5_AUTHENT(NULL
, &p
,
2135 (long)outl
)) == NULL
) {
2136 kssl_err_set(kssl_err
, SSL_R_KRB5_S_INIT
,
2137 "Error decoding authenticator body.\n");
2138 krb5rc
= KRB5KRB_AP_ERR_BAD_INTEGRITY
;
2142 memset(&tm_time
, 0, sizeof(struct tm
));
2143 if (k_gmtime(auth
->ctime
, &tm_time
) &&
2144 ((tr
= mktime(&tm_time
)) != (time_t)(-1))) {
2146 tm_g
= OPENSSL_gmtime(&now
, &gmt_result
);
2148 # if defined(OPENSSL_THREADS) && !defined(OPENSSL_SYS_WIN32) && \
2149 !defined(OPENSSL_SYS_OS2) && !defined(OPENSSL_SYS_SUNOS) && \
2150 (!defined(OPENSSL_SYS_VMS) || defined(localtime_r))
2151 tm_l
= localtime_r(&now
, <_result
);
2153 tm_l
= localtime(&now
);
2158 tz_offset
= tg
- tl
;
2160 *atimep
= (krb5_timestamp
)(tr
- tz_offset
);
2163 fprintf(stderr
, "kssl_check_authent: returns %d for client time ",
2165 if (auth
&& auth
->ctime
&& auth
->ctime
->length
&& auth
->ctime
->data
)
2166 fprintf(stderr
, "%.*s\n", auth
->ctime
->length
, auth
->ctime
->data
);
2168 fprintf(stderr
, "NULL\n");
2169 # endif /* KSSL_DEBUG */
2173 KRB5_AUTHENT_free((KRB5_AUTHENT
*) auth
);
2175 KRB5_ENCDATA_free(dec_authent
);
2177 free(unenc_authent
);
2178 EVP_CIPHER_CTX_cleanup(&ciph_ctx
);
2183 * Replaces krb5_build_principal_ext(), with varargs length == 2 (svc, host),
2184 * because I don't know how to stub varargs. Returns krb5_error_code ==
2185 * ENOMEM on alloc error, otherwise passes back newly constructed principal,
2186 * which should be freed by caller.
2188 krb5_error_code
kssl_build_principal_2(
2191 */ krb5_context context
,
2194 */ krb5_principal
*princ
,
2197 */ int rlen
, const char *realm
,
2200 */ int slen
, const char *svc
,
2203 */ int hlen
, const char *host
)
2205 krb5_data
*p_data
= NULL
;
2206 krb5_principal new_p
= NULL
;
2209 if ((p_data
= (krb5_data
*)calloc(2, sizeof(krb5_data
))) == NULL
||
2210 (new_p
= (krb5_principal
)calloc(1, sizeof(krb5_principal_data
)))
2214 new_p
->data
= p_data
;
2216 if ((new_r
= calloc(1, rlen
+ 1)) == NULL
)
2218 memcpy(new_r
, realm
, rlen
);
2219 krb5_princ_set_realm_length(context
, new_p
, rlen
);
2220 krb5_princ_set_realm_data(context
, new_p
, new_r
);
2222 if ((new_p
->data
[0].data
= calloc(1, slen
+ 1)) == NULL
)
2224 memcpy(new_p
->data
[0].data
, svc
, slen
);
2225 new_p
->data
[0].length
= slen
;
2227 if ((new_p
->data
[1].data
= calloc(1, hlen
+ 1)) == NULL
)
2229 memcpy(new_p
->data
[1].data
, host
, hlen
);
2230 new_p
->data
[1].length
= hlen
;
2232 krb5_princ_type(context
, new_p
) = KRB5_NT_UNKNOWN
;
2237 if (new_p
&& new_p
[0].data
)
2238 free(new_p
[0].data
);
2239 if (new_p
&& new_p
[1].data
)
2240 free(new_p
[1].data
);
2248 void SSL_set0_kssl_ctx(SSL
*s
, KSSL_CTX
*kctx
)
2253 KSSL_CTX
*SSL_get0_kssl_ctx(SSL
*s
)
2258 char *kssl_ctx_get0_client_princ(KSSL_CTX
*kctx
)
2261 return kctx
->client_princ
;
2265 #else /* !OPENSSL_NO_KRB5 */
2267 # if defined(PEDANTIC) || defined(OPENSSL_SYS_VMS)
2268 static void *dummy
= &dummy
;
2271 #endif /* !OPENSSL_NO_KRB5 */