]> git.ipfire.org Git - thirdparty/openssl.git/blame - ssl/kssl.c
Security fixes brought forward from 0.9.7.
[thirdparty/openssl.git] / ssl / kssl.c
CommitLineData
43fcc1b0
RL
1/* ssl/kssl.c -*- mode: C; c-file-style: "eay" -*- */
2/* Written by Vern Staats <staatsvr@asc.hpc.mil> for the OpenSSL project 2000.
3 */
4/* ====================================================================
5 * Copyright (c) 2000 The OpenSSL Project. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
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
17 * distribution.
18 *
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/)"
23 *
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.
28 *
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.
32 *
33 * 6. Redistributions of any form whatsoever must retain the following
34 * acknowledgment:
35 * "This product includes software developed by the OpenSSL Project
36 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
37 *
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 * ====================================================================
51 *
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).
55 *
56 */
57
58
2a1ef754 59/* ssl/kssl.c -- Routines to support (& debug) Kerberos5 auth for openssl
43fcc1b0 60**
2a1ef754
RL
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.
43fcc1b0
RL
67*/
68
7bd51947 69#include <openssl/opensslconf.h>
116daf4c 70
2a1ef754
RL
71#define _XOPEN_SOURCE /* glibc2 needs this to declare strptime() */
72#include <time.h>
7e998124 73#include <string.h>
2a1ef754 74
43fcc1b0 75#include <openssl/ssl.h>
2a1ef754
RL
76#include <openssl/evp.h>
77#include <openssl/objects.h>
78#include <openssl/krb5_asn.h>
43fcc1b0 79
116daf4c
RL
80#ifndef OPENSSL_NO_KRB5
81
f9b3bff6
RL
82/*
83 * When OpenSSL is built on Windows, we do not want to require that
84 * the Kerberos DLLs be available in order for the OpenSSL DLLs to
85 * work. Therefore, all Kerberos routines are loaded at run time
86 * and we do not link to a .LIB file.
87 */
88
bc36ee62 89#if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32)
f9b3bff6
RL
90/*
91 * The purpose of the following pre-processor statements is to provide
92 * compatibility with different releases of MIT Kerberos for Windows.
93 * All versions up to 1.2 used macros. But macros do not allow for
94 * a binary compatible interface for DLLs. Therefore, all macros are
95 * being replaced by function calls. The following code will allow
96 * an OpenSSL DLL built on Windows to work whether or not the macro
97 * or function form of the routines are utilized.
98 */
99#ifdef krb5_cc_get_principal
100#define NO_DEF_KRB5_CCACHE
101#undef krb5_cc_get_principal
102#endif
103#define krb5_cc_get_principal kssl_krb5_cc_get_principal
104
105#define krb5_free_data_contents kssl_krb5_free_data_contents
106#define krb5_free_context kssl_krb5_free_context
107#define krb5_auth_con_free kssl_krb5_auth_con_free
108#define krb5_free_principal kssl_krb5_free_principal
109#define krb5_mk_req_extended kssl_krb5_mk_req_extended
110#define krb5_get_credentials kssl_krb5_get_credentials
111#define krb5_cc_default kssl_krb5_cc_default
112#define krb5_sname_to_principal kssl_krb5_sname_to_principal
113#define krb5_init_context kssl_krb5_init_context
114#define krb5_free_ticket kssl_krb5_free_ticket
115#define krb5_rd_req kssl_krb5_rd_req
116#define krb5_kt_default kssl_krb5_kt_default
117#define krb5_kt_resolve kssl_krb5_kt_resolve
47ff5c62
BM
118/* macros in mit 1.2.2 and earlier; functions in mit 1.2.3 and greater */
119#ifndef krb5_kt_close
120#define krb5_kt_close kssl_krb5_kt_close
121#endif /* krb5_kt_close */
122#ifndef krb5_kt_get_entry
123#define krb5_kt_get_entry kssl_krb5_kt_get_entry
124#endif /* krb5_kt_get_entry */
f9b3bff6
RL
125#define krb5_auth_con_init kssl_krb5_auth_con_init
126
2a1ef754 127#define krb5_principal_compare kssl_krb5_principal_compare
2a1ef754
RL
128#define krb5_decrypt_tkt_part kssl_krb5_decrypt_tkt_part
129#define krb5_timeofday kssl_krb5_timeofday
130#define krb5_rc_default kssl_krb5_rc_default
cbc9d971 131
497810ca
DSH
132#ifdef krb5_rc_initialize
133#undef krb5_rc_initialize
cbc9d971 134#endif
497810ca 135#define krb5_rc_initialize kssl_krb5_rc_initialize
cbc9d971 136
497810ca
DSH
137#ifdef krb5_rc_get_lifespan
138#undef krb5_rc_get_lifespan
cbc9d971 139#endif
497810ca 140#define krb5_rc_get_lifespan kssl_krb5_rc_get_lifespan
cbc9d971 141
497810ca
DSH
142#ifdef krb5_rc_destroy
143#undef krb5_rc_destroy
cbc9d971 144#endif
497810ca 145#define krb5_rc_destroy kssl_krb5_rc_destroy
cbc9d971 146
8de83bf8
RL
147#define valid_cksumtype kssl_valid_cksumtype
148#define krb5_checksum_size kssl_krb5_checksum_size
149#define krb5_kt_free_entry kssl_krb5_kt_free_entry
a5224c34
RL
150#define krb5_auth_con_setrcache kssl_krb5_auth_con_setrcache
151#define krb5_auth_con_getrcache kssl_krb5_auth_con_getrcache
152#define krb5_get_server_rcache kssl_krb5_get_server_rcache
2a1ef754 153
f9b3bff6
RL
154/* Prototypes for built in stubs */
155void kssl_krb5_free_data_contents(krb5_context, krb5_data *);
156void kssl_krb5_free_principal(krb5_context, krb5_principal );
157krb5_error_code kssl_krb5_kt_resolve(krb5_context,
158 krb5_const char *,
159 krb5_keytab *);
160krb5_error_code kssl_krb5_kt_default(krb5_context,
161 krb5_keytab *);
162krb5_error_code kssl_krb5_free_ticket(krb5_context, krb5_ticket *);
163krb5_error_code kssl_krb5_rd_req(krb5_context, krb5_auth_context *,
164 krb5_const krb5_data *,
165 krb5_const_principal, krb5_keytab,
166 krb5_flags *,krb5_ticket **);
2a1ef754
RL
167
168krb5_boolean kssl_krb5_principal_compare(krb5_context, krb5_const_principal,
169 krb5_const_principal);
f9b3bff6
RL
170krb5_error_code kssl_krb5_mk_req_extended(krb5_context,
171 krb5_auth_context *,
172 krb5_const krb5_flags,
173 krb5_data *,
174 krb5_creds *,
175 krb5_data * );
176krb5_error_code kssl_krb5_init_context(krb5_context *);
177void kssl_krb5_free_context(krb5_context);
178krb5_error_code kssl_krb5_cc_default(krb5_context,krb5_ccache *);
179krb5_error_code kssl_krb5_sname_to_principal(krb5_context,
180 krb5_const char *,
181 krb5_const char *,
182 krb5_int32,
183 krb5_principal *);
184krb5_error_code kssl_krb5_get_credentials(krb5_context,
185 krb5_const krb5_flags,
186 krb5_ccache,
187 krb5_creds *,
188 krb5_creds * *);
189krb5_error_code kssl_krb5_auth_con_init(krb5_context,
190 krb5_auth_context *);
191krb5_error_code kssl_krb5_cc_get_principal(krb5_context context,
192 krb5_ccache cache,
193 krb5_principal *principal);
194krb5_error_code kssl_krb5_auth_con_free(krb5_context,krb5_auth_context);
8de83bf8
RL
195size_t kssl_krb5_checksum_size(krb5_context context,krb5_cksumtype ctype);
196krb5_boolean kssl_valid_cksumtype(krb5_cksumtype ctype);
197krb5_error_code krb5_kt_free_entry(krb5_context,krb5_keytab_entry FAR * );
a5224c34
RL
198krb5_error_code kssl_krb5_auth_con_setrcache(krb5_context,
199 krb5_auth_context,
200 krb5_rcache);
201krb5_error_code kssl_krb5_get_server_rcache(krb5_context,
202 krb5_const krb5_data *,
203 krb5_rcache *);
204krb5_error_code kssl_krb5_auth_con_getrcache(krb5_context,
205 krb5_auth_context,
206 krb5_rcache *);
f9b3bff6
RL
207
208/* Function pointers (almost all Kerberos functions are _stdcall) */
2a1ef754
RL
209static void (_stdcall *p_krb5_free_data_contents)(krb5_context, krb5_data *)
210 =NULL;
211static void (_stdcall *p_krb5_free_principal)(krb5_context, krb5_principal )
212 =NULL;
213static krb5_error_code(_stdcall *p_krb5_kt_resolve)
214 (krb5_context, krb5_const char *, krb5_keytab *)=NULL;
f9b3bff6
RL
215static krb5_error_code (_stdcall *p_krb5_kt_default)(krb5_context,
216 krb5_keytab *)=NULL;
217static krb5_error_code (_stdcall *p_krb5_free_ticket)(krb5_context,
218 krb5_ticket *)=NULL;
219static krb5_error_code (_stdcall *p_krb5_rd_req)(krb5_context,
220 krb5_auth_context *,
221 krb5_const krb5_data *,
222 krb5_const_principal,
223 krb5_keytab, krb5_flags *,
224 krb5_ticket **)=NULL;
2a1ef754
RL
225static krb5_error_code (_stdcall *p_krb5_mk_req_extended)
226 (krb5_context, krb5_auth_context *,
227 krb5_const krb5_flags, krb5_data *, krb5_creds *,
228 krb5_data * )=NULL;
f9b3bff6
RL
229static krb5_error_code (_stdcall *p_krb5_init_context)(krb5_context *)=NULL;
230static void (_stdcall *p_krb5_free_context)(krb5_context)=NULL;
231static krb5_error_code (_stdcall *p_krb5_cc_default)(krb5_context,
232 krb5_ccache *)=NULL;
2a1ef754
RL
233static krb5_error_code (_stdcall *p_krb5_sname_to_principal)
234 (krb5_context, krb5_const char *, krb5_const char *,
235 krb5_int32, krb5_principal *)=NULL;
236static krb5_error_code (_stdcall *p_krb5_get_credentials)
237 (krb5_context, krb5_const krb5_flags, krb5_ccache,
238 krb5_creds *, krb5_creds **)=NULL;
239static krb5_error_code (_stdcall *p_krb5_auth_con_init)
240 (krb5_context, krb5_auth_context *)=NULL;
241static krb5_error_code (_stdcall *p_krb5_cc_get_principal)
242 (krb5_context context, krb5_ccache cache,
243 krb5_principal *principal)=NULL;
244static krb5_error_code (_stdcall *p_krb5_auth_con_free)
245 (krb5_context, krb5_auth_context)=NULL;
8de83bf8
RL
246static krb5_error_code (_stdcall *p_krb5_decrypt_tkt_part)
247 (krb5_context, krb5_const krb5_keyblock *,
248 krb5_ticket *)=NULL;
249static krb5_error_code (_stdcall *p_krb5_timeofday)
250 (krb5_context context, krb5_int32 *timeret)=NULL;
251static krb5_error_code (_stdcall *p_krb5_rc_default)
252 (krb5_context context, krb5_rcache *rc)=NULL;
253static krb5_error_code (_stdcall *p_krb5_rc_initialize)
254 (krb5_context context, krb5_rcache rc,
255 krb5_deltat lifespan)=NULL;
256static krb5_error_code (_stdcall *p_krb5_rc_get_lifespan)
257 (krb5_context context, krb5_rcache rc,
258 krb5_deltat *lifespan)=NULL;
259static krb5_error_code (_stdcall *p_krb5_rc_destroy)
260 (krb5_context context, krb5_rcache rc)=NULL;
261static krb5_boolean (_stdcall *p_krb5_principal_compare)
262 (krb5_context, krb5_const_principal, krb5_const_principal)=NULL;
263static size_t (_stdcall *p_krb5_checksum_size)(krb5_context context,krb5_cksumtype ctype)=NULL;
264static krb5_boolean (_stdcall *p_valid_cksumtype)(krb5_cksumtype ctype)=NULL;
265static krb5_error_code (_stdcall *p_krb5_kt_free_entry)
266 (krb5_context,krb5_keytab_entry * )=NULL;
a5224c34
RL
267static krb5_error_code (_stdcall * p_krb5_auth_con_setrcache)(krb5_context,
268 krb5_auth_context,
269 krb5_rcache)=NULL;
270static krb5_error_code (_stdcall * p_krb5_get_server_rcache)(krb5_context,
271 krb5_const krb5_data *,
272 krb5_rcache *)=NULL;
273static krb5_error_code (* p_krb5_auth_con_getrcache)(krb5_context,
274 krb5_auth_context,
275 krb5_rcache *)=NULL;
47ff5c62
BM
276static krb5_error_code (_stdcall * p_krb5_kt_close)(krb5_context context,
277 krb5_keytab keytab)=NULL;
278static krb5_error_code (_stdcall * p_krb5_kt_get_entry)(krb5_context context,
279 krb5_keytab keytab,
280 krb5_const_principal principal, krb5_kvno vno,
281 krb5_enctype enctype, krb5_keytab_entry *entry)=NULL;
f9b3bff6
RL
282static int krb5_loaded = 0; /* only attempt to initialize func ptrs once */
283
284/* Function to Load the Kerberos 5 DLL and initialize function pointers */
285void
286load_krb5_dll(void)
287 {
288 HANDLE hKRB5_32;
289
290 krb5_loaded++;
291 hKRB5_32 = LoadLibrary("KRB5_32");
292 if (!hKRB5_32)
293 return;
294
295 (FARPROC) p_krb5_free_data_contents =
296 GetProcAddress( hKRB5_32, "krb5_free_data_contents" );
297 (FARPROC) p_krb5_free_context =
298 GetProcAddress( hKRB5_32, "krb5_free_context" );
299 (FARPROC) p_krb5_auth_con_free =
300 GetProcAddress( hKRB5_32, "krb5_auth_con_free" );
301 (FARPROC) p_krb5_free_principal =
302 GetProcAddress( hKRB5_32, "krb5_free_principal" );
303 (FARPROC) p_krb5_mk_req_extended =
304 GetProcAddress( hKRB5_32, "krb5_mk_req_extended" );
305 (FARPROC) p_krb5_get_credentials =
306 GetProcAddress( hKRB5_32, "krb5_get_credentials" );
307 (FARPROC) p_krb5_cc_get_principal =
308 GetProcAddress( hKRB5_32, "krb5_cc_get_principal" );
309 (FARPROC) p_krb5_cc_default =
310 GetProcAddress( hKRB5_32, "krb5_cc_default" );
311 (FARPROC) p_krb5_sname_to_principal =
312 GetProcAddress( hKRB5_32, "krb5_sname_to_principal" );
313 (FARPROC) p_krb5_init_context =
314 GetProcAddress( hKRB5_32, "krb5_init_context" );
315 (FARPROC) p_krb5_free_ticket =
316 GetProcAddress( hKRB5_32, "krb5_free_ticket" );
317 (FARPROC) p_krb5_rd_req =
318 GetProcAddress( hKRB5_32, "krb5_rd_req" );
2a1ef754
RL
319 (FARPROC) p_krb5_principal_compare =
320 GetProcAddress( hKRB5_32, "krb5_principal_compare" );
321 (FARPROC) p_krb5_decrypt_tkt_part =
322 GetProcAddress( hKRB5_32, "krb5_decrypt_tkt_part" );
323 (FARPROC) p_krb5_timeofday =
324 GetProcAddress( hKRB5_32, "krb5_timeofday" );
325 (FARPROC) p_krb5_rc_default =
326 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 =
332 GetProcAddress( hKRB5_32, "krb5_rc_destroy" );
f9b3bff6
RL
333 (FARPROC) p_krb5_kt_default =
334 GetProcAddress( hKRB5_32, "krb5_kt_default" );
335 (FARPROC) p_krb5_kt_resolve =
336 GetProcAddress( hKRB5_32, "krb5_kt_resolve" );
337 (FARPROC) p_krb5_auth_con_init =
338 GetProcAddress( hKRB5_32, "krb5_auth_con_init" );
8de83bf8
RL
339 (FARPROC) p_valid_cksumtype =
340 GetProcAddress( hKRB5_32, "valid_cksumtype" );
341 (FARPROC) p_krb5_checksum_size =
342 GetProcAddress( hKRB5_32, "krb5_checksum_size" );
343 (FARPROC) p_krb5_kt_free_entry =
344 GetProcAddress( hKRB5_32, "krb5_kt_free_entry" );
a5224c34
RL
345 (FARPROC) p_krb5_auth_con_setrcache =
346 GetProcAddress( hKRB5_32, "krb5_auth_con_setrcache" );
347 (FARPROC) p_krb5_get_server_rcache =
348 GetProcAddress( hKRB5_32, "krb5_get_server_rcache" );
349 (FARPROC) p_krb5_auth_con_getrcache =
350 GetProcAddress( hKRB5_32, "krb5_auth_con_getrcache" );
47ff5c62
BM
351 (FARPROC) p_krb5_kt_close =
352 GetProcAddress( hKRB5_32, "krb5_kt_close" );
353 (FARPROC) p_krb5_kt_get_entry =
354 GetProcAddress( hKRB5_32, "krb5_kt_get_entry" );
f9b3bff6
RL
355 }
356
357/* Stubs for each function to be dynamicly loaded */
358void
359kssl_krb5_free_data_contents(krb5_context CO, krb5_data * data)
360 {
361 if (!krb5_loaded)
362 load_krb5_dll();
363
364 if ( p_krb5_free_data_contents )
365 p_krb5_free_data_contents(CO,data);
366 }
367
368krb5_error_code
369kssl_krb5_mk_req_extended (krb5_context CO,
370 krb5_auth_context * pACO,
371 krb5_const krb5_flags F,
372 krb5_data * pD1,
373 krb5_creds * pC,
374 krb5_data * pD2)
375 {
376 if (!krb5_loaded)
377 load_krb5_dll();
378
379 if ( p_krb5_mk_req_extended )
380 return(p_krb5_mk_req_extended(CO,pACO,F,pD1,pC,pD2));
381 else
382 return KRB5KRB_ERR_GENERIC;
383 }
384krb5_error_code
385kssl_krb5_auth_con_init(krb5_context CO,
386 krb5_auth_context * pACO)
387 {
388 if (!krb5_loaded)
389 load_krb5_dll();
390
391 if ( p_krb5_auth_con_init )
392 return(p_krb5_auth_con_init(CO,pACO));
393 else
394 return KRB5KRB_ERR_GENERIC;
395 }
396krb5_error_code
397kssl_krb5_auth_con_free (krb5_context CO,
398 krb5_auth_context ACO)
399 {
400 if (!krb5_loaded)
401 load_krb5_dll();
402
403 if ( p_krb5_auth_con_free )
404 return(p_krb5_auth_con_free(CO,ACO));
405 else
406 return KRB5KRB_ERR_GENERIC;
407 }
408krb5_error_code
409kssl_krb5_get_credentials(krb5_context CO,
410 krb5_const krb5_flags F,
411 krb5_ccache CC,
412 krb5_creds * pCR,
413 krb5_creds ** ppCR)
414 {
415 if (!krb5_loaded)
416 load_krb5_dll();
417
418 if ( p_krb5_get_credentials )
419 return(p_krb5_get_credentials(CO,F,CC,pCR,ppCR));
420 else
421 return KRB5KRB_ERR_GENERIC;
422 }
423krb5_error_code
424kssl_krb5_sname_to_principal(krb5_context CO,
425 krb5_const char * pC1,
426 krb5_const char * pC2,
427 krb5_int32 I,
428 krb5_principal * pPR)
429 {
430 if (!krb5_loaded)
431 load_krb5_dll();
432
433 if ( p_krb5_sname_to_principal )
434 return(p_krb5_sname_to_principal(CO,pC1,pC2,I,pPR));
435 else
436 return KRB5KRB_ERR_GENERIC;
437 }
438
439krb5_error_code
440kssl_krb5_cc_default(krb5_context CO,
441 krb5_ccache * pCC)
442 {
443 if (!krb5_loaded)
444 load_krb5_dll();
445
446 if ( p_krb5_cc_default )
447 return(p_krb5_cc_default(CO,pCC));
448 else
449 return KRB5KRB_ERR_GENERIC;
450 }
451
452krb5_error_code
453kssl_krb5_init_context(krb5_context * pCO)
454 {
455 if (!krb5_loaded)
456 load_krb5_dll();
457
458 if ( p_krb5_init_context )
459 return(p_krb5_init_context(pCO));
460 else
461 return KRB5KRB_ERR_GENERIC;
462 }
463
464void
465kssl_krb5_free_context(krb5_context CO)
466 {
467 if (!krb5_loaded)
468 load_krb5_dll();
469
470 if ( p_krb5_free_context )
471 p_krb5_free_context(CO);
472 }
473
474void
475kssl_krb5_free_principal(krb5_context c, krb5_principal p)
476 {
477 if (!krb5_loaded)
478 load_krb5_dll();
479
480 if ( p_krb5_free_principal )
481 p_krb5_free_principal(c,p);
482 }
483
484krb5_error_code
485kssl_krb5_kt_resolve(krb5_context con,
486 krb5_const char * sz,
487 krb5_keytab * kt)
488 {
489 if (!krb5_loaded)
490 load_krb5_dll();
491
492 if ( p_krb5_kt_resolve )
493 return(p_krb5_kt_resolve(con,sz,kt));
494 else
495 return KRB5KRB_ERR_GENERIC;
496 }
497
498krb5_error_code
499kssl_krb5_kt_default(krb5_context con,
500 krb5_keytab * kt)
501 {
502 if (!krb5_loaded)
503 load_krb5_dll();
504
505 if ( p_krb5_kt_default )
506 return(p_krb5_kt_default(con,kt));
507 else
508 return KRB5KRB_ERR_GENERIC;
509 }
510
511krb5_error_code
512kssl_krb5_free_ticket(krb5_context con,
513 krb5_ticket * kt)
514 {
515 if (!krb5_loaded)
516 load_krb5_dll();
517
518 if ( p_krb5_free_ticket )
519 return(p_krb5_free_ticket(con,kt));
520 else
521 return KRB5KRB_ERR_GENERIC;
522 }
523
524krb5_error_code
525kssl_krb5_rd_req(krb5_context con, krb5_auth_context * pacon,
526 krb5_const krb5_data * data,
527 krb5_const_principal princ, krb5_keytab keytab,
528 krb5_flags * flags, krb5_ticket ** pptkt)
529 {
530 if (!krb5_loaded)
531 load_krb5_dll();
532
533 if ( p_krb5_rd_req )
534 return(p_krb5_rd_req(con,pacon,data,princ,keytab,flags,pptkt));
535 else
536 return KRB5KRB_ERR_GENERIC;
537 }
538
2a1ef754
RL
539krb5_boolean
540krb5_principal_compare(krb5_context con, krb5_const_principal princ1,
541 krb5_const_principal princ2)
542 {
543 if (!krb5_loaded)
544 load_krb5_dll();
545
546 if ( p_krb5_principal_compare )
8de83bf8 547 return(p_krb5_principal_compare(con,princ1,princ2));
2a1ef754
RL
548 else
549 return KRB5KRB_ERR_GENERIC;
550 }
551
552krb5_error_code
553krb5_decrypt_tkt_part(krb5_context con, krb5_const krb5_keyblock *keys,
554 krb5_ticket *ticket)
555 {
556 if (!krb5_loaded)
557 load_krb5_dll();
558
559 if ( p_krb5_decrypt_tkt_part )
8de83bf8 560 return(p_krb5_decrypt_tkt_part(con,keys,ticket));
2a1ef754
RL
561 else
562 return KRB5KRB_ERR_GENERIC;
563 }
564
565krb5_error_code
566krb5_timeofday(krb5_context con, krb5_int32 *timeret)
567 {
568 if (!krb5_loaded)
569 load_krb5_dll();
570
571 if ( p_krb5_timeofday )
8de83bf8 572 return(p_krb5_timeofday(con,timeret));
2a1ef754
RL
573 else
574 return KRB5KRB_ERR_GENERIC;
575 }
576
577krb5_error_code
578krb5_rc_default(krb5_context con, krb5_rcache *rc)
579 {
580 if (!krb5_loaded)
581 load_krb5_dll();
582
583 if ( p_krb5_rc_default )
8de83bf8 584 return(p_krb5_rc_default(con,rc));
2a1ef754
RL
585 else
586 return KRB5KRB_ERR_GENERIC;
587 }
588
589krb5_error_code
590krb5_rc_initialize(krb5_context con, krb5_rcache rc, krb5_deltat lifespan)
591 {
592 if (!krb5_loaded)
593 load_krb5_dll();
594
595 if ( p_krb5_rc_initialize )
8de83bf8 596 return(p_krb5_rc_initialize(con, rc, lifespan));
2a1ef754
RL
597 else
598 return KRB5KRB_ERR_GENERIC;
599 }
600
601krb5_error_code
602krb5_rc_get_lifespan(krb5_context con, krb5_rcache rc, krb5_deltat *lifespanp)
603 {
604 if (!krb5_loaded)
605 load_krb5_dll();
606
607 if ( p_krb5_rc_get_lifespan )
8de83bf8 608 return(p_krb5_rc_get_lifespan(con, rc, lifespanp));
2a1ef754
RL
609 else
610 return KRB5KRB_ERR_GENERIC;
611 }
612
613krb5_error_code
614krb5_rc_destroy(krb5_context con, krb5_rcache rc)
615 {
616 if (!krb5_loaded)
617 load_krb5_dll();
618
619 if ( p_krb5_rc_destroy )
8de83bf8 620 return(p_krb5_rc_destroy(con, rc));
2a1ef754
RL
621 else
622 return KRB5KRB_ERR_GENERIC;
623 }
624
8de83bf8
RL
625size_t
626krb5_checksum_size(krb5_context context,krb5_cksumtype ctype)
627 {
628 if (!krb5_loaded)
629 load_krb5_dll();
630
631 if ( p_krb5_checksum_size )
632 return(p_krb5_checksum_size(context, ctype));
633 else
634 return KRB5KRB_ERR_GENERIC;
635 }
636
637krb5_boolean
638valid_cksumtype(krb5_cksumtype ctype)
639 {
640 if (!krb5_loaded)
641 load_krb5_dll();
642
643 if ( p_valid_cksumtype )
644 return(p_valid_cksumtype(ctype));
645 else
646 return KRB5KRB_ERR_GENERIC;
647 }
648
649krb5_error_code
650krb5_kt_free_entry(krb5_context con,krb5_keytab_entry * entry)
651 {
652 if (!krb5_loaded)
653 load_krb5_dll();
654
655 if ( p_krb5_kt_free_entry )
656 return(p_krb5_kt_free_entry(con,entry));
657 else
658 return KRB5KRB_ERR_GENERIC;
659 }
660
f9b3bff6
RL
661/* Structure definitions */
662#ifndef NO_DEF_KRB5_CCACHE
663#ifndef krb5_x
664#define krb5_x(ptr,args) ((ptr)?((*(ptr)) args):(abort(),1))
665#define krb5_xc(ptr,args) ((ptr)?((*(ptr)) args):(abort(),(char*)0))
666#endif
667
668typedef krb5_pointer krb5_cc_cursor; /* cursor for sequential lookup */
669
670typedef struct _krb5_ccache
671 {
672 krb5_magic magic;
673 struct _krb5_cc_ops FAR *ops;
674 krb5_pointer data;
675 } *krb5_ccache;
676
677typedef struct _krb5_cc_ops
678 {
679 krb5_magic magic;
680 char *prefix;
2a1ef754 681 char * (KRB5_CALLCONV *get_name)
8de83bf8 682 (krb5_context, krb5_ccache);
2a1ef754 683 krb5_error_code (KRB5_CALLCONV *resolve)
8de83bf8 684 (krb5_context, krb5_ccache *, const char *);
2a1ef754 685 krb5_error_code (KRB5_CALLCONV *gen_new)
8de83bf8 686 (krb5_context, krb5_ccache *);
2a1ef754 687 krb5_error_code (KRB5_CALLCONV *init)
8de83bf8 688 (krb5_context, krb5_ccache, krb5_principal);
2a1ef754 689 krb5_error_code (KRB5_CALLCONV *destroy)
8de83bf8 690 (krb5_context, krb5_ccache);
2a1ef754 691 krb5_error_code (KRB5_CALLCONV *close)
8de83bf8 692 (krb5_context, krb5_ccache);
2a1ef754 693 krb5_error_code (KRB5_CALLCONV *store)
8de83bf8 694 (krb5_context, krb5_ccache, krb5_creds *);
2a1ef754 695 krb5_error_code (KRB5_CALLCONV *retrieve)
8de83bf8
RL
696 (krb5_context, krb5_ccache,
697 krb5_flags, krb5_creds *, krb5_creds *);
2a1ef754 698 krb5_error_code (KRB5_CALLCONV *get_princ)
8de83bf8 699 (krb5_context, krb5_ccache, krb5_principal *);
2a1ef754 700 krb5_error_code (KRB5_CALLCONV *get_first)
8de83bf8 701 (krb5_context, krb5_ccache, krb5_cc_cursor *);
2a1ef754 702 krb5_error_code (KRB5_CALLCONV *get_next)
8de83bf8
RL
703 (krb5_context, krb5_ccache,
704 krb5_cc_cursor *, krb5_creds *);
2a1ef754 705 krb5_error_code (KRB5_CALLCONV *end_get)
8de83bf8 706 (krb5_context, krb5_ccache, krb5_cc_cursor *);
2a1ef754 707 krb5_error_code (KRB5_CALLCONV *remove_cred)
8de83bf8
RL
708 (krb5_context, krb5_ccache,
709 krb5_flags, krb5_creds *);
2a1ef754 710 krb5_error_code (KRB5_CALLCONV *set_flags)
8de83bf8 711 (krb5_context, krb5_ccache, krb5_flags);
f9b3bff6
RL
712 } krb5_cc_ops;
713#endif /* NO_DEF_KRB5_CCACHE */
714
715krb5_error_code
716kssl_krb5_cc_get_principal
717 (krb5_context context, krb5_ccache cache,
718 krb5_principal *principal)
719 {
720 if ( p_krb5_cc_get_principal )
721 return(p_krb5_cc_get_principal(context,cache,principal));
722 else
2a1ef754
RL
723 return(krb5_x
724 ((cache)->ops->get_princ,(context, cache, principal)));
f9b3bff6 725 }
a5224c34
RL
726
727krb5_error_code
728kssl_krb5_auth_con_setrcache(krb5_context con, krb5_auth_context acon,
729 krb5_rcache rcache)
730 {
731 if ( p_krb5_auth_con_setrcache )
732 return(p_krb5_auth_con_setrcache(con,acon,rcache));
733 else
734 return KRB5KRB_ERR_GENERIC;
735 }
736
737krb5_error_code
738kssl_krb5_get_server_rcache(krb5_context con, krb5_const krb5_data * data,
739 krb5_rcache * rcache)
740 {
47ff5c62
BM
741 if ( p_krb5_get_server_rcache )
742 return(p_krb5_get_server_rcache(con,data,rcache));
743 else
744 return KRB5KRB_ERR_GENERIC;
a5224c34
RL
745 }
746
747krb5_error_code
748kssl_krb5_auth_con_getrcache(krb5_context con, krb5_auth_context acon,
749 krb5_rcache * prcache)
750 {
47ff5c62
BM
751 if ( p_krb5_auth_con_getrcache )
752 return(p_krb5_auth_con_getrcache(con,acon, prcache));
753 else
754 return KRB5KRB_ERR_GENERIC;
755 }
756
757krb5_error_code
758kssl_krb5_kt_close(krb5_context context, krb5_keytab keytab)
759 {
760 if ( p_krb5_kt_close )
761 return(p_krb5_kt_close(context,keytab));
762 else
763 return KRB5KRB_ERR_GENERIC;
764 }
765
766krb5_error_code
767kssl_krb5_kt_get_entry(krb5_context context, krb5_keytab keytab,
768 krb5_const_principal principal, krb5_kvno vno,
769 krb5_enctype enctype, krb5_keytab_entry *entry)
770 {
771 if ( p_krb5_kt_get_entry )
772 return(p_krb5_kt_get_entry(context,keytab,principal,vno,enctype,entry));
773 else
774 return KRB5KRB_ERR_GENERIC;
a5224c34 775 }
bc36ee62 776#endif /* OPENSSL_SYS_WINDOWS || OPENSSL_SYS_WIN32 */
f9b3bff6 777
43fcc1b0
RL
778char
779*kstring(char *string)
780 {
781 static char *null = "[NULL]";
782
783 return ((string == NULL)? null: string);
784 }
785
acdf4afb
RL
786/* Given KRB5 enctype (basically DES or 3DES),
787** return closest match openssl EVP_ encryption algorithm.
788** Return NULL for unknown or problematic (krb5_dk_encrypt) enctypes.
789** Assume ENCTYPE_*_RAW (krb5_raw_encrypt) are OK.
2a1ef754 790*/
0b4c91c0 791const EVP_CIPHER *
2a1ef754
RL
792kssl_map_enc(krb5_enctype enctype)
793 {
794 switch (enctype)
795 {
acdf4afb 796 case ENCTYPE_DES_HMAC_SHA1: /* EVP_des_cbc(); */
2a1ef754
RL
797 case ENCTYPE_DES_CBC_CRC:
798 case ENCTYPE_DES_CBC_MD4:
799 case ENCTYPE_DES_CBC_MD5:
800 case ENCTYPE_DES_CBC_RAW:
0b4c91c0 801 return EVP_des_cbc();
2a1ef754 802 break;
882e8912 803 case ENCTYPE_DES3_CBC_SHA1: /* EVP_des_ede3_cbc(); */
2a1ef754
RL
804 case ENCTYPE_DES3_CBC_SHA:
805 case ENCTYPE_DES3_CBC_RAW:
0b4c91c0 806 return EVP_des_ede3_cbc();
2a1ef754 807 break;
0b4c91c0 808 default: return NULL;
2a1ef754
RL
809 break;
810 }
811 }
812
813
814/* Return true:1 if p "looks like" the start of the real authenticator
815** described in kssl_skip_confound() below. The ASN.1 pattern is
816** "62 xx 30 yy" (APPLICATION-2, SEQUENCE), where xx-yy =~ 2, and
817** xx and yy are possibly multi-byte length fields.
818*/
819int kssl_test_confound(unsigned char *p)
820 {
821 int len = 2;
822 int xx = 0, yy = 0;
823
824 if (*p++ != 0x62) return 0;
825 if (*p > 0x82) return 0;
826 switch(*p) {
827 case 0x82: p++; xx = (*p++ << 8); xx += *p++; break;
828 case 0x81: p++; xx = *p++; break;
829 case 0x80: return 0;
830 default: xx = *p++; break;
831 }
832 if (*p++ != 0x30) return 0;
833 if (*p > 0x82) return 0;
834 switch(*p) {
835 case 0x82: p++; len+=2; yy = (*p++ << 8); yy += *p++; break;
836 case 0x81: p++; len++; yy = *p++; break;
837 case 0x80: return 0;
838 default: yy = *p++; break;
839 }
840
841 return (xx - len == yy)? 1: 0;
842 }
843
844/* Allocate, fill, and return cksumlens array of checksum lengths.
845** This array holds just the unique elements from the krb5_cksumarray[].
846** array[n] == 0 signals end of data.
8de83bf8
RL
847**
848** The krb5_cksumarray[] was an internal variable that has since been
849** replaced by a more general method for storing the data. It should
850** not be used. Instead we use real API calls and make a guess for
851** what the highest assigned CKSUMTYPE_ constant is. As of 1.2.2
852** it is 0x000c (CKSUMTYPE_HMAC_SHA1_DES3). So we will use 0x0010.
2a1ef754 853*/
fdc2bbca 854size_t *populate_cksumlens(void)
2a1ef754 855 {
acdf4afb 856 int i, j, n;
8de83bf8 857 static size_t *cklens = NULL;
2a1ef754 858
acdf4afb
RL
859#ifdef KRB5_MIT_OLD11
860 n = krb5_max_cksum;
861#else
862 n = 0x0010;
863#endif /* KRB5_MIT_OLD11 */
864
2a1ef754 865#ifdef KRB5CHECKAUTH
acdf4afb 866 if (!cklens && !(cklens = (size_t *) calloc(sizeof(int),n+1))) return NULL;
2a1ef754 867
8de83bf8
RL
868 for (i=0; i < n; i++) {
869 if (!valid_cksumtype(i)) continue; /* array has holes */
870 for (j=0; j < n; j++) {
2a1ef754 871 if (cklens[j] == 0) {
8de83bf8 872 cklens[j] = krb5_checksum_size(NULL,i);
2a1ef754
RL
873 break; /* krb5 elem was new: add */
874 }
8de83bf8 875 if (cklens[j] == krb5_checksum_size(NULL,i)) {
2a1ef754
RL
876 break; /* ignore duplicate elements */
877 }
878 }
879 }
880#endif /* KRB5CHECKAUTH */
881
882 return cklens;
883 }
884
885/* Return pointer to start of real authenticator within authenticator, or
886** return NULL on error.
887** Decrypted authenticator looks like this:
888** [0 or 8 byte confounder] [4-24 byte checksum] [real authent'r]
889** This hackery wouldn't be necessary if MIT KRB5 1.0.6 had the
890** krb5_auth_con_getcksumtype() function advertised in its krb5.h.
891*/
892unsigned char *kssl_skip_confound(krb5_enctype etype, unsigned char *a)
893 {
fdc2bbca
RL
894 int i, conlen;
895 size_t cklen;
896 static size_t *cksumlens = NULL;
2a1ef754
RL
897 unsigned char *test_auth;
898
899 conlen = (etype)? 8: 0;
900
901 if (!cksumlens && !(cksumlens = populate_cksumlens())) return NULL;
902 for (i=0; (cklen = cksumlens[i]) != 0; i++)
903 {
904 test_auth = a + conlen + cklen;
905 if (kssl_test_confound(test_auth)) return test_auth;
906 }
907
908 return NULL;
909 }
910
911
43fcc1b0
RL
912/* Set kssl_err error info when reason text is a simple string
913** kssl_err = struct { int reason; char text[KSSL_ERR_MAX+1]; }
914*/
915void
916kssl_err_set(KSSL_ERR *kssl_err, int reason, char *text)
917 {
918 if (kssl_err == NULL) return;
919
920 kssl_err->reason = reason;
921 BIO_snprintf(kssl_err->text, KSSL_ERR_MAX, text);
922 return;
923 }
924
925
926/* Display contents of krb5_data struct, for debugging
927*/
928void
929print_krb5_data(char *label, krb5_data *kdata)
930 {
cbc9d971 931 int i;
43fcc1b0
RL
932
933 printf("%s[%d] ", label, kdata->length);
934 for (i=0; i < kdata->length; i++)
935 {
2a1ef754 936 if (0 && isprint((int) kdata->data[i]))
43fcc1b0
RL
937 printf( "%c ", kdata->data[i]);
938 else
2a1ef754 939 printf( "%02x ", (unsigned char) kdata->data[i]);
43fcc1b0
RL
940 }
941 printf("\n");
942 }
943
944
945/* Display contents of krb5_authdata struct, for debugging
946*/
947void
948print_krb5_authdata(char *label, krb5_authdata **adata)
949 {
950 if (adata == NULL)
951 {
952 printf("%s, authdata==0\n", label);
953 return;
954 }
955 printf("%s [%p]\n", label, adata);
956#if 0
957 {
958 int i;
959 printf("%s[at%d:%d] ", label, adata->ad_type, adata->length);
960 for (i=0; i < adata->length; i++)
961 {
962 printf((isprint(adata->contents[i]))? "%c ": "%02x",
963 adata->contents[i]);
964 }
965 printf("\n");
966 }
967#endif
f9b3bff6 968 }
43fcc1b0
RL
969
970
971/* Display contents of krb5_keyblock struct, for debugging
972*/
973void
974print_krb5_keyblock(char *label, krb5_keyblock *keyblk)
975 {
cbc9d971 976 int i;
43fcc1b0
RL
977
978 if (keyblk == NULL)
979 {
980 printf("%s, keyblk==0\n", label);
981 return;
982 }
f9b3bff6 983#ifdef KRB5_HEIMDAL
2a1ef754
RL
984 printf("%s\n\t[et%d:%d]: ", label, keyblk->keytype,
985 keyblk->keyvalue->length);
f9b3bff6
RL
986 for (i=0; i < keyblk->keyvalue->length; i++)
987 {
988 printf("%02x",(unsigned char *)(keyblk->keyvalue->contents)[i]);
989 }
990 printf("\n");
991#else
43fcc1b0
RL
992 printf("%s\n\t[et%d:%d]: ", label, keyblk->enctype, keyblk->length);
993 for (i=0; i < keyblk->length; i++)
994 {
995 printf("%02x",keyblk->contents[i]);
996 }
997 printf("\n");
f9b3bff6 998#endif
43fcc1b0
RL
999 }
1000
1001
2a1ef754
RL
1002/* Display contents of krb5_principal_data struct, for debugging
1003** (krb5_principal is typedef'd == krb5_principal_data *)
1004*/
1005void
1006print_krb5_princ(char *label, krb5_principal_data *princ)
1007 {
cbc9d971 1008 int i, ui, uj;
2a1ef754
RL
1009
1010 printf("%s principal Realm: ", label);
1011 if (princ == NULL) return;
8de83bf8 1012 for (ui=0; ui < princ->realm.length; ui++) putchar(princ->realm.data[ui]);
2a1ef754
RL
1013 printf(" (nametype %d) has %d strings:\n", princ->type,princ->length);
1014 for (i=0; i < princ->length; i++)
1015 {
1016 printf("\t%d [%d]: ", i, princ->data[i].length);
8de83bf8
RL
1017 for (uj=0; uj < princ->data[i].length; uj++) {
1018 putchar(princ->data[i].data[uj]);
2a1ef754
RL
1019 }
1020 printf("\n");
1021 }
1022 return;
1023 }
1024
1025
43fcc1b0 1026/* Given krb5 service (typically "kssl") and hostname in kssl_ctx,
2a1ef754
RL
1027** Return encrypted Kerberos ticket for service @ hostname.
1028** If authenp is non-NULL, also return encrypted authenticator,
1029** whose data should be freed by caller.
1030** (Originally was: Create Kerberos AP_REQ message for SSL Client.)
43fcc1b0 1031**
2a1ef754
RL
1032** 19990628 VRS Started; Returns Kerberos AP_REQ message.
1033** 20010409 VRS Modified for RFC2712; Returns enc tkt.
1034** 20010606 VRS May also return optional authenticator.
43fcc1b0
RL
1035*/
1036krb5_error_code
1037kssl_cget_tkt( /* UPDATE */ KSSL_CTX *kssl_ctx,
2a1ef754
RL
1038 /* OUT */ krb5_data **enc_ticketp,
1039 /* UPDATE */ krb5_data *authenp,
1040 /* OUT */ KSSL_ERR *kssl_err)
f9b3bff6 1041 {
43fcc1b0
RL
1042 krb5_error_code krb5rc = KRB5KRB_ERR_GENERIC;
1043 krb5_context krb5context = NULL;
1044 krb5_auth_context krb5auth_context = NULL;
1045 krb5_ccache krb5ccdef = NULL;
1046 krb5_creds krb5creds, *krb5credsp = NULL;
2a1ef754 1047 krb5_data krb5_app_req;
43fcc1b0
RL
1048
1049 kssl_err_set(kssl_err, 0, "");
1050 memset((char *)&krb5creds, 0, sizeof(krb5creds));
1051
1052 if (!kssl_ctx)
1053 {
1054 kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
1055 "No kssl_ctx defined.\n");
1056 goto err;
1057 }
1058 else if (!kssl_ctx->service_host)
1059 {
1060 kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
1061 "kssl_ctx service_host undefined.\n");
1062 goto err;
1063 }
1064
1065 if ((krb5rc = krb5_init_context(&krb5context)) != 0)
1066 {
1067 BIO_snprintf(kssl_err->text,KSSL_ERR_MAX,
1068 "krb5_init_context() fails: %d\n", krb5rc);
1069 kssl_err->reason = SSL_R_KRB5_C_INIT;
1070 goto err;
1071 }
1072
1073 if ((krb5rc = krb5_sname_to_principal(krb5context,
1074 kssl_ctx->service_host,
1075 (kssl_ctx->service_name)? kssl_ctx->service_name: KRB5SVC,
1076 KRB5_NT_SRV_HST, &krb5creds.server)) != 0)
1077 {
1078 BIO_snprintf(kssl_err->text,KSSL_ERR_MAX,
1079 "krb5_sname_to_principal() fails for %s/%s\n",
1080 kssl_ctx->service_host,
2a1ef754
RL
1081 (kssl_ctx->service_name)? kssl_ctx->service_name:
1082 KRB5SVC);
43fcc1b0
RL
1083 kssl_err->reason = SSL_R_KRB5_C_INIT;
1084 goto err;
1085 }
1086
1087 if ((krb5rc = krb5_cc_default(krb5context, &krb5ccdef)) != 0)
1088 {
1089 kssl_err_set(kssl_err, SSL_R_KRB5_C_CC_PRINC,
1090 "krb5_cc_default fails.\n");
1091 goto err;
1092 }
1093
1094 if ((krb5rc = krb5_cc_get_principal(krb5context, krb5ccdef,
1095 &krb5creds.client)) != 0)
1096 {
1097 kssl_err_set(kssl_err, SSL_R_KRB5_C_CC_PRINC,
1098 "krb5_cc_get_principal() fails.\n");
1099 goto err;
1100 }
1101
1102 if ((krb5rc = krb5_get_credentials(krb5context, 0, krb5ccdef,
1103 &krb5creds, &krb5credsp)) != 0)
1104 {
1105 kssl_err_set(kssl_err, SSL_R_KRB5_C_GET_CRED,
1106 "krb5_get_credentials() fails.\n");
1107 goto err;
1108 }
1109
2a1ef754
RL
1110 *enc_ticketp = &krb5credsp->ticket;
1111#ifdef KRB5_HEIMDAL
1112 kssl_ctx->enctype = krb5credsp->session.keytype;
1113#else
1114 kssl_ctx->enctype = krb5credsp->keyblock.enctype;
1115#endif
43fcc1b0
RL
1116
1117 krb5rc = KRB5KRB_ERR_GENERIC;
1118 /* caller should free data of krb5_app_req */
2a1ef754
RL
1119 /* 20010406 VRS deleted for real KerberosWrapper
1120 ** 20010605 VRS reinstated to offer Authenticator to KerberosWrapper
1121 */
1122 krb5_app_req.length = 0;
1123 if (authenp)
43fcc1b0 1124 {
2a1ef754
RL
1125 krb5_data krb5in_data;
1126 unsigned char *p;
1127 long arlen;
1128 KRB5_APREQBODY *ap_req;
1129
1130 authenp->length = 0;
1131 krb5in_data.data = NULL;
1132 krb5in_data.length = 0;
1133 if ((krb5rc = krb5_mk_req_extended(krb5context,
1134 &krb5auth_context, 0, &krb5in_data, krb5credsp,
1135 &krb5_app_req)) != 0)
1136 {
1137 kssl_err_set(kssl_err, SSL_R_KRB5_C_MK_REQ,
1138 "krb5_mk_req_extended() fails.\n");
1139 goto err;
1140 }
1141
1142 arlen = krb5_app_req.length;
fdc2bbca 1143 p = (unsigned char *)krb5_app_req.data;
2a1ef754
RL
1144 ap_req = (KRB5_APREQBODY *) d2i_KRB5_APREQ(NULL, &p, arlen);
1145 if (ap_req)
1146 {
1147 authenp->length = i2d_KRB5_ENCDATA(
1148 ap_req->authenticator, NULL);
1149 if (authenp->length &&
1150 (authenp->data = malloc(authenp->length)))
1151 {
fdc2bbca 1152 unsigned char *adp = (unsigned char *)authenp->data;
2a1ef754 1153 authenp->length = i2d_KRB5_ENCDATA(
fdc2bbca 1154 ap_req->authenticator, &adp);
2a1ef754
RL
1155 }
1156 }
1157
1158 if (ap_req) KRB5_APREQ_free((KRB5_APREQ *) ap_req);
8de83bf8 1159 if (krb5_app_req.length)
a5224c34 1160 kssl_krb5_free_data_contents(krb5context,&krb5_app_req);
43fcc1b0 1161 }
f9b3bff6 1162#ifdef KRB5_HEIMDAL
2a1ef754 1163 if (kssl_ctx_setkey(kssl_ctx, &krb5credsp->session))
f9b3bff6
RL
1164 {
1165 kssl_err_set(kssl_err, SSL_R_KRB5_C_INIT,
1166 "kssl_ctx_setkey() fails.\n");
1167 }
1168#else
2a1ef754 1169 if (kssl_ctx_setkey(kssl_ctx, &krb5credsp->keyblock))
43fcc1b0
RL
1170 {
1171 kssl_err_set(kssl_err, SSL_R_KRB5_C_INIT,
1172 "kssl_ctx_setkey() fails.\n");
1173 }
f9b3bff6 1174#endif
43fcc1b0
RL
1175 else krb5rc = 0;
1176
1177 err:
1178#ifdef KSSL_DEBUG
1179 kssl_ctx_show(kssl_ctx);
1180#endif /* KSSL_DEBUG */
1181
2a1ef754
RL
1182 if (krb5creds.client) krb5_free_principal(krb5context,
1183 krb5creds.client);
1184 if (krb5creds.server) krb5_free_principal(krb5context,
1185 krb5creds.server);
1186 if (krb5auth_context) krb5_auth_con_free(krb5context,
1187 krb5auth_context);
43fcc1b0
RL
1188 if (krb5context) krb5_free_context(krb5context);
1189 return (krb5rc);
f9b3bff6 1190 }
43fcc1b0
RL
1191
1192
2a1ef754
RL
1193/* Given d2i_-decoded asn1ticket, allocate and return a new krb5_ticket.
1194** Return Kerberos error code and kssl_err struct on error.
1195** Allocates krb5_ticket and krb5_principal; caller should free these.
1196**
1197** 20010410 VRS Implemented krb5_decode_ticket() as
1198** old_krb5_decode_ticket(). Missing from MIT1.0.6.
1199** 20010615 VRS Re-cast as openssl/asn1 d2i_*() functions.
1200** Re-used some of the old krb5_decode_ticket()
1201** code here. This tkt should alloc/free just
1202** like the real thing.
1203*/
1204krb5_error_code
1205kssl_TKT2tkt( /* IN */ krb5_context krb5context,
1206 /* IN */ KRB5_TKTBODY *asn1ticket,
1207 /* OUT */ krb5_ticket **krb5ticket,
1208 /* OUT */ KSSL_ERR *kssl_err )
1209 {
1210 krb5_error_code krb5rc = KRB5KRB_ERR_GENERIC;
1211 krb5_ticket *new5ticket = NULL;
1212 ASN1_GENERALSTRING *gstr_svc, *gstr_host;
1213
1214 *krb5ticket = NULL;
1215
1216 if (asn1ticket == NULL || asn1ticket->realm == NULL ||
1217 asn1ticket->sname == NULL ||
0b4c91c0 1218 sk_ASN1_GENERALSTRING_num(asn1ticket->sname->namestring) < 2)
2a1ef754
RL
1219 {
1220 BIO_snprintf(kssl_err->text, KSSL_ERR_MAX,
1221 "Null field in asn1ticket.\n");
1222 kssl_err->reason = SSL_R_KRB5_S_RD_REQ;
1223 return KRB5KRB_ERR_GENERIC;
1224 }
1225
1226 if ((new5ticket = (krb5_ticket *) calloc(1, sizeof(krb5_ticket)))==NULL)
1227 {
1228 BIO_snprintf(kssl_err->text, KSSL_ERR_MAX,
1229 "Unable to allocate new krb5_ticket.\n");
1230 kssl_err->reason = SSL_R_KRB5_S_RD_REQ;
1231 return ENOMEM; /* or KRB5KRB_ERR_GENERIC; */
1232 }
1233
0b4c91c0
DSH
1234 gstr_svc = sk_ASN1_GENERALSTRING_value(asn1ticket->sname->namestring, 0);
1235 gstr_host = sk_ASN1_GENERALSTRING_value(asn1ticket->sname->namestring, 1);
2a1ef754
RL
1236
1237 if ((krb5rc = kssl_build_principal_2(krb5context,
1238 &new5ticket->server,
0b4c91c0
DSH
1239 asn1ticket->realm->length, (char *)asn1ticket->realm->data,
1240 gstr_svc->length, (char *)gstr_svc->data,
1241 gstr_host->length, (char *)gstr_host->data)) != 0)
2a1ef754
RL
1242 {
1243 free(new5ticket);
1244 BIO_snprintf(kssl_err->text, KSSL_ERR_MAX,
1245 "Error building ticket server principal.\n");
1246 kssl_err->reason = SSL_R_KRB5_S_RD_REQ;
1247 return krb5rc; /* or KRB5KRB_ERR_GENERIC; */
1248 }
1249
1250 krb5_princ_type(krb5context, new5ticket->server) =
1251 asn1ticket->sname->nametype->data[0];
1252 new5ticket->enc_part.enctype = asn1ticket->encdata->etype->data[0];
1253 new5ticket->enc_part.kvno = asn1ticket->encdata->kvno->data[0];
1254 new5ticket->enc_part.ciphertext.length =
1255 asn1ticket->encdata->cipher->length;
1256 if ((new5ticket->enc_part.ciphertext.data =
1257 calloc(1, asn1ticket->encdata->cipher->length)) == NULL)
1258 {
1259 free(new5ticket);
1260 BIO_snprintf(kssl_err->text, KSSL_ERR_MAX,
1261 "Error allocating cipher in krb5ticket.\n");
1262 kssl_err->reason = SSL_R_KRB5_S_RD_REQ;
1263 return KRB5KRB_ERR_GENERIC;
1264 }
1265 else
1266 {
1267 memcpy(new5ticket->enc_part.ciphertext.data,
1268 asn1ticket->encdata->cipher->data,
1269 asn1ticket->encdata->cipher->length);
1270 }
1271
1272 *krb5ticket = new5ticket;
1273 return 0;
1274 }
1275
1276
43fcc1b0
RL
1277/* Given krb5 service name in KSSL_CTX *kssl_ctx (typically "kssl"),
1278** and krb5 AP_REQ message & message length,
1279** Return Kerberos session key and client principle
1280** to SSL Server in KSSL_CTX *kssl_ctx.
1281**
1282** 19990702 VRS Started.
1283*/
1284krb5_error_code
2a1ef754
RL
1285kssl_sget_tkt( /* UPDATE */ KSSL_CTX *kssl_ctx,
1286 /* IN */ krb5_data *indata,
1287 /* OUT */ krb5_ticket_times *ttimes,
1288 /* OUT */ KSSL_ERR *kssl_err )
43fcc1b0
RL
1289 {
1290 krb5_error_code krb5rc = KRB5KRB_ERR_GENERIC;
1291 static krb5_context krb5context = NULL;
1292 static krb5_auth_context krb5auth_context = NULL;
1293 krb5_ticket *krb5ticket = NULL;
2a1ef754
RL
1294 KRB5_TKTBODY *asn1ticket = NULL;
1295 unsigned char *p;
43fcc1b0 1296 krb5_keytab krb5keytab = NULL;
2a1ef754 1297 krb5_keytab_entry kt_entry;
43fcc1b0 1298 krb5_principal krb5server;
a5224c34 1299 krb5_rcache rcache = NULL;
43fcc1b0
RL
1300
1301 kssl_err_set(kssl_err, 0, "");
1302
1303 if (!kssl_ctx)
1304 {
2a1ef754 1305 kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
a5224c34 1306 "No kssl_ctx defined.\n");
43fcc1b0
RL
1307 goto err;
1308 }
1309
1310#ifdef KSSL_DEBUG
1311 printf("in kssl_sget_tkt(%s)\n", kstring(kssl_ctx->service_name));
1312#endif /* KSSL_DEBUG */
1313
1314 if (!krb5context && (krb5rc = krb5_init_context(&krb5context)))
1315 {
1316 kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
1317 "krb5_init_context() fails.\n");
1318 goto err;
1319 }
1320 if (krb5auth_context &&
1321 (krb5rc = krb5_auth_con_free(krb5context, krb5auth_context)))
1322 {
1323 kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
1324 "krb5_auth_con_free() fails.\n");
1325 goto err;
1326 }
1327 else krb5auth_context = NULL;
1328 if (!krb5auth_context &&
1329 (krb5rc = krb5_auth_con_init(krb5context, &krb5auth_context)))
1330 {
1331 kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
1332 "krb5_auth_con_init() fails.\n");
1333 goto err;
1334 }
1335
a5224c34
RL
1336
1337 if ((krb5rc = krb5_auth_con_getrcache(krb5context, krb5auth_context,
1338 &rcache)))
1339 {
1340 kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
1341 "krb5_auth_con_getrcache() fails.\n");
1342 goto err;
1343 }
1344
43fcc1b0
RL
1345 if ((krb5rc = krb5_sname_to_principal(krb5context, NULL,
1346 (kssl_ctx->service_name)? kssl_ctx->service_name: KRB5SVC,
1347 KRB5_NT_SRV_HST, &krb5server)) != 0)
1348 {
1349 kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
1350 "krb5_sname_to_principal() fails.\n");
1351 goto err;
1352 }
1353
a5224c34
RL
1354 if (rcache == NULL)
1355 {
1356 if ((krb5rc = krb5_get_server_rcache(krb5context,
1357 krb5_princ_component(krb5context, krb5server, 0),
1358 &rcache)))
1359 {
1360 kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
1361 "krb5_get_server_rcache() fails.\n");
1362 goto err;
1363 }
1364 }
1365
1366 if ((krb5rc = krb5_auth_con_setrcache(krb5context, krb5auth_context, rcache)))
1367 {
1368 kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
1369 "krb5_auth_con_setrcache() fails.\n");
1370 goto err;
1371 }
1372
1373
f9b3bff6 1374 /* kssl_ctx->keytab_file == NULL ==> use Kerberos default
43fcc1b0 1375 */
f9b3bff6
RL
1376 if (kssl_ctx->keytab_file)
1377 {
1378 krb5rc = krb5_kt_resolve(krb5context, kssl_ctx->keytab_file,
1379 &krb5keytab);
3e7a6396 1380 if (krb5rc)
f9b3bff6
RL
1381 {
1382 kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
1383 "krb5_kt_resolve() fails.\n");
1384 goto err;
1385 }
1386 }
1387 else
1388 {
1389 krb5rc = krb5_kt_default(krb5context,&krb5keytab);
1390 if (krb5rc)
1391 {
1392 kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
1393 "krb5_kt_default() fails.\n");
1394 goto err;
1395 }
1396 }
43fcc1b0
RL
1397
1398 /* Actual Kerberos5 krb5_recvauth() has initial conversation here
2a1ef754
RL
1399 ** o check KRB5_SENDAUTH_BADAUTHVERS
1400 ** unless KRB5_RECVAUTH_SKIP_VERSION
43fcc1b0
RL
1401 ** o check KRB5_SENDAUTH_BADAPPLVERS
1402 ** o send "0" msg if all OK
1403 */
1404
2a1ef754
RL
1405 /* 20010411 was using AP_REQ instead of true KerberosWrapper
1406 **
1407 ** if ((krb5rc = krb5_rd_req(krb5context, &krb5auth_context,
1408 ** &krb5in_data, krb5server, krb5keytab,
1409 ** &ap_option, &krb5ticket)) != 0) { Error }
1410 */
1411
fdc2bbca 1412 p = (unsigned char *)indata->data;
2a1ef754
RL
1413 if ((asn1ticket = (KRB5_TKTBODY *) d2i_KRB5_TICKET(NULL, &p,
1414 (long) indata->length)) == NULL)
1415 {
1416 BIO_snprintf(kssl_err->text, KSSL_ERR_MAX,
1417 "d2i_KRB5_TICKET() ASN.1 decode failure.\n");
1418 kssl_err->reason = SSL_R_KRB5_S_RD_REQ;
1419 goto err;
1420 }
1421
1422 /* Was: krb5rc = krb5_decode_ticket(krb5in_data,&krb5ticket)) != 0) */
1423 if ((krb5rc = kssl_TKT2tkt(krb5context, asn1ticket, &krb5ticket,
1424 kssl_err)) != 0)
1425 {
1426 BIO_snprintf(kssl_err->text, KSSL_ERR_MAX,
1427 "Error converting ASN.1 ticket to krb5_ticket.\n");
1428 kssl_err->reason = SSL_R_KRB5_S_RD_REQ;
1429 goto err;
1430 }
1431
1432 if (! krb5_principal_compare(krb5context, krb5server,
1433 krb5ticket->server)) {
1434 krb5rc = KRB5_PRINC_NOMATCH;
1435 BIO_snprintf(kssl_err->text, KSSL_ERR_MAX,
1436 "server principal != ticket principal\n");
1437 kssl_err->reason = SSL_R_KRB5_S_RD_REQ;
1438 goto err;
1439 }
1440 if ((krb5rc = krb5_kt_get_entry(krb5context, krb5keytab,
1441 krb5ticket->server, krb5ticket->enc_part.kvno,
1442 krb5ticket->enc_part.enctype, &kt_entry)) != 0) {
43fcc1b0 1443 BIO_snprintf(kssl_err->text, KSSL_ERR_MAX,
2a1ef754 1444 "krb5_kt_get_entry() fails with %x.\n", krb5rc);
43fcc1b0
RL
1445 kssl_err->reason = SSL_R_KRB5_S_RD_REQ;
1446 goto err;
1447 }
2a1ef754
RL
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;
1453 goto err;
1454 }
1455 else {
1456 krb5_kt_free_entry(krb5context, &kt_entry);
1457#ifdef KSSL_DEBUG
1458 {
1459 int i; krb5_address **paddr = krb5ticket->enc_part2->caddrs;
1460 printf("Decrypted ticket fields:\n");
1461 printf("\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 printf("\tcaddrs: %p, authdata: %p\n",
1467 krb5ticket->enc_part2->caddrs,
1468 krb5ticket->enc_part2->authorization_data);
131645ec
RL
1469 if (paddr)
1470 {
1471 printf("\tcaddrs:\n");
1472 for (i=0; paddr[i] != NULL; i++)
1473 {
1474 krb5_data d;
1475 d.length=paddr[i]->length;
1476 d.data=paddr[i]->contents;
1477 print_krb5_data("\t\tIP: ", &d);
1478 }
2a1ef754
RL
1479 }
1480 printf("\tstart/auth/end times: %d / %d / %d\n",
1481 krb5ticket->enc_part2->times.starttime,
1482 krb5ticket->enc_part2->times.authtime,
1483 krb5ticket->enc_part2->times.endtime);
1484 }
1485#endif /* KSSL_DEBUG */
1486 }
43fcc1b0
RL
1487
1488 krb5rc = KRB5_NO_TKT_SUPPLIED;
1489 if (!krb5ticket || !krb5ticket->enc_part2 ||
1490 !krb5ticket->enc_part2->client ||
1491 !krb5ticket->enc_part2->client->data ||
1492 !krb5ticket->enc_part2->session)
1493 {
1494 kssl_err_set(kssl_err, SSL_R_KRB5_S_BAD_TICKET,
1495 "bad ticket from krb5_rd_req.\n");
1496 }
1497 else if (kssl_ctx_setprinc(kssl_ctx, KSSL_CLIENT,
1498 &krb5ticket->enc_part2->client->realm,
1499 krb5ticket->enc_part2->client->data))
1500 {
1501 kssl_err_set(kssl_err, SSL_R_KRB5_S_BAD_TICKET,
1502 "kssl_ctx_setprinc() fails.\n");
1503 }
1504 else if (kssl_ctx_setkey(kssl_ctx, krb5ticket->enc_part2->session))
1505 {
1506 kssl_err_set(kssl_err, SSL_R_KRB5_S_BAD_TICKET,
1507 "kssl_ctx_setkey() fails.\n");
1508 }
2a1ef754
RL
1509 else if (krb5ticket->enc_part2->flags & TKT_FLG_INVALID)
1510 {
1511 krb5rc = KRB5KRB_AP_ERR_TKT_INVALID;
1512 kssl_err_set(kssl_err, SSL_R_KRB5_S_BAD_TICKET,
1513 "invalid ticket from krb5_rd_req.\n");
1514 }
43fcc1b0
RL
1515 else krb5rc = 0;
1516
2a1ef754
RL
1517 kssl_ctx->enctype = krb5ticket->enc_part.enctype;
1518 ttimes->authtime = krb5ticket->enc_part2->times.authtime;
1519 ttimes->starttime = krb5ticket->enc_part2->times.starttime;
1520 ttimes->endtime = krb5ticket->enc_part2->times.endtime;
1521 ttimes->renew_till = krb5ticket->enc_part2->times.renew_till;
1522
43fcc1b0
RL
1523 err:
1524#ifdef KSSL_DEBUG
1525 kssl_ctx_show(kssl_ctx);
1526#endif /* KSSL_DEBUG */
1527
2a1ef754 1528 if (asn1ticket) KRB5_TICKET_free((KRB5_TICKET *) asn1ticket);
f9b3bff6 1529 if (krb5keytab) krb5_kt_close(krb5context, krb5keytab);
43fcc1b0
RL
1530 if (krb5ticket) krb5_free_ticket(krb5context, krb5ticket);
1531 if (krb5server) krb5_free_principal(krb5context, krb5server);
1532 return (krb5rc);
1533 }
1534
1535
1536/* Allocate & return a new kssl_ctx struct.
1537*/
1538KSSL_CTX *
1539kssl_ctx_new(void)
1540 {
1541 return ((KSSL_CTX *) calloc(1, sizeof(KSSL_CTX)));
1542 }
1543
1544
1545/* Frees a kssl_ctx struct and any allocated memory it holds.
1546** Returns NULL.
1547*/
1548KSSL_CTX *
1549kssl_ctx_free(KSSL_CTX *kssl_ctx)
1550 {
1551 if (kssl_ctx == NULL) return kssl_ctx;
1552
2a1ef754
RL
1553 if (kssl_ctx->key) memset(kssl_ctx->key, 0,
1554 kssl_ctx->length);
43fcc1b0
RL
1555 if (kssl_ctx->key) free(kssl_ctx->key);
1556 if (kssl_ctx->client_princ) free(kssl_ctx->client_princ);
1557 if (kssl_ctx->service_host) free(kssl_ctx->service_host);
1558 if (kssl_ctx->service_name) free(kssl_ctx->service_name);
1559 if (kssl_ctx->keytab_file) free(kssl_ctx->keytab_file);
1560
1561 free(kssl_ctx);
1562 return (KSSL_CTX *) NULL;
1563 }
1564
1565
1566/* Given a (krb5_data *) entity (and optional realm),
1567** set the plain (char *) client_princ or service_host member
1568** of the kssl_ctx struct.
1569*/
1570krb5_error_code
1571kssl_ctx_setprinc(KSSL_CTX *kssl_ctx, int which,
1572 krb5_data *realm, krb5_data *entity)
1573 {
1574 char **princ;
1575 int length;
1576
1577 if (kssl_ctx == NULL || entity == NULL) return KSSL_CTX_ERR;
1578
1579 switch (which)
1580 {
1581 case KSSL_CLIENT: princ = &kssl_ctx->client_princ; break;
1582 case KSSL_SERVER: princ = &kssl_ctx->service_host; break;
1583 default: return KSSL_CTX_ERR; break;
1584 }
1585 if (*princ) free(*princ);
1586
1587 length = entity->length + ((realm)? realm->length + 2: 1);
f9b3bff6
RL
1588 if ((*princ = calloc(1, length)) == NULL)
1589 return KSSL_CTX_ERR;
43fcc1b0
RL
1590 else
1591 {
1592 strncpy(*princ, entity->data, entity->length);
26414ee0 1593 (*princ)[entity->length]='\0';
43fcc1b0
RL
1594 if (realm)
1595 {
1596 strcat (*princ, "@");
1597 (void) strncat(*princ, realm->data, realm->length);
26414ee0 1598 (*princ)[entity->length+1+realm->length]='\0';
43fcc1b0
RL
1599 }
1600 }
1601
1602 return KSSL_CTX_OK;
1603 }
1604
1605
1606/* Set one of the plain (char *) string members of the kssl_ctx struct.
1607** Default values should be:
f9b3bff6
RL
1608** which == KSSL_SERVICE => "khost" (KRB5SVC)
1609** which == KSSL_KEYTAB => "/etc/krb5.keytab" (KRB5KEYTAB)
43fcc1b0
RL
1610*/
1611krb5_error_code
1612kssl_ctx_setstring(KSSL_CTX *kssl_ctx, int which, char *text)
1613 {
1614 char **string;
1615
1616 if (!kssl_ctx) return KSSL_CTX_ERR;
1617
1618 switch (which)
1619 {
1620 case KSSL_SERVICE: string = &kssl_ctx->service_name; break;
1621 case KSSL_SERVER: string = &kssl_ctx->service_host; break;
1622 case KSSL_CLIENT: string = &kssl_ctx->client_princ; break;
1623 case KSSL_KEYTAB: string = &kssl_ctx->keytab_file; break;
1624 default: return KSSL_CTX_ERR; break;
1625 }
1626 if (*string) free(*string);
1627
1628 if (!text)
1629 {
1630 *string = '\0';
1631 return KSSL_CTX_OK;
1632 }
1633
f9b3bff6
RL
1634 if ((*string = calloc(1, strlen(text) + 1)) == NULL)
1635 return KSSL_CTX_ERR;
1636 else
1637 strcpy(*string, text);
43fcc1b0
RL
1638
1639 return KSSL_CTX_OK;
1640 }
1641
1642
1643/* Copy the Kerberos session key from a (krb5_keyblock *) to a kssl_ctx
1644** struct. Clear kssl_ctx->key if Kerberos session key is NULL.
1645*/
1646krb5_error_code
1647kssl_ctx_setkey(KSSL_CTX *kssl_ctx, krb5_keyblock *session)
1648 {
2a1ef754
RL
1649 int length;
1650 krb5_enctype enctype;
1651 krb5_octet FAR *contents = NULL;
1652
43fcc1b0
RL
1653 if (!kssl_ctx) return KSSL_CTX_ERR;
1654
1655 if (kssl_ctx->key)
1656 {
1657 memset(kssl_ctx->key, 0, kssl_ctx->length);
1658 free(kssl_ctx->key);
1659 }
1660
1661 if (session)
1662 {
2a1ef754
RL
1663
1664#ifdef KRB5_HEIMDAL
1665 length = session->keyvalue->length;
1666 enctype = session->keytype;
1667 contents = session->keyvalue->contents;
1668#else
1669 length = session->length;
1670 enctype = session->enctype;
1671 contents = session->contents;
1672#endif
1673 kssl_ctx->enctype = enctype;
1674 kssl_ctx->length = length;
43fcc1b0
RL
1675 }
1676 else
1677 {
1678 kssl_ctx->enctype = ENCTYPE_UNKNOWN;
1679 kssl_ctx->length = 0;
1680 return KSSL_CTX_OK;
1681 }
1682
1683 if ((kssl_ctx->key =
1684 (krb5_octet FAR *) calloc(1, kssl_ctx->length)) == NULL)
1685 {
1686 kssl_ctx->length = 0;
1687 return KSSL_CTX_ERR;
1688 }
f9b3bff6 1689 else
2a1ef754 1690 memcpy(kssl_ctx->key, contents, length);
43fcc1b0
RL
1691
1692 return KSSL_CTX_OK;
1693 }
1694
1695
1696/* Display contents of kssl_ctx struct
1697*/
1698void
1699kssl_ctx_show(KSSL_CTX *kssl_ctx)
1700 {
a5224c34 1701 int i;
43fcc1b0
RL
1702
1703 printf("kssl_ctx: ");
1704 if (kssl_ctx == NULL)
1705 {
1706 printf("NULL\n");
1707 return;
1708 }
f9b3bff6
RL
1709 else
1710 printf("%p\n", kssl_ctx);
43fcc1b0
RL
1711
1712 printf("\tservice:\t%s\n",
1713 (kssl_ctx->service_name)? kssl_ctx->service_name: "NULL");
1714 printf("\tclient:\t%s\n",
1715 (kssl_ctx->client_princ)? kssl_ctx->client_princ: "NULL");
1716 printf("\tserver:\t%s\n",
1717 (kssl_ctx->service_host)? kssl_ctx->service_host: "NULL");
1718 printf("\tkeytab:\t%s\n",
1719 (kssl_ctx->keytab_file)? kssl_ctx->keytab_file: "NULL");
1720 printf("\tkey [%d:%d]:\t",
1721 kssl_ctx->enctype, kssl_ctx->length);
1722
1723 for (i=0; i < kssl_ctx->length && kssl_ctx->key; i++)
1724 {
1725 printf("%02x", kssl_ctx->key[i]);
1726 }
1727 printf("\n");
1728 return;
1729 }
1730
a5224c34
RL
1731 int
1732 kssl_keytab_is_available(KSSL_CTX *kssl_ctx)
1733{
1734 krb5_context krb5context = NULL;
1735 krb5_keytab krb5keytab = NULL;
1736 krb5_keytab_entry entry;
1737 krb5_principal princ = NULL;
1738 krb5_error_code krb5rc = KRB5KRB_ERR_GENERIC;
1739 int rc = 0;
1740
1741 if ((krb5rc = krb5_init_context(&krb5context)))
1742 return(0);
1743
1744 /* kssl_ctx->keytab_file == NULL ==> use Kerberos default
1745 */
1746 if (kssl_ctx->keytab_file)
1747 {
1748 krb5rc = krb5_kt_resolve(krb5context, kssl_ctx->keytab_file,
1749 &krb5keytab);
1750 if (krb5rc)
1751 goto exit;
1752 }
1753 else
1754 {
1755 krb5rc = krb5_kt_default(krb5context,&krb5keytab);
1756 if (krb5rc)
1757 goto exit;
1758 }
1759
1760 /* the host key we are looking for */
1761 krb5rc = krb5_sname_to_principal(krb5context, NULL,
1762 kssl_ctx->service_name ? kssl_ctx->service_name: KRB5SVC,
1763 KRB5_NT_SRV_HST, &princ);
1764
1765 krb5rc = krb5_kt_get_entry(krb5context, krb5keytab,
1766 princ,
1767 0 /* IGNORE_VNO */,
1768 0 /* IGNORE_ENCTYPE */,
1769 &entry);
1770 if ( krb5rc == KRB5_KT_NOTFOUND ) {
1771 rc = 1;
1772 goto exit;
1773 } else if ( krb5rc )
1774 goto exit;
1775
1776 krb5_kt_free_entry(krb5context, &entry);
1777 rc = 1;
1778
1779 exit:
1780 if (krb5keytab) krb5_kt_close(krb5context, krb5keytab);
1781 if (princ) krb5_free_principal(krb5context, princ);
1782 if (krb5context) krb5_free_context(krb5context);
1783 return(rc);
1784}
1785
1786int
1787kssl_tgt_is_available(KSSL_CTX *kssl_ctx)
1788 {
1789 krb5_error_code krb5rc = KRB5KRB_ERR_GENERIC;
1790 krb5_context krb5context = NULL;
1791 krb5_ccache krb5ccdef = NULL;
1792 krb5_creds krb5creds, *krb5credsp = NULL;
1793 int rc = 0;
1794
1795 memset((char *)&krb5creds, 0, sizeof(krb5creds));
1796
1797 if (!kssl_ctx)
1798 return(0);
1799
1800 if (!kssl_ctx->service_host)
1801 return(0);
1802
1803 if ((krb5rc = krb5_init_context(&krb5context)) != 0)
1804 goto err;
1805
1806 if ((krb5rc = krb5_sname_to_principal(krb5context,
1807 kssl_ctx->service_host,
1808 (kssl_ctx->service_name)? kssl_ctx->service_name: KRB5SVC,
1809 KRB5_NT_SRV_HST, &krb5creds.server)) != 0)
1810 goto err;
1811
1812 if ((krb5rc = krb5_cc_default(krb5context, &krb5ccdef)) != 0)
1813 goto err;
1814
1815 if ((krb5rc = krb5_cc_get_principal(krb5context, krb5ccdef,
1816 &krb5creds.client)) != 0)
1817 goto err;
1818
1819 if ((krb5rc = krb5_get_credentials(krb5context, 0, krb5ccdef,
1820 &krb5creds, &krb5credsp)) != 0)
1821 goto err;
1822
1823 rc = 1;
1824
1825 err:
1826#ifdef KSSL_DEBUG
1827 kssl_ctx_show(kssl_ctx);
1828#endif /* KSSL_DEBUG */
1829
1830 if (krb5creds.client) krb5_free_principal(krb5context, krb5creds.client);
1831 if (krb5creds.server) krb5_free_principal(krb5context, krb5creds.server);
1832 if (krb5context) krb5_free_context(krb5context);
1833 return(rc);
1834 }
1835
8de83bf8 1836#if !defined(OPENSSL_SYS_WINDOWS) && !defined(OPENSSL_SYS_WIN32)
f9b3bff6
RL
1837void kssl_krb5_free_data_contents(krb5_context context, krb5_data *data)
1838 {
1839#ifdef KRB5_HEIMDAL
1840 data->length = 0;
a5224c34
RL
1841 if (data->data)
1842 free(data->data);
2a1ef754
RL
1843#elif defined(KRB5_MIT_OLD11)
1844 if (data->data) {
1845 krb5_xfree(data->data);
1846 data->data = 0;
1847 }
f9b3bff6
RL
1848#else
1849 krb5_free_data_contents(NULL, data);
1850#endif
1851 }
8de83bf8 1852#endif /* !OPENSSL_SYS_WINDOWS && !OPENSSL_SYS_WIN32 */
2a1ef754 1853
acdf4afb
RL
1854
1855/* Given pointers to KerberosTime and struct tm structs, convert the
1856** KerberosTime string to struct tm. Note that KerberosTime is a
1857** ASN1_GENERALIZEDTIME value, constrained to GMT with no fractional
1858** seconds as defined in RFC 1510.
1859** Return pointer to the (partially) filled in struct tm on success,
1860** return NULL on failure.
1861*/
fdc2bbca 1862struct tm *k_gmtime(ASN1_GENERALIZEDTIME *gtime, struct tm *k_tm)
acdf4afb
RL
1863 {
1864 char c, *p;
1865
1866 if (!k_tm) return NULL;
fdc2bbca
RL
1867 if (gtime == NULL || gtime->length < 14) return NULL;
1868 if (gtime->data == NULL) return NULL;
acdf4afb 1869
fdc2bbca 1870 p = (char *)&gtime->data[14];
acdf4afb
RL
1871
1872 c = *p; *p = '\0'; p -= 2; k_tm->tm_sec = atoi(p); *(p+2) = c;
1873 c = *p; *p = '\0'; p -= 2; k_tm->tm_min = atoi(p); *(p+2) = c;
1874 c = *p; *p = '\0'; p -= 2; k_tm->tm_hour = atoi(p); *(p+2) = c;
1875 c = *p; *p = '\0'; p -= 2; k_tm->tm_mday = atoi(p); *(p+2) = c;
1876 c = *p; *p = '\0'; p -= 2; k_tm->tm_mon = atoi(p)-1; *(p+2) = c;
1877 c = *p; *p = '\0'; p -= 4; k_tm->tm_year = atoi(p)-1900; *(p+4) = c;
1878
1879 return k_tm;
1880 }
1881
1882
2a1ef754
RL
1883/* Helper function for kssl_validate_times().
1884** We need context->clockskew, but krb5_context is an opaque struct.
1885** So we try to sneek the clockskew out through the replay cache.
1886** If that fails just return a likely default (300 seconds).
1887*/
1888krb5_deltat get_rc_clockskew(krb5_context context)
1889 {
1890 krb5_rcache rc;
1891 krb5_deltat clockskew;
1892
1893 if (krb5_rc_default(context, &rc)) return KSSL_CLOCKSKEW;
1894 if (krb5_rc_initialize(context, rc, 0)) return KSSL_CLOCKSKEW;
1895 if (krb5_rc_get_lifespan(context, rc, &clockskew)) {
1896 clockskew = KSSL_CLOCKSKEW;
1897 }
1898 (void) krb5_rc_destroy(context, rc);
1899 return clockskew;
1900 }
1901
1902
1903/* kssl_validate_times() combines (and more importantly exposes)
1904** the MIT KRB5 internal function krb5_validate_times() and the
1905** in_clock_skew() macro. The authenticator client time is checked
1906** to be within clockskew secs of the current time and the current
1907** time is checked to be within the ticket start and expire times.
1908** Either check may be omitted by supplying a NULL value.
1909** Returns 0 for valid times, SSL_R_KRB5* error codes otherwise.
1910** See Also: (Kerberos source)/krb5/lib/krb5/krb/valid_times.c
1911** 20010420 VRS
1912*/
1913krb5_error_code kssl_validate_times( krb5_timestamp atime,
1914 krb5_ticket_times *ttimes)
1915 {
1916 krb5_deltat skew;
1917 krb5_timestamp start, now;
1918 krb5_error_code rc;
1919 krb5_context context;
1920
1921 if ((rc = krb5_init_context(&context))) return SSL_R_KRB5_S_BAD_TICKET;
1922 skew = get_rc_clockskew(context);
1923 if ((rc = krb5_timeofday(context,&now))) return SSL_R_KRB5_S_BAD_TICKET;
1924 krb5_free_context(context);
1925
1926 if (atime && labs(atime - now) >= skew) return SSL_R_KRB5_S_TKT_SKEW;
1927
1928 if (! ttimes) return 0;
1929
1930 start = (ttimes->starttime != 0)? ttimes->starttime: ttimes->authtime;
1931 if (start - now > skew) return SSL_R_KRB5_S_TKT_NYV;
1932 if ((now - ttimes->endtime) > skew) return SSL_R_KRB5_S_TKT_EXPIRED;
1933
1934#ifdef KSSL_DEBUG
1935 printf("kssl_validate_times: %d |<- | %d - %d | < %d ->| %d\n",
1936 start, atime, now, skew, ttimes->endtime);
1937#endif /* KSSL_DEBUG */
1938
1939 return 0;
1940 }
1941
1942
1943/* Decode and decrypt given DER-encoded authenticator, then pass
1944** authenticator ctime back in *atimep (or 0 if time unavailable).
1945** Returns krb5_error_code and kssl_err on error. A NULL
1946** authenticator (authentp->length == 0) is not considered an error.
1947** Note that kssl_check_authent() makes use of the KRB5 session key;
1948** you must call kssl_sget_tkt() to get the key before calling this routine.
1949*/
1950krb5_error_code kssl_check_authent(
1951 /* IN */ KSSL_CTX *kssl_ctx,
1952 /* IN */ krb5_data *authentp,
1953 /* OUT */ krb5_timestamp *atimep,
1954 /* OUT */ KSSL_ERR *kssl_err )
1955 {
1956 krb5_error_code krb5rc = 0;
1957 KRB5_ENCDATA *dec_authent = NULL;
1958 KRB5_AUTHENTBODY *auth = NULL;
1959 krb5_enctype enctype;
1960 EVP_CIPHER_CTX ciph_ctx;
0b4c91c0 1961 const EVP_CIPHER *enc = NULL;
2a1ef754 1962 unsigned char iv[EVP_MAX_IV_LENGTH];
acdf4afb 1963 unsigned char *p, *unenc_authent;
2a1ef754 1964 int padl, outl, unencbufsize;
acdf4afb
RL
1965 struct tm tm_time, *tm_l, *tm_g;
1966 time_t now, tl, tg, tr, tz_offset;
2a1ef754 1967
611ba3f4 1968 EVP_CIPHER_CTX_init(&ciph_ctx);
2a1ef754
RL
1969 *atimep = 0;
1970 kssl_err_set(kssl_err, 0, "");
1971
1972#ifndef KRB5CHECKAUTH
1973 authentp = NULL;
1974#else
1975#if KRB5CHECKAUTH == 0
1976 authentp = NULL;
1977#endif
1978#endif /* KRB5CHECKAUTH */
1979
1980 if (authentp == NULL || authentp->length == 0) return 0;
1981
1982#ifdef KSSL_DEBUG
45442167
RL
1983 {
1984 unsigned int ui;
2a1ef754
RL
1985 printf("kssl_check_authent: authenticator[%d]:\n",authentp->length);
1986 p = authentp->data;
45442167 1987 for (ui=0; ui < authentp->length; ui++) printf("%02x ",p[ui]);
2a1ef754 1988 printf("\n");
45442167 1989 }
2a1ef754
RL
1990#endif /* KSSL_DEBUG */
1991
1992 unencbufsize = 2 * authentp->length;
1993 if ((unenc_authent = calloc(1, unencbufsize)) == NULL)
1994 {
1995 kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
1996 "Unable to allocate authenticator buffer.\n");
1997 krb5rc = KRB5KRB_ERR_GENERIC;
1998 goto err;
1999 }
2000
fdc2bbca 2001 p = (unsigned char *)authentp->data;
2a1ef754
RL
2002 if ((dec_authent = d2i_KRB5_ENCDATA(NULL, &p,
2003 (long) authentp->length)) == NULL)
2004 {
2005 kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
2006 "Error decoding authenticator.\n");
2007 krb5rc = KRB5KRB_AP_ERR_BAD_INTEGRITY;
2008 goto err;
2009 }
2010
2011 enctype = dec_authent->etype->data[0]; /* should = kssl_ctx->enctype */
882e8912
RL
2012#if !defined(KRB5_MIT_OLD11)
2013 switch ( enctype ) {
2014 case ENCTYPE_DES3_CBC_SHA1: /* EVP_des_ede3_cbc(); */
2015 case ENCTYPE_DES3_CBC_SHA:
2016 case ENCTYPE_DES3_CBC_RAW:
2017 krb5rc = 0; /* Skip, can't handle derived keys */
2018 goto err;
2019 }
2020#endif
2a1ef754 2021 enc = kssl_map_enc(enctype);
54a656ef 2022 memset(iv, 0, sizeof iv); /* per RFC 1510 */
2a1ef754 2023
acdf4afb
RL
2024 if (enc == NULL)
2025 {
2026 /* Disable kssl_check_authent for ENCTYPE_DES3_CBC_SHA1.
2027 ** This enctype indicates the authenticator was encrypted
2028 ** using key-usage derived keys which openssl cannot decrypt.
2029 */
2030 goto err;
2031 }
581f1c84 2032 if (!EVP_DecryptInit_ex(&ciph_ctx, enc, NULL, kssl_ctx->key, iv))
acdf4afb
RL
2033 {
2034 kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
581f1c84 2035 "EVP_DecryptInit_ex error decrypting authenticator.\n");
acdf4afb
RL
2036 krb5rc = KRB5KRB_AP_ERR_BAD_INTEGRITY;
2037 goto err;
2038 }
2039 if (!EVP_DecryptUpdate(&ciph_ctx, unenc_authent, &outl,
2040 dec_authent->cipher->data, dec_authent->cipher->length))
2041 {
2042 kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
2043 "EVP_DecryptUpdate error decrypting authenticator.\n");
2044 krb5rc = KRB5KRB_AP_ERR_BAD_INTEGRITY;
2045 goto err;
2046 }
2a1ef754
RL
2047 if (outl > unencbufsize)
2048 {
2049 kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
2050 "Buffer overflow decrypting authenticator.\n");
2051 krb5rc = KRB5KRB_AP_ERR_BAD_INTEGRITY;
2052 goto err;
2053 }
581f1c84 2054 if (!EVP_DecryptFinal_ex(&ciph_ctx, &(unenc_authent[outl]), &padl))
acdf4afb
RL
2055 {
2056 kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
581f1c84 2057 "EVP_DecryptFinal_ex error decrypting authenticator.\n");
acdf4afb
RL
2058 krb5rc = KRB5KRB_AP_ERR_BAD_INTEGRITY;
2059 goto err;
2060 }
2a1ef754
RL
2061 outl += padl;
2062 if (outl > unencbufsize)
2063 {
2064 kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
2065 "Buffer overflow decrypting authenticator.\n");
2066 krb5rc = KRB5KRB_AP_ERR_BAD_INTEGRITY;
2067 goto err;
2068 }
2069 EVP_CIPHER_CTX_cleanup(&ciph_ctx);
2070
2071#ifdef KSSL_DEBUG
2072 printf("kssl_check_authent: decrypted authenticator[%d] =\n", outl);
2073 for (padl=0; padl < outl; padl++) printf("%02x ",unenc_authent[padl]);
2074 printf("\n");
2075#endif /* KSSL_DEBUG */
2076
2077 if ((p = kssl_skip_confound(enctype, unenc_authent)) == NULL)
2078 {
2079 kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
2080 "confounded by authenticator.\n");
2081 krb5rc = KRB5KRB_AP_ERR_BAD_INTEGRITY;
2082 goto err;
2083 }
2084 outl -= p - unenc_authent;
2085
2086 if ((auth = (KRB5_AUTHENTBODY *) d2i_KRB5_AUTHENT(NULL, &p,
2087 (long) outl))==NULL)
2088 {
2089 kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
2090 "Error decoding authenticator body.\n");
2091 krb5rc = KRB5KRB_AP_ERR_BAD_INTEGRITY;
2092 goto err;
2093 }
acdf4afb
RL
2094
2095 memset(&tm_time,0,sizeof(struct tm));
2096 if (k_gmtime(auth->ctime, &tm_time) &&
2097 ((tr = mktime(&tm_time)) != (time_t)(-1)))
2098 {
2099 now = time(&now);
2100 tm_l = localtime(&now); tl = mktime(tm_l);
2101 tm_g = gmtime(&now); tg = mktime(tm_g);
2102 tz_offset = tg - tl;
2103
2104 *atimep = tr - tz_offset;
2105 }
2106
2a1ef754 2107#ifdef KSSL_DEBUG
acdf4afb
RL
2108 printf("kssl_check_authent: returns %d for client time ", *atimep);
2109 if (auth && auth->ctime && auth->ctime->length && auth->ctime->data)
2110 printf("%.*s\n", auth->ctime->length, auth->ctime->data);
2111 else printf("NULL\n");
2a1ef754
RL
2112#endif /* KSSL_DEBUG */
2113
2114 err:
2a1ef754
RL
2115 if (auth) KRB5_AUTHENT_free((KRB5_AUTHENT *) auth);
2116 if (dec_authent) KRB5_ENCDATA_free(dec_authent);
2117 if (unenc_authent) free(unenc_authent);
2118 return krb5rc;
2119 }
2120
2121
2122/* Replaces krb5_build_principal_ext(), with varargs length == 2 (svc, host),
2123** because I dont't know how to stub varargs.
2124** Returns krb5_error_code == ENOMEM on alloc error, otherwise
2125** passes back newly constructed principal, which should be freed by caller.
2126*/
2127krb5_error_code kssl_build_principal_2(
2128 /* UPDATE */ krb5_context context,
2129 /* OUT */ krb5_principal *princ,
2130 /* IN */ int rlen, const char *realm,
2131 /* IN */ int slen, const char *svc,
2132 /* IN */ int hlen, const char *host)
2133 {
2134 krb5_data *p_data = NULL;
2135 krb5_principal new_p = NULL;
2136 char *new_r = NULL;
2137
2138 if ((p_data = (krb5_data *) calloc(2, sizeof(krb5_data))) == NULL ||
2139 (new_p = (krb5_principal) calloc(1, sizeof(krb5_principal_data)))
2140 == NULL) goto err;
2141 new_p->length = 2;
2142 new_p->data = p_data;
2143
2144 if ((new_r = calloc(1, rlen + 1)) == NULL) goto err;
2145 memcpy(new_r, realm, rlen);
2146 krb5_princ_set_realm_length(context, new_p, rlen);
2147 krb5_princ_set_realm_data(context, new_p, new_r);
2148
2149 if ((new_p->data[0].data = calloc(1, slen + 1)) == NULL) goto err;
2150 memcpy(new_p->data[0].data, svc, slen);
2151 new_p->data[0].length = slen;
2152
2153 if ((new_p->data[1].data = calloc(1, hlen + 1)) == NULL) goto err;
2154 memcpy(new_p->data[1].data, host, hlen);
2155 new_p->data[1].length = hlen;
2156
2157 krb5_princ_type(context, new_p) = KRB5_NT_UNKNOWN;
2158 *princ = new_p;
2159 return 0;
2160
2161 err:
2162 if (new_p && new_p[0].data) free(new_p[0].data);
2163 if (new_p && new_p[1].data) free(new_p[1].data);
2164 if (new_p) free(new_p);
2165 if (new_r) free(new_r);
2166 return ENOMEM;
2167 }
2168
2169
bc36ee62 2170#else /* !OPENSSL_NO_KRB5 */
b0dc680f 2171
32d0ad41 2172#if defined(PEDANTIC) || defined(OPENSSL_SYS_VMS)
b0dc680f
BL
2173static int dummy=(int)&dummy;
2174#endif
2175
bc36ee62 2176#endif /* !OPENSSL_NO_KRB5 */
43fcc1b0 2177