xref: /onnv-gate/usr/src/common/openssl/ssl/kssl.c (revision 2139:6243c3338933)
10Sstevel@tonic-gate /* ssl/kssl.c -*- mode: C; c-file-style: "eay" -*- */
20Sstevel@tonic-gate /* Written by Vern Staats <staatsvr@asc.hpc.mil> for the OpenSSL project 2000.
30Sstevel@tonic-gate  */
40Sstevel@tonic-gate /* ====================================================================
50Sstevel@tonic-gate  * Copyright (c) 2000 The OpenSSL Project.  All rights reserved.
60Sstevel@tonic-gate  *
70Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
80Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
90Sstevel@tonic-gate  * are met:
100Sstevel@tonic-gate  *
110Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
120Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
150Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in
160Sstevel@tonic-gate  *    the documentation and/or other materials provided with the
170Sstevel@tonic-gate  *    distribution.
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this
200Sstevel@tonic-gate  *    software must display the following acknowledgment:
210Sstevel@tonic-gate  *    "This product includes software developed by the OpenSSL Project
220Sstevel@tonic-gate  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
230Sstevel@tonic-gate  *
240Sstevel@tonic-gate  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
250Sstevel@tonic-gate  *    endorse or promote products derived from this software without
260Sstevel@tonic-gate  *    prior written permission. For written permission, please contact
270Sstevel@tonic-gate  *    licensing@OpenSSL.org.
280Sstevel@tonic-gate  *
290Sstevel@tonic-gate  * 5. Products derived from this software may not be called "OpenSSL"
300Sstevel@tonic-gate  *    nor may "OpenSSL" appear in their names without prior written
310Sstevel@tonic-gate  *    permission of the OpenSSL Project.
320Sstevel@tonic-gate  *
330Sstevel@tonic-gate  * 6. Redistributions of any form whatsoever must retain the following
340Sstevel@tonic-gate  *    acknowledgment:
350Sstevel@tonic-gate  *    "This product includes software developed by the OpenSSL Project
360Sstevel@tonic-gate  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
370Sstevel@tonic-gate  *
380Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
390Sstevel@tonic-gate  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
400Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
410Sstevel@tonic-gate  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
420Sstevel@tonic-gate  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
430Sstevel@tonic-gate  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
440Sstevel@tonic-gate  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
450Sstevel@tonic-gate  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
460Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
470Sstevel@tonic-gate  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
480Sstevel@tonic-gate  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
490Sstevel@tonic-gate  * OF THE POSSIBILITY OF SUCH DAMAGE.
500Sstevel@tonic-gate  * ====================================================================
510Sstevel@tonic-gate  *
520Sstevel@tonic-gate  * This product includes cryptographic software written by Eric Young
530Sstevel@tonic-gate  * (eay@cryptsoft.com).  This product includes software written by Tim
540Sstevel@tonic-gate  * Hudson (tjh@cryptsoft.com).
550Sstevel@tonic-gate  *
560Sstevel@tonic-gate  */
570Sstevel@tonic-gate 
580Sstevel@tonic-gate 
590Sstevel@tonic-gate /*  ssl/kssl.c  --  Routines to support (& debug) Kerberos5 auth for openssl
600Sstevel@tonic-gate **
610Sstevel@tonic-gate **  19990701	VRS 	Started.
620Sstevel@tonic-gate **  200011??	Jeffrey Altman, Richard Levitte
630Sstevel@tonic-gate **          		Generalized for Heimdal, Newer MIT, & Win32.
640Sstevel@tonic-gate **          		Integrated into main OpenSSL 0.9.7 snapshots.
650Sstevel@tonic-gate **  20010413	Simon Wilkinson, VRS
660Sstevel@tonic-gate **          		Real RFC2712 KerberosWrapper replaces AP_REQ.
670Sstevel@tonic-gate */
680Sstevel@tonic-gate 
690Sstevel@tonic-gate #include <openssl/opensslconf.h>
700Sstevel@tonic-gate 
71*2139Sjp161948 #define _XOPEN_SOURCE 500 /* glibc2 needs this to declare strptime() */
720Sstevel@tonic-gate #include <time.h>
73*2139Sjp161948 #if 0 /* experimental */
740Sstevel@tonic-gate #undef _XOPEN_SOURCE /* To avoid clashes with anything else... */
75*2139Sjp161948 #endif
760Sstevel@tonic-gate #include <string.h>
770Sstevel@tonic-gate 
78*2139Sjp161948 #define KRB5_PRIVATE	1
79*2139Sjp161948 
800Sstevel@tonic-gate #include <openssl/ssl.h>
810Sstevel@tonic-gate #include <openssl/evp.h>
820Sstevel@tonic-gate #include <openssl/objects.h>
830Sstevel@tonic-gate #include <openssl/krb5_asn.h>
840Sstevel@tonic-gate 
850Sstevel@tonic-gate #ifndef OPENSSL_NO_KRB5
860Sstevel@tonic-gate 
87*2139Sjp161948 #ifndef ENOMEM
88*2139Sjp161948 #define ENOMEM KRB5KRB_ERR_GENERIC
89*2139Sjp161948 #endif
90*2139Sjp161948 
910Sstevel@tonic-gate /*
920Sstevel@tonic-gate  * When OpenSSL is built on Windows, we do not want to require that
930Sstevel@tonic-gate  * the Kerberos DLLs be available in order for the OpenSSL DLLs to
940Sstevel@tonic-gate  * work.  Therefore, all Kerberos routines are loaded at run time
950Sstevel@tonic-gate  * and we do not link to a .LIB file.
960Sstevel@tonic-gate  */
970Sstevel@tonic-gate 
980Sstevel@tonic-gate #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32)
990Sstevel@tonic-gate /*
1000Sstevel@tonic-gate  * The purpose of the following pre-processor statements is to provide
1010Sstevel@tonic-gate  * compatibility with different releases of MIT Kerberos for Windows.
1020Sstevel@tonic-gate  * All versions up to 1.2 used macros.  But macros do not allow for
1030Sstevel@tonic-gate  * a binary compatible interface for DLLs.  Therefore, all macros are
1040Sstevel@tonic-gate  * being replaced by function calls.  The following code will allow
1050Sstevel@tonic-gate  * an OpenSSL DLL built on Windows to work whether or not the macro
1060Sstevel@tonic-gate  * or function form of the routines are utilized.
1070Sstevel@tonic-gate  */
1080Sstevel@tonic-gate #ifdef  krb5_cc_get_principal
1090Sstevel@tonic-gate #define NO_DEF_KRB5_CCACHE
1100Sstevel@tonic-gate #undef  krb5_cc_get_principal
1110Sstevel@tonic-gate #endif
1120Sstevel@tonic-gate #define krb5_cc_get_principal    kssl_krb5_cc_get_principal
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate #define krb5_free_data_contents  kssl_krb5_free_data_contents
1150Sstevel@tonic-gate #define krb5_free_context        kssl_krb5_free_context
1160Sstevel@tonic-gate #define krb5_auth_con_free       kssl_krb5_auth_con_free
1170Sstevel@tonic-gate #define krb5_free_principal      kssl_krb5_free_principal
1180Sstevel@tonic-gate #define krb5_mk_req_extended     kssl_krb5_mk_req_extended
1190Sstevel@tonic-gate #define krb5_get_credentials     kssl_krb5_get_credentials
1200Sstevel@tonic-gate #define krb5_cc_default          kssl_krb5_cc_default
1210Sstevel@tonic-gate #define krb5_sname_to_principal  kssl_krb5_sname_to_principal
1220Sstevel@tonic-gate #define krb5_init_context        kssl_krb5_init_context
1230Sstevel@tonic-gate #define krb5_free_ticket         kssl_krb5_free_ticket
1240Sstevel@tonic-gate #define krb5_rd_req              kssl_krb5_rd_req
1250Sstevel@tonic-gate #define krb5_kt_default          kssl_krb5_kt_default
1260Sstevel@tonic-gate #define krb5_kt_resolve          kssl_krb5_kt_resolve
1270Sstevel@tonic-gate /* macros in mit 1.2.2 and earlier; functions in mit 1.2.3 and greater */
1280Sstevel@tonic-gate #ifndef krb5_kt_close
1290Sstevel@tonic-gate #define krb5_kt_close            kssl_krb5_kt_close
1300Sstevel@tonic-gate #endif /* krb5_kt_close */
1310Sstevel@tonic-gate #ifndef krb5_kt_get_entry
1320Sstevel@tonic-gate #define krb5_kt_get_entry        kssl_krb5_kt_get_entry
1330Sstevel@tonic-gate #endif /* krb5_kt_get_entry */
1340Sstevel@tonic-gate #define krb5_auth_con_init       kssl_krb5_auth_con_init
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate #define krb5_principal_compare   kssl_krb5_principal_compare
1370Sstevel@tonic-gate #define krb5_decrypt_tkt_part    kssl_krb5_decrypt_tkt_part
1380Sstevel@tonic-gate #define krb5_timeofday           kssl_krb5_timeofday
1390Sstevel@tonic-gate #define krb5_rc_default           kssl_krb5_rc_default
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate #ifdef krb5_rc_initialize
1420Sstevel@tonic-gate #undef krb5_rc_initialize
1430Sstevel@tonic-gate #endif
1440Sstevel@tonic-gate #define krb5_rc_initialize   kssl_krb5_rc_initialize
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate #ifdef krb5_rc_get_lifespan
1470Sstevel@tonic-gate #undef krb5_rc_get_lifespan
1480Sstevel@tonic-gate #endif
1490Sstevel@tonic-gate #define krb5_rc_get_lifespan kssl_krb5_rc_get_lifespan
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate #ifdef krb5_rc_destroy
1520Sstevel@tonic-gate #undef krb5_rc_destroy
1530Sstevel@tonic-gate #endif
1540Sstevel@tonic-gate #define krb5_rc_destroy      kssl_krb5_rc_destroy
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate #define valid_cksumtype      kssl_valid_cksumtype
1570Sstevel@tonic-gate #define krb5_checksum_size   kssl_krb5_checksum_size
1580Sstevel@tonic-gate #define krb5_kt_free_entry   kssl_krb5_kt_free_entry
1590Sstevel@tonic-gate #define krb5_auth_con_setrcache  kssl_krb5_auth_con_setrcache
1600Sstevel@tonic-gate #define krb5_auth_con_getrcache  kssl_krb5_auth_con_getrcache
1610Sstevel@tonic-gate #define krb5_get_server_rcache   kssl_krb5_get_server_rcache
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate /* Prototypes for built in stubs */
1640Sstevel@tonic-gate void kssl_krb5_free_data_contents(krb5_context, krb5_data *);
1650Sstevel@tonic-gate void kssl_krb5_free_principal(krb5_context, krb5_principal );
1660Sstevel@tonic-gate krb5_error_code kssl_krb5_kt_resolve(krb5_context,
1670Sstevel@tonic-gate                                      krb5_const char *,
1680Sstevel@tonic-gate                                      krb5_keytab *);
1690Sstevel@tonic-gate krb5_error_code kssl_krb5_kt_default(krb5_context,
1700Sstevel@tonic-gate                                      krb5_keytab *);
1710Sstevel@tonic-gate krb5_error_code kssl_krb5_free_ticket(krb5_context, krb5_ticket *);
1720Sstevel@tonic-gate krb5_error_code kssl_krb5_rd_req(krb5_context, krb5_auth_context *,
1730Sstevel@tonic-gate                                  krb5_const krb5_data *,
1740Sstevel@tonic-gate                                  krb5_const_principal, krb5_keytab,
1750Sstevel@tonic-gate                                  krb5_flags *,krb5_ticket **);
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate krb5_boolean kssl_krb5_principal_compare(krb5_context, krb5_const_principal,
1780Sstevel@tonic-gate                                          krb5_const_principal);
1790Sstevel@tonic-gate krb5_error_code kssl_krb5_mk_req_extended(krb5_context,
1800Sstevel@tonic-gate                                           krb5_auth_context  *,
1810Sstevel@tonic-gate                                           krb5_const krb5_flags,
1820Sstevel@tonic-gate                                           krb5_data  *,
1830Sstevel@tonic-gate                                           krb5_creds  *,
1840Sstevel@tonic-gate                                           krb5_data  * );
1850Sstevel@tonic-gate krb5_error_code kssl_krb5_init_context(krb5_context *);
1860Sstevel@tonic-gate void kssl_krb5_free_context(krb5_context);
1870Sstevel@tonic-gate krb5_error_code kssl_krb5_cc_default(krb5_context,krb5_ccache  *);
1880Sstevel@tonic-gate krb5_error_code kssl_krb5_sname_to_principal(krb5_context,
1890Sstevel@tonic-gate                                              krb5_const char  *,
1900Sstevel@tonic-gate                                              krb5_const char  *,
1910Sstevel@tonic-gate                                              krb5_int32,
1920Sstevel@tonic-gate                                              krb5_principal  *);
1930Sstevel@tonic-gate krb5_error_code kssl_krb5_get_credentials(krb5_context,
1940Sstevel@tonic-gate                                           krb5_const krb5_flags,
1950Sstevel@tonic-gate                                           krb5_ccache,
1960Sstevel@tonic-gate                                           krb5_creds  *,
1970Sstevel@tonic-gate                                           krb5_creds  *  *);
1980Sstevel@tonic-gate krb5_error_code kssl_krb5_auth_con_init(krb5_context,
1990Sstevel@tonic-gate                                         krb5_auth_context  *);
2000Sstevel@tonic-gate krb5_error_code kssl_krb5_cc_get_principal(krb5_context context,
2010Sstevel@tonic-gate                                            krb5_ccache cache,
2020Sstevel@tonic-gate                                            krb5_principal *principal);
2030Sstevel@tonic-gate krb5_error_code kssl_krb5_auth_con_free(krb5_context,krb5_auth_context);
2040Sstevel@tonic-gate size_t kssl_krb5_checksum_size(krb5_context context,krb5_cksumtype ctype);
2050Sstevel@tonic-gate krb5_boolean kssl_valid_cksumtype(krb5_cksumtype ctype);
2060Sstevel@tonic-gate krb5_error_code krb5_kt_free_entry(krb5_context,krb5_keytab_entry FAR * );
2070Sstevel@tonic-gate krb5_error_code kssl_krb5_auth_con_setrcache(krb5_context,
2080Sstevel@tonic-gate                                              krb5_auth_context,
2090Sstevel@tonic-gate                                              krb5_rcache);
2100Sstevel@tonic-gate krb5_error_code kssl_krb5_get_server_rcache(krb5_context,
2110Sstevel@tonic-gate                                             krb5_const krb5_data *,
2120Sstevel@tonic-gate                                             krb5_rcache *);
2130Sstevel@tonic-gate krb5_error_code kssl_krb5_auth_con_getrcache(krb5_context,
2140Sstevel@tonic-gate                                              krb5_auth_context,
2150Sstevel@tonic-gate                                              krb5_rcache *);
2160Sstevel@tonic-gate 
2170Sstevel@tonic-gate /* Function pointers (almost all Kerberos functions are _stdcall) */
2180Sstevel@tonic-gate static void (_stdcall *p_krb5_free_data_contents)(krb5_context, krb5_data *)
2190Sstevel@tonic-gate 	=NULL;
2200Sstevel@tonic-gate static void (_stdcall *p_krb5_free_principal)(krb5_context, krb5_principal )
2210Sstevel@tonic-gate 	=NULL;
2220Sstevel@tonic-gate static krb5_error_code(_stdcall *p_krb5_kt_resolve)
2230Sstevel@tonic-gate 			(krb5_context, krb5_const char *, krb5_keytab *)=NULL;
2240Sstevel@tonic-gate static krb5_error_code (_stdcall *p_krb5_kt_default)(krb5_context,
2250Sstevel@tonic-gate                                                      krb5_keytab *)=NULL;
2260Sstevel@tonic-gate static krb5_error_code (_stdcall *p_krb5_free_ticket)(krb5_context,
2270Sstevel@tonic-gate                                                       krb5_ticket *)=NULL;
2280Sstevel@tonic-gate static krb5_error_code (_stdcall *p_krb5_rd_req)(krb5_context,
2290Sstevel@tonic-gate                                                  krb5_auth_context *,
2300Sstevel@tonic-gate                                                  krb5_const krb5_data *,
2310Sstevel@tonic-gate                                                  krb5_const_principal,
2320Sstevel@tonic-gate                                                  krb5_keytab, krb5_flags *,
2330Sstevel@tonic-gate                                                  krb5_ticket **)=NULL;
2340Sstevel@tonic-gate static krb5_error_code (_stdcall *p_krb5_mk_req_extended)
2350Sstevel@tonic-gate 			(krb5_context, krb5_auth_context *,
2360Sstevel@tonic-gate 			 krb5_const krb5_flags, krb5_data *, krb5_creds *,
2370Sstevel@tonic-gate 			 krb5_data * )=NULL;
2380Sstevel@tonic-gate static krb5_error_code (_stdcall *p_krb5_init_context)(krb5_context *)=NULL;
2390Sstevel@tonic-gate static void (_stdcall *p_krb5_free_context)(krb5_context)=NULL;
2400Sstevel@tonic-gate static krb5_error_code (_stdcall *p_krb5_cc_default)(krb5_context,
2410Sstevel@tonic-gate                                                      krb5_ccache  *)=NULL;
2420Sstevel@tonic-gate static krb5_error_code (_stdcall *p_krb5_sname_to_principal)
2430Sstevel@tonic-gate 			(krb5_context, krb5_const char *, krb5_const char *,
2440Sstevel@tonic-gate 			 krb5_int32, krb5_principal *)=NULL;
2450Sstevel@tonic-gate static krb5_error_code (_stdcall *p_krb5_get_credentials)
2460Sstevel@tonic-gate 			(krb5_context, krb5_const krb5_flags, krb5_ccache,
2470Sstevel@tonic-gate 			 krb5_creds *, krb5_creds **)=NULL;
2480Sstevel@tonic-gate static krb5_error_code (_stdcall *p_krb5_auth_con_init)
2490Sstevel@tonic-gate 			(krb5_context, krb5_auth_context *)=NULL;
2500Sstevel@tonic-gate static krb5_error_code (_stdcall *p_krb5_cc_get_principal)
2510Sstevel@tonic-gate 			(krb5_context context, krb5_ccache cache,
2520Sstevel@tonic-gate 			 krb5_principal *principal)=NULL;
2530Sstevel@tonic-gate static krb5_error_code (_stdcall *p_krb5_auth_con_free)
2540Sstevel@tonic-gate 			(krb5_context, krb5_auth_context)=NULL;
2550Sstevel@tonic-gate static krb5_error_code (_stdcall *p_krb5_decrypt_tkt_part)
2560Sstevel@tonic-gate                         (krb5_context, krb5_const krb5_keyblock *,
2570Sstevel@tonic-gate                                            krb5_ticket *)=NULL;
2580Sstevel@tonic-gate static krb5_error_code (_stdcall *p_krb5_timeofday)
2590Sstevel@tonic-gate                         (krb5_context context, krb5_int32 *timeret)=NULL;
2600Sstevel@tonic-gate static krb5_error_code (_stdcall *p_krb5_rc_default)
2610Sstevel@tonic-gate                         (krb5_context context, krb5_rcache *rc)=NULL;
2620Sstevel@tonic-gate static krb5_error_code (_stdcall *p_krb5_rc_initialize)
2630Sstevel@tonic-gate                         (krb5_context context, krb5_rcache rc,
2640Sstevel@tonic-gate                                      krb5_deltat lifespan)=NULL;
2650Sstevel@tonic-gate static krb5_error_code (_stdcall *p_krb5_rc_get_lifespan)
2660Sstevel@tonic-gate                         (krb5_context context, krb5_rcache rc,
2670Sstevel@tonic-gate                                        krb5_deltat *lifespan)=NULL;
2680Sstevel@tonic-gate static krb5_error_code (_stdcall *p_krb5_rc_destroy)
2690Sstevel@tonic-gate                         (krb5_context context, krb5_rcache rc)=NULL;
2700Sstevel@tonic-gate static krb5_boolean (_stdcall *p_krb5_principal_compare)
2710Sstevel@tonic-gate                      (krb5_context, krb5_const_principal, krb5_const_principal)=NULL;
2720Sstevel@tonic-gate static size_t (_stdcall *p_krb5_checksum_size)(krb5_context context,krb5_cksumtype ctype)=NULL;
2730Sstevel@tonic-gate static krb5_boolean (_stdcall *p_valid_cksumtype)(krb5_cksumtype ctype)=NULL;
2740Sstevel@tonic-gate static krb5_error_code (_stdcall *p_krb5_kt_free_entry)
2750Sstevel@tonic-gate                         (krb5_context,krb5_keytab_entry * )=NULL;
2760Sstevel@tonic-gate static krb5_error_code (_stdcall * p_krb5_auth_con_setrcache)(krb5_context,
2770Sstevel@tonic-gate                                                                krb5_auth_context,
2780Sstevel@tonic-gate                                                                krb5_rcache)=NULL;
2790Sstevel@tonic-gate static krb5_error_code (_stdcall * p_krb5_get_server_rcache)(krb5_context,
2800Sstevel@tonic-gate                                                               krb5_const krb5_data *,
2810Sstevel@tonic-gate                                                               krb5_rcache *)=NULL;
2820Sstevel@tonic-gate static krb5_error_code (* p_krb5_auth_con_getrcache)(krb5_context,
2830Sstevel@tonic-gate                                                       krb5_auth_context,
2840Sstevel@tonic-gate                                                       krb5_rcache *)=NULL;
2850Sstevel@tonic-gate static krb5_error_code (_stdcall * p_krb5_kt_close)(krb5_context context,
2860Sstevel@tonic-gate                                                     krb5_keytab keytab)=NULL;
2870Sstevel@tonic-gate static krb5_error_code (_stdcall * p_krb5_kt_get_entry)(krb5_context context,
2880Sstevel@tonic-gate                                                         krb5_keytab keytab,
2890Sstevel@tonic-gate                        krb5_const_principal principal, krb5_kvno vno,
2900Sstevel@tonic-gate                        krb5_enctype enctype, krb5_keytab_entry *entry)=NULL;
2910Sstevel@tonic-gate static int krb5_loaded = 0;     /* only attempt to initialize func ptrs once */
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate /* Function to Load the Kerberos 5 DLL and initialize function pointers */
2940Sstevel@tonic-gate void
load_krb5_dll(void)2950Sstevel@tonic-gate load_krb5_dll(void)
2960Sstevel@tonic-gate 	{
2970Sstevel@tonic-gate 	HANDLE hKRB5_32;
2980Sstevel@tonic-gate 
2990Sstevel@tonic-gate 	krb5_loaded++;
300*2139Sjp161948 	hKRB5_32 = LoadLibrary(TEXT("KRB5_32"));
3010Sstevel@tonic-gate 	if (!hKRB5_32)
3020Sstevel@tonic-gate 		return;
3030Sstevel@tonic-gate 
3040Sstevel@tonic-gate 	(FARPROC) p_krb5_free_data_contents =
3050Sstevel@tonic-gate 		GetProcAddress( hKRB5_32, "krb5_free_data_contents" );
3060Sstevel@tonic-gate 	(FARPROC) p_krb5_free_context =
3070Sstevel@tonic-gate 		GetProcAddress( hKRB5_32, "krb5_free_context" );
3080Sstevel@tonic-gate 	(FARPROC) p_krb5_auth_con_free =
3090Sstevel@tonic-gate 		GetProcAddress( hKRB5_32, "krb5_auth_con_free" );
3100Sstevel@tonic-gate 	(FARPROC) p_krb5_free_principal =
3110Sstevel@tonic-gate 		GetProcAddress( hKRB5_32, "krb5_free_principal" );
3120Sstevel@tonic-gate 	(FARPROC) p_krb5_mk_req_extended =
3130Sstevel@tonic-gate 		GetProcAddress( hKRB5_32, "krb5_mk_req_extended" );
3140Sstevel@tonic-gate 	(FARPROC) p_krb5_get_credentials =
3150Sstevel@tonic-gate 		GetProcAddress( hKRB5_32, "krb5_get_credentials" );
3160Sstevel@tonic-gate 	(FARPROC) p_krb5_cc_get_principal =
3170Sstevel@tonic-gate 		GetProcAddress( hKRB5_32, "krb5_cc_get_principal" );
3180Sstevel@tonic-gate 	(FARPROC) p_krb5_cc_default =
3190Sstevel@tonic-gate 		GetProcAddress( hKRB5_32, "krb5_cc_default" );
3200Sstevel@tonic-gate 	(FARPROC) p_krb5_sname_to_principal =
3210Sstevel@tonic-gate 		GetProcAddress( hKRB5_32, "krb5_sname_to_principal" );
3220Sstevel@tonic-gate 	(FARPROC) p_krb5_init_context =
3230Sstevel@tonic-gate 		GetProcAddress( hKRB5_32, "krb5_init_context" );
3240Sstevel@tonic-gate 	(FARPROC) p_krb5_free_ticket =
3250Sstevel@tonic-gate 		GetProcAddress( hKRB5_32, "krb5_free_ticket" );
3260Sstevel@tonic-gate 	(FARPROC) p_krb5_rd_req =
3270Sstevel@tonic-gate 		GetProcAddress( hKRB5_32, "krb5_rd_req" );
3280Sstevel@tonic-gate 	(FARPROC) p_krb5_principal_compare =
3290Sstevel@tonic-gate 		GetProcAddress( hKRB5_32, "krb5_principal_compare" );
3300Sstevel@tonic-gate 	(FARPROC) p_krb5_decrypt_tkt_part =
3310Sstevel@tonic-gate 		GetProcAddress( hKRB5_32, "krb5_decrypt_tkt_part" );
3320Sstevel@tonic-gate 	(FARPROC) p_krb5_timeofday =
3330Sstevel@tonic-gate 		GetProcAddress( hKRB5_32, "krb5_timeofday" );
3340Sstevel@tonic-gate 	(FARPROC) p_krb5_rc_default =
3350Sstevel@tonic-gate 		GetProcAddress( hKRB5_32, "krb5_rc_default" );
3360Sstevel@tonic-gate 	(FARPROC) p_krb5_rc_initialize =
3370Sstevel@tonic-gate 		GetProcAddress( hKRB5_32, "krb5_rc_initialize" );
3380Sstevel@tonic-gate 	(FARPROC) p_krb5_rc_get_lifespan =
3390Sstevel@tonic-gate 		GetProcAddress( hKRB5_32, "krb5_rc_get_lifespan" );
3400Sstevel@tonic-gate 	(FARPROC) p_krb5_rc_destroy =
3410Sstevel@tonic-gate 		GetProcAddress( hKRB5_32, "krb5_rc_destroy" );
3420Sstevel@tonic-gate 	(FARPROC) p_krb5_kt_default =
3430Sstevel@tonic-gate 		GetProcAddress( hKRB5_32, "krb5_kt_default" );
3440Sstevel@tonic-gate 	(FARPROC) p_krb5_kt_resolve =
3450Sstevel@tonic-gate 		GetProcAddress( hKRB5_32, "krb5_kt_resolve" );
3460Sstevel@tonic-gate 	(FARPROC) p_krb5_auth_con_init =
3470Sstevel@tonic-gate 		GetProcAddress( hKRB5_32, "krb5_auth_con_init" );
3480Sstevel@tonic-gate         (FARPROC) p_valid_cksumtype =
3490Sstevel@tonic-gate                 GetProcAddress( hKRB5_32, "valid_cksumtype" );
3500Sstevel@tonic-gate         (FARPROC) p_krb5_checksum_size =
3510Sstevel@tonic-gate                 GetProcAddress( hKRB5_32, "krb5_checksum_size" );
3520Sstevel@tonic-gate         (FARPROC) p_krb5_kt_free_entry =
3530Sstevel@tonic-gate                 GetProcAddress( hKRB5_32, "krb5_kt_free_entry" );
3540Sstevel@tonic-gate         (FARPROC) p_krb5_auth_con_setrcache =
3550Sstevel@tonic-gate                 GetProcAddress( hKRB5_32, "krb5_auth_con_setrcache" );
3560Sstevel@tonic-gate         (FARPROC) p_krb5_get_server_rcache =
3570Sstevel@tonic-gate                 GetProcAddress( hKRB5_32, "krb5_get_server_rcache" );
3580Sstevel@tonic-gate         (FARPROC) p_krb5_auth_con_getrcache =
3590Sstevel@tonic-gate                 GetProcAddress( hKRB5_32, "krb5_auth_con_getrcache" );
3600Sstevel@tonic-gate         (FARPROC) p_krb5_kt_close =
3610Sstevel@tonic-gate                 GetProcAddress( hKRB5_32, "krb5_kt_close" );
3620Sstevel@tonic-gate         (FARPROC) p_krb5_kt_get_entry =
3630Sstevel@tonic-gate                 GetProcAddress( hKRB5_32, "krb5_kt_get_entry" );
3640Sstevel@tonic-gate 	}
3650Sstevel@tonic-gate 
3660Sstevel@tonic-gate /* Stubs for each function to be dynamicly loaded */
3670Sstevel@tonic-gate void
kssl_krb5_free_data_contents(krb5_context CO,krb5_data * data)3680Sstevel@tonic-gate kssl_krb5_free_data_contents(krb5_context CO, krb5_data  * data)
3690Sstevel@tonic-gate 	{
3700Sstevel@tonic-gate 	if (!krb5_loaded)
3710Sstevel@tonic-gate 		load_krb5_dll();
3720Sstevel@tonic-gate 
3730Sstevel@tonic-gate 	if ( p_krb5_free_data_contents )
3740Sstevel@tonic-gate 		p_krb5_free_data_contents(CO,data);
3750Sstevel@tonic-gate 	}
3760Sstevel@tonic-gate 
3770Sstevel@tonic-gate 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)3780Sstevel@tonic-gate kssl_krb5_mk_req_extended (krb5_context CO,
3790Sstevel@tonic-gate                           krb5_auth_context  * pACO,
3800Sstevel@tonic-gate                           krb5_const krb5_flags F,
3810Sstevel@tonic-gate                           krb5_data  * pD1,
3820Sstevel@tonic-gate                           krb5_creds  * pC,
3830Sstevel@tonic-gate                           krb5_data  * pD2)
3840Sstevel@tonic-gate 	{
3850Sstevel@tonic-gate 	if (!krb5_loaded)
3860Sstevel@tonic-gate 		load_krb5_dll();
3870Sstevel@tonic-gate 
3880Sstevel@tonic-gate 	if ( p_krb5_mk_req_extended )
3890Sstevel@tonic-gate 		return(p_krb5_mk_req_extended(CO,pACO,F,pD1,pC,pD2));
3900Sstevel@tonic-gate 	else
3910Sstevel@tonic-gate 		return KRB5KRB_ERR_GENERIC;
3920Sstevel@tonic-gate 	}
3930Sstevel@tonic-gate krb5_error_code
kssl_krb5_auth_con_init(krb5_context CO,krb5_auth_context * pACO)3940Sstevel@tonic-gate kssl_krb5_auth_con_init(krb5_context CO,
3950Sstevel@tonic-gate                        krb5_auth_context  * pACO)
3960Sstevel@tonic-gate 	{
3970Sstevel@tonic-gate 	if (!krb5_loaded)
3980Sstevel@tonic-gate 		load_krb5_dll();
3990Sstevel@tonic-gate 
4000Sstevel@tonic-gate 	if ( p_krb5_auth_con_init )
4010Sstevel@tonic-gate 		return(p_krb5_auth_con_init(CO,pACO));
4020Sstevel@tonic-gate 	else
4030Sstevel@tonic-gate 		return KRB5KRB_ERR_GENERIC;
4040Sstevel@tonic-gate 	}
4050Sstevel@tonic-gate krb5_error_code
kssl_krb5_auth_con_free(krb5_context CO,krb5_auth_context ACO)4060Sstevel@tonic-gate kssl_krb5_auth_con_free (krb5_context CO,
4070Sstevel@tonic-gate                         krb5_auth_context ACO)
4080Sstevel@tonic-gate 	{
4090Sstevel@tonic-gate 	if (!krb5_loaded)
4100Sstevel@tonic-gate 		load_krb5_dll();
4110Sstevel@tonic-gate 
4120Sstevel@tonic-gate 	if ( p_krb5_auth_con_free )
4130Sstevel@tonic-gate 		return(p_krb5_auth_con_free(CO,ACO));
4140Sstevel@tonic-gate 	else
4150Sstevel@tonic-gate 		return KRB5KRB_ERR_GENERIC;
4160Sstevel@tonic-gate 	}
4170Sstevel@tonic-gate krb5_error_code
kssl_krb5_get_credentials(krb5_context CO,krb5_const krb5_flags F,krb5_ccache CC,krb5_creds * pCR,krb5_creds ** ppCR)4180Sstevel@tonic-gate kssl_krb5_get_credentials(krb5_context CO,
4190Sstevel@tonic-gate                          krb5_const krb5_flags F,
4200Sstevel@tonic-gate                          krb5_ccache CC,
4210Sstevel@tonic-gate                          krb5_creds  * pCR,
4220Sstevel@tonic-gate                          krb5_creds  ** ppCR)
4230Sstevel@tonic-gate 	{
4240Sstevel@tonic-gate 	if (!krb5_loaded)
4250Sstevel@tonic-gate 		load_krb5_dll();
4260Sstevel@tonic-gate 
4270Sstevel@tonic-gate 	if ( p_krb5_get_credentials )
4280Sstevel@tonic-gate 		return(p_krb5_get_credentials(CO,F,CC,pCR,ppCR));
4290Sstevel@tonic-gate 	else
4300Sstevel@tonic-gate 		return KRB5KRB_ERR_GENERIC;
4310Sstevel@tonic-gate 	}
4320Sstevel@tonic-gate 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)4330Sstevel@tonic-gate kssl_krb5_sname_to_principal(krb5_context CO,
4340Sstevel@tonic-gate                             krb5_const char  * pC1,
4350Sstevel@tonic-gate                             krb5_const char  * pC2,
4360Sstevel@tonic-gate                             krb5_int32 I,
4370Sstevel@tonic-gate                             krb5_principal  * pPR)
4380Sstevel@tonic-gate 	{
4390Sstevel@tonic-gate 	if (!krb5_loaded)
4400Sstevel@tonic-gate 		load_krb5_dll();
4410Sstevel@tonic-gate 
4420Sstevel@tonic-gate 	if ( p_krb5_sname_to_principal )
4430Sstevel@tonic-gate 		return(p_krb5_sname_to_principal(CO,pC1,pC2,I,pPR));
4440Sstevel@tonic-gate 	else
4450Sstevel@tonic-gate 		return KRB5KRB_ERR_GENERIC;
4460Sstevel@tonic-gate 	}
4470Sstevel@tonic-gate 
4480Sstevel@tonic-gate krb5_error_code
kssl_krb5_cc_default(krb5_context CO,krb5_ccache * pCC)4490Sstevel@tonic-gate kssl_krb5_cc_default(krb5_context CO,
4500Sstevel@tonic-gate                     krb5_ccache  * pCC)
4510Sstevel@tonic-gate 	{
4520Sstevel@tonic-gate 	if (!krb5_loaded)
4530Sstevel@tonic-gate 		load_krb5_dll();
4540Sstevel@tonic-gate 
4550Sstevel@tonic-gate 	if ( p_krb5_cc_default )
4560Sstevel@tonic-gate 		return(p_krb5_cc_default(CO,pCC));
4570Sstevel@tonic-gate 	else
4580Sstevel@tonic-gate 		return KRB5KRB_ERR_GENERIC;
4590Sstevel@tonic-gate 	}
4600Sstevel@tonic-gate 
4610Sstevel@tonic-gate krb5_error_code
kssl_krb5_init_context(krb5_context * pCO)4620Sstevel@tonic-gate kssl_krb5_init_context(krb5_context * pCO)
4630Sstevel@tonic-gate 	{
4640Sstevel@tonic-gate 	if (!krb5_loaded)
4650Sstevel@tonic-gate 		load_krb5_dll();
4660Sstevel@tonic-gate 
4670Sstevel@tonic-gate 	if ( p_krb5_init_context )
4680Sstevel@tonic-gate 		return(p_krb5_init_context(pCO));
4690Sstevel@tonic-gate 	else
4700Sstevel@tonic-gate 		return KRB5KRB_ERR_GENERIC;
4710Sstevel@tonic-gate 	}
4720Sstevel@tonic-gate 
4730Sstevel@tonic-gate void
kssl_krb5_free_context(krb5_context CO)4740Sstevel@tonic-gate kssl_krb5_free_context(krb5_context CO)
4750Sstevel@tonic-gate 	{
4760Sstevel@tonic-gate 	if (!krb5_loaded)
4770Sstevel@tonic-gate 		load_krb5_dll();
4780Sstevel@tonic-gate 
4790Sstevel@tonic-gate 	if ( p_krb5_free_context )
4800Sstevel@tonic-gate 		p_krb5_free_context(CO);
4810Sstevel@tonic-gate 	}
4820Sstevel@tonic-gate 
4830Sstevel@tonic-gate void
kssl_krb5_free_principal(krb5_context c,krb5_principal p)4840Sstevel@tonic-gate kssl_krb5_free_principal(krb5_context c, krb5_principal p)
4850Sstevel@tonic-gate 	{
4860Sstevel@tonic-gate 	if (!krb5_loaded)
4870Sstevel@tonic-gate 		load_krb5_dll();
4880Sstevel@tonic-gate 
4890Sstevel@tonic-gate 	if ( p_krb5_free_principal )
4900Sstevel@tonic-gate 		p_krb5_free_principal(c,p);
4910Sstevel@tonic-gate 	}
4920Sstevel@tonic-gate 
4930Sstevel@tonic-gate krb5_error_code
kssl_krb5_kt_resolve(krb5_context con,krb5_const char * sz,krb5_keytab * kt)4940Sstevel@tonic-gate kssl_krb5_kt_resolve(krb5_context con,
4950Sstevel@tonic-gate                     krb5_const char * sz,
4960Sstevel@tonic-gate                     krb5_keytab * kt)
4970Sstevel@tonic-gate 	{
4980Sstevel@tonic-gate 	if (!krb5_loaded)
4990Sstevel@tonic-gate 		load_krb5_dll();
5000Sstevel@tonic-gate 
5010Sstevel@tonic-gate 	if ( p_krb5_kt_resolve )
5020Sstevel@tonic-gate 		return(p_krb5_kt_resolve(con,sz,kt));
5030Sstevel@tonic-gate 	else
5040Sstevel@tonic-gate 		return KRB5KRB_ERR_GENERIC;
5050Sstevel@tonic-gate 	}
5060Sstevel@tonic-gate 
5070Sstevel@tonic-gate krb5_error_code
kssl_krb5_kt_default(krb5_context con,krb5_keytab * kt)5080Sstevel@tonic-gate kssl_krb5_kt_default(krb5_context con,
5090Sstevel@tonic-gate                     krb5_keytab * kt)
5100Sstevel@tonic-gate 	{
5110Sstevel@tonic-gate 	if (!krb5_loaded)
5120Sstevel@tonic-gate 		load_krb5_dll();
5130Sstevel@tonic-gate 
5140Sstevel@tonic-gate 	if ( p_krb5_kt_default )
5150Sstevel@tonic-gate 		return(p_krb5_kt_default(con,kt));
5160Sstevel@tonic-gate 	else
5170Sstevel@tonic-gate 		return KRB5KRB_ERR_GENERIC;
5180Sstevel@tonic-gate 	}
5190Sstevel@tonic-gate 
5200Sstevel@tonic-gate krb5_error_code
kssl_krb5_free_ticket(krb5_context con,krb5_ticket * kt)5210Sstevel@tonic-gate kssl_krb5_free_ticket(krb5_context con,
5220Sstevel@tonic-gate                      krb5_ticket * kt)
5230Sstevel@tonic-gate 	{
5240Sstevel@tonic-gate 	if (!krb5_loaded)
5250Sstevel@tonic-gate 		load_krb5_dll();
5260Sstevel@tonic-gate 
5270Sstevel@tonic-gate 	if ( p_krb5_free_ticket )
5280Sstevel@tonic-gate 		return(p_krb5_free_ticket(con,kt));
5290Sstevel@tonic-gate 	else
5300Sstevel@tonic-gate 		return KRB5KRB_ERR_GENERIC;
5310Sstevel@tonic-gate 	}
5320Sstevel@tonic-gate 
5330Sstevel@tonic-gate 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)5340Sstevel@tonic-gate kssl_krb5_rd_req(krb5_context con, krb5_auth_context * pacon,
5350Sstevel@tonic-gate                 krb5_const krb5_data * data,
5360Sstevel@tonic-gate                 krb5_const_principal princ, krb5_keytab keytab,
5370Sstevel@tonic-gate                 krb5_flags * flags, krb5_ticket ** pptkt)
5380Sstevel@tonic-gate 	{
5390Sstevel@tonic-gate 	if (!krb5_loaded)
5400Sstevel@tonic-gate 		load_krb5_dll();
5410Sstevel@tonic-gate 
5420Sstevel@tonic-gate 	if ( p_krb5_rd_req )
5430Sstevel@tonic-gate 		return(p_krb5_rd_req(con,pacon,data,princ,keytab,flags,pptkt));
5440Sstevel@tonic-gate 	else
5450Sstevel@tonic-gate 		return KRB5KRB_ERR_GENERIC;
5460Sstevel@tonic-gate 	}
5470Sstevel@tonic-gate 
5480Sstevel@tonic-gate krb5_boolean
krb5_principal_compare(krb5_context con,krb5_const_principal princ1,krb5_const_principal princ2)5490Sstevel@tonic-gate krb5_principal_compare(krb5_context con, krb5_const_principal princ1,
5500Sstevel@tonic-gate                 krb5_const_principal princ2)
5510Sstevel@tonic-gate 	{
5520Sstevel@tonic-gate 	if (!krb5_loaded)
5530Sstevel@tonic-gate 		load_krb5_dll();
5540Sstevel@tonic-gate 
5550Sstevel@tonic-gate 	if ( p_krb5_principal_compare )
5560Sstevel@tonic-gate 		return(p_krb5_principal_compare(con,princ1,princ2));
5570Sstevel@tonic-gate 	else
5580Sstevel@tonic-gate 		return KRB5KRB_ERR_GENERIC;
5590Sstevel@tonic-gate 	}
5600Sstevel@tonic-gate 
5610Sstevel@tonic-gate krb5_error_code
krb5_decrypt_tkt_part(krb5_context con,krb5_const krb5_keyblock * keys,krb5_ticket * ticket)5620Sstevel@tonic-gate krb5_decrypt_tkt_part(krb5_context con, krb5_const krb5_keyblock *keys,
5630Sstevel@tonic-gate                 krb5_ticket *ticket)
5640Sstevel@tonic-gate 	{
5650Sstevel@tonic-gate 	if (!krb5_loaded)
5660Sstevel@tonic-gate 		load_krb5_dll();
5670Sstevel@tonic-gate 
5680Sstevel@tonic-gate 	if ( p_krb5_decrypt_tkt_part )
5690Sstevel@tonic-gate 		return(p_krb5_decrypt_tkt_part(con,keys,ticket));
5700Sstevel@tonic-gate 	else
5710Sstevel@tonic-gate 		return KRB5KRB_ERR_GENERIC;
5720Sstevel@tonic-gate 	}
5730Sstevel@tonic-gate 
5740Sstevel@tonic-gate krb5_error_code
krb5_timeofday(krb5_context con,krb5_int32 * timeret)5750Sstevel@tonic-gate krb5_timeofday(krb5_context con, krb5_int32 *timeret)
5760Sstevel@tonic-gate 	{
5770Sstevel@tonic-gate 	if (!krb5_loaded)
5780Sstevel@tonic-gate 		load_krb5_dll();
5790Sstevel@tonic-gate 
5800Sstevel@tonic-gate 	if ( p_krb5_timeofday )
5810Sstevel@tonic-gate 		return(p_krb5_timeofday(con,timeret));
5820Sstevel@tonic-gate 	else
5830Sstevel@tonic-gate 		return KRB5KRB_ERR_GENERIC;
5840Sstevel@tonic-gate 	}
5850Sstevel@tonic-gate 
5860Sstevel@tonic-gate krb5_error_code
krb5_rc_default(krb5_context con,krb5_rcache * rc)5870Sstevel@tonic-gate krb5_rc_default(krb5_context con, krb5_rcache *rc)
5880Sstevel@tonic-gate 	{
5890Sstevel@tonic-gate 	if (!krb5_loaded)
5900Sstevel@tonic-gate 		load_krb5_dll();
5910Sstevel@tonic-gate 
5920Sstevel@tonic-gate 	if ( p_krb5_rc_default )
5930Sstevel@tonic-gate 		return(p_krb5_rc_default(con,rc));
5940Sstevel@tonic-gate 	else
5950Sstevel@tonic-gate 		return KRB5KRB_ERR_GENERIC;
5960Sstevel@tonic-gate 	}
5970Sstevel@tonic-gate 
5980Sstevel@tonic-gate krb5_error_code
krb5_rc_initialize(krb5_context con,krb5_rcache rc,krb5_deltat lifespan)5990Sstevel@tonic-gate krb5_rc_initialize(krb5_context con, krb5_rcache rc, krb5_deltat lifespan)
6000Sstevel@tonic-gate 	{
6010Sstevel@tonic-gate 	if (!krb5_loaded)
6020Sstevel@tonic-gate 		load_krb5_dll();
6030Sstevel@tonic-gate 
6040Sstevel@tonic-gate 	if ( p_krb5_rc_initialize )
6050Sstevel@tonic-gate 		return(p_krb5_rc_initialize(con, rc, lifespan));
6060Sstevel@tonic-gate 	else
6070Sstevel@tonic-gate 		return KRB5KRB_ERR_GENERIC;
6080Sstevel@tonic-gate 	}
6090Sstevel@tonic-gate 
6100Sstevel@tonic-gate krb5_error_code
krb5_rc_get_lifespan(krb5_context con,krb5_rcache rc,krb5_deltat * lifespanp)6110Sstevel@tonic-gate krb5_rc_get_lifespan(krb5_context con, krb5_rcache rc, krb5_deltat *lifespanp)
6120Sstevel@tonic-gate 	{
6130Sstevel@tonic-gate 	if (!krb5_loaded)
6140Sstevel@tonic-gate 		load_krb5_dll();
6150Sstevel@tonic-gate 
6160Sstevel@tonic-gate 	if ( p_krb5_rc_get_lifespan )
6170Sstevel@tonic-gate 		return(p_krb5_rc_get_lifespan(con, rc, lifespanp));
6180Sstevel@tonic-gate 	else
6190Sstevel@tonic-gate 		return KRB5KRB_ERR_GENERIC;
6200Sstevel@tonic-gate 	}
6210Sstevel@tonic-gate 
6220Sstevel@tonic-gate krb5_error_code
krb5_rc_destroy(krb5_context con,krb5_rcache rc)6230Sstevel@tonic-gate krb5_rc_destroy(krb5_context con, krb5_rcache rc)
6240Sstevel@tonic-gate 	{
6250Sstevel@tonic-gate 	if (!krb5_loaded)
6260Sstevel@tonic-gate 		load_krb5_dll();
6270Sstevel@tonic-gate 
6280Sstevel@tonic-gate 	if ( p_krb5_rc_destroy )
6290Sstevel@tonic-gate 		return(p_krb5_rc_destroy(con, rc));
6300Sstevel@tonic-gate 	else
6310Sstevel@tonic-gate 		return KRB5KRB_ERR_GENERIC;
6320Sstevel@tonic-gate 	}
6330Sstevel@tonic-gate 
6340Sstevel@tonic-gate size_t
krb5_checksum_size(krb5_context context,krb5_cksumtype ctype)6350Sstevel@tonic-gate krb5_checksum_size(krb5_context context,krb5_cksumtype ctype)
6360Sstevel@tonic-gate         {
6370Sstevel@tonic-gate         if (!krb5_loaded)
6380Sstevel@tonic-gate                 load_krb5_dll();
6390Sstevel@tonic-gate 
6400Sstevel@tonic-gate         if ( p_krb5_checksum_size )
6410Sstevel@tonic-gate                 return(p_krb5_checksum_size(context, ctype));
6420Sstevel@tonic-gate         else
6430Sstevel@tonic-gate                 return KRB5KRB_ERR_GENERIC;
6440Sstevel@tonic-gate         }
6450Sstevel@tonic-gate 
6460Sstevel@tonic-gate krb5_boolean
valid_cksumtype(krb5_cksumtype ctype)6470Sstevel@tonic-gate valid_cksumtype(krb5_cksumtype ctype)
6480Sstevel@tonic-gate         {
6490Sstevel@tonic-gate         if (!krb5_loaded)
6500Sstevel@tonic-gate                 load_krb5_dll();
6510Sstevel@tonic-gate 
6520Sstevel@tonic-gate         if ( p_valid_cksumtype )
6530Sstevel@tonic-gate                 return(p_valid_cksumtype(ctype));
6540Sstevel@tonic-gate         else
6550Sstevel@tonic-gate                 return KRB5KRB_ERR_GENERIC;
6560Sstevel@tonic-gate         }
6570Sstevel@tonic-gate 
6580Sstevel@tonic-gate krb5_error_code
krb5_kt_free_entry(krb5_context con,krb5_keytab_entry * entry)6590Sstevel@tonic-gate krb5_kt_free_entry(krb5_context con,krb5_keytab_entry * entry)
6600Sstevel@tonic-gate         {
6610Sstevel@tonic-gate         if (!krb5_loaded)
6620Sstevel@tonic-gate                 load_krb5_dll();
6630Sstevel@tonic-gate 
6640Sstevel@tonic-gate         if ( p_krb5_kt_free_entry )
6650Sstevel@tonic-gate                 return(p_krb5_kt_free_entry(con,entry));
6660Sstevel@tonic-gate         else
6670Sstevel@tonic-gate                 return KRB5KRB_ERR_GENERIC;
6680Sstevel@tonic-gate         }
6690Sstevel@tonic-gate 
6700Sstevel@tonic-gate /* Structure definitions  */
6710Sstevel@tonic-gate #ifndef NO_DEF_KRB5_CCACHE
6720Sstevel@tonic-gate #ifndef krb5_x
6730Sstevel@tonic-gate #define krb5_x(ptr,args) ((ptr)?((*(ptr)) args):(abort(),1))
6740Sstevel@tonic-gate #define krb5_xc(ptr,args) ((ptr)?((*(ptr)) args):(abort(),(char*)0))
6750Sstevel@tonic-gate #endif
6760Sstevel@tonic-gate 
6770Sstevel@tonic-gate typedef	krb5_pointer	krb5_cc_cursor;	/* cursor for sequential lookup */
6780Sstevel@tonic-gate 
6790Sstevel@tonic-gate typedef struct _krb5_ccache
6800Sstevel@tonic-gate 	{
6810Sstevel@tonic-gate 	krb5_magic magic;
6820Sstevel@tonic-gate 	struct _krb5_cc_ops FAR *ops;
6830Sstevel@tonic-gate 	krb5_pointer data;
6840Sstevel@tonic-gate 	} *krb5_ccache;
6850Sstevel@tonic-gate 
6860Sstevel@tonic-gate typedef struct _krb5_cc_ops
6870Sstevel@tonic-gate 	{
6880Sstevel@tonic-gate 	krb5_magic magic;
6890Sstevel@tonic-gate 	char  *prefix;
6900Sstevel@tonic-gate 	char  * (KRB5_CALLCONV *get_name)
6910Sstevel@tonic-gate 		(krb5_context, krb5_ccache);
6920Sstevel@tonic-gate 	krb5_error_code (KRB5_CALLCONV *resolve)
6930Sstevel@tonic-gate 		(krb5_context, krb5_ccache  *, const char  *);
6940Sstevel@tonic-gate 	krb5_error_code (KRB5_CALLCONV *gen_new)
6950Sstevel@tonic-gate 		(krb5_context, krb5_ccache  *);
6960Sstevel@tonic-gate 	krb5_error_code (KRB5_CALLCONV *init)
6970Sstevel@tonic-gate 		(krb5_context, krb5_ccache, krb5_principal);
6980Sstevel@tonic-gate 	krb5_error_code (KRB5_CALLCONV *destroy)
6990Sstevel@tonic-gate 		(krb5_context, krb5_ccache);
7000Sstevel@tonic-gate 	krb5_error_code (KRB5_CALLCONV *close)
7010Sstevel@tonic-gate 		(krb5_context, krb5_ccache);
7020Sstevel@tonic-gate 	krb5_error_code (KRB5_CALLCONV *store)
7030Sstevel@tonic-gate 		(krb5_context, krb5_ccache, krb5_creds  *);
7040Sstevel@tonic-gate 	krb5_error_code (KRB5_CALLCONV *retrieve)
7050Sstevel@tonic-gate 		(krb5_context, krb5_ccache,
7060Sstevel@tonic-gate 		krb5_flags, krb5_creds  *, krb5_creds  *);
7070Sstevel@tonic-gate 	krb5_error_code (KRB5_CALLCONV *get_princ)
7080Sstevel@tonic-gate 		(krb5_context, krb5_ccache, krb5_principal  *);
7090Sstevel@tonic-gate 	krb5_error_code (KRB5_CALLCONV *get_first)
7100Sstevel@tonic-gate 		(krb5_context, krb5_ccache, krb5_cc_cursor  *);
7110Sstevel@tonic-gate 	krb5_error_code (KRB5_CALLCONV *get_next)
7120Sstevel@tonic-gate 		(krb5_context, krb5_ccache,
7130Sstevel@tonic-gate 		krb5_cc_cursor  *, krb5_creds  *);
7140Sstevel@tonic-gate 	krb5_error_code (KRB5_CALLCONV *end_get)
7150Sstevel@tonic-gate 		(krb5_context, krb5_ccache, krb5_cc_cursor  *);
7160Sstevel@tonic-gate 	krb5_error_code (KRB5_CALLCONV *remove_cred)
7170Sstevel@tonic-gate 		(krb5_context, krb5_ccache,
7180Sstevel@tonic-gate 		krb5_flags, krb5_creds  *);
7190Sstevel@tonic-gate 	krb5_error_code (KRB5_CALLCONV *set_flags)
7200Sstevel@tonic-gate 		(krb5_context, krb5_ccache, krb5_flags);
7210Sstevel@tonic-gate 	} krb5_cc_ops;
7220Sstevel@tonic-gate #endif /* NO_DEF_KRB5_CCACHE */
7230Sstevel@tonic-gate 
7240Sstevel@tonic-gate krb5_error_code
kssl_krb5_cc_get_principal(krb5_context context,krb5_ccache cache,krb5_principal * principal)7250Sstevel@tonic-gate kssl_krb5_cc_get_principal
7260Sstevel@tonic-gate     (krb5_context context, krb5_ccache cache,
7270Sstevel@tonic-gate       krb5_principal *principal)
7280Sstevel@tonic-gate 	{
7290Sstevel@tonic-gate 	if ( p_krb5_cc_get_principal )
7300Sstevel@tonic-gate 		return(p_krb5_cc_get_principal(context,cache,principal));
7310Sstevel@tonic-gate 	else
7320Sstevel@tonic-gate 		return(krb5_x
7330Sstevel@tonic-gate 			((cache)->ops->get_princ,(context, cache, principal)));
7340Sstevel@tonic-gate 	}
7350Sstevel@tonic-gate 
7360Sstevel@tonic-gate krb5_error_code
kssl_krb5_auth_con_setrcache(krb5_context con,krb5_auth_context acon,krb5_rcache rcache)7370Sstevel@tonic-gate kssl_krb5_auth_con_setrcache(krb5_context con, krb5_auth_context acon,
7380Sstevel@tonic-gate                              krb5_rcache rcache)
7390Sstevel@tonic-gate         {
7400Sstevel@tonic-gate         if ( p_krb5_auth_con_setrcache )
7410Sstevel@tonic-gate                  return(p_krb5_auth_con_setrcache(con,acon,rcache));
7420Sstevel@tonic-gate         else
7430Sstevel@tonic-gate                  return KRB5KRB_ERR_GENERIC;
7440Sstevel@tonic-gate         }
7450Sstevel@tonic-gate 
7460Sstevel@tonic-gate krb5_error_code
kssl_krb5_get_server_rcache(krb5_context con,krb5_const krb5_data * data,krb5_rcache * rcache)7470Sstevel@tonic-gate kssl_krb5_get_server_rcache(krb5_context con, krb5_const krb5_data * data,
7480Sstevel@tonic-gate                             krb5_rcache * rcache)
7490Sstevel@tonic-gate         {
7500Sstevel@tonic-gate 	if ( p_krb5_get_server_rcache )
7510Sstevel@tonic-gate 		return(p_krb5_get_server_rcache(con,data,rcache));
7520Sstevel@tonic-gate 	else
7530Sstevel@tonic-gate 		return KRB5KRB_ERR_GENERIC;
7540Sstevel@tonic-gate         }
7550Sstevel@tonic-gate 
7560Sstevel@tonic-gate krb5_error_code
kssl_krb5_auth_con_getrcache(krb5_context con,krb5_auth_context acon,krb5_rcache * prcache)7570Sstevel@tonic-gate kssl_krb5_auth_con_getrcache(krb5_context con, krb5_auth_context acon,
7580Sstevel@tonic-gate                              krb5_rcache * prcache)
7590Sstevel@tonic-gate         {
7600Sstevel@tonic-gate 	if ( p_krb5_auth_con_getrcache )
7610Sstevel@tonic-gate 		return(p_krb5_auth_con_getrcache(con,acon, prcache));
7620Sstevel@tonic-gate 	else
7630Sstevel@tonic-gate 		return KRB5KRB_ERR_GENERIC;
7640Sstevel@tonic-gate 	}
7650Sstevel@tonic-gate 
7660Sstevel@tonic-gate krb5_error_code
kssl_krb5_kt_close(krb5_context context,krb5_keytab keytab)7670Sstevel@tonic-gate kssl_krb5_kt_close(krb5_context context, krb5_keytab keytab)
7680Sstevel@tonic-gate 	{
7690Sstevel@tonic-gate 	if ( p_krb5_kt_close )
7700Sstevel@tonic-gate 		return(p_krb5_kt_close(context,keytab));
7710Sstevel@tonic-gate 	else
7720Sstevel@tonic-gate 		return KRB5KRB_ERR_GENERIC;
7730Sstevel@tonic-gate 	}
7740Sstevel@tonic-gate 
7750Sstevel@tonic-gate 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)7760Sstevel@tonic-gate kssl_krb5_kt_get_entry(krb5_context context, krb5_keytab keytab,
7770Sstevel@tonic-gate                        krb5_const_principal principal, krb5_kvno vno,
7780Sstevel@tonic-gate                        krb5_enctype enctype, krb5_keytab_entry *entry)
7790Sstevel@tonic-gate 	{
7800Sstevel@tonic-gate 	if ( p_krb5_kt_get_entry )
7810Sstevel@tonic-gate 		return(p_krb5_kt_get_entry(context,keytab,principal,vno,enctype,entry));
7820Sstevel@tonic-gate 	else
7830Sstevel@tonic-gate 		return KRB5KRB_ERR_GENERIC;
7840Sstevel@tonic-gate         }
7850Sstevel@tonic-gate #endif  /* OPENSSL_SYS_WINDOWS || OPENSSL_SYS_WIN32 */
7860Sstevel@tonic-gate 
7870Sstevel@tonic-gate char
kstring(char * string)7880Sstevel@tonic-gate *kstring(char *string)
7890Sstevel@tonic-gate         {
7900Sstevel@tonic-gate         static char	*null = "[NULL]";
7910Sstevel@tonic-gate 
7920Sstevel@tonic-gate 	return ((string == NULL)? null: string);
7930Sstevel@tonic-gate         }
7940Sstevel@tonic-gate 
7950Sstevel@tonic-gate /*	Given KRB5 enctype (basically DES or 3DES),
7960Sstevel@tonic-gate **	return closest match openssl EVP_ encryption algorithm.
7970Sstevel@tonic-gate **	Return NULL for unknown or problematic (krb5_dk_encrypt) enctypes.
7980Sstevel@tonic-gate **	Assume ENCTYPE_*_RAW (krb5_raw_encrypt) are OK.
7990Sstevel@tonic-gate */
8000Sstevel@tonic-gate const EVP_CIPHER *
kssl_map_enc(krb5_enctype enctype)8010Sstevel@tonic-gate kssl_map_enc(krb5_enctype enctype)
8020Sstevel@tonic-gate         {
8030Sstevel@tonic-gate 	switch (enctype)
8040Sstevel@tonic-gate 		{
8050Sstevel@tonic-gate 	case ENCTYPE_DES_HMAC_SHA1:		/*    EVP_des_cbc();       */
8060Sstevel@tonic-gate 	case ENCTYPE_DES_CBC_CRC:
8070Sstevel@tonic-gate 	case ENCTYPE_DES_CBC_MD4:
8080Sstevel@tonic-gate 	case ENCTYPE_DES_CBC_MD5:
8090Sstevel@tonic-gate 	case ENCTYPE_DES_CBC_RAW:
8100Sstevel@tonic-gate 				return EVP_des_cbc();
8110Sstevel@tonic-gate 				break;
8120Sstevel@tonic-gate 	case ENCTYPE_DES3_CBC_SHA1:		/*    EVP_des_ede3_cbc();  */
8130Sstevel@tonic-gate 	case ENCTYPE_DES3_CBC_SHA:
8140Sstevel@tonic-gate 	case ENCTYPE_DES3_CBC_RAW:
8150Sstevel@tonic-gate 				return EVP_des_ede3_cbc();
8160Sstevel@tonic-gate 				break;
8170Sstevel@tonic-gate 	default:                return NULL;
8180Sstevel@tonic-gate 				break;
8190Sstevel@tonic-gate 		}
8200Sstevel@tonic-gate 	}
8210Sstevel@tonic-gate 
8220Sstevel@tonic-gate 
8230Sstevel@tonic-gate /*	Return true:1 if p "looks like" the start of the real authenticator
8240Sstevel@tonic-gate **	described in kssl_skip_confound() below.  The ASN.1 pattern is
8250Sstevel@tonic-gate **	"62 xx 30 yy" (APPLICATION-2, SEQUENCE), where xx-yy =~ 2, and
8260Sstevel@tonic-gate **	xx and yy are possibly multi-byte length fields.
8270Sstevel@tonic-gate */
kssl_test_confound(unsigned char * p)8280Sstevel@tonic-gate int 	kssl_test_confound(unsigned char *p)
8290Sstevel@tonic-gate 	{
8300Sstevel@tonic-gate 	int 	len = 2;
8310Sstevel@tonic-gate 	int 	xx = 0, yy = 0;
8320Sstevel@tonic-gate 
8330Sstevel@tonic-gate 	if (*p++ != 0x62)  return 0;
8340Sstevel@tonic-gate 	if (*p > 0x82)  return 0;
8350Sstevel@tonic-gate 	switch(*p)  {
8360Sstevel@tonic-gate 		case 0x82:  p++;          xx = (*p++ << 8);  xx += *p++;  break;
8370Sstevel@tonic-gate 		case 0x81:  p++;          xx =  *p++;  break;
8380Sstevel@tonic-gate 		case 0x80:  return 0;
8390Sstevel@tonic-gate 		default:    xx = *p++;  break;
8400Sstevel@tonic-gate 		}
8410Sstevel@tonic-gate 	if (*p++ != 0x30)  return 0;
8420Sstevel@tonic-gate 	if (*p > 0x82)  return 0;
8430Sstevel@tonic-gate 	switch(*p)  {
8440Sstevel@tonic-gate 		case 0x82:  p++; len+=2;  yy = (*p++ << 8);  yy += *p++;  break;
8450Sstevel@tonic-gate 		case 0x81:  p++; len++;   yy =  *p++;  break;
8460Sstevel@tonic-gate 		case 0x80:  return 0;
8470Sstevel@tonic-gate 		default:    yy = *p++;  break;
8480Sstevel@tonic-gate 		}
8490Sstevel@tonic-gate 
8500Sstevel@tonic-gate 	return (xx - len == yy)? 1: 0;
8510Sstevel@tonic-gate 	}
8520Sstevel@tonic-gate 
8530Sstevel@tonic-gate /*	Allocate, fill, and return cksumlens array of checksum lengths.
8540Sstevel@tonic-gate **	This array holds just the unique elements from the krb5_cksumarray[].
8550Sstevel@tonic-gate **	array[n] == 0 signals end of data.
8560Sstevel@tonic-gate **
8570Sstevel@tonic-gate **      The krb5_cksumarray[] was an internal variable that has since been
8580Sstevel@tonic-gate **      replaced by a more general method for storing the data.  It should
8590Sstevel@tonic-gate **      not be used.  Instead we use real API calls and make a guess for
8600Sstevel@tonic-gate **      what the highest assigned CKSUMTYPE_ constant is.  As of 1.2.2
8610Sstevel@tonic-gate **      it is 0x000c (CKSUMTYPE_HMAC_SHA1_DES3).  So we will use 0x0010.
8620Sstevel@tonic-gate */
populate_cksumlens(void)8630Sstevel@tonic-gate size_t  *populate_cksumlens(void)
8640Sstevel@tonic-gate 	{
8650Sstevel@tonic-gate 	int 		i, j, n;
8660Sstevel@tonic-gate 	static size_t 	*cklens = NULL;
8670Sstevel@tonic-gate 
8680Sstevel@tonic-gate #ifdef KRB5_MIT_OLD11
8690Sstevel@tonic-gate 	n = krb5_max_cksum;
8700Sstevel@tonic-gate #else
8710Sstevel@tonic-gate 	n = 0x0010;
8720Sstevel@tonic-gate #endif	/* KRB5_MIT_OLD11 */
8730Sstevel@tonic-gate 
8740Sstevel@tonic-gate #ifdef KRB5CHECKAUTH
8750Sstevel@tonic-gate 	if (!cklens && !(cklens = (size_t *) calloc(sizeof(int),n+1)))  return NULL;
8760Sstevel@tonic-gate 
8770Sstevel@tonic-gate 	for (i=0; i < n; i++)  {
8780Sstevel@tonic-gate 		if (!valid_cksumtype(i))  continue;	/*  array has holes  */
8790Sstevel@tonic-gate 		for (j=0; j < n; j++)  {
8800Sstevel@tonic-gate 			if (cklens[j] == 0)  {
8810Sstevel@tonic-gate 				cklens[j] = krb5_checksum_size(NULL,i);
8820Sstevel@tonic-gate 				break;		/*  krb5 elem was new: add   */
8830Sstevel@tonic-gate 				}
8840Sstevel@tonic-gate 			if (cklens[j] == krb5_checksum_size(NULL,i))  {
8850Sstevel@tonic-gate 				break;		/*  ignore duplicate elements */
8860Sstevel@tonic-gate 				}
8870Sstevel@tonic-gate 			}
8880Sstevel@tonic-gate 		}
8890Sstevel@tonic-gate #endif	/* KRB5CHECKAUTH */
8900Sstevel@tonic-gate 
8910Sstevel@tonic-gate 	return cklens;
8920Sstevel@tonic-gate 	}
8930Sstevel@tonic-gate 
8940Sstevel@tonic-gate /*	Return pointer to start of real authenticator within authenticator, or
8950Sstevel@tonic-gate **	return NULL on error.
8960Sstevel@tonic-gate **	Decrypted authenticator looks like this:
8970Sstevel@tonic-gate **		[0 or 8 byte confounder] [4-24 byte checksum] [real authent'r]
8980Sstevel@tonic-gate **	This hackery wouldn't be necessary if MIT KRB5 1.0.6 had the
8990Sstevel@tonic-gate **	krb5_auth_con_getcksumtype() function advertised in its krb5.h.
9000Sstevel@tonic-gate */
kssl_skip_confound(krb5_enctype etype,unsigned char * a)9010Sstevel@tonic-gate unsigned char	*kssl_skip_confound(krb5_enctype etype, unsigned char *a)
9020Sstevel@tonic-gate 	{
9030Sstevel@tonic-gate 	int 		i, conlen;
9040Sstevel@tonic-gate 	size_t		cklen;
9050Sstevel@tonic-gate 	static size_t 	*cksumlens = NULL;
9060Sstevel@tonic-gate 	unsigned char	*test_auth;
9070Sstevel@tonic-gate 
9080Sstevel@tonic-gate 	conlen = (etype)? 8: 0;
9090Sstevel@tonic-gate 
9100Sstevel@tonic-gate 	if (!cksumlens  &&  !(cksumlens = populate_cksumlens()))  return NULL;
9110Sstevel@tonic-gate 	for (i=0; (cklen = cksumlens[i]) != 0; i++)
9120Sstevel@tonic-gate 		{
9130Sstevel@tonic-gate 		test_auth = a + conlen + cklen;
9140Sstevel@tonic-gate 		if (kssl_test_confound(test_auth))  return test_auth;
9150Sstevel@tonic-gate 		}
9160Sstevel@tonic-gate 
9170Sstevel@tonic-gate 	return NULL;
9180Sstevel@tonic-gate 	}
9190Sstevel@tonic-gate 
9200Sstevel@tonic-gate 
9210Sstevel@tonic-gate /*	Set kssl_err error info when reason text is a simple string
9220Sstevel@tonic-gate **		kssl_err = struct { int reason; char text[KSSL_ERR_MAX+1]; }
9230Sstevel@tonic-gate */
9240Sstevel@tonic-gate void
kssl_err_set(KSSL_ERR * kssl_err,int reason,char * text)9250Sstevel@tonic-gate kssl_err_set(KSSL_ERR *kssl_err, int reason, char *text)
9260Sstevel@tonic-gate         {
9270Sstevel@tonic-gate 	if (kssl_err == NULL)  return;
9280Sstevel@tonic-gate 
9290Sstevel@tonic-gate 	kssl_err->reason = reason;
9300Sstevel@tonic-gate 	BIO_snprintf(kssl_err->text, KSSL_ERR_MAX, text);
9310Sstevel@tonic-gate 	return;
9320Sstevel@tonic-gate         }
9330Sstevel@tonic-gate 
9340Sstevel@tonic-gate 
9350Sstevel@tonic-gate /*	Display contents of krb5_data struct, for debugging
9360Sstevel@tonic-gate */
9370Sstevel@tonic-gate void
print_krb5_data(char * label,krb5_data * kdata)9380Sstevel@tonic-gate print_krb5_data(char *label, krb5_data *kdata)
9390Sstevel@tonic-gate         {
9400Sstevel@tonic-gate 	int i;
9410Sstevel@tonic-gate 
9420Sstevel@tonic-gate 	printf("%s[%d] ", label, kdata->length);
943*2139Sjp161948 	for (i=0; i < (int)kdata->length; i++)
9440Sstevel@tonic-gate                 {
9450Sstevel@tonic-gate 		if (0 &&  isprint((int) kdata->data[i]))
9460Sstevel@tonic-gate                         printf(	"%c ",  kdata->data[i]);
9470Sstevel@tonic-gate 		else
9480Sstevel@tonic-gate                         printf(	"%02x ", (unsigned char) kdata->data[i]);
9490Sstevel@tonic-gate 		}
9500Sstevel@tonic-gate 	printf("\n");
9510Sstevel@tonic-gate         }
9520Sstevel@tonic-gate 
9530Sstevel@tonic-gate 
9540Sstevel@tonic-gate /*	Display contents of krb5_authdata struct, for debugging
9550Sstevel@tonic-gate */
9560Sstevel@tonic-gate void
print_krb5_authdata(char * label,krb5_authdata ** adata)9570Sstevel@tonic-gate print_krb5_authdata(char *label, krb5_authdata **adata)
9580Sstevel@tonic-gate         {
9590Sstevel@tonic-gate 	if (adata == NULL)
9600Sstevel@tonic-gate                 {
9610Sstevel@tonic-gate 		printf("%s, authdata==0\n", label);
9620Sstevel@tonic-gate 		return;
9630Sstevel@tonic-gate 		}
9640Sstevel@tonic-gate 	printf("%s [%p]\n", label, (void *)adata);
9650Sstevel@tonic-gate #if 0
9660Sstevel@tonic-gate 	{
9670Sstevel@tonic-gate         int 	i;
9680Sstevel@tonic-gate 	printf("%s[at%d:%d] ", label, adata->ad_type, adata->length);
9690Sstevel@tonic-gate 	for (i=0; i < adata->length; i++)
9700Sstevel@tonic-gate                 {
9710Sstevel@tonic-gate                 printf((isprint(adata->contents[i]))? "%c ": "%02x",
9720Sstevel@tonic-gate                         adata->contents[i]);
9730Sstevel@tonic-gate 		}
9740Sstevel@tonic-gate 	printf("\n");
9750Sstevel@tonic-gate 	}
9760Sstevel@tonic-gate #endif
9770Sstevel@tonic-gate 	}
9780Sstevel@tonic-gate 
9790Sstevel@tonic-gate 
9800Sstevel@tonic-gate /*	Display contents of krb5_keyblock struct, for debugging
9810Sstevel@tonic-gate */
9820Sstevel@tonic-gate void
print_krb5_keyblock(char * label,krb5_keyblock * keyblk)9830Sstevel@tonic-gate print_krb5_keyblock(char *label, krb5_keyblock *keyblk)
9840Sstevel@tonic-gate         {
9850Sstevel@tonic-gate 	int i;
9860Sstevel@tonic-gate 
9870Sstevel@tonic-gate 	if (keyblk == NULL)
9880Sstevel@tonic-gate                 {
9890Sstevel@tonic-gate 		printf("%s, keyblk==0\n", label);
9900Sstevel@tonic-gate 		return;
9910Sstevel@tonic-gate 		}
9920Sstevel@tonic-gate #ifdef KRB5_HEIMDAL
9930Sstevel@tonic-gate 	printf("%s\n\t[et%d:%d]: ", label, keyblk->keytype,
9940Sstevel@tonic-gate 					   keyblk->keyvalue->length);
995*2139Sjp161948 	for (i=0; i < (int)keyblk->keyvalue->length; i++)
9960Sstevel@tonic-gate                 {
9970Sstevel@tonic-gate 		printf("%02x",(unsigned char *)(keyblk->keyvalue->contents)[i]);
9980Sstevel@tonic-gate 		}
9990Sstevel@tonic-gate 	printf("\n");
10000Sstevel@tonic-gate #else
10010Sstevel@tonic-gate 	printf("%s\n\t[et%d:%d]: ", label, keyblk->enctype, keyblk->length);
1002*2139Sjp161948 	for (i=0; i < (int)keyblk->length; i++)
10030Sstevel@tonic-gate                 {
10040Sstevel@tonic-gate 		printf("%02x",keyblk->contents[i]);
10050Sstevel@tonic-gate 		}
10060Sstevel@tonic-gate 	printf("\n");
10070Sstevel@tonic-gate #endif
10080Sstevel@tonic-gate         }
10090Sstevel@tonic-gate 
10100Sstevel@tonic-gate 
10110Sstevel@tonic-gate /*	Display contents of krb5_principal_data struct, for debugging
10120Sstevel@tonic-gate **	(krb5_principal is typedef'd == krb5_principal_data *)
10130Sstevel@tonic-gate */
10140Sstevel@tonic-gate void
print_krb5_princ(char * label,krb5_principal_data * princ)10150Sstevel@tonic-gate print_krb5_princ(char *label, krb5_principal_data *princ)
10160Sstevel@tonic-gate         {
10170Sstevel@tonic-gate 	int i, ui, uj;
10180Sstevel@tonic-gate 
10190Sstevel@tonic-gate 	printf("%s principal Realm: ", label);
10200Sstevel@tonic-gate 	if (princ == NULL)  return;
1021*2139Sjp161948 	for (ui=0; ui < (int)princ->realm.length; ui++)  putchar(princ->realm.data[ui]);
10220Sstevel@tonic-gate 	printf(" (nametype %d) has %d strings:\n", princ->type,princ->length);
1023*2139Sjp161948 	for (i=0; i < (int)princ->length; i++)
10240Sstevel@tonic-gate                 {
10250Sstevel@tonic-gate 		printf("\t%d [%d]: ", i, princ->data[i].length);
1026*2139Sjp161948 		for (uj=0; uj < (int)princ->data[i].length; uj++)  {
10270Sstevel@tonic-gate 			putchar(princ->data[i].data[uj]);
10280Sstevel@tonic-gate 			}
10290Sstevel@tonic-gate 		printf("\n");
10300Sstevel@tonic-gate 		}
10310Sstevel@tonic-gate 	return;
10320Sstevel@tonic-gate         }
10330Sstevel@tonic-gate 
10340Sstevel@tonic-gate 
10350Sstevel@tonic-gate /*	Given krb5 service (typically "kssl") and hostname in kssl_ctx,
10360Sstevel@tonic-gate **	Return encrypted Kerberos ticket for service @ hostname.
10370Sstevel@tonic-gate **	If authenp is non-NULL, also return encrypted authenticator,
10380Sstevel@tonic-gate **	whose data should be freed by caller.
10390Sstevel@tonic-gate **	(Originally was: Create Kerberos AP_REQ message for SSL Client.)
10400Sstevel@tonic-gate **
10410Sstevel@tonic-gate **	19990628	VRS 	Started; Returns Kerberos AP_REQ message.
10420Sstevel@tonic-gate **	20010409	VRS 	Modified for RFC2712; Returns enc tkt.
10430Sstevel@tonic-gate **	20010606	VRS 	May also return optional authenticator.
10440Sstevel@tonic-gate */
10450Sstevel@tonic-gate krb5_error_code
kssl_cget_tkt(KSSL_CTX * kssl_ctx,krb5_data ** enc_ticketp,krb5_data * authenp,KSSL_ERR * kssl_err)10460Sstevel@tonic-gate kssl_cget_tkt(	/* UPDATE */	KSSL_CTX *kssl_ctx,
10470Sstevel@tonic-gate                 /* OUT    */	krb5_data **enc_ticketp,
10480Sstevel@tonic-gate                 /* UPDATE */	krb5_data *authenp,
10490Sstevel@tonic-gate                 /* OUT    */	KSSL_ERR *kssl_err)
10500Sstevel@tonic-gate 	{
10510Sstevel@tonic-gate 	krb5_error_code		krb5rc = KRB5KRB_ERR_GENERIC;
10520Sstevel@tonic-gate 	krb5_context		krb5context = NULL;
10530Sstevel@tonic-gate 	krb5_auth_context	krb5auth_context = NULL;
10540Sstevel@tonic-gate 	krb5_ccache 		krb5ccdef = NULL;
10550Sstevel@tonic-gate 	krb5_creds		krb5creds, *krb5credsp = NULL;
10560Sstevel@tonic-gate 	krb5_data		krb5_app_req;
10570Sstevel@tonic-gate 
10580Sstevel@tonic-gate 	kssl_err_set(kssl_err, 0, "");
10590Sstevel@tonic-gate 	memset((char *)&krb5creds, 0, sizeof(krb5creds));
10600Sstevel@tonic-gate 
10610Sstevel@tonic-gate 	if (!kssl_ctx)
10620Sstevel@tonic-gate                 {
10630Sstevel@tonic-gate 		kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
10640Sstevel@tonic-gate                         "No kssl_ctx defined.\n");
10650Sstevel@tonic-gate 		goto err;
10660Sstevel@tonic-gate 		}
10670Sstevel@tonic-gate 	else if (!kssl_ctx->service_host)
10680Sstevel@tonic-gate                 {
10690Sstevel@tonic-gate 		kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
10700Sstevel@tonic-gate                         "kssl_ctx service_host undefined.\n");
10710Sstevel@tonic-gate 		goto err;
10720Sstevel@tonic-gate 		}
10730Sstevel@tonic-gate 
10740Sstevel@tonic-gate 	if ((krb5rc = krb5_init_context(&krb5context)) != 0)
10750Sstevel@tonic-gate                 {
10760Sstevel@tonic-gate 		BIO_snprintf(kssl_err->text,KSSL_ERR_MAX,
10770Sstevel@tonic-gate                         "krb5_init_context() fails: %d\n", krb5rc);
10780Sstevel@tonic-gate 		kssl_err->reason = SSL_R_KRB5_C_INIT;
10790Sstevel@tonic-gate 		goto err;
10800Sstevel@tonic-gate 		}
10810Sstevel@tonic-gate 
10820Sstevel@tonic-gate 	if ((krb5rc = krb5_sname_to_principal(krb5context,
10830Sstevel@tonic-gate                 kssl_ctx->service_host,
10840Sstevel@tonic-gate                 (kssl_ctx->service_name)? kssl_ctx->service_name: KRB5SVC,
10850Sstevel@tonic-gate                 KRB5_NT_SRV_HST, &krb5creds.server)) != 0)
10860Sstevel@tonic-gate                 {
10870Sstevel@tonic-gate 		BIO_snprintf(kssl_err->text,KSSL_ERR_MAX,
10880Sstevel@tonic-gate                         "krb5_sname_to_principal() fails for %s/%s\n",
10890Sstevel@tonic-gate                         kssl_ctx->service_host,
10900Sstevel@tonic-gate                         (kssl_ctx->service_name)? kssl_ctx->service_name:
10910Sstevel@tonic-gate 						  KRB5SVC);
10920Sstevel@tonic-gate 		kssl_err->reason = SSL_R_KRB5_C_INIT;
10930Sstevel@tonic-gate 		goto err;
10940Sstevel@tonic-gate 		}
10950Sstevel@tonic-gate 
10960Sstevel@tonic-gate 	if ((krb5rc = krb5_cc_default(krb5context, &krb5ccdef)) != 0)
10970Sstevel@tonic-gate                 {
10980Sstevel@tonic-gate 		kssl_err_set(kssl_err, SSL_R_KRB5_C_CC_PRINC,
10990Sstevel@tonic-gate                         "krb5_cc_default fails.\n");
11000Sstevel@tonic-gate 		goto err;
11010Sstevel@tonic-gate 		}
11020Sstevel@tonic-gate 
11030Sstevel@tonic-gate 	if ((krb5rc = krb5_cc_get_principal(krb5context, krb5ccdef,
11040Sstevel@tonic-gate                 &krb5creds.client)) != 0)
11050Sstevel@tonic-gate                 {
11060Sstevel@tonic-gate 		kssl_err_set(kssl_err, SSL_R_KRB5_C_CC_PRINC,
11070Sstevel@tonic-gate                         "krb5_cc_get_principal() fails.\n");
11080Sstevel@tonic-gate 		goto err;
11090Sstevel@tonic-gate 		}
11100Sstevel@tonic-gate 
11110Sstevel@tonic-gate 	if ((krb5rc = krb5_get_credentials(krb5context, 0, krb5ccdef,
11120Sstevel@tonic-gate                 &krb5creds, &krb5credsp)) != 0)
11130Sstevel@tonic-gate                 {
11140Sstevel@tonic-gate 		kssl_err_set(kssl_err, SSL_R_KRB5_C_GET_CRED,
11150Sstevel@tonic-gate                         "krb5_get_credentials() fails.\n");
11160Sstevel@tonic-gate 		goto err;
11170Sstevel@tonic-gate 		}
11180Sstevel@tonic-gate 
11190Sstevel@tonic-gate 	*enc_ticketp = &krb5credsp->ticket;
11200Sstevel@tonic-gate #ifdef KRB5_HEIMDAL
11210Sstevel@tonic-gate 	kssl_ctx->enctype = krb5credsp->session.keytype;
11220Sstevel@tonic-gate #else
11230Sstevel@tonic-gate 	kssl_ctx->enctype = krb5credsp->keyblock.enctype;
11240Sstevel@tonic-gate #endif
11250Sstevel@tonic-gate 
11260Sstevel@tonic-gate 	krb5rc = KRB5KRB_ERR_GENERIC;
11270Sstevel@tonic-gate 	/*	caller should free data of krb5_app_req  */
11280Sstevel@tonic-gate 	/*  20010406 VRS deleted for real KerberosWrapper
11290Sstevel@tonic-gate 	**  20010605 VRS reinstated to offer Authenticator to KerberosWrapper
11300Sstevel@tonic-gate 	*/
11310Sstevel@tonic-gate 	krb5_app_req.length = 0;
11320Sstevel@tonic-gate 	if (authenp)
11330Sstevel@tonic-gate                 {
11340Sstevel@tonic-gate 		krb5_data	krb5in_data;
1135*2139Sjp161948 		const unsigned char	*p;
11360Sstevel@tonic-gate 		long		arlen;
11370Sstevel@tonic-gate 		KRB5_APREQBODY	*ap_req;
11380Sstevel@tonic-gate 
11390Sstevel@tonic-gate 		authenp->length = 0;
11400Sstevel@tonic-gate 		krb5in_data.data = NULL;
11410Sstevel@tonic-gate 		krb5in_data.length = 0;
11420Sstevel@tonic-gate 		if ((krb5rc = krb5_mk_req_extended(krb5context,
11430Sstevel@tonic-gate 			&krb5auth_context, 0, &krb5in_data, krb5credsp,
11440Sstevel@tonic-gate 			&krb5_app_req)) != 0)
11450Sstevel@tonic-gate 			{
11460Sstevel@tonic-gate 			kssl_err_set(kssl_err, SSL_R_KRB5_C_MK_REQ,
11470Sstevel@tonic-gate 				"krb5_mk_req_extended() fails.\n");
11480Sstevel@tonic-gate 			goto err;
11490Sstevel@tonic-gate 			}
11500Sstevel@tonic-gate 
11510Sstevel@tonic-gate 		arlen = krb5_app_req.length;
11520Sstevel@tonic-gate 		p = (unsigned char *)krb5_app_req.data;
11530Sstevel@tonic-gate 		ap_req = (KRB5_APREQBODY *) d2i_KRB5_APREQ(NULL, &p, arlen);
11540Sstevel@tonic-gate 		if (ap_req)
11550Sstevel@tonic-gate 			{
11560Sstevel@tonic-gate 			authenp->length = i2d_KRB5_ENCDATA(
11570Sstevel@tonic-gate 					ap_req->authenticator, NULL);
11580Sstevel@tonic-gate 			if (authenp->length  &&
11590Sstevel@tonic-gate 				(authenp->data = malloc(authenp->length)))
11600Sstevel@tonic-gate 				{
11610Sstevel@tonic-gate 				unsigned char	*adp = (unsigned char *)authenp->data;
11620Sstevel@tonic-gate 				authenp->length = i2d_KRB5_ENCDATA(
11630Sstevel@tonic-gate 						ap_req->authenticator, &adp);
11640Sstevel@tonic-gate 				}
11650Sstevel@tonic-gate 			}
11660Sstevel@tonic-gate 
11670Sstevel@tonic-gate 		if (ap_req)  KRB5_APREQ_free((KRB5_APREQ *) ap_req);
11680Sstevel@tonic-gate 		if (krb5_app_req.length)
11690Sstevel@tonic-gate                         kssl_krb5_free_data_contents(krb5context,&krb5_app_req);
11700Sstevel@tonic-gate 		}
11710Sstevel@tonic-gate #ifdef KRB5_HEIMDAL
11720Sstevel@tonic-gate 	if (kssl_ctx_setkey(kssl_ctx, &krb5credsp->session))
11730Sstevel@tonic-gate                 {
11740Sstevel@tonic-gate 		kssl_err_set(kssl_err, SSL_R_KRB5_C_INIT,
11750Sstevel@tonic-gate                         "kssl_ctx_setkey() fails.\n");
11760Sstevel@tonic-gate 		}
11770Sstevel@tonic-gate #else
11780Sstevel@tonic-gate 	if (kssl_ctx_setkey(kssl_ctx, &krb5credsp->keyblock))
11790Sstevel@tonic-gate                 {
11800Sstevel@tonic-gate 		kssl_err_set(kssl_err, SSL_R_KRB5_C_INIT,
11810Sstevel@tonic-gate                         "kssl_ctx_setkey() fails.\n");
11820Sstevel@tonic-gate 		}
11830Sstevel@tonic-gate #endif
11840Sstevel@tonic-gate 	else	krb5rc = 0;
11850Sstevel@tonic-gate 
11860Sstevel@tonic-gate  err:
11870Sstevel@tonic-gate #ifdef KSSL_DEBUG
11880Sstevel@tonic-gate 	kssl_ctx_show(kssl_ctx);
11890Sstevel@tonic-gate #endif	/* KSSL_DEBUG */
11900Sstevel@tonic-gate 
11910Sstevel@tonic-gate 	if (krb5creds.client)	krb5_free_principal(krb5context,
11920Sstevel@tonic-gate 							krb5creds.client);
11930Sstevel@tonic-gate 	if (krb5creds.server)	krb5_free_principal(krb5context,
11940Sstevel@tonic-gate 							krb5creds.server);
11950Sstevel@tonic-gate 	if (krb5auth_context)	krb5_auth_con_free(krb5context,
11960Sstevel@tonic-gate 							krb5auth_context);
11970Sstevel@tonic-gate 	if (krb5context)	krb5_free_context(krb5context);
11980Sstevel@tonic-gate 	return (krb5rc);
11990Sstevel@tonic-gate 	}
12000Sstevel@tonic-gate 
12010Sstevel@tonic-gate 
12020Sstevel@tonic-gate /*  Given d2i_-decoded asn1ticket, allocate and return a new krb5_ticket.
12030Sstevel@tonic-gate **  Return Kerberos error code and kssl_err struct on error.
12040Sstevel@tonic-gate **  Allocates krb5_ticket and krb5_principal; caller should free these.
12050Sstevel@tonic-gate **
12060Sstevel@tonic-gate **	20010410	VRS	Implemented krb5_decode_ticket() as
12070Sstevel@tonic-gate **				old_krb5_decode_ticket(). Missing from MIT1.0.6.
12080Sstevel@tonic-gate **	20010615	VRS 	Re-cast as openssl/asn1 d2i_*() functions.
12090Sstevel@tonic-gate **				Re-used some of the old krb5_decode_ticket()
12100Sstevel@tonic-gate **				code here.  This tkt should alloc/free just
12110Sstevel@tonic-gate **				like the real thing.
12120Sstevel@tonic-gate */
12130Sstevel@tonic-gate krb5_error_code
kssl_TKT2tkt(krb5_context krb5context,KRB5_TKTBODY * asn1ticket,krb5_ticket ** krb5ticket,KSSL_ERR * kssl_err)12140Sstevel@tonic-gate kssl_TKT2tkt(	/* IN     */	krb5_context	krb5context,
12150Sstevel@tonic-gate 		/* IN     */	KRB5_TKTBODY	*asn1ticket,
12160Sstevel@tonic-gate 		/* OUT    */	krb5_ticket	**krb5ticket,
12170Sstevel@tonic-gate 		/* OUT    */	KSSL_ERR *kssl_err  )
12180Sstevel@tonic-gate         {
12190Sstevel@tonic-gate         krb5_error_code			krb5rc = KRB5KRB_ERR_GENERIC;
12200Sstevel@tonic-gate 	krb5_ticket 			*new5ticket = NULL;
12210Sstevel@tonic-gate 	ASN1_GENERALSTRING		*gstr_svc, *gstr_host;
12220Sstevel@tonic-gate 
12230Sstevel@tonic-gate 	*krb5ticket = NULL;
12240Sstevel@tonic-gate 
12250Sstevel@tonic-gate 	if (asn1ticket == NULL  ||  asn1ticket->realm == NULL  ||
12260Sstevel@tonic-gate 		asn1ticket->sname == NULL  ||
12270Sstevel@tonic-gate 		sk_ASN1_GENERALSTRING_num(asn1ticket->sname->namestring) < 2)
12280Sstevel@tonic-gate 		{
12290Sstevel@tonic-gate 		BIO_snprintf(kssl_err->text, KSSL_ERR_MAX,
12300Sstevel@tonic-gate 			"Null field in asn1ticket.\n");
12310Sstevel@tonic-gate 		kssl_err->reason = SSL_R_KRB5_S_RD_REQ;
12320Sstevel@tonic-gate 		return KRB5KRB_ERR_GENERIC;
12330Sstevel@tonic-gate 		}
12340Sstevel@tonic-gate 
12350Sstevel@tonic-gate 	if ((new5ticket = (krb5_ticket *) calloc(1, sizeof(krb5_ticket)))==NULL)
12360Sstevel@tonic-gate 		{
12370Sstevel@tonic-gate 		BIO_snprintf(kssl_err->text, KSSL_ERR_MAX,
12380Sstevel@tonic-gate 			"Unable to allocate new krb5_ticket.\n");
12390Sstevel@tonic-gate 		kssl_err->reason = SSL_R_KRB5_S_RD_REQ;
12400Sstevel@tonic-gate 		return ENOMEM;		/*  or  KRB5KRB_ERR_GENERIC;	*/
12410Sstevel@tonic-gate 		}
12420Sstevel@tonic-gate 
12430Sstevel@tonic-gate 	gstr_svc  = sk_ASN1_GENERALSTRING_value(asn1ticket->sname->namestring, 0);
12440Sstevel@tonic-gate 	gstr_host = sk_ASN1_GENERALSTRING_value(asn1ticket->sname->namestring, 1);
12450Sstevel@tonic-gate 
12460Sstevel@tonic-gate 	if ((krb5rc = kssl_build_principal_2(krb5context,
12470Sstevel@tonic-gate 			&new5ticket->server,
12480Sstevel@tonic-gate 			asn1ticket->realm->length, (char *)asn1ticket->realm->data,
12490Sstevel@tonic-gate 			gstr_svc->length,  (char *)gstr_svc->data,
12500Sstevel@tonic-gate 			gstr_host->length, (char *)gstr_host->data)) != 0)
12510Sstevel@tonic-gate 		{
12520Sstevel@tonic-gate 		free(new5ticket);
12530Sstevel@tonic-gate 		BIO_snprintf(kssl_err->text, KSSL_ERR_MAX,
12540Sstevel@tonic-gate 			"Error building ticket server principal.\n");
12550Sstevel@tonic-gate 		kssl_err->reason = SSL_R_KRB5_S_RD_REQ;
12560Sstevel@tonic-gate 		return krb5rc;		/*  or  KRB5KRB_ERR_GENERIC;	*/
12570Sstevel@tonic-gate 		}
12580Sstevel@tonic-gate 
12590Sstevel@tonic-gate 	krb5_princ_type(krb5context, new5ticket->server) =
12600Sstevel@tonic-gate 			asn1ticket->sname->nametype->data[0];
12610Sstevel@tonic-gate 	new5ticket->enc_part.enctype = asn1ticket->encdata->etype->data[0];
12620Sstevel@tonic-gate 	new5ticket->enc_part.kvno = asn1ticket->encdata->kvno->data[0];
12630Sstevel@tonic-gate 	new5ticket->enc_part.ciphertext.length =
12640Sstevel@tonic-gate 			asn1ticket->encdata->cipher->length;
12650Sstevel@tonic-gate 	if ((new5ticket->enc_part.ciphertext.data =
12660Sstevel@tonic-gate 		calloc(1, asn1ticket->encdata->cipher->length)) == NULL)
12670Sstevel@tonic-gate 		{
12680Sstevel@tonic-gate 		free(new5ticket);
12690Sstevel@tonic-gate 		BIO_snprintf(kssl_err->text, KSSL_ERR_MAX,
12700Sstevel@tonic-gate 			"Error allocating cipher in krb5ticket.\n");
12710Sstevel@tonic-gate 		kssl_err->reason = SSL_R_KRB5_S_RD_REQ;
12720Sstevel@tonic-gate 		return KRB5KRB_ERR_GENERIC;
12730Sstevel@tonic-gate 		}
12740Sstevel@tonic-gate 	else
12750Sstevel@tonic-gate 		{
12760Sstevel@tonic-gate 		memcpy(new5ticket->enc_part.ciphertext.data,
12770Sstevel@tonic-gate 			asn1ticket->encdata->cipher->data,
12780Sstevel@tonic-gate 			asn1ticket->encdata->cipher->length);
12790Sstevel@tonic-gate 		}
12800Sstevel@tonic-gate 
12810Sstevel@tonic-gate 	*krb5ticket = new5ticket;
12820Sstevel@tonic-gate 	return 0;
12830Sstevel@tonic-gate 	}
12840Sstevel@tonic-gate 
12850Sstevel@tonic-gate 
12860Sstevel@tonic-gate /*	Given krb5 service name in KSSL_CTX *kssl_ctx (typically "kssl"),
12870Sstevel@tonic-gate **		and krb5 AP_REQ message & message length,
12880Sstevel@tonic-gate **	Return Kerberos session key and client principle
12890Sstevel@tonic-gate **		to SSL Server in KSSL_CTX *kssl_ctx.
12900Sstevel@tonic-gate **
12910Sstevel@tonic-gate **	19990702	VRS 	Started.
12920Sstevel@tonic-gate */
12930Sstevel@tonic-gate krb5_error_code
kssl_sget_tkt(KSSL_CTX * kssl_ctx,krb5_data * indata,krb5_ticket_times * ttimes,KSSL_ERR * kssl_err)12940Sstevel@tonic-gate kssl_sget_tkt(	/* UPDATE */	KSSL_CTX		*kssl_ctx,
12950Sstevel@tonic-gate 		/* IN     */	krb5_data		*indata,
12960Sstevel@tonic-gate 		/* OUT    */	krb5_ticket_times	*ttimes,
12970Sstevel@tonic-gate 		/* OUT    */	KSSL_ERR		*kssl_err  )
12980Sstevel@tonic-gate         {
12990Sstevel@tonic-gate         krb5_error_code			krb5rc = KRB5KRB_ERR_GENERIC;
13000Sstevel@tonic-gate         static krb5_context		krb5context = NULL;
13010Sstevel@tonic-gate 	static krb5_auth_context	krb5auth_context = NULL;
13020Sstevel@tonic-gate 	krb5_ticket 			*krb5ticket = NULL;
13030Sstevel@tonic-gate 	KRB5_TKTBODY 			*asn1ticket = NULL;
1304*2139Sjp161948 	const unsigned char		*p;
13050Sstevel@tonic-gate 	krb5_keytab 			krb5keytab = NULL;
13060Sstevel@tonic-gate 	krb5_keytab_entry		kt_entry;
13070Sstevel@tonic-gate 	krb5_principal			krb5server;
13080Sstevel@tonic-gate         krb5_rcache                     rcache = NULL;
13090Sstevel@tonic-gate 
13100Sstevel@tonic-gate 	kssl_err_set(kssl_err, 0, "");
13110Sstevel@tonic-gate 
13120Sstevel@tonic-gate 	if (!kssl_ctx)
13130Sstevel@tonic-gate                 {
13140Sstevel@tonic-gate 		kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
13150Sstevel@tonic-gate 			"No kssl_ctx defined.\n");
13160Sstevel@tonic-gate 		goto err;
13170Sstevel@tonic-gate 		}
13180Sstevel@tonic-gate 
13190Sstevel@tonic-gate #ifdef KSSL_DEBUG
13200Sstevel@tonic-gate 	printf("in kssl_sget_tkt(%s)\n", kstring(kssl_ctx->service_name));
13210Sstevel@tonic-gate #endif	/* KSSL_DEBUG */
13220Sstevel@tonic-gate 
13230Sstevel@tonic-gate 	if (!krb5context  &&  (krb5rc = krb5_init_context(&krb5context)))
13240Sstevel@tonic-gate                 {
13250Sstevel@tonic-gate 		kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
13260Sstevel@tonic-gate                         "krb5_init_context() fails.\n");
13270Sstevel@tonic-gate 		goto err;
13280Sstevel@tonic-gate 		}
13290Sstevel@tonic-gate 	if (krb5auth_context  &&
13300Sstevel@tonic-gate 		(krb5rc = krb5_auth_con_free(krb5context, krb5auth_context)))
13310Sstevel@tonic-gate                 {
13320Sstevel@tonic-gate 		kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
13330Sstevel@tonic-gate                         "krb5_auth_con_free() fails.\n");
13340Sstevel@tonic-gate 		goto err;
13350Sstevel@tonic-gate 		}
13360Sstevel@tonic-gate 	else  krb5auth_context = NULL;
13370Sstevel@tonic-gate 	if (!krb5auth_context  &&
13380Sstevel@tonic-gate 		(krb5rc = krb5_auth_con_init(krb5context, &krb5auth_context)))
13390Sstevel@tonic-gate                 {
13400Sstevel@tonic-gate 		kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
13410Sstevel@tonic-gate                         "krb5_auth_con_init() fails.\n");
13420Sstevel@tonic-gate 		goto err;
13430Sstevel@tonic-gate 		}
13440Sstevel@tonic-gate 
13450Sstevel@tonic-gate 
13460Sstevel@tonic-gate 	if ((krb5rc = krb5_auth_con_getrcache(krb5context, krb5auth_context,
13470Sstevel@tonic-gate 		&rcache)))
13480Sstevel@tonic-gate 		{
13490Sstevel@tonic-gate  		kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
13500Sstevel@tonic-gate 			"krb5_auth_con_getrcache() fails.\n");
13510Sstevel@tonic-gate  		goto err;
13520Sstevel@tonic-gate 		}
13530Sstevel@tonic-gate 
13540Sstevel@tonic-gate 	if ((krb5rc = krb5_sname_to_principal(krb5context, NULL,
13550Sstevel@tonic-gate                 (kssl_ctx->service_name)? kssl_ctx->service_name: KRB5SVC,
13560Sstevel@tonic-gate                 KRB5_NT_SRV_HST, &krb5server)) != 0)
13570Sstevel@tonic-gate                 {
13580Sstevel@tonic-gate 		kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
13590Sstevel@tonic-gate                         "krb5_sname_to_principal() fails.\n");
13600Sstevel@tonic-gate 		goto err;
13610Sstevel@tonic-gate 		}
13620Sstevel@tonic-gate 
13630Sstevel@tonic-gate 	if (rcache == NULL)
13640Sstevel@tonic-gate                 {
13650Sstevel@tonic-gate                 if ((krb5rc = krb5_get_server_rcache(krb5context,
13660Sstevel@tonic-gate 			krb5_princ_component(krb5context, krb5server, 0),
13670Sstevel@tonic-gate 			&rcache)))
13680Sstevel@tonic-gate                         {
13690Sstevel@tonic-gate 		        kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
13700Sstevel@tonic-gate                                 "krb5_get_server_rcache() fails.\n");
13710Sstevel@tonic-gate                   	goto err;
13720Sstevel@tonic-gate                         }
13730Sstevel@tonic-gate                 }
13740Sstevel@tonic-gate 
13750Sstevel@tonic-gate         if ((krb5rc = krb5_auth_con_setrcache(krb5context, krb5auth_context, rcache)))
13760Sstevel@tonic-gate                 {
13770Sstevel@tonic-gate                 kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
13780Sstevel@tonic-gate 			"krb5_auth_con_setrcache() fails.\n");
13790Sstevel@tonic-gate                 goto err;
13800Sstevel@tonic-gate                 }
13810Sstevel@tonic-gate 
13820Sstevel@tonic-gate 
13830Sstevel@tonic-gate 	/*	kssl_ctx->keytab_file == NULL ==> use Kerberos default
13840Sstevel@tonic-gate 	*/
13850Sstevel@tonic-gate 	if (kssl_ctx->keytab_file)
13860Sstevel@tonic-gate 		{
13870Sstevel@tonic-gate 		krb5rc = krb5_kt_resolve(krb5context, kssl_ctx->keytab_file,
13880Sstevel@tonic-gate                         &krb5keytab);
13890Sstevel@tonic-gate 		if (krb5rc)
13900Sstevel@tonic-gate 			{
13910Sstevel@tonic-gate 			kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
13920Sstevel@tonic-gate 				"krb5_kt_resolve() fails.\n");
13930Sstevel@tonic-gate 			goto err;
13940Sstevel@tonic-gate 			}
13950Sstevel@tonic-gate 		}
13960Sstevel@tonic-gate 	else
13970Sstevel@tonic-gate 		{
13980Sstevel@tonic-gate                 krb5rc = krb5_kt_default(krb5context,&krb5keytab);
13990Sstevel@tonic-gate                 if (krb5rc)
14000Sstevel@tonic-gate 			{
14010Sstevel@tonic-gate 			kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
14020Sstevel@tonic-gate 				"krb5_kt_default() fails.\n");
14030Sstevel@tonic-gate 			goto err;
14040Sstevel@tonic-gate 			}
14050Sstevel@tonic-gate 		}
14060Sstevel@tonic-gate 
14070Sstevel@tonic-gate 	/*	Actual Kerberos5 krb5_recvauth() has initial conversation here
14080Sstevel@tonic-gate 	**	o	check KRB5_SENDAUTH_BADAUTHVERS
14090Sstevel@tonic-gate 	**		unless KRB5_RECVAUTH_SKIP_VERSION
14100Sstevel@tonic-gate 	**	o	check KRB5_SENDAUTH_BADAPPLVERS
14110Sstevel@tonic-gate 	**	o	send "0" msg if all OK
14120Sstevel@tonic-gate 	*/
14130Sstevel@tonic-gate 
14140Sstevel@tonic-gate 	/*  20010411 was using AP_REQ instead of true KerberosWrapper
14150Sstevel@tonic-gate 	**
14160Sstevel@tonic-gate 	**  if ((krb5rc = krb5_rd_req(krb5context, &krb5auth_context,
14170Sstevel@tonic-gate 	**			&krb5in_data, krb5server, krb5keytab,
14180Sstevel@tonic-gate 	**			&ap_option, &krb5ticket)) != 0)  { Error }
14190Sstevel@tonic-gate 	*/
14200Sstevel@tonic-gate 
14210Sstevel@tonic-gate 	p = (unsigned char *)indata->data;
14220Sstevel@tonic-gate 	if ((asn1ticket = (KRB5_TKTBODY *) d2i_KRB5_TICKET(NULL, &p,
14230Sstevel@tonic-gate 						(long) indata->length)) == NULL)
14240Sstevel@tonic-gate 		{
14250Sstevel@tonic-gate 		BIO_snprintf(kssl_err->text, KSSL_ERR_MAX,
14260Sstevel@tonic-gate 			"d2i_KRB5_TICKET() ASN.1 decode failure.\n");
14270Sstevel@tonic-gate 		kssl_err->reason = SSL_R_KRB5_S_RD_REQ;
14280Sstevel@tonic-gate 		goto err;
14290Sstevel@tonic-gate 		}
14300Sstevel@tonic-gate 
14310Sstevel@tonic-gate 	/* Was:  krb5rc = krb5_decode_ticket(krb5in_data,&krb5ticket)) != 0) */
14320Sstevel@tonic-gate 	if ((krb5rc = kssl_TKT2tkt(krb5context, asn1ticket, &krb5ticket,
14330Sstevel@tonic-gate 					kssl_err)) != 0)
14340Sstevel@tonic-gate 		{
14350Sstevel@tonic-gate 		BIO_snprintf(kssl_err->text, KSSL_ERR_MAX,
14360Sstevel@tonic-gate 			"Error converting ASN.1 ticket to krb5_ticket.\n");
14370Sstevel@tonic-gate 		kssl_err->reason = SSL_R_KRB5_S_RD_REQ;
14380Sstevel@tonic-gate 		goto err;
14390Sstevel@tonic-gate 		}
14400Sstevel@tonic-gate 
14410Sstevel@tonic-gate 	if (! krb5_principal_compare(krb5context, krb5server,
14420Sstevel@tonic-gate 						  krb5ticket->server))  {
14430Sstevel@tonic-gate 		krb5rc = KRB5_PRINC_NOMATCH;
14440Sstevel@tonic-gate 		BIO_snprintf(kssl_err->text, KSSL_ERR_MAX,
14450Sstevel@tonic-gate 			"server principal != ticket principal\n");
14460Sstevel@tonic-gate 		kssl_err->reason = SSL_R_KRB5_S_RD_REQ;
14470Sstevel@tonic-gate 		goto err;
14480Sstevel@tonic-gate 		}
14490Sstevel@tonic-gate 	if ((krb5rc = krb5_kt_get_entry(krb5context, krb5keytab,
14500Sstevel@tonic-gate 			krb5ticket->server, krb5ticket->enc_part.kvno,
14510Sstevel@tonic-gate 			krb5ticket->enc_part.enctype, &kt_entry)) != 0)  {
14520Sstevel@tonic-gate 		BIO_snprintf(kssl_err->text, KSSL_ERR_MAX,
14530Sstevel@tonic-gate 			"krb5_kt_get_entry() fails with %x.\n", krb5rc);
14540Sstevel@tonic-gate 		kssl_err->reason = SSL_R_KRB5_S_RD_REQ;
14550Sstevel@tonic-gate 		goto err;
14560Sstevel@tonic-gate 		}
14570Sstevel@tonic-gate 	if ((krb5rc = krb5_decrypt_tkt_part(krb5context, &kt_entry.key,
14580Sstevel@tonic-gate 			krb5ticket)) != 0)  {
14590Sstevel@tonic-gate 		BIO_snprintf(kssl_err->text, KSSL_ERR_MAX,
14600Sstevel@tonic-gate 			"krb5_decrypt_tkt_part() failed.\n");
14610Sstevel@tonic-gate 		kssl_err->reason = SSL_R_KRB5_S_RD_REQ;
14620Sstevel@tonic-gate 		goto err;
14630Sstevel@tonic-gate 		}
14640Sstevel@tonic-gate 	else  {
14650Sstevel@tonic-gate 		krb5_kt_free_entry(krb5context, &kt_entry);
14660Sstevel@tonic-gate #ifdef KSSL_DEBUG
14670Sstevel@tonic-gate 		{
14680Sstevel@tonic-gate 		int i; krb5_address **paddr = krb5ticket->enc_part2->caddrs;
14690Sstevel@tonic-gate 		printf("Decrypted ticket fields:\n");
14700Sstevel@tonic-gate 		printf("\tflags: %X, transit-type: %X",
14710Sstevel@tonic-gate 			krb5ticket->enc_part2->flags,
14720Sstevel@tonic-gate 			krb5ticket->enc_part2->transited.tr_type);
14730Sstevel@tonic-gate 		print_krb5_data("\ttransit-data: ",
14740Sstevel@tonic-gate 			&(krb5ticket->enc_part2->transited.tr_contents));
14750Sstevel@tonic-gate 		printf("\tcaddrs: %p, authdata: %p\n",
14760Sstevel@tonic-gate 			krb5ticket->enc_part2->caddrs,
14770Sstevel@tonic-gate 			krb5ticket->enc_part2->authorization_data);
14780Sstevel@tonic-gate 		if (paddr)
14790Sstevel@tonic-gate 			{
14800Sstevel@tonic-gate 			printf("\tcaddrs:\n");
14810Sstevel@tonic-gate 			for (i=0; paddr[i] != NULL; i++)
14820Sstevel@tonic-gate 				{
14830Sstevel@tonic-gate 				krb5_data d;
14840Sstevel@tonic-gate 				d.length=paddr[i]->length;
14850Sstevel@tonic-gate 				d.data=paddr[i]->contents;
14860Sstevel@tonic-gate 				print_krb5_data("\t\tIP: ", &d);
14870Sstevel@tonic-gate 				}
14880Sstevel@tonic-gate 			}
14890Sstevel@tonic-gate 		printf("\tstart/auth/end times: %d / %d / %d\n",
14900Sstevel@tonic-gate 			krb5ticket->enc_part2->times.starttime,
14910Sstevel@tonic-gate 			krb5ticket->enc_part2->times.authtime,
14920Sstevel@tonic-gate 			krb5ticket->enc_part2->times.endtime);
14930Sstevel@tonic-gate 		}
14940Sstevel@tonic-gate #endif	/* KSSL_DEBUG */
14950Sstevel@tonic-gate 		}
14960Sstevel@tonic-gate 
14970Sstevel@tonic-gate 	krb5rc = KRB5_NO_TKT_SUPPLIED;
14980Sstevel@tonic-gate 	if (!krb5ticket  ||	!krb5ticket->enc_part2  ||
14990Sstevel@tonic-gate                 !krb5ticket->enc_part2->client  ||
15000Sstevel@tonic-gate                 !krb5ticket->enc_part2->client->data  ||
15010Sstevel@tonic-gate                 !krb5ticket->enc_part2->session)
15020Sstevel@tonic-gate                 {
15030Sstevel@tonic-gate                 kssl_err_set(kssl_err, SSL_R_KRB5_S_BAD_TICKET,
15040Sstevel@tonic-gate                         "bad ticket from krb5_rd_req.\n");
15050Sstevel@tonic-gate 		}
15060Sstevel@tonic-gate 	else if (kssl_ctx_setprinc(kssl_ctx, KSSL_CLIENT,
15070Sstevel@tonic-gate 		 &krb5ticket->enc_part2->client->realm,
15080Sstevel@tonic-gate 		 krb5ticket->enc_part2->client->data,
15090Sstevel@tonic-gate 		 krb5ticket->enc_part2->client->length))
15100Sstevel@tonic-gate                 {
15110Sstevel@tonic-gate 		kssl_err_set(kssl_err, SSL_R_KRB5_S_BAD_TICKET,
15120Sstevel@tonic-gate                         "kssl_ctx_setprinc() fails.\n");
15130Sstevel@tonic-gate 		}
15140Sstevel@tonic-gate 	else if (kssl_ctx_setkey(kssl_ctx, krb5ticket->enc_part2->session))
15150Sstevel@tonic-gate                 {
15160Sstevel@tonic-gate 		kssl_err_set(kssl_err, SSL_R_KRB5_S_BAD_TICKET,
15170Sstevel@tonic-gate                         "kssl_ctx_setkey() fails.\n");
15180Sstevel@tonic-gate 		}
15190Sstevel@tonic-gate 	else if (krb5ticket->enc_part2->flags & TKT_FLG_INVALID)
15200Sstevel@tonic-gate                 {
15210Sstevel@tonic-gate 		krb5rc = KRB5KRB_AP_ERR_TKT_INVALID;
15220Sstevel@tonic-gate                 kssl_err_set(kssl_err, SSL_R_KRB5_S_BAD_TICKET,
15230Sstevel@tonic-gate                         "invalid ticket from krb5_rd_req.\n");
15240Sstevel@tonic-gate 		}
15250Sstevel@tonic-gate 	else	krb5rc = 0;
15260Sstevel@tonic-gate 
15270Sstevel@tonic-gate 	kssl_ctx->enctype	= krb5ticket->enc_part.enctype;
15280Sstevel@tonic-gate 	ttimes->authtime	= krb5ticket->enc_part2->times.authtime;
15290Sstevel@tonic-gate 	ttimes->starttime	= krb5ticket->enc_part2->times.starttime;
15300Sstevel@tonic-gate 	ttimes->endtime 	= krb5ticket->enc_part2->times.endtime;
15310Sstevel@tonic-gate 	ttimes->renew_till	= krb5ticket->enc_part2->times.renew_till;
15320Sstevel@tonic-gate 
15330Sstevel@tonic-gate  err:
15340Sstevel@tonic-gate #ifdef KSSL_DEBUG
15350Sstevel@tonic-gate 	kssl_ctx_show(kssl_ctx);
15360Sstevel@tonic-gate #endif	/* KSSL_DEBUG */
15370Sstevel@tonic-gate 
15380Sstevel@tonic-gate 	if (asn1ticket) 	KRB5_TICKET_free((KRB5_TICKET *) asn1ticket);
15390Sstevel@tonic-gate         if (krb5keytab)         krb5_kt_close(krb5context, krb5keytab);
15400Sstevel@tonic-gate 	if (krb5ticket) 	krb5_free_ticket(krb5context, krb5ticket);
15410Sstevel@tonic-gate 	if (krb5server) 	krb5_free_principal(krb5context, krb5server);
15420Sstevel@tonic-gate 	return (krb5rc);
15430Sstevel@tonic-gate         }
15440Sstevel@tonic-gate 
15450Sstevel@tonic-gate 
15460Sstevel@tonic-gate /*	Allocate & return a new kssl_ctx struct.
15470Sstevel@tonic-gate */
15480Sstevel@tonic-gate KSSL_CTX	*
kssl_ctx_new(void)15490Sstevel@tonic-gate kssl_ctx_new(void)
15500Sstevel@tonic-gate         {
15510Sstevel@tonic-gate 	return ((KSSL_CTX *) calloc(1, sizeof(KSSL_CTX)));
15520Sstevel@tonic-gate         }
15530Sstevel@tonic-gate 
15540Sstevel@tonic-gate 
15550Sstevel@tonic-gate /*	Frees a kssl_ctx struct and any allocated memory it holds.
15560Sstevel@tonic-gate **	Returns NULL.
15570Sstevel@tonic-gate */
15580Sstevel@tonic-gate KSSL_CTX	*
kssl_ctx_free(KSSL_CTX * kssl_ctx)15590Sstevel@tonic-gate kssl_ctx_free(KSSL_CTX *kssl_ctx)
15600Sstevel@tonic-gate         {
15610Sstevel@tonic-gate 	if (kssl_ctx == NULL)  return kssl_ctx;
15620Sstevel@tonic-gate 
15630Sstevel@tonic-gate 	if (kssl_ctx->key)  		OPENSSL_cleanse(kssl_ctx->key,
15640Sstevel@tonic-gate 							      kssl_ctx->length);
15650Sstevel@tonic-gate 	if (kssl_ctx->key)  		free(kssl_ctx->key);
15660Sstevel@tonic-gate 	if (kssl_ctx->client_princ) 	free(kssl_ctx->client_princ);
15670Sstevel@tonic-gate 	if (kssl_ctx->service_host) 	free(kssl_ctx->service_host);
15680Sstevel@tonic-gate 	if (kssl_ctx->service_name) 	free(kssl_ctx->service_name);
15690Sstevel@tonic-gate 	if (kssl_ctx->keytab_file) 	free(kssl_ctx->keytab_file);
15700Sstevel@tonic-gate 
15710Sstevel@tonic-gate 	free(kssl_ctx);
15720Sstevel@tonic-gate 	return (KSSL_CTX *) NULL;
15730Sstevel@tonic-gate         }
15740Sstevel@tonic-gate 
15750Sstevel@tonic-gate 
15760Sstevel@tonic-gate /*	Given an array of (krb5_data *) entity (and optional realm),
15770Sstevel@tonic-gate **	set the plain (char *) client_princ or service_host member
15780Sstevel@tonic-gate **	of the kssl_ctx struct.
15790Sstevel@tonic-gate */
15800Sstevel@tonic-gate krb5_error_code
kssl_ctx_setprinc(KSSL_CTX * kssl_ctx,int which,krb5_data * realm,krb5_data * entity,int nentities)15810Sstevel@tonic-gate kssl_ctx_setprinc(KSSL_CTX *kssl_ctx, int which,
15820Sstevel@tonic-gate         krb5_data *realm, krb5_data *entity, int nentities)
15830Sstevel@tonic-gate         {
15840Sstevel@tonic-gate 	char	**princ;
15850Sstevel@tonic-gate 	int 	length;
15860Sstevel@tonic-gate 	int i;
15870Sstevel@tonic-gate 
15880Sstevel@tonic-gate 	if (kssl_ctx == NULL  ||  entity == NULL)  return KSSL_CTX_ERR;
15890Sstevel@tonic-gate 
15900Sstevel@tonic-gate 	switch (which)
15910Sstevel@tonic-gate                 {
15920Sstevel@tonic-gate         case KSSL_CLIENT:	princ = &kssl_ctx->client_princ;	break;
15930Sstevel@tonic-gate         case KSSL_SERVER:	princ = &kssl_ctx->service_host;	break;
15940Sstevel@tonic-gate         default:		return KSSL_CTX_ERR;			break;
15950Sstevel@tonic-gate 		}
15960Sstevel@tonic-gate 	if (*princ)  free(*princ);
15970Sstevel@tonic-gate 
15980Sstevel@tonic-gate 	/* Add up all the entity->lengths */
15990Sstevel@tonic-gate 	length = 0;
16000Sstevel@tonic-gate 	for (i=0; i < nentities; i++)
16010Sstevel@tonic-gate 		{
16020Sstevel@tonic-gate 		length += entity[i].length;
16030Sstevel@tonic-gate 		}
16040Sstevel@tonic-gate 	/* Add in space for the '/' character(s) (if any) */
16050Sstevel@tonic-gate 	length += nentities-1;
16060Sstevel@tonic-gate 	/* Space for the ('@'+realm+NULL | NULL) */
16070Sstevel@tonic-gate 	length += ((realm)? realm->length + 2: 1);
16080Sstevel@tonic-gate 
16090Sstevel@tonic-gate 	if ((*princ = calloc(1, length)) == NULL)
16100Sstevel@tonic-gate 		return KSSL_CTX_ERR;
16110Sstevel@tonic-gate 	else
16120Sstevel@tonic-gate 		{
16130Sstevel@tonic-gate 		for (i = 0; i < nentities; i++)
16140Sstevel@tonic-gate 			{
16150Sstevel@tonic-gate 			strncat(*princ, entity[i].data, entity[i].length);
16160Sstevel@tonic-gate 			if (i < nentities-1)
16170Sstevel@tonic-gate 				{
16180Sstevel@tonic-gate 				strcat (*princ, "/");
16190Sstevel@tonic-gate 				}
16200Sstevel@tonic-gate 			}
16210Sstevel@tonic-gate 		if (realm)
16220Sstevel@tonic-gate                         {
16230Sstevel@tonic-gate 			strcat (*princ, "@");
16240Sstevel@tonic-gate 			(void) strncat(*princ, realm->data, realm->length);
16250Sstevel@tonic-gate 			}
16260Sstevel@tonic-gate 		}
16270Sstevel@tonic-gate 
16280Sstevel@tonic-gate 	return KSSL_CTX_OK;
16290Sstevel@tonic-gate         }
16300Sstevel@tonic-gate 
16310Sstevel@tonic-gate 
16320Sstevel@tonic-gate /*	Set one of the plain (char *) string members of the kssl_ctx struct.
16330Sstevel@tonic-gate **	Default values should be:
16340Sstevel@tonic-gate **		which == KSSL_SERVICE	=>	"khost" (KRB5SVC)
16350Sstevel@tonic-gate **		which == KSSL_KEYTAB	=>	"/etc/krb5.keytab" (KRB5KEYTAB)
16360Sstevel@tonic-gate */
16370Sstevel@tonic-gate krb5_error_code
kssl_ctx_setstring(KSSL_CTX * kssl_ctx,int which,char * text)16380Sstevel@tonic-gate kssl_ctx_setstring(KSSL_CTX *kssl_ctx, int which, char *text)
16390Sstevel@tonic-gate         {
16400Sstevel@tonic-gate 	char	**string;
16410Sstevel@tonic-gate 
16420Sstevel@tonic-gate 	if (!kssl_ctx)  return KSSL_CTX_ERR;
16430Sstevel@tonic-gate 
16440Sstevel@tonic-gate 	switch (which)
16450Sstevel@tonic-gate                 {
16460Sstevel@tonic-gate         case KSSL_SERVICE:	string = &kssl_ctx->service_name;	break;
16470Sstevel@tonic-gate         case KSSL_SERVER:	string = &kssl_ctx->service_host;	break;
16480Sstevel@tonic-gate         case KSSL_CLIENT:	string = &kssl_ctx->client_princ;	break;
16490Sstevel@tonic-gate         case KSSL_KEYTAB:	string = &kssl_ctx->keytab_file;	break;
16500Sstevel@tonic-gate         default:		return KSSL_CTX_ERR;			break;
16510Sstevel@tonic-gate 		}
16520Sstevel@tonic-gate 	if (*string)  free(*string);
16530Sstevel@tonic-gate 
16540Sstevel@tonic-gate 	if (!text)
16550Sstevel@tonic-gate                 {
16560Sstevel@tonic-gate 		*string = '\0';
16570Sstevel@tonic-gate 		return KSSL_CTX_OK;
16580Sstevel@tonic-gate 		}
16590Sstevel@tonic-gate 
16600Sstevel@tonic-gate 	if ((*string = calloc(1, strlen(text) + 1)) == NULL)
16610Sstevel@tonic-gate 		return KSSL_CTX_ERR;
16620Sstevel@tonic-gate 	else
16630Sstevel@tonic-gate 		strcpy(*string, text);
16640Sstevel@tonic-gate 
16650Sstevel@tonic-gate 	return KSSL_CTX_OK;
16660Sstevel@tonic-gate         }
16670Sstevel@tonic-gate 
16680Sstevel@tonic-gate 
16690Sstevel@tonic-gate /*	Copy the Kerberos session key from a (krb5_keyblock *) to a kssl_ctx
16700Sstevel@tonic-gate **	struct.  Clear kssl_ctx->key if Kerberos session key is NULL.
16710Sstevel@tonic-gate */
16720Sstevel@tonic-gate krb5_error_code
kssl_ctx_setkey(KSSL_CTX * kssl_ctx,krb5_keyblock * session)16730Sstevel@tonic-gate kssl_ctx_setkey(KSSL_CTX *kssl_ctx, krb5_keyblock *session)
16740Sstevel@tonic-gate         {
16750Sstevel@tonic-gate 	int 		length;
16760Sstevel@tonic-gate 	krb5_enctype	enctype;
16770Sstevel@tonic-gate 	krb5_octet FAR	*contents = NULL;
16780Sstevel@tonic-gate 
16790Sstevel@tonic-gate 	if (!kssl_ctx)  return KSSL_CTX_ERR;
16800Sstevel@tonic-gate 
16810Sstevel@tonic-gate 	if (kssl_ctx->key)
16820Sstevel@tonic-gate                 {
16830Sstevel@tonic-gate 		OPENSSL_cleanse(kssl_ctx->key, kssl_ctx->length);
16840Sstevel@tonic-gate 		free(kssl_ctx->key);
16850Sstevel@tonic-gate 		}
16860Sstevel@tonic-gate 
16870Sstevel@tonic-gate 	if (session)
16880Sstevel@tonic-gate                 {
16890Sstevel@tonic-gate 
16900Sstevel@tonic-gate #ifdef KRB5_HEIMDAL
16910Sstevel@tonic-gate 		length = session->keyvalue->length;
16920Sstevel@tonic-gate 		enctype = session->keytype;
16930Sstevel@tonic-gate 		contents = session->keyvalue->contents;
16940Sstevel@tonic-gate #else
16950Sstevel@tonic-gate 		length = session->length;
16960Sstevel@tonic-gate 		enctype = session->enctype;
16970Sstevel@tonic-gate 		contents = session->contents;
16980Sstevel@tonic-gate #endif
16990Sstevel@tonic-gate 		kssl_ctx->enctype = enctype;
17000Sstevel@tonic-gate 		kssl_ctx->length  = length;
17010Sstevel@tonic-gate 		}
17020Sstevel@tonic-gate 	else
17030Sstevel@tonic-gate                 {
17040Sstevel@tonic-gate 		kssl_ctx->enctype = ENCTYPE_UNKNOWN;
17050Sstevel@tonic-gate 		kssl_ctx->length  = 0;
17060Sstevel@tonic-gate 		return KSSL_CTX_OK;
17070Sstevel@tonic-gate 		}
17080Sstevel@tonic-gate 
17090Sstevel@tonic-gate 	if ((kssl_ctx->key =
17100Sstevel@tonic-gate                 (krb5_octet FAR *) calloc(1, kssl_ctx->length)) == NULL)
17110Sstevel@tonic-gate                 {
17120Sstevel@tonic-gate 		kssl_ctx->length  = 0;
17130Sstevel@tonic-gate 		return KSSL_CTX_ERR;
17140Sstevel@tonic-gate 		}
17150Sstevel@tonic-gate 	else
17160Sstevel@tonic-gate 		memcpy(kssl_ctx->key, contents, length);
17170Sstevel@tonic-gate 
17180Sstevel@tonic-gate 	return KSSL_CTX_OK;
17190Sstevel@tonic-gate         }
17200Sstevel@tonic-gate 
17210Sstevel@tonic-gate 
17220Sstevel@tonic-gate /*	Display contents of kssl_ctx struct
17230Sstevel@tonic-gate */
17240Sstevel@tonic-gate void
kssl_ctx_show(KSSL_CTX * kssl_ctx)17250Sstevel@tonic-gate kssl_ctx_show(KSSL_CTX *kssl_ctx)
17260Sstevel@tonic-gate         {
17270Sstevel@tonic-gate 	int 	i;
17280Sstevel@tonic-gate 
17290Sstevel@tonic-gate 	printf("kssl_ctx: ");
17300Sstevel@tonic-gate 	if (kssl_ctx == NULL)
17310Sstevel@tonic-gate                 {
17320Sstevel@tonic-gate 		printf("NULL\n");
17330Sstevel@tonic-gate 		return;
17340Sstevel@tonic-gate 		}
17350Sstevel@tonic-gate 	else
17360Sstevel@tonic-gate 		printf("%p\n", (void *)kssl_ctx);
17370Sstevel@tonic-gate 
17380Sstevel@tonic-gate 	printf("\tservice:\t%s\n",
17390Sstevel@tonic-gate                 (kssl_ctx->service_name)? kssl_ctx->service_name: "NULL");
17400Sstevel@tonic-gate 	printf("\tclient:\t%s\n",
17410Sstevel@tonic-gate                 (kssl_ctx->client_princ)? kssl_ctx->client_princ: "NULL");
17420Sstevel@tonic-gate 	printf("\tserver:\t%s\n",
17430Sstevel@tonic-gate                 (kssl_ctx->service_host)? kssl_ctx->service_host: "NULL");
17440Sstevel@tonic-gate 	printf("\tkeytab:\t%s\n",
17450Sstevel@tonic-gate                 (kssl_ctx->keytab_file)? kssl_ctx->keytab_file: "NULL");
17460Sstevel@tonic-gate 	printf("\tkey [%d:%d]:\t",
17470Sstevel@tonic-gate                 kssl_ctx->enctype, kssl_ctx->length);
17480Sstevel@tonic-gate 
17490Sstevel@tonic-gate 	for (i=0; i < kssl_ctx->length  &&  kssl_ctx->key; i++)
17500Sstevel@tonic-gate                 {
17510Sstevel@tonic-gate 		printf("%02x", kssl_ctx->key[i]);
17520Sstevel@tonic-gate 		}
17530Sstevel@tonic-gate 	printf("\n");
17540Sstevel@tonic-gate 	return;
17550Sstevel@tonic-gate         }
17560Sstevel@tonic-gate 
17570Sstevel@tonic-gate     int
kssl_keytab_is_available(KSSL_CTX * kssl_ctx)17580Sstevel@tonic-gate     kssl_keytab_is_available(KSSL_CTX *kssl_ctx)
17590Sstevel@tonic-gate {
17600Sstevel@tonic-gate     krb5_context		krb5context = NULL;
17610Sstevel@tonic-gate     krb5_keytab 		krb5keytab = NULL;
17620Sstevel@tonic-gate     krb5_keytab_entry           entry;
17630Sstevel@tonic-gate     krb5_principal              princ = NULL;
17640Sstevel@tonic-gate     krb5_error_code  		krb5rc = KRB5KRB_ERR_GENERIC;
17650Sstevel@tonic-gate     int rc = 0;
17660Sstevel@tonic-gate 
17670Sstevel@tonic-gate     if ((krb5rc = krb5_init_context(&krb5context)))
17680Sstevel@tonic-gate         return(0);
17690Sstevel@tonic-gate 
17700Sstevel@tonic-gate     /*	kssl_ctx->keytab_file == NULL ==> use Kerberos default
17710Sstevel@tonic-gate     */
17720Sstevel@tonic-gate     if (kssl_ctx->keytab_file)
17730Sstevel@tonic-gate     {
17740Sstevel@tonic-gate         krb5rc = krb5_kt_resolve(krb5context, kssl_ctx->keytab_file,
17750Sstevel@tonic-gate                                   &krb5keytab);
17760Sstevel@tonic-gate         if (krb5rc)
17770Sstevel@tonic-gate             goto exit;
17780Sstevel@tonic-gate     }
17790Sstevel@tonic-gate     else
17800Sstevel@tonic-gate     {
17810Sstevel@tonic-gate         krb5rc = krb5_kt_default(krb5context,&krb5keytab);
17820Sstevel@tonic-gate         if (krb5rc)
17830Sstevel@tonic-gate             goto exit;
17840Sstevel@tonic-gate     }
17850Sstevel@tonic-gate 
17860Sstevel@tonic-gate     /* the host key we are looking for */
17870Sstevel@tonic-gate     krb5rc = krb5_sname_to_principal(krb5context, NULL,
17880Sstevel@tonic-gate                                      kssl_ctx->service_name ? kssl_ctx->service_name: KRB5SVC,
17890Sstevel@tonic-gate                                      KRB5_NT_SRV_HST, &princ);
17900Sstevel@tonic-gate 
17910Sstevel@tonic-gate     krb5rc = krb5_kt_get_entry(krb5context, krb5keytab,
17920Sstevel@tonic-gate                                 princ,
17930Sstevel@tonic-gate                                 0 /* IGNORE_VNO */,
17940Sstevel@tonic-gate                                 0 /* IGNORE_ENCTYPE */,
17950Sstevel@tonic-gate                                 &entry);
17960Sstevel@tonic-gate     if ( krb5rc == KRB5_KT_NOTFOUND ) {
17970Sstevel@tonic-gate         rc = 1;
17980Sstevel@tonic-gate         goto exit;
17990Sstevel@tonic-gate     } else if ( krb5rc )
18000Sstevel@tonic-gate         goto exit;
18010Sstevel@tonic-gate 
18020Sstevel@tonic-gate     krb5_kt_free_entry(krb5context, &entry);
18030Sstevel@tonic-gate     rc = 1;
18040Sstevel@tonic-gate 
18050Sstevel@tonic-gate   exit:
18060Sstevel@tonic-gate     if (krb5keytab)     krb5_kt_close(krb5context, krb5keytab);
18070Sstevel@tonic-gate     if (princ)          krb5_free_principal(krb5context, princ);
18080Sstevel@tonic-gate     if (krb5context)	krb5_free_context(krb5context);
18090Sstevel@tonic-gate     return(rc);
18100Sstevel@tonic-gate }
18110Sstevel@tonic-gate 
18120Sstevel@tonic-gate int
kssl_tgt_is_available(KSSL_CTX * kssl_ctx)18130Sstevel@tonic-gate kssl_tgt_is_available(KSSL_CTX *kssl_ctx)
18140Sstevel@tonic-gate         {
18150Sstevel@tonic-gate         krb5_error_code		krb5rc = KRB5KRB_ERR_GENERIC;
18160Sstevel@tonic-gate         krb5_context		krb5context = NULL;
18170Sstevel@tonic-gate         krb5_ccache 		krb5ccdef = NULL;
18180Sstevel@tonic-gate         krb5_creds		krb5creds, *krb5credsp = NULL;
18190Sstevel@tonic-gate         int                     rc = 0;
18200Sstevel@tonic-gate 
18210Sstevel@tonic-gate         memset((char *)&krb5creds, 0, sizeof(krb5creds));
18220Sstevel@tonic-gate 
18230Sstevel@tonic-gate         if (!kssl_ctx)
18240Sstevel@tonic-gate             return(0);
18250Sstevel@tonic-gate 
18260Sstevel@tonic-gate         if (!kssl_ctx->service_host)
18270Sstevel@tonic-gate             return(0);
18280Sstevel@tonic-gate 
18290Sstevel@tonic-gate         if ((krb5rc = krb5_init_context(&krb5context)) != 0)
18300Sstevel@tonic-gate             goto err;
18310Sstevel@tonic-gate 
18320Sstevel@tonic-gate         if ((krb5rc = krb5_sname_to_principal(krb5context,
18330Sstevel@tonic-gate                                               kssl_ctx->service_host,
18340Sstevel@tonic-gate                                               (kssl_ctx->service_name)? kssl_ctx->service_name: KRB5SVC,
18350Sstevel@tonic-gate                                               KRB5_NT_SRV_HST, &krb5creds.server)) != 0)
18360Sstevel@tonic-gate             goto err;
18370Sstevel@tonic-gate 
18380Sstevel@tonic-gate         if ((krb5rc = krb5_cc_default(krb5context, &krb5ccdef)) != 0)
18390Sstevel@tonic-gate             goto err;
18400Sstevel@tonic-gate 
18410Sstevel@tonic-gate         if ((krb5rc = krb5_cc_get_principal(krb5context, krb5ccdef,
18420Sstevel@tonic-gate                                              &krb5creds.client)) != 0)
18430Sstevel@tonic-gate             goto err;
18440Sstevel@tonic-gate 
18450Sstevel@tonic-gate         if ((krb5rc = krb5_get_credentials(krb5context, 0, krb5ccdef,
18460Sstevel@tonic-gate                                             &krb5creds, &krb5credsp)) != 0)
18470Sstevel@tonic-gate             goto err;
18480Sstevel@tonic-gate 
18490Sstevel@tonic-gate         rc = 1;
18500Sstevel@tonic-gate 
18510Sstevel@tonic-gate       err:
18520Sstevel@tonic-gate #ifdef KSSL_DEBUG
18530Sstevel@tonic-gate 	kssl_ctx_show(kssl_ctx);
18540Sstevel@tonic-gate #endif	/* KSSL_DEBUG */
18550Sstevel@tonic-gate 
18560Sstevel@tonic-gate 	if (krb5creds.client)	krb5_free_principal(krb5context, krb5creds.client);
18570Sstevel@tonic-gate 	if (krb5creds.server)	krb5_free_principal(krb5context, krb5creds.server);
18580Sstevel@tonic-gate 	if (krb5context)	krb5_free_context(krb5context);
18590Sstevel@tonic-gate         return(rc);
18600Sstevel@tonic-gate 	}
18610Sstevel@tonic-gate 
18620Sstevel@tonic-gate #if !defined(OPENSSL_SYS_WINDOWS) && !defined(OPENSSL_SYS_WIN32)
kssl_krb5_free_data_contents(krb5_context context,krb5_data * data)18630Sstevel@tonic-gate void kssl_krb5_free_data_contents(krb5_context context, krb5_data *data)
18640Sstevel@tonic-gate 	{
18650Sstevel@tonic-gate #ifdef KRB5_HEIMDAL
18660Sstevel@tonic-gate 	data->length = 0;
18670Sstevel@tonic-gate         if (data->data)
18680Sstevel@tonic-gate             free(data->data);
18690Sstevel@tonic-gate #elif defined(KRB5_MIT_OLD11)
18700Sstevel@tonic-gate 	if (data->data)  {
18710Sstevel@tonic-gate 		krb5_xfree(data->data);
18720Sstevel@tonic-gate 		data->data = 0;
18730Sstevel@tonic-gate 		}
18740Sstevel@tonic-gate #else
18750Sstevel@tonic-gate 	krb5_free_data_contents(NULL, data);
18760Sstevel@tonic-gate #endif
18770Sstevel@tonic-gate 	}
18780Sstevel@tonic-gate #endif /* !OPENSSL_SYS_WINDOWS && !OPENSSL_SYS_WIN32 */
18790Sstevel@tonic-gate 
18800Sstevel@tonic-gate 
18810Sstevel@tonic-gate /*  Given pointers to KerberosTime and struct tm structs, convert the
18820Sstevel@tonic-gate **  KerberosTime string to struct tm.  Note that KerberosTime is a
18830Sstevel@tonic-gate **  ASN1_GENERALIZEDTIME value, constrained to GMT with no fractional
18840Sstevel@tonic-gate **  seconds as defined in RFC 1510.
18850Sstevel@tonic-gate **  Return pointer to the (partially) filled in struct tm on success,
18860Sstevel@tonic-gate **  return NULL on failure.
18870Sstevel@tonic-gate */
k_gmtime(ASN1_GENERALIZEDTIME * gtime,struct tm * k_tm)18880Sstevel@tonic-gate struct tm	*k_gmtime(ASN1_GENERALIZEDTIME *gtime, struct tm *k_tm)
18890Sstevel@tonic-gate 	{
18900Sstevel@tonic-gate 	char 		c, *p;
18910Sstevel@tonic-gate 
18920Sstevel@tonic-gate 	if (!k_tm)  return NULL;
18930Sstevel@tonic-gate 	if (gtime == NULL  ||  gtime->length < 14)  return NULL;
18940Sstevel@tonic-gate 	if (gtime->data == NULL)  return NULL;
18950Sstevel@tonic-gate 
18960Sstevel@tonic-gate 	p = (char *)&gtime->data[14];
18970Sstevel@tonic-gate 
18980Sstevel@tonic-gate 	c = *p;	 *p = '\0';  p -= 2;  k_tm->tm_sec  = atoi(p);      *(p+2) = c;
18990Sstevel@tonic-gate 	c = *p;	 *p = '\0';  p -= 2;  k_tm->tm_min  = atoi(p);      *(p+2) = c;
19000Sstevel@tonic-gate 	c = *p;	 *p = '\0';  p -= 2;  k_tm->tm_hour = atoi(p);      *(p+2) = c;
19010Sstevel@tonic-gate 	c = *p;	 *p = '\0';  p -= 2;  k_tm->tm_mday = atoi(p);      *(p+2) = c;
19020Sstevel@tonic-gate 	c = *p;	 *p = '\0';  p -= 2;  k_tm->tm_mon  = atoi(p)-1;    *(p+2) = c;
19030Sstevel@tonic-gate 	c = *p;	 *p = '\0';  p -= 4;  k_tm->tm_year = atoi(p)-1900; *(p+4) = c;
19040Sstevel@tonic-gate 
19050Sstevel@tonic-gate 	return k_tm;
19060Sstevel@tonic-gate 	}
19070Sstevel@tonic-gate 
19080Sstevel@tonic-gate 
19090Sstevel@tonic-gate /*  Helper function for kssl_validate_times().
19100Sstevel@tonic-gate **  We need context->clockskew, but krb5_context is an opaque struct.
19110Sstevel@tonic-gate **  So we try to sneek the clockskew out through the replay cache.
19120Sstevel@tonic-gate **	If that fails just return a likely default (300 seconds).
19130Sstevel@tonic-gate */
get_rc_clockskew(krb5_context context)19140Sstevel@tonic-gate krb5_deltat	get_rc_clockskew(krb5_context context)
19150Sstevel@tonic-gate 	{
19160Sstevel@tonic-gate 	krb5_rcache 	rc;
19170Sstevel@tonic-gate 	krb5_deltat 	clockskew;
19180Sstevel@tonic-gate 
19190Sstevel@tonic-gate 	if (krb5_rc_default(context, &rc))  return KSSL_CLOCKSKEW;
19200Sstevel@tonic-gate 	if (krb5_rc_initialize(context, rc, 0))  return KSSL_CLOCKSKEW;
19210Sstevel@tonic-gate 	if (krb5_rc_get_lifespan(context, rc, &clockskew))  {
19220Sstevel@tonic-gate 		clockskew = KSSL_CLOCKSKEW;
19230Sstevel@tonic-gate 		}
19240Sstevel@tonic-gate 	(void) krb5_rc_destroy(context, rc);
19250Sstevel@tonic-gate 	return clockskew;
19260Sstevel@tonic-gate 	}
19270Sstevel@tonic-gate 
19280Sstevel@tonic-gate 
19290Sstevel@tonic-gate /*  kssl_validate_times() combines (and more importantly exposes)
19300Sstevel@tonic-gate **  the MIT KRB5 internal function krb5_validate_times() and the
19310Sstevel@tonic-gate **  in_clock_skew() macro.  The authenticator client time is checked
19320Sstevel@tonic-gate **  to be within clockskew secs of the current time and the current
19330Sstevel@tonic-gate **  time is checked to be within the ticket start and expire times.
19340Sstevel@tonic-gate **  Either check may be omitted by supplying a NULL value.
19350Sstevel@tonic-gate **  Returns 0 for valid times, SSL_R_KRB5* error codes otherwise.
19360Sstevel@tonic-gate **  See Also: (Kerberos source)/krb5/lib/krb5/krb/valid_times.c
19370Sstevel@tonic-gate **  20010420 VRS
19380Sstevel@tonic-gate */
kssl_validate_times(krb5_timestamp atime,krb5_ticket_times * ttimes)19390Sstevel@tonic-gate krb5_error_code  kssl_validate_times(	krb5_timestamp atime,
19400Sstevel@tonic-gate 					krb5_ticket_times *ttimes)
19410Sstevel@tonic-gate 	{
19420Sstevel@tonic-gate 	krb5_deltat 	skew;
19430Sstevel@tonic-gate 	krb5_timestamp	start, now;
19440Sstevel@tonic-gate 	krb5_error_code	rc;
19450Sstevel@tonic-gate 	krb5_context	context;
19460Sstevel@tonic-gate 
19470Sstevel@tonic-gate 	if ((rc = krb5_init_context(&context)))	 return SSL_R_KRB5_S_BAD_TICKET;
19480Sstevel@tonic-gate 	skew = get_rc_clockskew(context);
19490Sstevel@tonic-gate 	if ((rc = krb5_timeofday(context,&now))) return SSL_R_KRB5_S_BAD_TICKET;
19500Sstevel@tonic-gate 	krb5_free_context(context);
19510Sstevel@tonic-gate 
19520Sstevel@tonic-gate 	if (atime  &&  labs(atime - now) >= skew)  return SSL_R_KRB5_S_TKT_SKEW;
19530Sstevel@tonic-gate 
19540Sstevel@tonic-gate 	if (! ttimes)  return 0;
19550Sstevel@tonic-gate 
19560Sstevel@tonic-gate 	start = (ttimes->starttime != 0)? ttimes->starttime: ttimes->authtime;
19570Sstevel@tonic-gate 	if (start - now > skew)  return SSL_R_KRB5_S_TKT_NYV;
19580Sstevel@tonic-gate 	if ((now - ttimes->endtime) > skew)  return SSL_R_KRB5_S_TKT_EXPIRED;
19590Sstevel@tonic-gate 
19600Sstevel@tonic-gate #ifdef KSSL_DEBUG
19610Sstevel@tonic-gate 	printf("kssl_validate_times: %d |<-  | %d - %d | < %d  ->| %d\n",
19620Sstevel@tonic-gate 		start, atime, now, skew, ttimes->endtime);
19630Sstevel@tonic-gate #endif	/* KSSL_DEBUG */
19640Sstevel@tonic-gate 
19650Sstevel@tonic-gate 	return 0;
19660Sstevel@tonic-gate 	}
19670Sstevel@tonic-gate 
19680Sstevel@tonic-gate 
19690Sstevel@tonic-gate /*  Decode and decrypt given DER-encoded authenticator, then pass
19700Sstevel@tonic-gate **  authenticator ctime back in *atimep (or 0 if time unavailable).
19710Sstevel@tonic-gate **  Returns krb5_error_code and kssl_err on error.  A NULL
19720Sstevel@tonic-gate **  authenticator (authentp->length == 0) is not considered an error.
19730Sstevel@tonic-gate **  Note that kssl_check_authent() makes use of the KRB5 session key;
19740Sstevel@tonic-gate **  you must call kssl_sget_tkt() to get the key before calling this routine.
19750Sstevel@tonic-gate */
kssl_check_authent(KSSL_CTX * kssl_ctx,krb5_data * authentp,krb5_timestamp * atimep,KSSL_ERR * kssl_err)19760Sstevel@tonic-gate krb5_error_code  kssl_check_authent(
19770Sstevel@tonic-gate 			/* IN     */	KSSL_CTX	*kssl_ctx,
19780Sstevel@tonic-gate                         /* IN     */   	krb5_data	*authentp,
19790Sstevel@tonic-gate 			/* OUT    */	krb5_timestamp	*atimep,
19800Sstevel@tonic-gate 			/* OUT    */    KSSL_ERR	*kssl_err  )
19810Sstevel@tonic-gate 	{
19820Sstevel@tonic-gate         krb5_error_code		krb5rc = 0;
19830Sstevel@tonic-gate 	KRB5_ENCDATA		*dec_authent = NULL;
19840Sstevel@tonic-gate 	KRB5_AUTHENTBODY	*auth = NULL;
19850Sstevel@tonic-gate 	krb5_enctype		enctype;
19860Sstevel@tonic-gate 	EVP_CIPHER_CTX		ciph_ctx;
19870Sstevel@tonic-gate 	const EVP_CIPHER	*enc = NULL;
19880Sstevel@tonic-gate 	unsigned char		iv[EVP_MAX_IV_LENGTH];
1989*2139Sjp161948 	const unsigned char	*p;
1990*2139Sjp161948 	unsigned char		*unenc_authent;
19910Sstevel@tonic-gate 	int 			outl, unencbufsize;
19920Sstevel@tonic-gate 	struct tm		tm_time, *tm_l, *tm_g;
19930Sstevel@tonic-gate 	time_t			now, tl, tg, tr, tz_offset;
19940Sstevel@tonic-gate 
19950Sstevel@tonic-gate 	EVP_CIPHER_CTX_init(&ciph_ctx);
19960Sstevel@tonic-gate 	*atimep = 0;
19970Sstevel@tonic-gate 	kssl_err_set(kssl_err, 0, "");
19980Sstevel@tonic-gate 
19990Sstevel@tonic-gate #ifndef KRB5CHECKAUTH
20000Sstevel@tonic-gate 	authentp = NULL;
20010Sstevel@tonic-gate #else
20020Sstevel@tonic-gate #if	KRB5CHECKAUTH == 0
20030Sstevel@tonic-gate 	authentp = NULL;
20040Sstevel@tonic-gate #endif
20050Sstevel@tonic-gate #endif	/* KRB5CHECKAUTH */
20060Sstevel@tonic-gate 
20070Sstevel@tonic-gate 	if (authentp == NULL  ||  authentp->length == 0)  return 0;
20080Sstevel@tonic-gate 
20090Sstevel@tonic-gate #ifdef KSSL_DEBUG
20100Sstevel@tonic-gate         {
20110Sstevel@tonic-gate         unsigned int ui;
20120Sstevel@tonic-gate 	printf("kssl_check_authent: authenticator[%d]:\n",authentp->length);
20130Sstevel@tonic-gate 	p = authentp->data;
20140Sstevel@tonic-gate 	for (ui=0; ui < authentp->length; ui++)  printf("%02x ",p[ui]);
20150Sstevel@tonic-gate 	printf("\n");
20160Sstevel@tonic-gate         }
20170Sstevel@tonic-gate #endif	/* KSSL_DEBUG */
20180Sstevel@tonic-gate 
20190Sstevel@tonic-gate 	unencbufsize = 2 * authentp->length;
20200Sstevel@tonic-gate 	if ((unenc_authent = calloc(1, unencbufsize)) == NULL)
20210Sstevel@tonic-gate 		{
20220Sstevel@tonic-gate 		kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
20230Sstevel@tonic-gate 			"Unable to allocate authenticator buffer.\n");
20240Sstevel@tonic-gate 		krb5rc = KRB5KRB_ERR_GENERIC;
20250Sstevel@tonic-gate 		goto err;
20260Sstevel@tonic-gate 		}
20270Sstevel@tonic-gate 
20280Sstevel@tonic-gate 	p = (unsigned char *)authentp->data;
20290Sstevel@tonic-gate 	if ((dec_authent = d2i_KRB5_ENCDATA(NULL, &p,
20300Sstevel@tonic-gate 					(long) authentp->length)) == NULL)
20310Sstevel@tonic-gate 		{
20320Sstevel@tonic-gate 		kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
20330Sstevel@tonic-gate                         "Error decoding authenticator.\n");
20340Sstevel@tonic-gate 		krb5rc = KRB5KRB_AP_ERR_BAD_INTEGRITY;
20350Sstevel@tonic-gate 		goto err;
20360Sstevel@tonic-gate 		}
20370Sstevel@tonic-gate 
20380Sstevel@tonic-gate 	enctype = dec_authent->etype->data[0];	/* should = kssl_ctx->enctype */
20390Sstevel@tonic-gate #if !defined(KRB5_MIT_OLD11)
20400Sstevel@tonic-gate             switch ( enctype ) {
20410Sstevel@tonic-gate             case ENCTYPE_DES3_CBC_SHA1:		/*    EVP_des_ede3_cbc();  */
20420Sstevel@tonic-gate             case ENCTYPE_DES3_CBC_SHA:
20430Sstevel@tonic-gate             case ENCTYPE_DES3_CBC_RAW:
20440Sstevel@tonic-gate                 krb5rc = 0;                     /* Skip, can't handle derived keys */
20450Sstevel@tonic-gate                 goto err;
20460Sstevel@tonic-gate             }
20470Sstevel@tonic-gate #endif
20480Sstevel@tonic-gate 	enc = kssl_map_enc(enctype);
20490Sstevel@tonic-gate 	memset(iv, 0, sizeof iv);       /* per RFC 1510 */
20500Sstevel@tonic-gate 
20510Sstevel@tonic-gate 	if (enc == NULL)
20520Sstevel@tonic-gate 		{
20530Sstevel@tonic-gate 		/*  Disable kssl_check_authent for ENCTYPE_DES3_CBC_SHA1.
20540Sstevel@tonic-gate 		**  This enctype indicates the authenticator was encrypted
20550Sstevel@tonic-gate 		**  using key-usage derived keys which openssl cannot decrypt.
20560Sstevel@tonic-gate 		*/
20570Sstevel@tonic-gate 		goto err;
20580Sstevel@tonic-gate 		}
20590Sstevel@tonic-gate 
20600Sstevel@tonic-gate         if (!EVP_CipherInit(&ciph_ctx,enc,kssl_ctx->key,iv,0))
20610Sstevel@tonic-gate                 {
20620Sstevel@tonic-gate                 kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
20630Sstevel@tonic-gate                         "EVP_CipherInit error decrypting authenticator.\n");
20640Sstevel@tonic-gate                 krb5rc = KRB5KRB_AP_ERR_BAD_INTEGRITY;
20650Sstevel@tonic-gate                 goto err;
20660Sstevel@tonic-gate                 }
20670Sstevel@tonic-gate         outl = dec_authent->cipher->length;
20680Sstevel@tonic-gate         if (!EVP_Cipher(&ciph_ctx,unenc_authent,dec_authent->cipher->data,outl))
20690Sstevel@tonic-gate                 {
20700Sstevel@tonic-gate                 kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
20710Sstevel@tonic-gate                         "EVP_Cipher error decrypting authenticator.\n");
20720Sstevel@tonic-gate                 krb5rc = KRB5KRB_AP_ERR_BAD_INTEGRITY;
20730Sstevel@tonic-gate                 goto err;
20740Sstevel@tonic-gate                 }
20750Sstevel@tonic-gate         EVP_CIPHER_CTX_cleanup(&ciph_ctx);
20760Sstevel@tonic-gate 
20770Sstevel@tonic-gate #ifdef KSSL_DEBUG
20780Sstevel@tonic-gate 	printf("kssl_check_authent: decrypted authenticator[%d] =\n", outl);
20790Sstevel@tonic-gate 	for (padl=0; padl < outl; padl++) printf("%02x ",unenc_authent[padl]);
20800Sstevel@tonic-gate 	printf("\n");
20810Sstevel@tonic-gate #endif	/* KSSL_DEBUG */
20820Sstevel@tonic-gate 
20830Sstevel@tonic-gate 	if ((p = kssl_skip_confound(enctype, unenc_authent)) == NULL)
20840Sstevel@tonic-gate 		{
20850Sstevel@tonic-gate 		kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
20860Sstevel@tonic-gate                         "confounded by authenticator.\n");
20870Sstevel@tonic-gate 		krb5rc = KRB5KRB_AP_ERR_BAD_INTEGRITY;
20880Sstevel@tonic-gate 		goto err;
20890Sstevel@tonic-gate 		}
20900Sstevel@tonic-gate 	outl -= p - unenc_authent;
20910Sstevel@tonic-gate 
20920Sstevel@tonic-gate 	if ((auth = (KRB5_AUTHENTBODY *) d2i_KRB5_AUTHENT(NULL, &p,
20930Sstevel@tonic-gate 							  (long) outl))==NULL)
20940Sstevel@tonic-gate 		{
20950Sstevel@tonic-gate 		kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
20960Sstevel@tonic-gate                         "Error decoding authenticator body.\n");
20970Sstevel@tonic-gate 		krb5rc = KRB5KRB_AP_ERR_BAD_INTEGRITY;
20980Sstevel@tonic-gate 		goto err;
20990Sstevel@tonic-gate 		}
21000Sstevel@tonic-gate 
21010Sstevel@tonic-gate 	memset(&tm_time,0,sizeof(struct tm));
21020Sstevel@tonic-gate 	if (k_gmtime(auth->ctime, &tm_time)  &&
21030Sstevel@tonic-gate 		((tr = mktime(&tm_time)) != (time_t)(-1)))
21040Sstevel@tonic-gate  		{
21050Sstevel@tonic-gate  		now  = time(&now);
21060Sstevel@tonic-gate  		tm_l = localtime(&now); 	tl = mktime(tm_l);
21070Sstevel@tonic-gate  		tm_g = gmtime(&now);		tg = mktime(tm_g);
21080Sstevel@tonic-gate  		tz_offset = tg - tl;
21090Sstevel@tonic-gate 
21100Sstevel@tonic-gate 		*atimep = tr - tz_offset;
21110Sstevel@tonic-gate  		}
21120Sstevel@tonic-gate 
21130Sstevel@tonic-gate #ifdef KSSL_DEBUG
21140Sstevel@tonic-gate 	printf("kssl_check_authent: returns %d for client time ", *atimep);
21150Sstevel@tonic-gate 	if (auth && auth->ctime && auth->ctime->length && auth->ctime->data)
21160Sstevel@tonic-gate 		printf("%.*s\n", auth->ctime->length, auth->ctime->data);
21170Sstevel@tonic-gate 	else	printf("NULL\n");
21180Sstevel@tonic-gate #endif	/* KSSL_DEBUG */
21190Sstevel@tonic-gate 
21200Sstevel@tonic-gate  err:
21210Sstevel@tonic-gate 	if (auth)		KRB5_AUTHENT_free((KRB5_AUTHENT *) auth);
21220Sstevel@tonic-gate 	if (dec_authent)	KRB5_ENCDATA_free(dec_authent);
21230Sstevel@tonic-gate 	if (unenc_authent)	free(unenc_authent);
21240Sstevel@tonic-gate 	EVP_CIPHER_CTX_cleanup(&ciph_ctx);
21250Sstevel@tonic-gate 	return krb5rc;
21260Sstevel@tonic-gate 	}
21270Sstevel@tonic-gate 
21280Sstevel@tonic-gate 
21290Sstevel@tonic-gate /*  Replaces krb5_build_principal_ext(), with varargs length == 2 (svc, host),
21300Sstevel@tonic-gate **  because I dont't know how to stub varargs.
21310Sstevel@tonic-gate **  Returns krb5_error_code == ENOMEM on alloc error, otherwise
21320Sstevel@tonic-gate **  passes back newly constructed principal, which should be freed by caller.
21330Sstevel@tonic-gate */
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)21340Sstevel@tonic-gate krb5_error_code  kssl_build_principal_2(
21350Sstevel@tonic-gate 			/* UPDATE */	krb5_context	context,
21360Sstevel@tonic-gate 			/* OUT    */	krb5_principal	*princ,
21370Sstevel@tonic-gate 			/* IN     */	int rlen,  const char *realm,
21380Sstevel@tonic-gate 			/* IN	  */	int slen,  const char *svc,
21390Sstevel@tonic-gate 			/* IN	  */	int hlen,  const char *host)
21400Sstevel@tonic-gate 	{
21410Sstevel@tonic-gate 	krb5_data		*p_data = NULL;
21420Sstevel@tonic-gate 	krb5_principal		new_p = NULL;
21430Sstevel@tonic-gate         char			*new_r = NULL;
21440Sstevel@tonic-gate 
21450Sstevel@tonic-gate 	if ((p_data = (krb5_data *) calloc(2, sizeof(krb5_data))) == NULL  ||
21460Sstevel@tonic-gate 	    (new_p = (krb5_principal) calloc(1, sizeof(krb5_principal_data)))
21470Sstevel@tonic-gate 			== NULL)  goto err;
21480Sstevel@tonic-gate 	new_p->length = 2;
21490Sstevel@tonic-gate 	new_p->data = p_data;
21500Sstevel@tonic-gate 
21510Sstevel@tonic-gate 	if ((new_r = calloc(1, rlen + 1)) == NULL)  goto err;
21520Sstevel@tonic-gate 	memcpy(new_r, realm, rlen);
21530Sstevel@tonic-gate 	krb5_princ_set_realm_length(context, new_p, rlen);
21540Sstevel@tonic-gate 	krb5_princ_set_realm_data(context, new_p, new_r);
21550Sstevel@tonic-gate 
21560Sstevel@tonic-gate 	if ((new_p->data[0].data = calloc(1, slen + 1)) == NULL)  goto err;
21570Sstevel@tonic-gate 	memcpy(new_p->data[0].data, svc, slen);
21580Sstevel@tonic-gate 	new_p->data[0].length = slen;
21590Sstevel@tonic-gate 
21600Sstevel@tonic-gate 	if ((new_p->data[1].data = calloc(1, hlen + 1)) == NULL)  goto err;
21610Sstevel@tonic-gate 	memcpy(new_p->data[1].data, host, hlen);
21620Sstevel@tonic-gate 	new_p->data[1].length = hlen;
21630Sstevel@tonic-gate 
21640Sstevel@tonic-gate 	krb5_princ_type(context, new_p) = KRB5_NT_UNKNOWN;
21650Sstevel@tonic-gate 	*princ = new_p;
21660Sstevel@tonic-gate 	return 0;
21670Sstevel@tonic-gate 
21680Sstevel@tonic-gate  err:
21690Sstevel@tonic-gate 	if (new_p  &&  new_p[0].data)	free(new_p[0].data);
21700Sstevel@tonic-gate 	if (new_p  &&  new_p[1].data)	free(new_p[1].data);
21710Sstevel@tonic-gate 	if (new_p)	free(new_p);
21720Sstevel@tonic-gate 	if (new_r)	free(new_r);
21730Sstevel@tonic-gate 	return ENOMEM;
21740Sstevel@tonic-gate 	}
21750Sstevel@tonic-gate 
21760Sstevel@tonic-gate 
21770Sstevel@tonic-gate #else /* !OPENSSL_NO_KRB5 */
21780Sstevel@tonic-gate 
21790Sstevel@tonic-gate #if defined(PEDANTIC) || defined(OPENSSL_SYS_VMS)
21800Sstevel@tonic-gate static int dummy=(int)&dummy;
21810Sstevel@tonic-gate #endif
21820Sstevel@tonic-gate 
21830Sstevel@tonic-gate #endif	/* !OPENSSL_NO_KRB5	*/
21840Sstevel@tonic-gate 
2185