1 /* ssl/kssl.c -*- mode: C; c-file-style: "eay" -*- */
2 /* Written by Vern Staats <staatsvr@asc.hpc.mil> for the OpenSSL project 2000.
4 /* ====================================================================
5 * Copyright (c) 2000 The OpenSSL Project. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
19 * 3. All advertising materials mentioning features or use of this
20 * software must display the following acknowledgment:
21 * "This product includes software developed by the OpenSSL Project
22 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
25 * endorse or promote products derived from this software without
26 * prior written permission. For written permission, please contact
27 * licensing@OpenSSL.org.
29 * 5. Products derived from this software may not be called "OpenSSL"
30 * nor may "OpenSSL" appear in their names without prior written
31 * permission of the OpenSSL Project.
33 * 6. Redistributions of any form whatsoever must retain the following
35 * "This product includes software developed by the OpenSSL Project
36 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
39 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
40 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
41 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
42 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
44 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
45 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
46 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
47 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
49 * OF THE POSSIBILITY OF SUCH DAMAGE.
50 * ====================================================================
52 * This product includes cryptographic software written by Eric Young
53 * (eay@cryptsoft.com). This product includes software written by Tim
54 * Hudson (tjh@cryptsoft.com).
59 /* ssl/kssl.c -- Routines to support (& debug) Kerberos5 auth for openssl
61 ** 19990701 VRS Started.
62 ** 200011?? Jeffrey Altman, Richard Levitte
63 ** Generalized for Heimdal, Newer MIT, & Win32.
64 ** Integrated into main OpenSSL 0.9.7 snapshots.
65 ** 20010413 Simon Wilkinson, VRS
66 ** Real RFC2712 KerberosWrapper replaces AP_REQ.
69 #include <openssl/opensslconf.h>
73 #define KRB5_PRIVATE 1
75 #include <openssl/ssl.h>
76 #include <openssl/evp.h>
77 #include <openssl/objects.h>
78 #include <openssl/krb5_asn.h>
81 #ifndef OPENSSL_NO_KRB5
84 #define ENOMEM KRB5KRB_ERR_GENERIC
88 * When OpenSSL is built on Windows, we do not want to require that
89 * the Kerberos DLLs be available in order for the OpenSSL DLLs to
90 * work. Therefore, all Kerberos routines are loaded at run time
91 * and we do not link to a .LIB file.
94 #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32)
96 * The purpose of the following pre-processor statements is to provide
97 * compatibility with different releases of MIT Kerberos for Windows.
98 * All versions up to 1.2 used macros. But macros do not allow for
99 * a binary compatible interface for DLLs. Therefore, all macros are
100 * being replaced by function calls. The following code will allow
101 * an OpenSSL DLL built on Windows to work whether or not the macro
102 * or function form of the routines are utilized.
104 #ifdef krb5_cc_get_principal
105 #define NO_DEF_KRB5_CCACHE
106 #undef krb5_cc_get_principal
108 #define krb5_cc_get_principal kssl_krb5_cc_get_principal
110 #define krb5_free_data_contents kssl_krb5_free_data_contents
111 #define krb5_free_context kssl_krb5_free_context
112 #define krb5_auth_con_free kssl_krb5_auth_con_free
113 #define krb5_free_principal kssl_krb5_free_principal
114 #define krb5_mk_req_extended kssl_krb5_mk_req_extended
115 #define krb5_get_credentials kssl_krb5_get_credentials
116 #define krb5_cc_default kssl_krb5_cc_default
117 #define krb5_sname_to_principal kssl_krb5_sname_to_principal
118 #define krb5_init_context kssl_krb5_init_context
119 #define krb5_free_ticket kssl_krb5_free_ticket
120 #define krb5_rd_req kssl_krb5_rd_req
121 #define krb5_kt_default kssl_krb5_kt_default
122 #define krb5_kt_resolve kssl_krb5_kt_resolve
123 /* macros in mit 1.2.2 and earlier; functions in mit 1.2.3 and greater */
124 #ifndef krb5_kt_close
125 #define krb5_kt_close kssl_krb5_kt_close
126 #endif /* krb5_kt_close */
127 #ifndef krb5_kt_get_entry
128 #define krb5_kt_get_entry kssl_krb5_kt_get_entry
129 #endif /* krb5_kt_get_entry */
130 #define krb5_auth_con_init kssl_krb5_auth_con_init
132 #define krb5_principal_compare kssl_krb5_principal_compare
133 #define krb5_decrypt_tkt_part kssl_krb5_decrypt_tkt_part
134 #define krb5_timeofday kssl_krb5_timeofday
135 #define krb5_rc_default kssl_krb5_rc_default
137 #ifdef krb5_rc_initialize
138 #undef krb5_rc_initialize
140 #define krb5_rc_initialize kssl_krb5_rc_initialize
142 #ifdef krb5_rc_get_lifespan
143 #undef krb5_rc_get_lifespan
145 #define krb5_rc_get_lifespan kssl_krb5_rc_get_lifespan
147 #ifdef krb5_rc_destroy
148 #undef krb5_rc_destroy
150 #define krb5_rc_destroy kssl_krb5_rc_destroy
152 #define valid_cksumtype kssl_valid_cksumtype
153 #define krb5_checksum_size kssl_krb5_checksum_size
154 #define krb5_kt_free_entry kssl_krb5_kt_free_entry
155 #define krb5_auth_con_setrcache kssl_krb5_auth_con_setrcache
156 #define krb5_auth_con_getrcache kssl_krb5_auth_con_getrcache
157 #define krb5_get_server_rcache kssl_krb5_get_server_rcache
159 /* Prototypes for built in stubs */
160 void kssl_krb5_free_data_contents(krb5_context
, krb5_data
*);
161 void kssl_krb5_free_principal(krb5_context
, krb5_principal
);
162 krb5_error_code
kssl_krb5_kt_resolve(krb5_context
,
165 krb5_error_code
kssl_krb5_kt_default(krb5_context
,
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
,
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
,
189 krb5_error_code
kssl_krb5_get_credentials(krb5_context
,
190 krb5_const krb5_flags
,
194 krb5_error_code
kssl_krb5_auth_con_init(krb5_context
,
195 krb5_auth_context
*);
196 krb5_error_code
kssl_krb5_cc_get_principal(krb5_context context
,
198 krb5_principal
*principal
);
199 krb5_error_code
kssl_krb5_auth_con_free(krb5_context
,krb5_auth_context
);
200 size_t kssl_krb5_checksum_size(krb5_context context
,krb5_cksumtype ctype
);
201 krb5_boolean
kssl_valid_cksumtype(krb5_cksumtype ctype
);
202 krb5_error_code
krb5_kt_free_entry(krb5_context
,krb5_keytab_entry FAR
* );
203 krb5_error_code
kssl_krb5_auth_con_setrcache(krb5_context
,
206 krb5_error_code
kssl_krb5_get_server_rcache(krb5_context
,
207 krb5_const krb5_data
*,
209 krb5_error_code
kssl_krb5_auth_con_getrcache(krb5_context
,
213 /* Function pointers (almost all Kerberos functions are _stdcall) */
214 static void (_stdcall
*p_krb5_free_data_contents
)(krb5_context
, krb5_data
*)
216 static void (_stdcall
*p_krb5_free_principal
)(krb5_context
, krb5_principal
)
218 static krb5_error_code(_stdcall
*p_krb5_kt_resolve
)
219 (krb5_context
, krb5_const
char *, krb5_keytab
*)=NULL
;
220 static krb5_error_code (_stdcall
*p_krb5_kt_default
)(krb5_context
,
222 static krb5_error_code (_stdcall
*p_krb5_free_ticket
)(krb5_context
,
224 static krb5_error_code (_stdcall
*p_krb5_rd_req
)(krb5_context
,
226 krb5_const krb5_data
*,
227 krb5_const_principal
,
228 krb5_keytab
, krb5_flags
*,
229 krb5_ticket
**)=NULL
;
230 static krb5_error_code (_stdcall
*p_krb5_mk_req_extended
)
231 (krb5_context
, krb5_auth_context
*,
232 krb5_const krb5_flags
, krb5_data
*, krb5_creds
*,
234 static krb5_error_code (_stdcall
*p_krb5_init_context
)(krb5_context
*)=NULL
;
235 static void (_stdcall
*p_krb5_free_context
)(krb5_context
)=NULL
;
236 static krb5_error_code (_stdcall
*p_krb5_cc_default
)(krb5_context
,
238 static krb5_error_code (_stdcall
*p_krb5_sname_to_principal
)
239 (krb5_context
, krb5_const
char *, krb5_const
char *,
240 krb5_int32
, krb5_principal
*)=NULL
;
241 static krb5_error_code (_stdcall
*p_krb5_get_credentials
)
242 (krb5_context
, krb5_const krb5_flags
, krb5_ccache
,
243 krb5_creds
*, krb5_creds
**)=NULL
;
244 static krb5_error_code (_stdcall
*p_krb5_auth_con_init
)
245 (krb5_context
, krb5_auth_context
*)=NULL
;
246 static krb5_error_code (_stdcall
*p_krb5_cc_get_principal
)
247 (krb5_context context
, krb5_ccache cache
,
248 krb5_principal
*principal
)=NULL
;
249 static krb5_error_code (_stdcall
*p_krb5_auth_con_free
)
250 (krb5_context
, krb5_auth_context
)=NULL
;
251 static krb5_error_code (_stdcall
*p_krb5_decrypt_tkt_part
)
252 (krb5_context
, krb5_const krb5_keyblock
*,
254 static krb5_error_code (_stdcall
*p_krb5_timeofday
)
255 (krb5_context context
, krb5_int32
*timeret
)=NULL
;
256 static krb5_error_code (_stdcall
*p_krb5_rc_default
)
257 (krb5_context context
, krb5_rcache
*rc
)=NULL
;
258 static krb5_error_code (_stdcall
*p_krb5_rc_initialize
)
259 (krb5_context context
, krb5_rcache rc
,
260 krb5_deltat lifespan
)=NULL
;
261 static krb5_error_code (_stdcall
*p_krb5_rc_get_lifespan
)
262 (krb5_context context
, krb5_rcache rc
,
263 krb5_deltat
*lifespan
)=NULL
;
264 static krb5_error_code (_stdcall
*p_krb5_rc_destroy
)
265 (krb5_context context
, krb5_rcache rc
)=NULL
;
266 static krb5_boolean (_stdcall
*p_krb5_principal_compare
)
267 (krb5_context
, krb5_const_principal
, krb5_const_principal
)=NULL
;
268 static size_t (_stdcall
*p_krb5_checksum_size
)(krb5_context context
,krb5_cksumtype ctype
)=NULL
;
269 static krb5_boolean (_stdcall
*p_valid_cksumtype
)(krb5_cksumtype ctype
)=NULL
;
270 static krb5_error_code (_stdcall
*p_krb5_kt_free_entry
)
271 (krb5_context
,krb5_keytab_entry
* )=NULL
;
272 static krb5_error_code (_stdcall
* p_krb5_auth_con_setrcache
)(krb5_context
,
275 static krb5_error_code (_stdcall
* p_krb5_get_server_rcache
)(krb5_context
,
276 krb5_const krb5_data
*,
278 static krb5_error_code (* p_krb5_auth_con_getrcache
)(krb5_context
,
281 static krb5_error_code (_stdcall
* p_krb5_kt_close
)(krb5_context context
,
282 krb5_keytab keytab
)=NULL
;
283 static krb5_error_code (_stdcall
* p_krb5_kt_get_entry
)(krb5_context context
,
285 krb5_const_principal principal
, krb5_kvno vno
,
286 krb5_enctype enctype
, krb5_keytab_entry
*entry
)=NULL
;
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 */
296 hKRB5_32
= LoadLibrary(TEXT("KRB5_32"));
300 (FARPROC
) p_krb5_free_data_contents
=
301 GetProcAddress( hKRB5_32
, "krb5_free_data_contents" );
302 (FARPROC
) p_krb5_free_context
=
303 GetProcAddress( hKRB5_32
, "krb5_free_context" );
304 (FARPROC
) p_krb5_auth_con_free
=
305 GetProcAddress( hKRB5_32
, "krb5_auth_con_free" );
306 (FARPROC
) p_krb5_free_principal
=
307 GetProcAddress( hKRB5_32
, "krb5_free_principal" );
308 (FARPROC
) p_krb5_mk_req_extended
=
309 GetProcAddress( hKRB5_32
, "krb5_mk_req_extended" );
310 (FARPROC
) p_krb5_get_credentials
=
311 GetProcAddress( hKRB5_32
, "krb5_get_credentials" );
312 (FARPROC
) p_krb5_cc_get_principal
=
313 GetProcAddress( hKRB5_32
, "krb5_cc_get_principal" );
314 (FARPROC
) p_krb5_cc_default
=
315 GetProcAddress( hKRB5_32
, "krb5_cc_default" );
316 (FARPROC
) p_krb5_sname_to_principal
=
317 GetProcAddress( hKRB5_32
, "krb5_sname_to_principal" );
318 (FARPROC
) p_krb5_init_context
=
319 GetProcAddress( hKRB5_32
, "krb5_init_context" );
320 (FARPROC
) p_krb5_free_ticket
=
321 GetProcAddress( hKRB5_32
, "krb5_free_ticket" );
322 (FARPROC
) p_krb5_rd_req
=
323 GetProcAddress( hKRB5_32
, "krb5_rd_req" );
324 (FARPROC
) p_krb5_principal_compare
=
325 GetProcAddress( hKRB5_32
, "krb5_principal_compare" );
326 (FARPROC
) p_krb5_decrypt_tkt_part
=
327 GetProcAddress( hKRB5_32
, "krb5_decrypt_tkt_part" );
328 (FARPROC
) p_krb5_timeofday
=
329 GetProcAddress( hKRB5_32
, "krb5_timeofday" );
330 (FARPROC
) p_krb5_rc_default
=
331 GetProcAddress( hKRB5_32
, "krb5_rc_default" );
332 (FARPROC
) p_krb5_rc_initialize
=
333 GetProcAddress( hKRB5_32
, "krb5_rc_initialize" );
334 (FARPROC
) p_krb5_rc_get_lifespan
=
335 GetProcAddress( hKRB5_32
, "krb5_rc_get_lifespan" );
336 (FARPROC
) p_krb5_rc_destroy
=
337 GetProcAddress( hKRB5_32
, "krb5_rc_destroy" );
338 (FARPROC
) p_krb5_kt_default
=
339 GetProcAddress( hKRB5_32
, "krb5_kt_default" );
340 (FARPROC
) p_krb5_kt_resolve
=
341 GetProcAddress( hKRB5_32
, "krb5_kt_resolve" );
342 (FARPROC
) p_krb5_auth_con_init
=
343 GetProcAddress( hKRB5_32
, "krb5_auth_con_init" );
344 (FARPROC
) p_valid_cksumtype
=
345 GetProcAddress( hKRB5_32
, "valid_cksumtype" );
346 (FARPROC
) p_krb5_checksum_size
=
347 GetProcAddress( hKRB5_32
, "krb5_checksum_size" );
348 (FARPROC
) p_krb5_kt_free_entry
=
349 GetProcAddress( hKRB5_32
, "krb5_kt_free_entry" );
350 (FARPROC
) p_krb5_auth_con_setrcache
=
351 GetProcAddress( hKRB5_32
, "krb5_auth_con_setrcache" );
352 (FARPROC
) p_krb5_get_server_rcache
=
353 GetProcAddress( hKRB5_32
, "krb5_get_server_rcache" );
354 (FARPROC
) p_krb5_auth_con_getrcache
=
355 GetProcAddress( hKRB5_32
, "krb5_auth_con_getrcache" );
356 (FARPROC
) p_krb5_kt_close
=
357 GetProcAddress( hKRB5_32
, "krb5_kt_close" );
358 (FARPROC
) p_krb5_kt_get_entry
=
359 GetProcAddress( hKRB5_32
, "krb5_kt_get_entry" );
362 /* Stubs for each function to be dynamicly loaded */
364 kssl_krb5_free_data_contents(krb5_context CO
, krb5_data
* data
)
369 if ( p_krb5_free_data_contents
)
370 p_krb5_free_data_contents(CO
,data
);
374 kssl_krb5_mk_req_extended (krb5_context CO
,
375 krb5_auth_context
* pACO
,
376 krb5_const krb5_flags F
,
384 if ( p_krb5_mk_req_extended
)
385 return(p_krb5_mk_req_extended(CO
,pACO
,F
,pD1
,pC
,pD2
));
387 return KRB5KRB_ERR_GENERIC
;
390 kssl_krb5_auth_con_init(krb5_context CO
,
391 krb5_auth_context
* pACO
)
396 if ( p_krb5_auth_con_init
)
397 return(p_krb5_auth_con_init(CO
,pACO
));
399 return KRB5KRB_ERR_GENERIC
;
402 kssl_krb5_auth_con_free (krb5_context CO
,
403 krb5_auth_context ACO
)
408 if ( p_krb5_auth_con_free
)
409 return(p_krb5_auth_con_free(CO
,ACO
));
411 return KRB5KRB_ERR_GENERIC
;
414 kssl_krb5_get_credentials(krb5_context CO
,
415 krb5_const krb5_flags F
,
423 if ( p_krb5_get_credentials
)
424 return(p_krb5_get_credentials(CO
,F
,CC
,pCR
,ppCR
));
426 return KRB5KRB_ERR_GENERIC
;
429 kssl_krb5_sname_to_principal(krb5_context CO
,
430 krb5_const
char * pC1
,
431 krb5_const
char * pC2
,
433 krb5_principal
* pPR
)
438 if ( p_krb5_sname_to_principal
)
439 return(p_krb5_sname_to_principal(CO
,pC1
,pC2
,I
,pPR
));
441 return KRB5KRB_ERR_GENERIC
;
445 kssl_krb5_cc_default(krb5_context CO
,
451 if ( p_krb5_cc_default
)
452 return(p_krb5_cc_default(CO
,pCC
));
454 return KRB5KRB_ERR_GENERIC
;
458 kssl_krb5_init_context(krb5_context
* pCO
)
463 if ( p_krb5_init_context
)
464 return(p_krb5_init_context(pCO
));
466 return KRB5KRB_ERR_GENERIC
;
470 kssl_krb5_free_context(krb5_context CO
)
475 if ( p_krb5_free_context
)
476 p_krb5_free_context(CO
);
480 kssl_krb5_free_principal(krb5_context c
, krb5_principal p
)
485 if ( p_krb5_free_principal
)
486 p_krb5_free_principal(c
,p
);
490 kssl_krb5_kt_resolve(krb5_context con
,
491 krb5_const
char * sz
,
497 if ( p_krb5_kt_resolve
)
498 return(p_krb5_kt_resolve(con
,sz
,kt
));
500 return KRB5KRB_ERR_GENERIC
;
504 kssl_krb5_kt_default(krb5_context con
,
510 if ( p_krb5_kt_default
)
511 return(p_krb5_kt_default(con
,kt
));
513 return KRB5KRB_ERR_GENERIC
;
517 kssl_krb5_free_ticket(krb5_context con
,
523 if ( p_krb5_free_ticket
)
524 return(p_krb5_free_ticket(con
,kt
));
526 return KRB5KRB_ERR_GENERIC
;
530 kssl_krb5_rd_req(krb5_context con
, krb5_auth_context
* pacon
,
531 krb5_const krb5_data
* data
,
532 krb5_const_principal princ
, krb5_keytab keytab
,
533 krb5_flags
* flags
, krb5_ticket
** pptkt
)
539 return(p_krb5_rd_req(con
,pacon
,data
,princ
,keytab
,flags
,pptkt
));
541 return KRB5KRB_ERR_GENERIC
;
545 krb5_principal_compare(krb5_context con
, krb5_const_principal princ1
,
546 krb5_const_principal princ2
)
551 if ( p_krb5_principal_compare
)
552 return(p_krb5_principal_compare(con
,princ1
,princ2
));
554 return KRB5KRB_ERR_GENERIC
;
558 krb5_decrypt_tkt_part(krb5_context con
, krb5_const krb5_keyblock
*keys
,
564 if ( p_krb5_decrypt_tkt_part
)
565 return(p_krb5_decrypt_tkt_part(con
,keys
,ticket
));
567 return KRB5KRB_ERR_GENERIC
;
571 krb5_timeofday(krb5_context con
, krb5_int32
*timeret
)
576 if ( p_krb5_timeofday
)
577 return(p_krb5_timeofday(con
,timeret
));
579 return KRB5KRB_ERR_GENERIC
;
583 krb5_rc_default(krb5_context con
, krb5_rcache
*rc
)
588 if ( p_krb5_rc_default
)
589 return(p_krb5_rc_default(con
,rc
));
591 return KRB5KRB_ERR_GENERIC
;
595 krb5_rc_initialize(krb5_context con
, krb5_rcache rc
, krb5_deltat lifespan
)
600 if ( p_krb5_rc_initialize
)
601 return(p_krb5_rc_initialize(con
, rc
, lifespan
));
603 return KRB5KRB_ERR_GENERIC
;
607 krb5_rc_get_lifespan(krb5_context con
, krb5_rcache rc
, krb5_deltat
*lifespanp
)
612 if ( p_krb5_rc_get_lifespan
)
613 return(p_krb5_rc_get_lifespan(con
, rc
, lifespanp
));
615 return KRB5KRB_ERR_GENERIC
;
619 krb5_rc_destroy(krb5_context con
, krb5_rcache rc
)
624 if ( p_krb5_rc_destroy
)
625 return(p_krb5_rc_destroy(con
, rc
));
627 return KRB5KRB_ERR_GENERIC
;
631 krb5_checksum_size(krb5_context context
,krb5_cksumtype ctype
)
636 if ( p_krb5_checksum_size
)
637 return(p_krb5_checksum_size(context
, ctype
));
639 return KRB5KRB_ERR_GENERIC
;
643 valid_cksumtype(krb5_cksumtype ctype
)
648 if ( p_valid_cksumtype
)
649 return(p_valid_cksumtype(ctype
));
651 return KRB5KRB_ERR_GENERIC
;
655 krb5_kt_free_entry(krb5_context con
,krb5_keytab_entry
* entry
)
660 if ( p_krb5_kt_free_entry
)
661 return(p_krb5_kt_free_entry(con
,entry
));
663 return KRB5KRB_ERR_GENERIC
;
666 /* Structure definitions */
667 #ifndef NO_DEF_KRB5_CCACHE
669 #define krb5_x(ptr,args) ((ptr)?((*(ptr)) args):(abort(),1))
670 #define krb5_xc(ptr,args) ((ptr)?((*(ptr)) args):(abort(),(char*)0))
673 typedef krb5_pointer krb5_cc_cursor
; /* cursor for sequential lookup */
675 typedef struct _krb5_ccache
678 struct _krb5_cc_ops FAR
*ops
;
682 typedef struct _krb5_cc_ops
686 char * (KRB5_CALLCONV
*get_name
)
687 (krb5_context
, krb5_ccache
);
688 krb5_error_code (KRB5_CALLCONV
*resolve
)
689 (krb5_context
, krb5_ccache
*, const char *);
690 krb5_error_code (KRB5_CALLCONV
*gen_new
)
691 (krb5_context
, krb5_ccache
*);
692 krb5_error_code (KRB5_CALLCONV
*init
)
693 (krb5_context
, krb5_ccache
, krb5_principal
);
694 krb5_error_code (KRB5_CALLCONV
*destroy
)
695 (krb5_context
, krb5_ccache
);
696 krb5_error_code (KRB5_CALLCONV
*close
)
697 (krb5_context
, krb5_ccache
);
698 krb5_error_code (KRB5_CALLCONV
*store
)
699 (krb5_context
, krb5_ccache
, krb5_creds
*);
700 krb5_error_code (KRB5_CALLCONV
*retrieve
)
701 (krb5_context
, krb5_ccache
,
702 krb5_flags
, krb5_creds
*, krb5_creds
*);
703 krb5_error_code (KRB5_CALLCONV
*get_princ
)
704 (krb5_context
, krb5_ccache
, krb5_principal
*);
705 krb5_error_code (KRB5_CALLCONV
*get_first
)
706 (krb5_context
, krb5_ccache
, krb5_cc_cursor
*);
707 krb5_error_code (KRB5_CALLCONV
*get_next
)
708 (krb5_context
, krb5_ccache
,
709 krb5_cc_cursor
*, krb5_creds
*);
710 krb5_error_code (KRB5_CALLCONV
*end_get
)
711 (krb5_context
, krb5_ccache
, krb5_cc_cursor
*);
712 krb5_error_code (KRB5_CALLCONV
*remove_cred
)
713 (krb5_context
, krb5_ccache
,
714 krb5_flags
, krb5_creds
*);
715 krb5_error_code (KRB5_CALLCONV
*set_flags
)
716 (krb5_context
, krb5_ccache
, krb5_flags
);
718 #endif /* NO_DEF_KRB5_CCACHE */
721 kssl_krb5_cc_get_principal
722 (krb5_context context
, krb5_ccache cache
,
723 krb5_principal
*principal
)
725 if ( p_krb5_cc_get_principal
)
726 return(p_krb5_cc_get_principal(context
,cache
,principal
));
729 ((cache
)->ops
->get_princ
,(context
, cache
, principal
)));
733 kssl_krb5_auth_con_setrcache(krb5_context con
, krb5_auth_context acon
,
736 if ( p_krb5_auth_con_setrcache
)
737 return(p_krb5_auth_con_setrcache(con
,acon
,rcache
));
739 return KRB5KRB_ERR_GENERIC
;
743 kssl_krb5_get_server_rcache(krb5_context con
, krb5_const krb5_data
* data
,
744 krb5_rcache
* rcache
)
746 if ( p_krb5_get_server_rcache
)
747 return(p_krb5_get_server_rcache(con
,data
,rcache
));
749 return KRB5KRB_ERR_GENERIC
;
753 kssl_krb5_auth_con_getrcache(krb5_context con
, krb5_auth_context acon
,
754 krb5_rcache
* prcache
)
756 if ( p_krb5_auth_con_getrcache
)
757 return(p_krb5_auth_con_getrcache(con
,acon
, prcache
));
759 return KRB5KRB_ERR_GENERIC
;
763 kssl_krb5_kt_close(krb5_context context
, krb5_keytab keytab
)
765 if ( p_krb5_kt_close
)
766 return(p_krb5_kt_close(context
,keytab
));
768 return KRB5KRB_ERR_GENERIC
;
772 kssl_krb5_kt_get_entry(krb5_context context
, krb5_keytab keytab
,
773 krb5_const_principal principal
, krb5_kvno vno
,
774 krb5_enctype enctype
, krb5_keytab_entry
*entry
)
776 if ( p_krb5_kt_get_entry
)
777 return(p_krb5_kt_get_entry(context
,keytab
,principal
,vno
,enctype
,entry
));
779 return KRB5KRB_ERR_GENERIC
;
781 #endif /* OPENSSL_SYS_WINDOWS || OPENSSL_SYS_WIN32 */
784 /* memory allocation functions for non-temporary storage
785 * (e.g. stuff that gets saved into the kssl context) */
786 static void* kssl_calloc(size_t nmemb
, size_t size
)
790 p
=OPENSSL_malloc(nmemb
*size
);
792 memset(p
, 0, nmemb
*size
);
797 #define kssl_malloc(size) OPENSSL_malloc((size))
798 #define kssl_realloc(ptr, size) OPENSSL_realloc(ptr, size)
799 #define kssl_free(ptr) OPENSSL_free((ptr))
803 *kstring(char *string
)
805 static char *null
= "[NULL]";
807 return ((string
== NULL
)? null
: string
);
810 /* Given KRB5 enctype (basically DES or 3DES),
811 ** return closest match openssl EVP_ encryption algorithm.
812 ** Return NULL for unknown or problematic (krb5_dk_encrypt) enctypes.
813 ** Assume ENCTYPE_*_RAW (krb5_raw_encrypt) are OK.
816 kssl_map_enc(krb5_enctype enctype
)
820 case ENCTYPE_DES_HMAC_SHA1
: /* EVP_des_cbc(); */
821 case ENCTYPE_DES_CBC_CRC
:
822 case ENCTYPE_DES_CBC_MD4
:
823 case ENCTYPE_DES_CBC_MD5
:
824 case ENCTYPE_DES_CBC_RAW
:
825 return EVP_des_cbc();
827 case ENCTYPE_DES3_CBC_SHA1
: /* EVP_des_ede3_cbc(); */
828 case ENCTYPE_DES3_CBC_SHA
:
829 case ENCTYPE_DES3_CBC_RAW
:
830 return EVP_des_ede3_cbc();
832 default: return NULL
;
838 /* Return true:1 if p "looks like" the start of the real authenticator
839 ** described in kssl_skip_confound() below. The ASN.1 pattern is
840 ** "62 xx 30 yy" (APPLICATION-2, SEQUENCE), where xx-yy =~ 2, and
841 ** xx and yy are possibly multi-byte length fields.
843 static int kssl_test_confound(unsigned char *p
)
848 if (*p
++ != 0x62) return 0;
849 if (*p
> 0x82) return 0;
851 case 0x82: p
++; xx
= (*p
++ << 8); xx
+= *p
++; break;
852 case 0x81: p
++; xx
= *p
++; break;
854 default: xx
= *p
++; break;
856 if (*p
++ != 0x30) return 0;
857 if (*p
> 0x82) return 0;
859 case 0x82: p
++; len
+=2; yy
= (*p
++ << 8); yy
+= *p
++; break;
860 case 0x81: p
++; len
++; yy
= *p
++; break;
862 default: yy
= *p
++; break;
865 return (xx
- len
== yy
)? 1: 0;
868 /* Allocate, fill, and return cksumlens array of checksum lengths.
869 ** This array holds just the unique elements from the krb5_cksumarray[].
870 ** array[n] == 0 signals end of data.
872 ** The krb5_cksumarray[] was an internal variable that has since been
873 ** replaced by a more general method for storing the data. It should
874 ** not be used. Instead we use real API calls and make a guess for
875 ** what the highest assigned CKSUMTYPE_ constant is. As of 1.2.2
876 ** it is 0x000c (CKSUMTYPE_HMAC_SHA1_DES3). So we will use 0x0010.
878 static size_t *populate_cksumlens(void)
881 static size_t *cklens
= NULL
;
883 #ifdef KRB5_MIT_OLD11
887 #endif /* KRB5_MIT_OLD11 */
890 if (!cklens
&& !(cklens
= (size_t *) calloc(sizeof(int),n
+1))) return NULL
;
892 for (i
=0; i
< n
; i
++) {
893 if (!valid_cksumtype(i
)) continue; /* array has holes */
894 for (j
=0; j
< n
; j
++) {
895 if (cklens
[j
] == 0) {
896 cklens
[j
] = krb5_checksum_size(NULL
,i
);
897 break; /* krb5 elem was new: add */
899 if (cklens
[j
] == krb5_checksum_size(NULL
,i
)) {
900 break; /* ignore duplicate elements */
904 #endif /* KRB5CHECKAUTH */
909 /* Return pointer to start of real authenticator within authenticator, or
910 ** return NULL on error.
911 ** Decrypted authenticator looks like this:
912 ** [0 or 8 byte confounder] [4-24 byte checksum] [real authent'r]
913 ** This hackery wouldn't be necessary if MIT KRB5 1.0.6 had the
914 ** krb5_auth_con_getcksumtype() function advertised in its krb5.h.
916 unsigned char *kssl_skip_confound(krb5_enctype etype
, unsigned char *a
)
920 static size_t *cksumlens
= NULL
;
921 unsigned char *test_auth
;
923 conlen
= (etype
)? 8: 0;
925 if (!cksumlens
&& !(cksumlens
= populate_cksumlens())) return NULL
;
926 for (i
=0; (cklen
= cksumlens
[i
]) != 0; i
++)
928 test_auth
= a
+ conlen
+ cklen
;
929 if (kssl_test_confound(test_auth
)) return test_auth
;
936 /* Set kssl_err error info when reason text is a simple string
937 ** kssl_err = struct { int reason; char text[KSSL_ERR_MAX+1]; }
940 kssl_err_set(KSSL_ERR
*kssl_err
, int reason
, char *text
)
942 if (kssl_err
== NULL
) return;
944 kssl_err
->reason
= reason
;
945 BIO_snprintf(kssl_err
->text
, KSSL_ERR_MAX
, "%s", text
);
950 /* Display contents of krb5_data struct, for debugging
953 print_krb5_data(char *label
, krb5_data
*kdata
)
957 printf("%s[%d] ", label
, kdata
->length
);
958 for (i
=0; i
< (int)kdata
->length
; i
++)
960 if (0 && isprint((int) kdata
->data
[i
]))
961 printf( "%c ", kdata
->data
[i
]);
963 printf( "%02x ", (unsigned char) kdata
->data
[i
]);
969 /* Display contents of krb5_authdata struct, for debugging
972 print_krb5_authdata(char *label
, krb5_authdata
**adata
)
976 printf("%s, authdata==0\n", label
);
979 printf("%s [%p]\n", label
, (void *)adata
);
983 printf("%s[at%d:%d] ", label
, adata
->ad_type
, adata
->length
);
984 for (i
=0; i
< adata
->length
; i
++)
986 printf((isprint(adata
->contents
[i
]))? "%c ": "%02x",
995 /* Display contents of krb5_keyblock struct, for debugging
998 print_krb5_keyblock(char *label
, krb5_keyblock
*keyblk
)
1004 printf("%s, keyblk==0\n", label
);
1008 printf("%s\n\t[et%d:%d]: ", label
, keyblk
->keytype
,
1009 keyblk
->keyvalue
->length
);
1010 for (i
=0; i
< (int)keyblk
->keyvalue
->length
; i
++)
1012 printf("%02x",(unsigned char *)(keyblk
->keyvalue
->contents
)[i
]);
1016 printf("%s\n\t[et%d:%d]: ", label
, keyblk
->enctype
, keyblk
->length
);
1017 for (i
=0; i
< (int)keyblk
->length
; i
++)
1019 printf("%02x",keyblk
->contents
[i
]);
1026 /* Display contents of krb5_principal_data struct, for debugging
1027 ** (krb5_principal is typedef'd == krb5_principal_data *)
1030 print_krb5_princ(char *label
, krb5_principal_data
*princ
)
1034 printf("%s principal Realm: ", label
);
1035 if (princ
== NULL
) return;
1036 for (ui
=0; ui
< (int)princ
->realm
.length
; ui
++) putchar(princ
->realm
.data
[ui
]);
1037 printf(" (nametype %d) has %d strings:\n", princ
->type
,princ
->length
);
1038 for (i
=0; i
< (int)princ
->length
; i
++)
1040 printf("\t%d [%d]: ", i
, princ
->data
[i
].length
);
1041 for (uj
=0; uj
< (int)princ
->data
[i
].length
; uj
++) {
1042 putchar(princ
->data
[i
].data
[uj
]);
1050 /* Given krb5 service (typically "kssl") and hostname in kssl_ctx,
1051 ** Return encrypted Kerberos ticket for service @ hostname.
1052 ** If authenp is non-NULL, also return encrypted authenticator,
1053 ** whose data should be freed by caller.
1054 ** (Originally was: Create Kerberos AP_REQ message for SSL Client.)
1056 ** 19990628 VRS Started; Returns Kerberos AP_REQ message.
1057 ** 20010409 VRS Modified for RFC2712; Returns enc tkt.
1058 ** 20010606 VRS May also return optional authenticator.
1061 kssl_cget_tkt( /* UPDATE */ KSSL_CTX
*kssl_ctx
,
1062 /* OUT */ krb5_data
**enc_ticketp
,
1063 /* UPDATE */ krb5_data
*authenp
,
1064 /* OUT */ KSSL_ERR
*kssl_err
)
1066 krb5_error_code krb5rc
= KRB5KRB_ERR_GENERIC
;
1067 krb5_context krb5context
= NULL
;
1068 krb5_auth_context krb5auth_context
= NULL
;
1069 krb5_ccache krb5ccdef
= NULL
;
1070 krb5_creds krb5creds
, *krb5credsp
= NULL
;
1071 krb5_data krb5_app_req
;
1073 kssl_err_set(kssl_err
, 0, "");
1074 memset((char *)&krb5creds
, 0, sizeof(krb5creds
));
1078 kssl_err_set(kssl_err
, SSL_R_KRB5_S_INIT
,
1079 "No kssl_ctx defined.\n");
1082 else if (!kssl_ctx
->service_host
)
1084 kssl_err_set(kssl_err
, SSL_R_KRB5_S_INIT
,
1085 "kssl_ctx service_host undefined.\n");
1089 if ((krb5rc
= krb5_init_context(&krb5context
)) != 0)
1091 BIO_snprintf(kssl_err
->text
,KSSL_ERR_MAX
,
1092 "krb5_init_context() fails: %d\n", krb5rc
);
1093 kssl_err
->reason
= SSL_R_KRB5_C_INIT
;
1097 if ((krb5rc
= krb5_sname_to_principal(krb5context
,
1098 kssl_ctx
->service_host
,
1099 (kssl_ctx
->service_name
)? kssl_ctx
->service_name
: KRB5SVC
,
1100 KRB5_NT_SRV_HST
, &krb5creds
.server
)) != 0)
1102 BIO_snprintf(kssl_err
->text
,KSSL_ERR_MAX
,
1103 "krb5_sname_to_principal() fails for %s/%s\n",
1104 kssl_ctx
->service_host
,
1105 (kssl_ctx
->service_name
)? kssl_ctx
->service_name
:
1107 kssl_err
->reason
= SSL_R_KRB5_C_INIT
;
1111 if ((krb5rc
= krb5_cc_default(krb5context
, &krb5ccdef
)) != 0)
1113 kssl_err_set(kssl_err
, SSL_R_KRB5_C_CC_PRINC
,
1114 "krb5_cc_default fails.\n");
1118 if ((krb5rc
= krb5_cc_get_principal(krb5context
, krb5ccdef
,
1119 &krb5creds
.client
)) != 0)
1121 kssl_err_set(kssl_err
, SSL_R_KRB5_C_CC_PRINC
,
1122 "krb5_cc_get_principal() fails.\n");
1126 if ((krb5rc
= krb5_get_credentials(krb5context
, 0, krb5ccdef
,
1127 &krb5creds
, &krb5credsp
)) != 0)
1129 kssl_err_set(kssl_err
, SSL_R_KRB5_C_GET_CRED
,
1130 "krb5_get_credentials() fails.\n");
1134 *enc_ticketp
= &krb5credsp
->ticket
;
1136 kssl_ctx
->enctype
= krb5credsp
->session
.keytype
;
1138 kssl_ctx
->enctype
= krb5credsp
->keyblock
.enctype
;
1141 krb5rc
= KRB5KRB_ERR_GENERIC
;
1142 /* caller should free data of krb5_app_req */
1143 /* 20010406 VRS deleted for real KerberosWrapper
1144 ** 20010605 VRS reinstated to offer Authenticator to KerberosWrapper
1146 krb5_app_req
.length
= 0;
1149 krb5_data krb5in_data
;
1150 const unsigned char *p
;
1152 KRB5_APREQBODY
*ap_req
;
1154 authenp
->length
= 0;
1155 krb5in_data
.data
= NULL
;
1156 krb5in_data
.length
= 0;
1157 if ((krb5rc
= krb5_mk_req_extended(krb5context
,
1158 &krb5auth_context
, 0, &krb5in_data
, krb5credsp
,
1159 &krb5_app_req
)) != 0)
1161 kssl_err_set(kssl_err
, SSL_R_KRB5_C_MK_REQ
,
1162 "krb5_mk_req_extended() fails.\n");
1166 arlen
= krb5_app_req
.length
;
1167 p
= (unsigned char *)krb5_app_req
.data
;
1168 ap_req
= (KRB5_APREQBODY
*) d2i_KRB5_APREQ(NULL
, &p
, arlen
);
1171 authenp
->length
= i2d_KRB5_ENCDATA(
1172 ap_req
->authenticator
, NULL
);
1173 if (authenp
->length
&&
1174 (authenp
->data
= malloc(authenp
->length
)))
1176 unsigned char *adp
= (unsigned char *)authenp
->data
;
1177 authenp
->length
= i2d_KRB5_ENCDATA(
1178 ap_req
->authenticator
, &adp
);
1182 if (ap_req
) KRB5_APREQ_free((KRB5_APREQ
*) ap_req
);
1183 if (krb5_app_req
.length
)
1184 kssl_krb5_free_data_contents(krb5context
,&krb5_app_req
);
1187 if (kssl_ctx_setkey(kssl_ctx
, &krb5credsp
->session
))
1189 kssl_err_set(kssl_err
, SSL_R_KRB5_C_INIT
,
1190 "kssl_ctx_setkey() fails.\n");
1193 if (kssl_ctx_setkey(kssl_ctx
, &krb5credsp
->keyblock
))
1195 kssl_err_set(kssl_err
, SSL_R_KRB5_C_INIT
,
1196 "kssl_ctx_setkey() fails.\n");
1203 kssl_ctx_show(kssl_ctx
);
1204 #endif /* KSSL_DEBUG */
1206 if (krb5creds
.client
) krb5_free_principal(krb5context
,
1208 if (krb5creds
.server
) krb5_free_principal(krb5context
,
1210 if (krb5auth_context
) krb5_auth_con_free(krb5context
,
1212 if (krb5context
) krb5_free_context(krb5context
);
1217 /* Given d2i_-decoded asn1ticket, allocate and return a new krb5_ticket.
1218 ** Return Kerberos error code and kssl_err struct on error.
1219 ** Allocates krb5_ticket and krb5_principal; caller should free these.
1221 ** 20010410 VRS Implemented krb5_decode_ticket() as
1222 ** old_krb5_decode_ticket(). Missing from MIT1.0.6.
1223 ** 20010615 VRS Re-cast as openssl/asn1 d2i_*() functions.
1224 ** Re-used some of the old krb5_decode_ticket()
1225 ** code here. This tkt should alloc/free just
1226 ** like the real thing.
1228 static krb5_error_code
1229 kssl_TKT2tkt( /* IN */ krb5_context krb5context
,
1230 /* IN */ KRB5_TKTBODY
*asn1ticket
,
1231 /* OUT */ krb5_ticket
**krb5ticket
,
1232 /* OUT */ KSSL_ERR
*kssl_err
)
1234 krb5_error_code krb5rc
= KRB5KRB_ERR_GENERIC
;
1235 krb5_ticket
*new5ticket
= NULL
;
1236 ASN1_GENERALSTRING
*gstr_svc
, *gstr_host
;
1240 if (asn1ticket
== NULL
|| asn1ticket
->realm
== NULL
||
1241 asn1ticket
->sname
== NULL
||
1242 sk_ASN1_GENERALSTRING_num(asn1ticket
->sname
->namestring
) < 2)
1244 BIO_snprintf(kssl_err
->text
, KSSL_ERR_MAX
,
1245 "Null field in asn1ticket.\n");
1246 kssl_err
->reason
= SSL_R_KRB5_S_RD_REQ
;
1247 return KRB5KRB_ERR_GENERIC
;
1250 if ((new5ticket
= (krb5_ticket
*) calloc(1, sizeof(krb5_ticket
)))==NULL
)
1252 BIO_snprintf(kssl_err
->text
, KSSL_ERR_MAX
,
1253 "Unable to allocate new krb5_ticket.\n");
1254 kssl_err
->reason
= SSL_R_KRB5_S_RD_REQ
;
1255 return ENOMEM
; /* or KRB5KRB_ERR_GENERIC; */
1258 gstr_svc
= sk_ASN1_GENERALSTRING_value(asn1ticket
->sname
->namestring
, 0);
1259 gstr_host
= sk_ASN1_GENERALSTRING_value(asn1ticket
->sname
->namestring
, 1);
1261 if ((krb5rc
= kssl_build_principal_2(krb5context
,
1262 &new5ticket
->server
,
1263 asn1ticket
->realm
->length
, (char *)asn1ticket
->realm
->data
,
1264 gstr_svc
->length
, (char *)gstr_svc
->data
,
1265 gstr_host
->length
, (char *)gstr_host
->data
)) != 0)
1268 BIO_snprintf(kssl_err
->text
, KSSL_ERR_MAX
,
1269 "Error building ticket server principal.\n");
1270 kssl_err
->reason
= SSL_R_KRB5_S_RD_REQ
;
1271 return krb5rc
; /* or KRB5KRB_ERR_GENERIC; */
1274 krb5_princ_type(krb5context
, new5ticket
->server
) =
1275 asn1ticket
->sname
->nametype
->data
[0];
1276 new5ticket
->enc_part
.enctype
= asn1ticket
->encdata
->etype
->data
[0];
1277 new5ticket
->enc_part
.kvno
= asn1ticket
->encdata
->kvno
->data
[0];
1278 new5ticket
->enc_part
.ciphertext
.length
=
1279 asn1ticket
->encdata
->cipher
->length
;
1280 if ((new5ticket
->enc_part
.ciphertext
.data
=
1281 calloc(1, asn1ticket
->encdata
->cipher
->length
)) == NULL
)
1284 BIO_snprintf(kssl_err
->text
, KSSL_ERR_MAX
,
1285 "Error allocating cipher in krb5ticket.\n");
1286 kssl_err
->reason
= SSL_R_KRB5_S_RD_REQ
;
1287 return KRB5KRB_ERR_GENERIC
;
1291 memcpy(new5ticket
->enc_part
.ciphertext
.data
,
1292 asn1ticket
->encdata
->cipher
->data
,
1293 asn1ticket
->encdata
->cipher
->length
);
1296 *krb5ticket
= new5ticket
;
1301 /* Given krb5 service name in KSSL_CTX *kssl_ctx (typically "kssl"),
1302 ** and krb5 AP_REQ message & message length,
1303 ** Return Kerberos session key and client principle
1304 ** to SSL Server in KSSL_CTX *kssl_ctx.
1306 ** 19990702 VRS Started.
1309 kssl_sget_tkt( /* UPDATE */ KSSL_CTX
*kssl_ctx
,
1310 /* IN */ krb5_data
*indata
,
1311 /* OUT */ krb5_ticket_times
*ttimes
,
1312 /* OUT */ KSSL_ERR
*kssl_err
)
1314 krb5_error_code krb5rc
= KRB5KRB_ERR_GENERIC
;
1315 static krb5_context krb5context
= NULL
;
1316 static krb5_auth_context krb5auth_context
= NULL
;
1317 krb5_ticket
*krb5ticket
= NULL
;
1318 KRB5_TKTBODY
*asn1ticket
= NULL
;
1319 const unsigned char *p
;
1320 krb5_keytab krb5keytab
= NULL
;
1321 krb5_keytab_entry kt_entry
;
1322 krb5_principal krb5server
;
1323 krb5_rcache rcache
= NULL
;
1325 kssl_err_set(kssl_err
, 0, "");
1329 kssl_err_set(kssl_err
, SSL_R_KRB5_S_INIT
,
1330 "No kssl_ctx defined.\n");
1335 printf("in kssl_sget_tkt(%s)\n", kstring(kssl_ctx
->service_name
));
1336 #endif /* KSSL_DEBUG */
1338 if (!krb5context
&& (krb5rc
= krb5_init_context(&krb5context
)))
1340 kssl_err_set(kssl_err
, SSL_R_KRB5_S_INIT
,
1341 "krb5_init_context() fails.\n");
1344 if (krb5auth_context
&&
1345 (krb5rc
= krb5_auth_con_free(krb5context
, krb5auth_context
)))
1347 kssl_err_set(kssl_err
, SSL_R_KRB5_S_INIT
,
1348 "krb5_auth_con_free() fails.\n");
1351 else krb5auth_context
= NULL
;
1352 if (!krb5auth_context
&&
1353 (krb5rc
= krb5_auth_con_init(krb5context
, &krb5auth_context
)))
1355 kssl_err_set(kssl_err
, SSL_R_KRB5_S_INIT
,
1356 "krb5_auth_con_init() fails.\n");
1361 if ((krb5rc
= krb5_auth_con_getrcache(krb5context
, krb5auth_context
,
1364 kssl_err_set(kssl_err
, SSL_R_KRB5_S_INIT
,
1365 "krb5_auth_con_getrcache() fails.\n");
1369 if ((krb5rc
= krb5_sname_to_principal(krb5context
, NULL
,
1370 (kssl_ctx
->service_name
)? kssl_ctx
->service_name
: KRB5SVC
,
1371 KRB5_NT_SRV_HST
, &krb5server
)) != 0)
1373 kssl_err_set(kssl_err
, SSL_R_KRB5_S_INIT
,
1374 "krb5_sname_to_principal() fails.\n");
1380 if ((krb5rc
= krb5_get_server_rcache(krb5context
,
1381 krb5_princ_component(krb5context
, krb5server
, 0),
1384 kssl_err_set(kssl_err
, SSL_R_KRB5_S_INIT
,
1385 "krb5_get_server_rcache() fails.\n");
1390 if ((krb5rc
= krb5_auth_con_setrcache(krb5context
, krb5auth_context
, rcache
)))
1392 kssl_err_set(kssl_err
, SSL_R_KRB5_S_INIT
,
1393 "krb5_auth_con_setrcache() fails.\n");
1398 /* kssl_ctx->keytab_file == NULL ==> use Kerberos default
1400 if (kssl_ctx
->keytab_file
)
1402 krb5rc
= krb5_kt_resolve(krb5context
, kssl_ctx
->keytab_file
,
1406 kssl_err_set(kssl_err
, SSL_R_KRB5_S_INIT
,
1407 "krb5_kt_resolve() fails.\n");
1413 krb5rc
= krb5_kt_default(krb5context
,&krb5keytab
);
1416 kssl_err_set(kssl_err
, SSL_R_KRB5_S_INIT
,
1417 "krb5_kt_default() fails.\n");
1422 /* Actual Kerberos5 krb5_recvauth() has initial conversation here
1423 ** o check KRB5_SENDAUTH_BADAUTHVERS
1424 ** unless KRB5_RECVAUTH_SKIP_VERSION
1425 ** o check KRB5_SENDAUTH_BADAPPLVERS
1426 ** o send "0" msg if all OK
1429 /* 20010411 was using AP_REQ instead of true KerberosWrapper
1431 ** if ((krb5rc = krb5_rd_req(krb5context, &krb5auth_context,
1432 ** &krb5in_data, krb5server, krb5keytab,
1433 ** &ap_option, &krb5ticket)) != 0) { Error }
1436 p
= (unsigned char *)indata
->data
;
1437 if ((asn1ticket
= (KRB5_TKTBODY
*) d2i_KRB5_TICKET(NULL
, &p
,
1438 (long) indata
->length
)) == NULL
)
1440 BIO_snprintf(kssl_err
->text
, KSSL_ERR_MAX
,
1441 "d2i_KRB5_TICKET() ASN.1 decode failure.\n");
1442 kssl_err
->reason
= SSL_R_KRB5_S_RD_REQ
;
1446 /* Was: krb5rc = krb5_decode_ticket(krb5in_data,&krb5ticket)) != 0) */
1447 if ((krb5rc
= kssl_TKT2tkt(krb5context
, asn1ticket
, &krb5ticket
,
1450 BIO_snprintf(kssl_err
->text
, KSSL_ERR_MAX
,
1451 "Error converting ASN.1 ticket to krb5_ticket.\n");
1452 kssl_err
->reason
= SSL_R_KRB5_S_RD_REQ
;
1456 if (! krb5_principal_compare(krb5context
, krb5server
,
1457 krb5ticket
->server
)) {
1458 krb5rc
= KRB5_PRINC_NOMATCH
;
1459 BIO_snprintf(kssl_err
->text
, KSSL_ERR_MAX
,
1460 "server principal != ticket principal\n");
1461 kssl_err
->reason
= SSL_R_KRB5_S_RD_REQ
;
1464 if ((krb5rc
= krb5_kt_get_entry(krb5context
, krb5keytab
,
1465 krb5ticket
->server
, krb5ticket
->enc_part
.kvno
,
1466 krb5ticket
->enc_part
.enctype
, &kt_entry
)) != 0) {
1467 BIO_snprintf(kssl_err
->text
, KSSL_ERR_MAX
,
1468 "krb5_kt_get_entry() fails with %x.\n", krb5rc
);
1469 kssl_err
->reason
= SSL_R_KRB5_S_RD_REQ
;
1472 if ((krb5rc
= krb5_decrypt_tkt_part(krb5context
, &kt_entry
.key
,
1473 krb5ticket
)) != 0) {
1474 BIO_snprintf(kssl_err
->text
, KSSL_ERR_MAX
,
1475 "krb5_decrypt_tkt_part() failed.\n");
1476 kssl_err
->reason
= SSL_R_KRB5_S_RD_REQ
;
1480 krb5_kt_free_entry(krb5context
, &kt_entry
);
1483 int i
; krb5_address
**paddr
= krb5ticket
->enc_part2
->caddrs
;
1484 printf("Decrypted ticket fields:\n");
1485 printf("\tflags: %X, transit-type: %X",
1486 krb5ticket
->enc_part2
->flags
,
1487 krb5ticket
->enc_part2
->transited
.tr_type
);
1488 print_krb5_data("\ttransit-data: ",
1489 &(krb5ticket
->enc_part2
->transited
.tr_contents
));
1490 printf("\tcaddrs: %p, authdata: %p\n",
1491 krb5ticket
->enc_part2
->caddrs
,
1492 krb5ticket
->enc_part2
->authorization_data
);
1495 printf("\tcaddrs:\n");
1496 for (i
=0; paddr
[i
] != NULL
; i
++)
1499 d
.length
=paddr
[i
]->length
;
1500 d
.data
=paddr
[i
]->contents
;
1501 print_krb5_data("\t\tIP: ", &d
);
1504 printf("\tstart/auth/end times: %d / %d / %d\n",
1505 krb5ticket
->enc_part2
->times
.starttime
,
1506 krb5ticket
->enc_part2
->times
.authtime
,
1507 krb5ticket
->enc_part2
->times
.endtime
);
1509 #endif /* KSSL_DEBUG */
1512 krb5rc
= KRB5_NO_TKT_SUPPLIED
;
1513 if (!krb5ticket
|| !krb5ticket
->enc_part2
||
1514 !krb5ticket
->enc_part2
->client
||
1515 !krb5ticket
->enc_part2
->client
->data
||
1516 !krb5ticket
->enc_part2
->session
)
1518 kssl_err_set(kssl_err
, SSL_R_KRB5_S_BAD_TICKET
,
1519 "bad ticket from krb5_rd_req.\n");
1521 else if (kssl_ctx_setprinc(kssl_ctx
, KSSL_CLIENT
,
1522 &krb5ticket
->enc_part2
->client
->realm
,
1523 krb5ticket
->enc_part2
->client
->data
,
1524 krb5ticket
->enc_part2
->client
->length
))
1526 kssl_err_set(kssl_err
, SSL_R_KRB5_S_BAD_TICKET
,
1527 "kssl_ctx_setprinc() fails.\n");
1529 else if (kssl_ctx_setkey(kssl_ctx
, krb5ticket
->enc_part2
->session
))
1531 kssl_err_set(kssl_err
, SSL_R_KRB5_S_BAD_TICKET
,
1532 "kssl_ctx_setkey() fails.\n");
1534 else if (krb5ticket
->enc_part2
->flags
& TKT_FLG_INVALID
)
1536 krb5rc
= KRB5KRB_AP_ERR_TKT_INVALID
;
1537 kssl_err_set(kssl_err
, SSL_R_KRB5_S_BAD_TICKET
,
1538 "invalid ticket from krb5_rd_req.\n");
1542 kssl_ctx
->enctype
= krb5ticket
->enc_part
.enctype
;
1543 ttimes
->authtime
= krb5ticket
->enc_part2
->times
.authtime
;
1544 ttimes
->starttime
= krb5ticket
->enc_part2
->times
.starttime
;
1545 ttimes
->endtime
= krb5ticket
->enc_part2
->times
.endtime
;
1546 ttimes
->renew_till
= krb5ticket
->enc_part2
->times
.renew_till
;
1550 kssl_ctx_show(kssl_ctx
);
1551 #endif /* KSSL_DEBUG */
1553 if (asn1ticket
) KRB5_TICKET_free((KRB5_TICKET
*) asn1ticket
);
1554 if (krb5keytab
) krb5_kt_close(krb5context
, krb5keytab
);
1555 if (krb5ticket
) krb5_free_ticket(krb5context
, krb5ticket
);
1556 if (krb5server
) krb5_free_principal(krb5context
, krb5server
);
1561 /* Allocate & return a new kssl_ctx struct.
1566 return ((KSSL_CTX
*) kssl_calloc(1, sizeof(KSSL_CTX
)));
1570 /* Frees a kssl_ctx struct and any allocated memory it holds.
1574 kssl_ctx_free(KSSL_CTX
*kssl_ctx
)
1576 if (kssl_ctx
== NULL
) return kssl_ctx
;
1578 if (kssl_ctx
->key
) OPENSSL_cleanse(kssl_ctx
->key
,
1580 if (kssl_ctx
->key
) kssl_free(kssl_ctx
->key
);
1581 if (kssl_ctx
->client_princ
) kssl_free(kssl_ctx
->client_princ
);
1582 if (kssl_ctx
->service_host
) kssl_free(kssl_ctx
->service_host
);
1583 if (kssl_ctx
->service_name
) kssl_free(kssl_ctx
->service_name
);
1584 if (kssl_ctx
->keytab_file
) kssl_free(kssl_ctx
->keytab_file
);
1586 kssl_free(kssl_ctx
);
1587 return (KSSL_CTX
*) NULL
;
1591 /* Given an array of (krb5_data *) entity (and optional realm),
1592 ** set the plain (char *) client_princ or service_host member
1593 ** of the kssl_ctx struct.
1596 kssl_ctx_setprinc(KSSL_CTX
*kssl_ctx
, int which
,
1597 krb5_data
*realm
, krb5_data
*entity
, int nentities
)
1603 if (kssl_ctx
== NULL
|| entity
== NULL
) return KSSL_CTX_ERR
;
1607 case KSSL_CLIENT
: princ
= &kssl_ctx
->client_princ
; break;
1608 case KSSL_SERVER
: princ
= &kssl_ctx
->service_host
; break;
1609 default: return KSSL_CTX_ERR
; break;
1611 if (*princ
) kssl_free(*princ
);
1613 /* Add up all the entity->lengths */
1615 for (i
=0; i
< nentities
; i
++)
1617 length
+= entity
[i
].length
;
1619 /* Add in space for the '/' character(s) (if any) */
1620 length
+= nentities
-1;
1621 /* Space for the ('@'+realm+NULL | NULL) */
1622 length
+= ((realm
)? realm
->length
+ 2: 1);
1624 if ((*princ
= kssl_calloc(1, length
)) == NULL
)
1625 return KSSL_CTX_ERR
;
1628 for (i
= 0; i
< nentities
; i
++)
1630 strncat(*princ
, entity
[i
].data
, entity
[i
].length
);
1631 if (i
< nentities
-1)
1633 strcat (*princ
, "/");
1638 strcat (*princ
, "@");
1639 (void) strncat(*princ
, realm
->data
, realm
->length
);
1647 /* Set one of the plain (char *) string members of the kssl_ctx struct.
1648 ** Default values should be:
1649 ** which == KSSL_SERVICE => "khost" (KRB5SVC)
1650 ** which == KSSL_KEYTAB => "/etc/krb5.keytab" (KRB5KEYTAB)
1653 kssl_ctx_setstring(KSSL_CTX
*kssl_ctx
, int which
, char *text
)
1657 if (!kssl_ctx
) return KSSL_CTX_ERR
;
1661 case KSSL_SERVICE
: string
= &kssl_ctx
->service_name
; break;
1662 case KSSL_SERVER
: string
= &kssl_ctx
->service_host
; break;
1663 case KSSL_CLIENT
: string
= &kssl_ctx
->client_princ
; break;
1664 case KSSL_KEYTAB
: string
= &kssl_ctx
->keytab_file
; break;
1665 default: return KSSL_CTX_ERR
; break;
1667 if (*string
) kssl_free(*string
);
1675 if ((*string
= kssl_calloc(1, strlen(text
) + 1)) == NULL
)
1676 return KSSL_CTX_ERR
;
1678 strcpy(*string
, text
);
1684 /* Copy the Kerberos session key from a (krb5_keyblock *) to a kssl_ctx
1685 ** struct. Clear kssl_ctx->key if Kerberos session key is NULL.
1688 kssl_ctx_setkey(KSSL_CTX
*kssl_ctx
, krb5_keyblock
*session
)
1691 krb5_enctype enctype
;
1692 krb5_octet FAR
*contents
= NULL
;
1694 if (!kssl_ctx
) return KSSL_CTX_ERR
;
1698 OPENSSL_cleanse(kssl_ctx
->key
, kssl_ctx
->length
);
1699 kssl_free(kssl_ctx
->key
);
1706 length
= session
->keyvalue
->length
;
1707 enctype
= session
->keytype
;
1708 contents
= session
->keyvalue
->contents
;
1710 length
= session
->length
;
1711 enctype
= session
->enctype
;
1712 contents
= session
->contents
;
1714 kssl_ctx
->enctype
= enctype
;
1715 kssl_ctx
->length
= length
;
1719 kssl_ctx
->enctype
= ENCTYPE_UNKNOWN
;
1720 kssl_ctx
->length
= 0;
1724 if ((kssl_ctx
->key
=
1725 (krb5_octet FAR
*) kssl_calloc(1, kssl_ctx
->length
)) == NULL
)
1727 kssl_ctx
->length
= 0;
1728 return KSSL_CTX_ERR
;
1731 memcpy(kssl_ctx
->key
, contents
, length
);
1737 /* Display contents of kssl_ctx struct
1740 kssl_ctx_show(KSSL_CTX
*kssl_ctx
)
1744 printf("kssl_ctx: ");
1745 if (kssl_ctx
== NULL
)
1751 printf("%p\n", (void *)kssl_ctx
);
1753 printf("\tservice:\t%s\n",
1754 (kssl_ctx
->service_name
)? kssl_ctx
->service_name
: "NULL");
1755 printf("\tclient:\t%s\n",
1756 (kssl_ctx
->client_princ
)? kssl_ctx
->client_princ
: "NULL");
1757 printf("\tserver:\t%s\n",
1758 (kssl_ctx
->service_host
)? kssl_ctx
->service_host
: "NULL");
1759 printf("\tkeytab:\t%s\n",
1760 (kssl_ctx
->keytab_file
)? kssl_ctx
->keytab_file
: "NULL");
1761 printf("\tkey [%d:%d]:\t",
1762 kssl_ctx
->enctype
, kssl_ctx
->length
);
1764 for (i
=0; i
< kssl_ctx
->length
&& kssl_ctx
->key
; i
++)
1766 printf("%02x", kssl_ctx
->key
[i
]);
1773 kssl_keytab_is_available(KSSL_CTX
*kssl_ctx
)
1775 krb5_context krb5context
= NULL
;
1776 krb5_keytab krb5keytab
= NULL
;
1777 krb5_keytab_entry entry
;
1778 krb5_principal princ
= NULL
;
1779 krb5_error_code krb5rc
= KRB5KRB_ERR_GENERIC
;
1782 if ((krb5rc
= krb5_init_context(&krb5context
)))
1785 /* kssl_ctx->keytab_file == NULL ==> use Kerberos default
1787 if (kssl_ctx
->keytab_file
)
1789 krb5rc
= krb5_kt_resolve(krb5context
, kssl_ctx
->keytab_file
,
1796 krb5rc
= krb5_kt_default(krb5context
,&krb5keytab
);
1801 /* the host key we are looking for */
1802 krb5rc
= krb5_sname_to_principal(krb5context
, NULL
,
1803 kssl_ctx
->service_name
? kssl_ctx
->service_name
: KRB5SVC
,
1804 KRB5_NT_SRV_HST
, &princ
);
1806 krb5rc
= krb5_kt_get_entry(krb5context
, krb5keytab
,
1809 0 /* IGNORE_ENCTYPE */,
1811 if ( krb5rc
== KRB5_KT_NOTFOUND
) {
1814 } else if ( krb5rc
)
1817 krb5_kt_free_entry(krb5context
, &entry
);
1821 if (krb5keytab
) krb5_kt_close(krb5context
, krb5keytab
);
1822 if (princ
) krb5_free_principal(krb5context
, princ
);
1823 if (krb5context
) krb5_free_context(krb5context
);
1828 kssl_tgt_is_available(KSSL_CTX
*kssl_ctx
)
1830 krb5_error_code krb5rc
= KRB5KRB_ERR_GENERIC
;
1831 krb5_context krb5context
= NULL
;
1832 krb5_ccache krb5ccdef
= NULL
;
1833 krb5_creds krb5creds
, *krb5credsp
= NULL
;
1836 memset((char *)&krb5creds
, 0, sizeof(krb5creds
));
1841 if (!kssl_ctx
->service_host
)
1844 if ((krb5rc
= krb5_init_context(&krb5context
)) != 0)
1847 if ((krb5rc
= krb5_sname_to_principal(krb5context
,
1848 kssl_ctx
->service_host
,
1849 (kssl_ctx
->service_name
)? kssl_ctx
->service_name
: KRB5SVC
,
1850 KRB5_NT_SRV_HST
, &krb5creds
.server
)) != 0)
1853 if ((krb5rc
= krb5_cc_default(krb5context
, &krb5ccdef
)) != 0)
1856 if ((krb5rc
= krb5_cc_get_principal(krb5context
, krb5ccdef
,
1857 &krb5creds
.client
)) != 0)
1860 if ((krb5rc
= krb5_get_credentials(krb5context
, 0, krb5ccdef
,
1861 &krb5creds
, &krb5credsp
)) != 0)
1868 kssl_ctx_show(kssl_ctx
);
1869 #endif /* KSSL_DEBUG */
1871 if (krb5creds
.client
) krb5_free_principal(krb5context
, krb5creds
.client
);
1872 if (krb5creds
.server
) krb5_free_principal(krb5context
, krb5creds
.server
);
1873 if (krb5context
) krb5_free_context(krb5context
);
1877 #if !defined(OPENSSL_SYS_WINDOWS) && !defined(OPENSSL_SYS_WIN32)
1878 void kssl_krb5_free_data_contents(krb5_context context
, krb5_data
*data
)
1884 #elif defined(KRB5_MIT_OLD11)
1886 krb5_xfree(data
->data
);
1890 krb5_free_data_contents(NULL
, data
);
1893 #endif /* !OPENSSL_SYS_WINDOWS && !OPENSSL_SYS_WIN32 */
1896 /* Given pointers to KerberosTime and struct tm structs, convert the
1897 ** KerberosTime string to struct tm. Note that KerberosTime is a
1898 ** ASN1_GENERALIZEDTIME value, constrained to GMT with no fractional
1899 ** seconds as defined in RFC 1510.
1900 ** Return pointer to the (partially) filled in struct tm on success,
1901 ** return NULL on failure.
1903 static struct tm
*k_gmtime(ASN1_GENERALIZEDTIME
*gtime
, struct tm
*k_tm
)
1907 if (!k_tm
) return NULL
;
1908 if (gtime
== NULL
|| gtime
->length
< 14) return NULL
;
1909 if (gtime
->data
== NULL
) return NULL
;
1911 p
= (char *)>ime
->data
[14];
1913 c
= *p
; *p
= '\0'; p
-= 2; k_tm
->tm_sec
= atoi(p
); *(p
+2) = c
;
1914 c
= *p
; *p
= '\0'; p
-= 2; k_tm
->tm_min
= atoi(p
); *(p
+2) = c
;
1915 c
= *p
; *p
= '\0'; p
-= 2; k_tm
->tm_hour
= atoi(p
); *(p
+2) = c
;
1916 c
= *p
; *p
= '\0'; p
-= 2; k_tm
->tm_mday
= atoi(p
); *(p
+2) = c
;
1917 c
= *p
; *p
= '\0'; p
-= 2; k_tm
->tm_mon
= atoi(p
)-1; *(p
+2) = c
;
1918 c
= *p
; *p
= '\0'; p
-= 4; k_tm
->tm_year
= atoi(p
)-1900; *(p
+4) = c
;
1924 /* Helper function for kssl_validate_times().
1925 ** We need context->clockskew, but krb5_context is an opaque struct.
1926 ** So we try to sneek the clockskew out through the replay cache.
1927 ** If that fails just return a likely default (300 seconds).
1929 static krb5_deltat
get_rc_clockskew(krb5_context context
)
1932 krb5_deltat clockskew
;
1934 if (krb5_rc_default(context
, &rc
)) return KSSL_CLOCKSKEW
;
1935 if (krb5_rc_initialize(context
, rc
, 0)) return KSSL_CLOCKSKEW
;
1936 if (krb5_rc_get_lifespan(context
, rc
, &clockskew
)) {
1937 clockskew
= KSSL_CLOCKSKEW
;
1939 (void) krb5_rc_destroy(context
, rc
);
1944 /* kssl_validate_times() combines (and more importantly exposes)
1945 ** the MIT KRB5 internal function krb5_validate_times() and the
1946 ** in_clock_skew() macro. The authenticator client time is checked
1947 ** to be within clockskew secs of the current time and the current
1948 ** time is checked to be within the ticket start and expire times.
1949 ** Either check may be omitted by supplying a NULL value.
1950 ** Returns 0 for valid times, SSL_R_KRB5* error codes otherwise.
1951 ** See Also: (Kerberos source)/krb5/lib/krb5/krb/valid_times.c
1954 krb5_error_code
kssl_validate_times( krb5_timestamp atime
,
1955 krb5_ticket_times
*ttimes
)
1958 krb5_timestamp start
, now
;
1960 krb5_context context
;
1962 if ((rc
= krb5_init_context(&context
))) return SSL_R_KRB5_S_BAD_TICKET
;
1963 skew
= get_rc_clockskew(context
);
1964 if ((rc
= krb5_timeofday(context
,&now
))) return SSL_R_KRB5_S_BAD_TICKET
;
1965 krb5_free_context(context
);
1967 if (atime
&& labs(atime
- now
) >= skew
) return SSL_R_KRB5_S_TKT_SKEW
;
1969 if (! ttimes
) return 0;
1971 start
= (ttimes
->starttime
!= 0)? ttimes
->starttime
: ttimes
->authtime
;
1972 if (start
- now
> skew
) return SSL_R_KRB5_S_TKT_NYV
;
1973 if ((now
- ttimes
->endtime
) > skew
) return SSL_R_KRB5_S_TKT_EXPIRED
;
1976 printf("kssl_validate_times: %d |<- | %d - %d | < %d ->| %d\n",
1977 start
, atime
, now
, skew
, ttimes
->endtime
);
1978 #endif /* KSSL_DEBUG */
1984 /* Decode and decrypt given DER-encoded authenticator, then pass
1985 ** authenticator ctime back in *atimep (or 0 if time unavailable).
1986 ** Returns krb5_error_code and kssl_err on error. A NULL
1987 ** authenticator (authentp->length == 0) is not considered an error.
1988 ** Note that kssl_check_authent() makes use of the KRB5 session key;
1989 ** you must call kssl_sget_tkt() to get the key before calling this routine.
1991 krb5_error_code
kssl_check_authent(
1992 /* IN */ KSSL_CTX
*kssl_ctx
,
1993 /* IN */ krb5_data
*authentp
,
1994 /* OUT */ krb5_timestamp
*atimep
,
1995 /* OUT */ KSSL_ERR
*kssl_err
)
1997 krb5_error_code krb5rc
= 0;
1998 KRB5_ENCDATA
*dec_authent
= NULL
;
1999 KRB5_AUTHENTBODY
*auth
= NULL
;
2000 krb5_enctype enctype
;
2001 EVP_CIPHER_CTX ciph_ctx
;
2002 const EVP_CIPHER
*enc
= NULL
;
2003 unsigned char iv
[EVP_MAX_IV_LENGTH
];
2004 const unsigned char *p
;
2005 unsigned char *unenc_authent
;
2006 int outl
, unencbufsize
;
2007 struct tm tm_time
, *tm_l
, *tm_g
;
2008 time_t now
, tl
, tg
, tr
, tz_offset
;
2010 EVP_CIPHER_CTX_init(&ciph_ctx
);
2012 kssl_err_set(kssl_err
, 0, "");
2014 #ifndef KRB5CHECKAUTH
2017 #if KRB5CHECKAUTH == 0
2020 #endif /* KRB5CHECKAUTH */
2022 if (authentp
== NULL
|| authentp
->length
== 0) return 0;
2027 printf("kssl_check_authent: authenticator[%d]:\n",authentp
->length
);
2029 for (ui
=0; ui
< authentp
->length
; ui
++) printf("%02x ",p
[ui
]);
2032 #endif /* KSSL_DEBUG */
2034 unencbufsize
= 2 * authentp
->length
;
2035 if ((unenc_authent
= calloc(1, unencbufsize
)) == NULL
)
2037 kssl_err_set(kssl_err
, SSL_R_KRB5_S_INIT
,
2038 "Unable to allocate authenticator buffer.\n");
2039 krb5rc
= KRB5KRB_ERR_GENERIC
;
2043 p
= (unsigned char *)authentp
->data
;
2044 if ((dec_authent
= d2i_KRB5_ENCDATA(NULL
, &p
,
2045 (long) authentp
->length
)) == NULL
)
2047 kssl_err_set(kssl_err
, SSL_R_KRB5_S_INIT
,
2048 "Error decoding authenticator.\n");
2049 krb5rc
= KRB5KRB_AP_ERR_BAD_INTEGRITY
;
2053 enctype
= dec_authent
->etype
->data
[0]; /* should = kssl_ctx->enctype */
2054 #if !defined(KRB5_MIT_OLD11)
2055 switch ( enctype
) {
2056 case ENCTYPE_DES3_CBC_SHA1
: /* EVP_des_ede3_cbc(); */
2057 case ENCTYPE_DES3_CBC_SHA
:
2058 case ENCTYPE_DES3_CBC_RAW
:
2059 krb5rc
= 0; /* Skip, can't handle derived keys */
2063 enc
= kssl_map_enc(enctype
);
2064 memset(iv
, 0, sizeof iv
); /* per RFC 1510 */
2068 /* Disable kssl_check_authent for ENCTYPE_DES3_CBC_SHA1.
2069 ** This enctype indicates the authenticator was encrypted
2070 ** using key-usage derived keys which openssl cannot decrypt.
2075 if (!EVP_CipherInit(&ciph_ctx
,enc
,kssl_ctx
->key
,iv
,0))
2077 kssl_err_set(kssl_err
, SSL_R_KRB5_S_INIT
,
2078 "EVP_CipherInit error decrypting authenticator.\n");
2079 krb5rc
= KRB5KRB_AP_ERR_BAD_INTEGRITY
;
2082 outl
= dec_authent
->cipher
->length
;
2083 if (!EVP_Cipher(&ciph_ctx
,unenc_authent
,dec_authent
->cipher
->data
,outl
))
2085 kssl_err_set(kssl_err
, SSL_R_KRB5_S_INIT
,
2086 "EVP_Cipher error decrypting authenticator.\n");
2087 krb5rc
= KRB5KRB_AP_ERR_BAD_INTEGRITY
;
2090 EVP_CIPHER_CTX_cleanup(&ciph_ctx
);
2093 printf("kssl_check_authent: decrypted authenticator[%d] =\n", outl
);
2094 for (padl
=0; padl
< outl
; padl
++) printf("%02x ",unenc_authent
[padl
]);
2096 #endif /* KSSL_DEBUG */
2098 if ((p
= kssl_skip_confound(enctype
, unenc_authent
)) == NULL
)
2100 kssl_err_set(kssl_err
, SSL_R_KRB5_S_INIT
,
2101 "confounded by authenticator.\n");
2102 krb5rc
= KRB5KRB_AP_ERR_BAD_INTEGRITY
;
2105 outl
-= p
- unenc_authent
;
2107 if ((auth
= (KRB5_AUTHENTBODY
*) d2i_KRB5_AUTHENT(NULL
, &p
,
2108 (long) outl
))==NULL
)
2110 kssl_err_set(kssl_err
, SSL_R_KRB5_S_INIT
,
2111 "Error decoding authenticator body.\n");
2112 krb5rc
= KRB5KRB_AP_ERR_BAD_INTEGRITY
;
2116 memset(&tm_time
,0,sizeof(struct tm
));
2117 if (k_gmtime(auth
->ctime
, &tm_time
) &&
2118 ((tr
= mktime(&tm_time
)) != (time_t)(-1)))
2121 tm_l
= localtime(&now
); tl
= mktime(tm_l
);
2122 tm_g
= gmtime(&now
); tg
= mktime(tm_g
);
2123 tz_offset
= tg
- tl
;
2125 *atimep
= (krb5_timestamp
)(tr
- tz_offset
);
2129 printf("kssl_check_authent: returns %d for client time ", *atimep
);
2130 if (auth
&& auth
->ctime
&& auth
->ctime
->length
&& auth
->ctime
->data
)
2131 printf("%.*s\n", auth
->ctime
->length
, auth
->ctime
->data
);
2132 else printf("NULL\n");
2133 #endif /* KSSL_DEBUG */
2136 if (auth
) KRB5_AUTHENT_free((KRB5_AUTHENT
*) auth
);
2137 if (dec_authent
) KRB5_ENCDATA_free(dec_authent
);
2138 if (unenc_authent
) free(unenc_authent
);
2139 EVP_CIPHER_CTX_cleanup(&ciph_ctx
);
2144 /* Replaces krb5_build_principal_ext(), with varargs length == 2 (svc, host),
2145 ** because I dont't know how to stub varargs.
2146 ** Returns krb5_error_code == ENOMEM on alloc error, otherwise
2147 ** passes back newly constructed principal, which should be freed by caller.
2149 krb5_error_code
kssl_build_principal_2(
2150 /* UPDATE */ krb5_context context
,
2151 /* OUT */ krb5_principal
*princ
,
2152 /* IN */ int rlen
, const char *realm
,
2153 /* IN */ int slen
, const char *svc
,
2154 /* IN */ int hlen
, const char *host
)
2156 krb5_data
*p_data
= NULL
;
2157 krb5_principal new_p
= NULL
;
2160 if ((p_data
= (krb5_data
*) calloc(2, sizeof(krb5_data
))) == NULL
||
2161 (new_p
= (krb5_principal
) calloc(1, sizeof(krb5_principal_data
)))
2164 new_p
->data
= p_data
;
2166 if ((new_r
= calloc(1, rlen
+ 1)) == NULL
) goto err
;
2167 memcpy(new_r
, realm
, rlen
);
2168 krb5_princ_set_realm_length(context
, new_p
, rlen
);
2169 krb5_princ_set_realm_data(context
, new_p
, new_r
);
2171 if ((new_p
->data
[0].data
= calloc(1, slen
+ 1)) == NULL
) goto err
;
2172 memcpy(new_p
->data
[0].data
, svc
, slen
);
2173 new_p
->data
[0].length
= slen
;
2175 if ((new_p
->data
[1].data
= calloc(1, hlen
+ 1)) == NULL
) goto err
;
2176 memcpy(new_p
->data
[1].data
, host
, hlen
);
2177 new_p
->data
[1].length
= hlen
;
2179 krb5_princ_type(context
, new_p
) = KRB5_NT_UNKNOWN
;
2184 if (new_p
&& new_p
[0].data
) free(new_p
[0].data
);
2185 if (new_p
&& new_p
[1].data
) free(new_p
[1].data
);
2186 if (new_p
) free(new_p
);
2187 if (new_r
) free(new_r
);
2192 #else /* !OPENSSL_NO_KRB5 */
2194 #if defined(PEDANTIC) || defined(OPENSSL_SYS_VMS)
2195 static void *dummy
=&dummy
;
2198 #endif /* !OPENSSL_NO_KRB5 */