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