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