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