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