kssl.c 57.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
/* ssl/kssl.c -*- mode: C; c-file-style: "eay" -*- */
/* Written by Vern Staats <staatsvr@asc.hpc.mil> for the OpenSSL project 2000.
 */
/* ====================================================================
 * Copyright (c) 2000 The OpenSSL Project.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer. 
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. All advertising materials mentioning features or use of this
 *    software must display the following acknowledgment:
 *    "This product includes software developed by the OpenSSL Project
 *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
 *
 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
 *    endorse or promote products derived from this software without
 *    prior written permission. For written permission, please contact
 *    licensing@OpenSSL.org.
 *
 * 5. Products derived from this software may not be called "OpenSSL"
 *    nor may "OpenSSL" appear in their names without prior written
 *    permission of the OpenSSL Project.
 *
 * 6. Redistributions of any form whatsoever must retain the following
 *    acknowledgment:
 *    "This product includes software developed by the OpenSSL Project
 *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
 *
 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 * ====================================================================
 *
 * This product includes cryptographic software written by Eric Young
 * (eay@cryptsoft.com).  This product includes software written by Tim
 * Hudson (tjh@cryptsoft.com).
 *
 */


59
/*  ssl/kssl.c  --  Routines to support (& debug) Kerberos5 auth for openssl
60
**
61 62 63 64 65 66
**  19990701	VRS 	Started.
**  200011??	Jeffrey Altman, Richard Levitte
**          		Generalized for Heimdal, Newer MIT, & Win32.
**          		Integrated into main OpenSSL 0.9.7 snapshots.
**  20010413	Simon Wilkinson, VRS
**          		Real RFC2712 KerberosWrapper replaces AP_REQ.
67 68
*/

U
Ulf Möller 已提交
69
#include <openssl/opensslconf.h>
70
#ifndef OPENSSL_NO_KRB5
71 72
#define _XOPEN_SOURCE /* glibc2 needs this to declare strptime() */
#include <time.h>
73
#include <string.h>
74
#include <ctype.h>
75

76
#include <openssl/ssl.h>
77 78 79
#include <openssl/evp.h>
#include <openssl/objects.h>
#include <openssl/krb5_asn.h>
80

81 82 83 84 85 86 87
/* 
 * When OpenSSL is built on Windows, we do not want to require that
 * the Kerberos DLLs be available in order for the OpenSSL DLLs to
 * work.  Therefore, all Kerberos routines are loaded at run time
 * and we do not link to a .LIB file.
 */

88
#if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32)
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
/* 
 * The purpose of the following pre-processor statements is to provide
 * compatibility with different releases of MIT Kerberos for Windows.
 * All versions up to 1.2 used macros.  But macros do not allow for
 * a binary compatible interface for DLLs.  Therefore, all macros are
 * being replaced by function calls.  The following code will allow
 * an OpenSSL DLL built on Windows to work whether or not the macro
 * or function form of the routines are utilized.
 */
#ifdef  krb5_cc_get_principal
#define NO_DEF_KRB5_CCACHE
#undef  krb5_cc_get_principal
#endif
#define krb5_cc_get_principal    kssl_krb5_cc_get_principal

#define krb5_free_data_contents  kssl_krb5_free_data_contents   
#define krb5_free_context        kssl_krb5_free_context         
#define krb5_auth_con_free       kssl_krb5_auth_con_free        
#define krb5_free_principal      kssl_krb5_free_principal       
#define krb5_mk_req_extended     kssl_krb5_mk_req_extended      
#define krb5_get_credentials     kssl_krb5_get_credentials      
#define krb5_cc_default          kssl_krb5_cc_default           
#define krb5_sname_to_principal  kssl_krb5_sname_to_principal   
#define krb5_init_context        kssl_krb5_init_context         
#define krb5_free_ticket         kssl_krb5_free_ticket          
#define krb5_rd_req              kssl_krb5_rd_req               
#define krb5_kt_default          kssl_krb5_kt_default           
#define krb5_kt_resolve          kssl_krb5_kt_resolve           
#define krb5_auth_con_init       kssl_krb5_auth_con_init        

119 120 121 122 123
#define krb5_principal_compare   kssl_krb5_principal_compare
/* macro  #define krb5_kt_get_entry        kssl_krb5_kt_get_entry  */
#define krb5_decrypt_tkt_part    kssl_krb5_decrypt_tkt_part
#define krb5_timeofday           kssl_krb5_timeofday
#define krb5_rc_default           kssl_krb5_rc_default
124 125 126 127 128 129
#define krb5_rc_initialize   kssl_krb5_rc_initialize
#define krb5_rc_get_lifespan kssl_krb5_rc_get_lifespan
#define krb5_rc_destroy      kssl_krb5_rc_destroy
#define valid_cksumtype      kssl_valid_cksumtype
#define krb5_checksum_size   kssl_krb5_checksum_size
#define krb5_kt_free_entry   kssl_krb5_kt_free_entry
130

131 132 133 134 135 136 137 138 139 140 141 142 143
/* Prototypes for built in stubs */
void kssl_krb5_free_data_contents(krb5_context, krb5_data *);
void kssl_krb5_free_principal(krb5_context, krb5_principal );
krb5_error_code kssl_krb5_kt_resolve(krb5_context,
                                     krb5_const char *,
                                     krb5_keytab *);
krb5_error_code kssl_krb5_kt_default(krb5_context,
                                     krb5_keytab *);
krb5_error_code kssl_krb5_free_ticket(krb5_context, krb5_ticket *);
krb5_error_code kssl_krb5_rd_req(krb5_context, krb5_auth_context *, 
                                 krb5_const krb5_data *,
                                 krb5_const_principal, krb5_keytab, 
                                 krb5_flags *,krb5_ticket **);
144 145 146

krb5_boolean kssl_krb5_principal_compare(krb5_context, krb5_const_principal,
                                         krb5_const_principal);
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
krb5_error_code kssl_krb5_mk_req_extended(krb5_context,
                                          krb5_auth_context  *,
                                          krb5_const krb5_flags,
                                          krb5_data  *,
                                          krb5_creds  *,
                                          krb5_data  * );
krb5_error_code kssl_krb5_init_context(krb5_context *);
void kssl_krb5_free_context(krb5_context);
krb5_error_code kssl_krb5_cc_default(krb5_context,krb5_ccache  *);
krb5_error_code kssl_krb5_sname_to_principal(krb5_context,
                                             krb5_const char  *,
                                             krb5_const char  *,
                                             krb5_int32,
                                             krb5_principal  *);
krb5_error_code kssl_krb5_get_credentials(krb5_context,
                                          krb5_const krb5_flags,
                                          krb5_ccache,
                                          krb5_creds  *,
                                          krb5_creds  *  *);
krb5_error_code kssl_krb5_auth_con_init(krb5_context,
                                        krb5_auth_context  *);
krb5_error_code kssl_krb5_cc_get_principal(krb5_context context, 
                                           krb5_ccache cache,
                                           krb5_principal *principal);
krb5_error_code kssl_krb5_auth_con_free(krb5_context,krb5_auth_context);
172 173 174
size_t kssl_krb5_checksum_size(krb5_context context,krb5_cksumtype ctype);
krb5_boolean kssl_valid_cksumtype(krb5_cksumtype ctype);
krb5_error_code krb5_kt_free_entry(krb5_context,krb5_keytab_entry FAR * );
175 176

/* Function pointers (almost all Kerberos functions are _stdcall) */
177 178 179 180 181 182
static void (_stdcall *p_krb5_free_data_contents)(krb5_context, krb5_data *)
	=NULL;
static void (_stdcall *p_krb5_free_principal)(krb5_context, krb5_principal )
	=NULL;
static krb5_error_code(_stdcall *p_krb5_kt_resolve)
			(krb5_context, krb5_const char *, krb5_keytab *)=NULL;
183 184 185 186 187 188 189 190 191 192
static krb5_error_code (_stdcall *p_krb5_kt_default)(krb5_context,
                                                     krb5_keytab *)=NULL;
static krb5_error_code (_stdcall *p_krb5_free_ticket)(krb5_context, 
                                                      krb5_ticket *)=NULL;
static krb5_error_code (_stdcall *p_krb5_rd_req)(krb5_context, 
                                                 krb5_auth_context *, 
                                                 krb5_const krb5_data *,
                                                 krb5_const_principal, 
                                                 krb5_keytab, krb5_flags *,
                                                 krb5_ticket **)=NULL;
193 194 195 196
static krb5_error_code (_stdcall *p_krb5_mk_req_extended)
			(krb5_context, krb5_auth_context *,
			 krb5_const krb5_flags, krb5_data *, krb5_creds *,
			 krb5_data * )=NULL;
197 198 199 200
static krb5_error_code (_stdcall *p_krb5_init_context)(krb5_context *)=NULL;
static void (_stdcall *p_krb5_free_context)(krb5_context)=NULL;
static krb5_error_code (_stdcall *p_krb5_cc_default)(krb5_context,
                                                     krb5_ccache  *)=NULL;
201 202 203 204 205 206 207 208 209 210 211 212 213
static krb5_error_code (_stdcall *p_krb5_sname_to_principal)
			(krb5_context, krb5_const char *, krb5_const char *,
			 krb5_int32, krb5_principal *)=NULL;
static krb5_error_code (_stdcall *p_krb5_get_credentials)
			(krb5_context, krb5_const krb5_flags, krb5_ccache,
			 krb5_creds *, krb5_creds **)=NULL;
static krb5_error_code (_stdcall *p_krb5_auth_con_init)
			(krb5_context, krb5_auth_context *)=NULL;
static krb5_error_code (_stdcall *p_krb5_cc_get_principal)
			(krb5_context context, krb5_ccache cache,
			 krb5_principal *principal)=NULL;
static krb5_error_code (_stdcall *p_krb5_auth_con_free)
			(krb5_context, krb5_auth_context)=NULL;
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
static krb5_error_code (_stdcall *p_krb5_decrypt_tkt_part)
                        (krb5_context, krb5_const krb5_keyblock *,
                                           krb5_ticket *)=NULL;
static krb5_error_code (_stdcall *p_krb5_timeofday)
                        (krb5_context context, krb5_int32 *timeret)=NULL;
static krb5_error_code (_stdcall *p_krb5_rc_default)
                        (krb5_context context, krb5_rcache *rc)=NULL;
static krb5_error_code (_stdcall *p_krb5_rc_initialize)
                        (krb5_context context, krb5_rcache rc,
                                     krb5_deltat lifespan)=NULL;
static krb5_error_code (_stdcall *p_krb5_rc_get_lifespan)
                        (krb5_context context, krb5_rcache rc,
                                       krb5_deltat *lifespan)=NULL;
static krb5_error_code (_stdcall *p_krb5_rc_destroy)
                        (krb5_context context, krb5_rcache rc)=NULL;
static krb5_boolean (_stdcall *p_krb5_principal_compare)
                     (krb5_context, krb5_const_principal, krb5_const_principal)=NULL;
static size_t (_stdcall *p_krb5_checksum_size)(krb5_context context,krb5_cksumtype ctype)=NULL;
static krb5_boolean (_stdcall *p_valid_cksumtype)(krb5_cksumtype ctype)=NULL;
static krb5_error_code (_stdcall *p_krb5_kt_free_entry)
                        (krb5_context,krb5_keytab_entry * )=NULL;
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
static int krb5_loaded = 0;     /* only attempt to initialize func ptrs once */

/* Function to Load the Kerberos 5 DLL and initialize function pointers */
void
load_krb5_dll(void)
	{
	HANDLE hKRB5_32;
    
	krb5_loaded++;
	hKRB5_32 = LoadLibrary("KRB5_32");
	if (!hKRB5_32)
		return;

	(FARPROC) p_krb5_free_data_contents =
		GetProcAddress( hKRB5_32, "krb5_free_data_contents" );
	(FARPROC) p_krb5_free_context =
		GetProcAddress( hKRB5_32, "krb5_free_context" );
	(FARPROC) p_krb5_auth_con_free =
		GetProcAddress( hKRB5_32, "krb5_auth_con_free" );
	(FARPROC) p_krb5_free_principal =
		GetProcAddress( hKRB5_32, "krb5_free_principal" );
	(FARPROC) p_krb5_mk_req_extended =
		GetProcAddress( hKRB5_32, "krb5_mk_req_extended" );
	(FARPROC) p_krb5_get_credentials =
		GetProcAddress( hKRB5_32, "krb5_get_credentials" );
	(FARPROC) p_krb5_cc_get_principal =
		GetProcAddress( hKRB5_32, "krb5_cc_get_principal" );
	(FARPROC) p_krb5_cc_default =
		GetProcAddress( hKRB5_32, "krb5_cc_default" );
	(FARPROC) p_krb5_sname_to_principal =
		GetProcAddress( hKRB5_32, "krb5_sname_to_principal" );
	(FARPROC) p_krb5_init_context =
		GetProcAddress( hKRB5_32, "krb5_init_context" );
	(FARPROC) p_krb5_free_ticket =
		GetProcAddress( hKRB5_32, "krb5_free_ticket" );
	(FARPROC) p_krb5_rd_req =
		GetProcAddress( hKRB5_32, "krb5_rd_req" );
272 273 274 275 276 277 278 279 280 281 282 283 284 285
	(FARPROC) p_krb5_principal_compare =
		GetProcAddress( hKRB5_32, "krb5_principal_compare" );
	(FARPROC) p_krb5_decrypt_tkt_part =
		GetProcAddress( hKRB5_32, "krb5_decrypt_tkt_part" );
	(FARPROC) p_krb5_timeofday =
		GetProcAddress( hKRB5_32, "krb5_timeofday" );
	(FARPROC) p_krb5_rc_default =
		GetProcAddress( hKRB5_32, "krb5_rc_default" );
	(FARPROC) p_krb5_rc_initialize =
		GetProcAddress( hKRB5_32, "krb5_rc_initialize" );
	(FARPROC) p_krb5_rc_get_lifespan =
		GetProcAddress( hKRB5_32, "krb5_rc_get_lifespan" );
	(FARPROC) p_krb5_rc_destroy =
		GetProcAddress( hKRB5_32, "krb5_rc_destroy" );
286 287 288 289 290 291
	(FARPROC) p_krb5_kt_default =
		GetProcAddress( hKRB5_32, "krb5_kt_default" );
	(FARPROC) p_krb5_kt_resolve =
		GetProcAddress( hKRB5_32, "krb5_kt_resolve" );
	(FARPROC) p_krb5_auth_con_init =
		GetProcAddress( hKRB5_32, "krb5_auth_con_init" );
292 293 294 295 296 297
        (FARPROC) p_valid_cksumtype =
                GetProcAddress( hKRB5_32, "valid_cksumtype" );
        (FARPROC) p_krb5_checksum_size =
                GetProcAddress( hKRB5_32, "krb5_checksum_size" );
        (FARPROC) p_krb5_kt_free_entry =
                GetProcAddress( hKRB5_32, "krb5_kt_free_entry" );
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
	}

/* Stubs for each function to be dynamicly loaded */
void
kssl_krb5_free_data_contents(krb5_context CO, krb5_data  * data)
	{
	if (!krb5_loaded)
		load_krb5_dll();

	if ( p_krb5_free_data_contents )
		p_krb5_free_data_contents(CO,data);
	}

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)
	{
	if (!krb5_loaded)
		load_krb5_dll();

	if ( p_krb5_mk_req_extended )
		return(p_krb5_mk_req_extended(CO,pACO,F,pD1,pC,pD2));
	else
		return KRB5KRB_ERR_GENERIC;
	}
krb5_error_code
kssl_krb5_auth_con_init(krb5_context CO,
                       krb5_auth_context  * pACO)
	{
	if (!krb5_loaded)
		load_krb5_dll();

	if ( p_krb5_auth_con_init )
		return(p_krb5_auth_con_init(CO,pACO));
	else
		return KRB5KRB_ERR_GENERIC;
	}
krb5_error_code
kssl_krb5_auth_con_free (krb5_context CO,
                        krb5_auth_context ACO)
	{
	if (!krb5_loaded)
		load_krb5_dll();

	if ( p_krb5_auth_con_free )
		return(p_krb5_auth_con_free(CO,ACO));
	else
		return KRB5KRB_ERR_GENERIC;
	}
krb5_error_code
kssl_krb5_get_credentials(krb5_context CO,
                         krb5_const krb5_flags F,
                         krb5_ccache CC,
                         krb5_creds  * pCR,
                         krb5_creds  ** ppCR)
	{
	if (!krb5_loaded)
		load_krb5_dll();

	if ( p_krb5_get_credentials )
		return(p_krb5_get_credentials(CO,F,CC,pCR,ppCR));
	else
		return KRB5KRB_ERR_GENERIC;
	}
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)
	{
	if (!krb5_loaded)
		load_krb5_dll();

	if ( p_krb5_sname_to_principal )
		return(p_krb5_sname_to_principal(CO,pC1,pC2,I,pPR));
	else
		return KRB5KRB_ERR_GENERIC;
	}

krb5_error_code
kssl_krb5_cc_default(krb5_context CO,
                    krb5_ccache  * pCC)
	{
	if (!krb5_loaded)
		load_krb5_dll();

	if ( p_krb5_cc_default )
		return(p_krb5_cc_default(CO,pCC));
	else
		return KRB5KRB_ERR_GENERIC;
	}

krb5_error_code
kssl_krb5_init_context(krb5_context * pCO)
	{
	if (!krb5_loaded)
		load_krb5_dll();

	if ( p_krb5_init_context )
		return(p_krb5_init_context(pCO));
	else
		return KRB5KRB_ERR_GENERIC;
	}

void
kssl_krb5_free_context(krb5_context CO)
	{
	if (!krb5_loaded)
		load_krb5_dll();

	if ( p_krb5_free_context )
		p_krb5_free_context(CO);
	}

void
kssl_krb5_free_principal(krb5_context c, krb5_principal p)
	{
	if (!krb5_loaded)
		load_krb5_dll();

	if ( p_krb5_free_principal )
		p_krb5_free_principal(c,p);
	}

krb5_error_code
kssl_krb5_kt_resolve(krb5_context con,
                    krb5_const char * sz,
                    krb5_keytab * kt)
	{
	if (!krb5_loaded)
		load_krb5_dll();

	if ( p_krb5_kt_resolve )
		return(p_krb5_kt_resolve(con,sz,kt));
	else
		return KRB5KRB_ERR_GENERIC;
	}

krb5_error_code
kssl_krb5_kt_default(krb5_context con,
                    krb5_keytab * kt)
	{
	if (!krb5_loaded)
		load_krb5_dll();

	if ( p_krb5_kt_default )
		return(p_krb5_kt_default(con,kt));
	else
		return KRB5KRB_ERR_GENERIC;
	}

krb5_error_code
kssl_krb5_free_ticket(krb5_context con,
                     krb5_ticket * kt)
	{
	if (!krb5_loaded)
		load_krb5_dll();

	if ( p_krb5_free_ticket )
		return(p_krb5_free_ticket(con,kt));
	else
		return KRB5KRB_ERR_GENERIC;
	}

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)
	{
	if (!krb5_loaded)
		load_krb5_dll();

	if ( p_krb5_rd_req )
		return(p_krb5_rd_req(con,pacon,data,princ,keytab,flags,pptkt));
	else
		return KRB5KRB_ERR_GENERIC;
	}

482 483 484 485 486 487 488 489
krb5_boolean
krb5_principal_compare(krb5_context con, krb5_const_principal princ1,
                krb5_const_principal princ2)
	{
	if (!krb5_loaded)
		load_krb5_dll();

	if ( p_krb5_principal_compare )
490
		return(p_krb5_principal_compare(con,princ1,princ2));
491 492 493 494 495 496 497 498 499 500 501 502
	else
		return KRB5KRB_ERR_GENERIC;
	}

krb5_error_code
krb5_decrypt_tkt_part(krb5_context con, krb5_const krb5_keyblock *keys,
                krb5_ticket *ticket)
	{
	if (!krb5_loaded)
		load_krb5_dll();

	if ( p_krb5_decrypt_tkt_part )
503
		return(p_krb5_decrypt_tkt_part(con,keys,ticket));
504 505 506 507 508 509 510 511 512 513 514
	else
		return KRB5KRB_ERR_GENERIC;
	}

krb5_error_code
krb5_timeofday(krb5_context con, krb5_int32 *timeret)
	{
	if (!krb5_loaded)
		load_krb5_dll();

	if ( p_krb5_timeofday )
515
		return(p_krb5_timeofday(con,timeret));
516 517 518 519 520 521 522 523 524 525 526
	else
		return KRB5KRB_ERR_GENERIC;
	}

krb5_error_code
krb5_rc_default(krb5_context con, krb5_rcache *rc)
	{
	if (!krb5_loaded)
		load_krb5_dll();

	if ( p_krb5_rc_default )
527
		return(p_krb5_rc_default(con,rc));
528 529 530 531 532 533 534 535 536 537 538
	else
		return KRB5KRB_ERR_GENERIC;
	}

krb5_error_code
krb5_rc_initialize(krb5_context con, krb5_rcache rc, krb5_deltat lifespan)
	{
	if (!krb5_loaded)
		load_krb5_dll();

	if ( p_krb5_rc_initialize )
539
		return(p_krb5_rc_initialize(con, rc, lifespan));
540 541 542 543 544 545 546 547 548 549 550
	else
		return KRB5KRB_ERR_GENERIC;
	}

krb5_error_code
krb5_rc_get_lifespan(krb5_context con, krb5_rcache rc, krb5_deltat *lifespanp)
	{
	if (!krb5_loaded)
		load_krb5_dll();

	if ( p_krb5_rc_get_lifespan )
551
		return(p_krb5_rc_get_lifespan(con, rc, lifespanp));
552 553 554 555 556 557 558 559 560 561 562
	else
		return KRB5KRB_ERR_GENERIC;
	}

krb5_error_code
krb5_rc_destroy(krb5_context con, krb5_rcache rc)
	{
	if (!krb5_loaded)
		load_krb5_dll();

	if ( p_krb5_rc_destroy )
563
		return(p_krb5_rc_destroy(con, rc));
564 565 566 567
	else
		return KRB5KRB_ERR_GENERIC;
	}

568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603
size_t 
krb5_checksum_size(krb5_context context,krb5_cksumtype ctype)
        {
        if (!krb5_loaded)
                load_krb5_dll();

        if ( p_krb5_checksum_size )
                return(p_krb5_checksum_size(context, ctype));
        else
                return KRB5KRB_ERR_GENERIC;
        }

krb5_boolean 
valid_cksumtype(krb5_cksumtype ctype)
        {
        if (!krb5_loaded)
                load_krb5_dll();

        if ( p_valid_cksumtype )
                return(p_valid_cksumtype(ctype));
        else
                return KRB5KRB_ERR_GENERIC;
        }

krb5_error_code 
krb5_kt_free_entry(krb5_context con,krb5_keytab_entry * entry)
        {
        if (!krb5_loaded)
                load_krb5_dll();

        if ( p_krb5_kt_free_entry )
                return(p_krb5_kt_free_entry(con,entry));
        else
                return KRB5KRB_ERR_GENERIC;
        }
                 
604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623
/* Structure definitions  */
#ifndef NO_DEF_KRB5_CCACHE
#ifndef krb5_x
#define krb5_x(ptr,args) ((ptr)?((*(ptr)) args):(abort(),1))
#define krb5_xc(ptr,args) ((ptr)?((*(ptr)) args):(abort(),(char*)0))
#endif 

typedef	krb5_pointer	krb5_cc_cursor;	/* cursor for sequential lookup */

typedef struct _krb5_ccache
	{
	krb5_magic magic;
	struct _krb5_cc_ops FAR *ops;
	krb5_pointer data;
	} *krb5_ccache;

typedef struct _krb5_cc_ops
	{
	krb5_magic magic;
	char  *prefix;
624
	char  * (KRB5_CALLCONV *get_name)
625
		(krb5_context, krb5_ccache);
626
	krb5_error_code (KRB5_CALLCONV *resolve)
627
		(krb5_context, krb5_ccache  *, const char  *);
628
	krb5_error_code (KRB5_CALLCONV *gen_new)
629
		(krb5_context, krb5_ccache  *);
630
	krb5_error_code (KRB5_CALLCONV *init)
631
		(krb5_context, krb5_ccache, krb5_principal);
632
	krb5_error_code (KRB5_CALLCONV *destroy)
633
		(krb5_context, krb5_ccache);
634
	krb5_error_code (KRB5_CALLCONV *close)
635
		(krb5_context, krb5_ccache);
636
	krb5_error_code (KRB5_CALLCONV *store)
637
		(krb5_context, krb5_ccache, krb5_creds  *);
638
	krb5_error_code (KRB5_CALLCONV *retrieve)
639 640
		(krb5_context, krb5_ccache,
		krb5_flags, krb5_creds  *, krb5_creds  *);
641
	krb5_error_code (KRB5_CALLCONV *get_princ)
642
		(krb5_context, krb5_ccache, krb5_principal  *);
643
	krb5_error_code (KRB5_CALLCONV *get_first)
644
		(krb5_context, krb5_ccache, krb5_cc_cursor  *);
645
	krb5_error_code (KRB5_CALLCONV *get_next)
646 647
		(krb5_context, krb5_ccache,
		krb5_cc_cursor  *, krb5_creds  *);
648
	krb5_error_code (KRB5_CALLCONV *end_get)
649
		(krb5_context, krb5_ccache, krb5_cc_cursor  *);
650
	krb5_error_code (KRB5_CALLCONV *remove_cred)
651 652
		(krb5_context, krb5_ccache,
		krb5_flags, krb5_creds  *);
653
	krb5_error_code (KRB5_CALLCONV *set_flags)
654
		(krb5_context, krb5_ccache, krb5_flags);
655 656 657 658 659 660 661 662 663 664 665
	} krb5_cc_ops;
#endif /* NO_DEF_KRB5_CCACHE */

krb5_error_code 
kssl_krb5_cc_get_principal
    (krb5_context context, krb5_ccache cache,
      krb5_principal *principal)
	{
	if ( p_krb5_cc_get_principal )
		return(p_krb5_cc_get_principal(context,cache,principal));
	else
666 667
		return(krb5_x
			((cache)->ops->get_princ,(context, cache, principal)));
668
	}
669
#else
670
#endif  /* OPENSSL_SYS_WINDOWS || OPENSSL_SYS_WIN32 */
671

672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694
char
*kstring(char *string)
        {
        static char	*null = "[NULL]";

	return ((string == NULL)? null: string);
        }

#define	MAXKNUM	255
char
*knumber(int len, krb5_octet *contents)
        {
	static char	buf[MAXKNUM+1];
	int 		i;

	BIO_snprintf(buf, MAXKNUM, "[%d] ", len);

	for (i=0; i < len  &&  MAXKNUM > strlen(buf)+3; i++)
                {
                BIO_snprintf(&buf[strlen(buf)], 3, "%02x", contents[i]);
                }

	return (buf);
695
	}
696 697


698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759
/*	Given KRB5 enctype (basically DES or 3DES), return
*/
EVP_CIPHER *
kssl_map_enc(krb5_enctype enctype)
        {
	switch (enctype)
		{
	case ENCTYPE_DES_CBC_CRC:
	case ENCTYPE_DES_CBC_MD4:
	case ENCTYPE_DES_CBC_MD5:
	case ENCTYPE_DES_CBC_RAW:
#if ! defined(KRB5_MIT_OLD11)
	case ENCTYPE_DES_HMAC_SHA1:
#endif
				return (EVP_CIPHER *) EVP_des_cbc();
				break;
	case ENCTYPE_DES3_CBC_SHA:
	case ENCTYPE_DES3_CBC_RAW:
#if ! defined(KRB5_MIT_OLD11)
	case ENCTYPE_DES3_CBC_SHA1:
#endif
				return (EVP_CIPHER *) EVP_des_ede3_cbc();
				break;
	default:                return (EVP_CIPHER *) NULL;
				break;
		}
	}


/*	Return true:1 if p "looks like" the start of the real authenticator
**	described in kssl_skip_confound() below.  The ASN.1 pattern is
**	"62 xx 30 yy" (APPLICATION-2, SEQUENCE), where xx-yy =~ 2, and
**	xx and yy are possibly multi-byte length fields.
*/
int 	kssl_test_confound(unsigned char *p)
	{
	int 	len = 2;
	int 	xx = 0, yy = 0;

	if (*p++ != 0x62)  return 0;
	if (*p > 0x82)  return 0;
	switch(*p)  {
		case 0x82:  p++;          xx = (*p++ << 8);  xx += *p++;  break;
		case 0x81:  p++;          xx =  *p++;  break;
		case 0x80:  return 0;
		default:    xx = *p++;  break;
		}
	if (*p++ != 0x30)  return 0;
	if (*p > 0x82)  return 0;
	switch(*p)  {
		case 0x82:  p++; len+=2;  yy = (*p++ << 8);  yy += *p++;  break;
		case 0x81:  p++; len++;   yy =  *p++;  break;
		case 0x80:  return 0;
		default:    yy = *p++;  break;
		}

	return (xx - len == yy)? 1: 0;
	}

/*	Allocate, fill, and return cksumlens array of checksum lengths.
**	This array holds just the unique elements from the krb5_cksumarray[].
**	array[n] == 0 signals end of data.
760 761 762 763 764 765
**
**      The krb5_cksumarray[] was an internal variable that has since been
**      replaced by a more general method for storing the data.  It should
**      not be used.  Instead we use real API calls and make a guess for 
**      what the highest assigned CKSUMTYPE_ constant is.  As of 1.2.2
**      it is 0x000c (CKSUMTYPE_HMAC_SHA1_DES3).  So we will use 0x0010.
766 767 768
*/
int 	*populate_cksumlens(void)
	{
769 770
	int 		i, j, n = 0x0010+1;
	static size_t 	*cklens = NULL;
771 772

#ifdef KRB5CHECKAUTH
773
	if (!cklens && !(cklens = (size_t *) calloc(sizeof(int), n)))  return NULL;
774

775 776 777
	for (i=0; i < n; i++)  {
		if (!valid_cksumtype(i))  continue;	/*  array has holes  */
		for (j=0; j < n; j++)  {
778
			if (cklens[j] == 0)  {
779
				cklens[j] = krb5_checksum_size(NULL,i);
780 781
				break;		/*  krb5 elem was new: add   */
				}
782
			if (cklens[j] == krb5_checksum_size(NULL,i))  {
783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817
				break;		/*  ignore duplicate elements */
				}
			}
		}
#endif	/* KRB5CHECKAUTH */

	return cklens;
	}

/*	Return pointer to start of real authenticator within authenticator, or
**	return NULL on error.
**	Decrypted authenticator looks like this:
**		[0 or 8 byte confounder] [4-24 byte checksum] [real authent'r]
**	This hackery wouldn't be necessary if MIT KRB5 1.0.6 had the
**	krb5_auth_con_getcksumtype() function advertised in its krb5.h.
*/
unsigned char	*kssl_skip_confound(krb5_enctype etype, unsigned char *a)
	{
	int 		i, cklen, conlen;
	static int 	*cksumlens = NULL;
	unsigned char	*test_auth;

	conlen = (etype)? 8: 0;

	if (!cksumlens  &&  !(cksumlens = populate_cksumlens()))  return NULL;
	for (i=0; (cklen = cksumlens[i]) != 0; i++)
		{
		test_auth = a + conlen + cklen;
		if (kssl_test_confound(test_auth))  return test_auth;
		}

	return NULL;
	}


818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836
/*	Set kssl_err error info when reason text is a simple string
**		kssl_err = struct { int reason; char text[KSSL_ERR_MAX+1]; }
*/
void
kssl_err_set(KSSL_ERR *kssl_err, int reason, char *text)
        {
	if (kssl_err == NULL)  return;

	kssl_err->reason = reason;
	BIO_snprintf(kssl_err->text, KSSL_ERR_MAX, text);
	return;
        }


/*	Display contents of krb5_data struct, for debugging
*/
void
print_krb5_data(char *label, krb5_data *kdata)
        {
837
	unsigned int 	i;
838 839 840 841

	printf("%s[%d] ", label, kdata->length);
	for (i=0; i < kdata->length; i++)
                {
842
		if (0 &&  isprint((int) kdata->data[i]))
843 844
                        printf(	"%c ",  kdata->data[i]);
		else
845
                        printf(	"%02x ", (unsigned char) kdata->data[i]);
846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873
		}
	printf("\n");
        }


/*	Display contents of krb5_authdata struct, for debugging
*/
void
print_krb5_authdata(char *label, krb5_authdata **adata)
        {
	if (adata == NULL)
                {
		printf("%s, authdata==0\n", label);
		return;
		}
	printf("%s [%p]\n", label, adata);
#if 0
	{
        int 	i;
	printf("%s[at%d:%d] ", label, adata->ad_type, adata->length);
	for (i=0; i < adata->length; i++)
                {
                printf((isprint(adata->contents[i]))? "%c ": "%02x",
                        adata->contents[i]);
		}
	printf("\n");
	}
#endif
874
	}
875 876 877 878 879 880 881


/*	Display contents of krb5_keyblock struct, for debugging
*/
void
print_krb5_keyblock(char *label, krb5_keyblock *keyblk)
        {
882
	unsigned int 	i;
883 884 885 886 887 888

	if (keyblk == NULL)
                {
		printf("%s, keyblk==0\n", label);
		return;
		}
889
#ifdef KRB5_HEIMDAL
890 891
	printf("%s\n\t[et%d:%d]: ", label, keyblk->keytype,
					   keyblk->keyvalue->length);
892 893 894 895 896 897
	for (i=0; i < keyblk->keyvalue->length; i++)
                {
		printf("%02x",(unsigned char *)(keyblk->keyvalue->contents)[i]);
		}
	printf("\n");
#else
898 899 900 901 902 903
	printf("%s\n\t[et%d:%d]: ", label, keyblk->enctype, keyblk->length);
	for (i=0; i < keyblk->length; i++)
                {
		printf("%02x",keyblk->contents[i]);
		}
	printf("\n");
904
#endif
905 906 907
        }


908 909 910 911 912 913
/*	Display contents of krb5_principal_data struct, for debugging
**	(krb5_principal is typedef'd == krb5_principal_data *)
*/
void
print_krb5_princ(char *label, krb5_principal_data *princ)
        {
914 915
	unsigned int 	ui, uj;
        int i;
916 917 918

	printf("%s principal Realm: ", label);
	if (princ == NULL)  return;
919
	for (ui=0; ui < princ->realm.length; ui++)  putchar(princ->realm.data[ui]);
920 921 922 923
	printf(" (nametype %d) has %d strings:\n", princ->type,princ->length);
	for (i=0; i < princ->length; i++)
                {
		printf("\t%d [%d]: ", i, princ->data[i].length);
924 925
		for (uj=0; uj < princ->data[i].length; uj++)  {
			putchar(princ->data[i].data[uj]);
926 927 928 929 930 931 932
			}
		printf("\n");
		}
	return;
        }


933
/*	Given krb5 service (typically "kssl") and hostname in kssl_ctx,
934 935 936 937
**	Return encrypted Kerberos ticket for service @ hostname.
**	If authenp is non-NULL, also return encrypted authenticator,
**	whose data should be freed by caller.
**	(Originally was: Create Kerberos AP_REQ message for SSL Client.)
938
**
939 940 941
**	19990628	VRS 	Started; Returns Kerberos AP_REQ message.
**	20010409	VRS 	Modified for RFC2712; Returns enc tkt.
**	20010606	VRS 	May also return optional authenticator.
942 943 944
*/
krb5_error_code
kssl_cget_tkt(	/* UPDATE */	KSSL_CTX *kssl_ctx,
945 946 947
                /* OUT    */	krb5_data **enc_ticketp,
                /* UPDATE */	krb5_data *authenp,
                /* OUT    */	KSSL_ERR *kssl_err)
948
	{
949 950 951 952 953
	krb5_error_code		krb5rc = KRB5KRB_ERR_GENERIC;
	krb5_context		krb5context = NULL;
	krb5_auth_context	krb5auth_context = NULL;
	krb5_ccache 		krb5ccdef = NULL;
	krb5_creds		krb5creds, *krb5credsp = NULL;
954
	krb5_data		krb5_app_req;
955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987

	kssl_err_set(kssl_err, 0, "");
	memset((char *)&krb5creds, 0, sizeof(krb5creds));

	if (!kssl_ctx)
                {
		kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
                        "No kssl_ctx defined.\n");
		goto err;
		}
	else if (!kssl_ctx->service_host)
                {
		kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
                        "kssl_ctx service_host undefined.\n");
		goto err;
		}

	if ((krb5rc = krb5_init_context(&krb5context)) != 0)
                {
		BIO_snprintf(kssl_err->text,KSSL_ERR_MAX,
                        "krb5_init_context() fails: %d\n", krb5rc);
		kssl_err->reason = SSL_R_KRB5_C_INIT;
		goto err;
		}

	if ((krb5rc = krb5_sname_to_principal(krb5context,
                kssl_ctx->service_host,
                (kssl_ctx->service_name)? kssl_ctx->service_name: KRB5SVC,
                KRB5_NT_SRV_HST, &krb5creds.server)) != 0)
                {
		BIO_snprintf(kssl_err->text,KSSL_ERR_MAX,
                        "krb5_sname_to_principal() fails for %s/%s\n",
                        kssl_ctx->service_host,
988 989
                        (kssl_ctx->service_name)? kssl_ctx->service_name:
						  KRB5SVC);
990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016
		kssl_err->reason = SSL_R_KRB5_C_INIT;
		goto err;
		}

	if ((krb5rc = krb5_cc_default(krb5context, &krb5ccdef)) != 0)
                {
		kssl_err_set(kssl_err, SSL_R_KRB5_C_CC_PRINC,
                        "krb5_cc_default fails.\n");
		goto err;
		}

	if ((krb5rc = krb5_cc_get_principal(krb5context, krb5ccdef,
                &krb5creds.client)) != 0)
                {
		kssl_err_set(kssl_err, SSL_R_KRB5_C_CC_PRINC,
                        "krb5_cc_get_principal() fails.\n");
		goto err;
		}

	if ((krb5rc = krb5_get_credentials(krb5context, 0, krb5ccdef,
                &krb5creds, &krb5credsp)) != 0)
                {
		kssl_err_set(kssl_err, SSL_R_KRB5_C_GET_CRED,
                        "krb5_get_credentials() fails.\n");
		goto err;
		}

1017 1018 1019 1020 1021 1022
	*enc_ticketp = &krb5credsp->ticket;
#ifdef KRB5_HEIMDAL
	kssl_ctx->enctype = krb5credsp->session.keytype;
#else
	kssl_ctx->enctype = krb5credsp->keyblock.enctype;
#endif
1023 1024 1025

	krb5rc = KRB5KRB_ERR_GENERIC;
	/*	caller should free data of krb5_app_req  */
1026 1027 1028 1029 1030
	/*  20010406 VRS deleted for real KerberosWrapper
	**  20010605 VRS reinstated to offer Authenticator to KerberosWrapper
	*/
	krb5_app_req.length = 0;
	if (authenp)
1031
                {
1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065
		krb5_data	krb5in_data;
		unsigned char	*p;
		long		arlen;
		KRB5_APREQBODY	*ap_req;

		authenp->length = 0;
		krb5in_data.data = NULL;
		krb5in_data.length = 0;
		if ((krb5rc = krb5_mk_req_extended(krb5context,
			&krb5auth_context, 0, &krb5in_data, krb5credsp,
			&krb5_app_req)) != 0)
			{
			kssl_err_set(kssl_err, SSL_R_KRB5_C_MK_REQ,
				"krb5_mk_req_extended() fails.\n");
			goto err;
			}

		arlen = krb5_app_req.length;
		p = krb5_app_req.data;
		ap_req = (KRB5_APREQBODY *) d2i_KRB5_APREQ(NULL, &p, arlen);
		if (ap_req)
			{
			authenp->length = i2d_KRB5_ENCDATA(
					ap_req->authenticator, NULL);
			if (authenp->length  && 
				(authenp->data = malloc(authenp->length)))
				{
				unsigned char	*p = authenp->data;
				authenp->length = i2d_KRB5_ENCDATA(
						ap_req->authenticator, &p);
				}
			}

		if (ap_req)  KRB5_APREQ_free((KRB5_APREQ *) ap_req);
1066 1067
		if (krb5_app_req.length)  
                        krb5_free_data_contents(krb5context,&krb5_app_req);
1068
		}
1069
#ifdef KRB5_HEIMDAL
1070
	if (kssl_ctx_setkey(kssl_ctx, &krb5credsp->session))
1071 1072 1073 1074 1075
                {
		kssl_err_set(kssl_err, SSL_R_KRB5_C_INIT,
                        "kssl_ctx_setkey() fails.\n");
		}
#else
1076
	if (kssl_ctx_setkey(kssl_ctx, &krb5credsp->keyblock))
1077 1078 1079 1080
                {
		kssl_err_set(kssl_err, SSL_R_KRB5_C_INIT,
                        "kssl_ctx_setkey() fails.\n");
		}
1081
#endif
1082 1083 1084 1085 1086 1087 1088
	else	krb5rc = 0;

 err:
#ifdef KSSL_DEBUG
	kssl_ctx_show(kssl_ctx);
#endif	/* KSSL_DEBUG */

1089 1090 1091 1092 1093 1094
	if (krb5creds.client)	krb5_free_principal(krb5context,
							krb5creds.client);
	if (krb5creds.server)	krb5_free_principal(krb5context,
							krb5creds.server);
	if (krb5auth_context)	krb5_auth_con_free(krb5context,
							krb5auth_context);
1095 1096
	if (krb5context)	krb5_free_context(krb5context);
	return (krb5rc);
1097
	}
1098 1099


1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184
/*  Given d2i_-decoded asn1ticket, allocate and return a new krb5_ticket.
**  Return Kerberos error code and kssl_err struct on error.
**  Allocates krb5_ticket and krb5_principal; caller should free these.
**
**	20010410	VRS	Implemented krb5_decode_ticket() as
**				old_krb5_decode_ticket(). Missing from MIT1.0.6.
**	20010615	VRS 	Re-cast as openssl/asn1 d2i_*() functions.
**				Re-used some of the old krb5_decode_ticket()
**				code here.  This tkt should alloc/free just
**				like the real thing.
*/
krb5_error_code
kssl_TKT2tkt(	/* IN     */	krb5_context	krb5context,
		/* IN     */	KRB5_TKTBODY	*asn1ticket,
		/* OUT    */	krb5_ticket	**krb5ticket,
		/* OUT    */	KSSL_ERR *kssl_err  )
        {
        krb5_error_code			krb5rc = KRB5KRB_ERR_GENERIC;
	krb5_ticket 			*new5ticket = NULL;
	ASN1_GENERALSTRING		*gstr_svc, *gstr_host;

	*krb5ticket = NULL;

	if (asn1ticket == NULL  ||  asn1ticket->realm == NULL  ||
		asn1ticket->sname == NULL  || 
		asn1ticket->sname->namestring == NULL  ||
		asn1ticket->sname->namestring->num < 2)
		{
		BIO_snprintf(kssl_err->text, KSSL_ERR_MAX,
			"Null field in asn1ticket.\n");
		kssl_err->reason = SSL_R_KRB5_S_RD_REQ;
		return KRB5KRB_ERR_GENERIC;
		}

	if ((new5ticket = (krb5_ticket *) calloc(1, sizeof(krb5_ticket)))==NULL)
		{
		BIO_snprintf(kssl_err->text, KSSL_ERR_MAX,
			"Unable to allocate new krb5_ticket.\n");
		kssl_err->reason = SSL_R_KRB5_S_RD_REQ;
		return ENOMEM;		/*  or  KRB5KRB_ERR_GENERIC;	*/
		}

	gstr_svc  = (ASN1_GENERALSTRING*)asn1ticket->sname->namestring->data[0];
	gstr_host = (ASN1_GENERALSTRING*)asn1ticket->sname->namestring->data[1];

	if ((krb5rc = kssl_build_principal_2(krb5context,
			&new5ticket->server,
			asn1ticket->realm->length, asn1ticket->realm->data,
			gstr_svc->length,  gstr_svc->data,
			gstr_host->length, gstr_host->data)) != 0)
		{
		free(new5ticket);
		BIO_snprintf(kssl_err->text, KSSL_ERR_MAX,
			"Error building ticket server principal.\n");
		kssl_err->reason = SSL_R_KRB5_S_RD_REQ;
		return krb5rc;		/*  or  KRB5KRB_ERR_GENERIC;	*/
		}

	krb5_princ_type(krb5context, new5ticket->server) =
			asn1ticket->sname->nametype->data[0];
	new5ticket->enc_part.enctype = asn1ticket->encdata->etype->data[0];
	new5ticket->enc_part.kvno = asn1ticket->encdata->kvno->data[0];
	new5ticket->enc_part.ciphertext.length =
			asn1ticket->encdata->cipher->length;
	if ((new5ticket->enc_part.ciphertext.data =
		calloc(1, asn1ticket->encdata->cipher->length)) == NULL)
		{
		free(new5ticket);
		BIO_snprintf(kssl_err->text, KSSL_ERR_MAX,
			"Error allocating cipher in krb5ticket.\n");
		kssl_err->reason = SSL_R_KRB5_S_RD_REQ;
		return KRB5KRB_ERR_GENERIC;
		}
	else
		{
		memcpy(new5ticket->enc_part.ciphertext.data,
			asn1ticket->encdata->cipher->data,
			asn1ticket->encdata->cipher->length);
		}

	*krb5ticket = new5ticket;
	return 0;
	}


1185 1186 1187 1188 1189 1190 1191 1192
/*	Given krb5 service name in KSSL_CTX *kssl_ctx (typically "kssl"),
**		and krb5 AP_REQ message & message length,
**	Return Kerberos session key and client principle
**		to SSL Server in KSSL_CTX *kssl_ctx.
**
**	19990702	VRS 	Started.
*/
krb5_error_code
1193 1194 1195 1196
kssl_sget_tkt(	/* UPDATE */	KSSL_CTX		*kssl_ctx,
		/* IN     */	krb5_data		*indata,
		/* OUT    */	krb5_ticket_times	*ttimes,
		/* OUT    */	KSSL_ERR		*kssl_err  )
1197 1198 1199 1200 1201
        {
        krb5_error_code			krb5rc = KRB5KRB_ERR_GENERIC;
        static krb5_context		krb5context = NULL;
	static krb5_auth_context	krb5auth_context = NULL;
	krb5_ticket 			*krb5ticket = NULL;
1202 1203
	KRB5_TKTBODY 			*asn1ticket = NULL;
	unsigned char			*p;
1204
	krb5_keytab 			krb5keytab = NULL;
1205
	krb5_keytab_entry		kt_entry;
1206 1207 1208 1209 1210 1211
	krb5_principal			krb5server;

	kssl_err_set(kssl_err, 0, "");

	if (!kssl_ctx)
                {
1212 1213
		kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
					"No kssl_ctx defined.\n");
1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251
		goto err;
		}

#ifdef KSSL_DEBUG
	printf("in kssl_sget_tkt(%s)\n", kstring(kssl_ctx->service_name));
#endif	/* KSSL_DEBUG */

	if (!krb5context  &&  (krb5rc = krb5_init_context(&krb5context)))
                {
		kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
                        "krb5_init_context() fails.\n");
		goto err;
		}
	if (krb5auth_context  &&
		(krb5rc = krb5_auth_con_free(krb5context, krb5auth_context)))
                {
		kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
                        "krb5_auth_con_free() fails.\n");
		goto err;
		}
	else  krb5auth_context = NULL;
	if (!krb5auth_context  &&
		(krb5rc = krb5_auth_con_init(krb5context, &krb5auth_context)))
                {
		kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
                        "krb5_auth_con_init() fails.\n");
		goto err;
		}

	if ((krb5rc = krb5_sname_to_principal(krb5context, NULL,
                (kssl_ctx->service_name)? kssl_ctx->service_name: KRB5SVC,
                KRB5_NT_SRV_HST, &krb5server)) != 0)
                {
		kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
                        "krb5_sname_to_principal() fails.\n");
		goto err;
		}

1252
	/*	kssl_ctx->keytab_file == NULL ==> use Kerberos default
1253
	*/
1254 1255 1256 1257
	if (kssl_ctx->keytab_file)
		{
		krb5rc = krb5_kt_resolve(krb5context, kssl_ctx->keytab_file,
                        &krb5keytab);
R
Richard Levitte 已提交
1258
		if (krb5rc)
1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274
			{
			kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
				"krb5_kt_resolve() fails.\n");
			goto err;
			}
		}
	else
		{
                krb5rc = krb5_kt_default(krb5context,&krb5keytab);
                if (krb5rc)
			{
			kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT, 
				"krb5_kt_default() fails.\n");
			goto err;
			}
		}
1275 1276

	/*	Actual Kerberos5 krb5_recvauth() has initial conversation here
1277 1278
	**	o	check KRB5_SENDAUTH_BADAUTHVERS
	**		unless KRB5_RECVAUTH_SKIP_VERSION
1279 1280 1281 1282
	**	o	check KRB5_SENDAUTH_BADAPPLVERS
	**	o	send "0" msg if all OK
	*/

1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320
	/*  20010411 was using AP_REQ instead of true KerberosWrapper
	**
	**  if ((krb5rc = krb5_rd_req(krb5context, &krb5auth_context,
	**			&krb5in_data, krb5server, krb5keytab,
	**			&ap_option, &krb5ticket)) != 0)  { Error }
	*/

	p = indata->data;
	if ((asn1ticket = (KRB5_TKTBODY *) d2i_KRB5_TICKET(NULL, &p,
						(long) indata->length)) == NULL)
		{
		BIO_snprintf(kssl_err->text, KSSL_ERR_MAX,
			"d2i_KRB5_TICKET() ASN.1 decode failure.\n");
		kssl_err->reason = SSL_R_KRB5_S_RD_REQ;
		goto err;
		}
	
	/* Was:  krb5rc = krb5_decode_ticket(krb5in_data,&krb5ticket)) != 0) */
	if ((krb5rc = kssl_TKT2tkt(krb5context, asn1ticket, &krb5ticket,
					kssl_err)) != 0)
		{
		BIO_snprintf(kssl_err->text, KSSL_ERR_MAX,
			"Error converting ASN.1 ticket to krb5_ticket.\n");
		kssl_err->reason = SSL_R_KRB5_S_RD_REQ;
		goto err;
		}

	if (! krb5_principal_compare(krb5context, krb5server,
						  krb5ticket->server))  {
		krb5rc = KRB5_PRINC_NOMATCH;
		BIO_snprintf(kssl_err->text, KSSL_ERR_MAX,
			"server principal != ticket principal\n");
		kssl_err->reason = SSL_R_KRB5_S_RD_REQ;
		goto err;
		}
	if ((krb5rc = krb5_kt_get_entry(krb5context, krb5keytab,
			krb5ticket->server, krb5ticket->enc_part.kvno,
			krb5ticket->enc_part.enctype, &kt_entry)) != 0)  {
1321
		BIO_snprintf(kssl_err->text, KSSL_ERR_MAX,
1322
			"krb5_kt_get_entry() fails with %x.\n", krb5rc);
1323 1324 1325
		kssl_err->reason = SSL_R_KRB5_S_RD_REQ;
		goto err;
		}
1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359
	if ((krb5rc = krb5_decrypt_tkt_part(krb5context, &kt_entry.key,
			krb5ticket)) != 0)  {
		BIO_snprintf(kssl_err->text, KSSL_ERR_MAX,
			"krb5_decrypt_tkt_part() failed.\n");
		kssl_err->reason = SSL_R_KRB5_S_RD_REQ;
		goto err;
		}
	else  {
		krb5_kt_free_entry(krb5context, &kt_entry);
#ifdef KSSL_DEBUG
		{
		int i; krb5_address **paddr = krb5ticket->enc_part2->caddrs;
		printf("Decrypted ticket fields:\n");
		printf("\tflags: %X, transit-type: %X",
			krb5ticket->enc_part2->flags,
			krb5ticket->enc_part2->transited.tr_type);
		print_krb5_data("\ttransit-data: ",
			&(krb5ticket->enc_part2->transited.tr_contents));
		printf("\tcaddrs: %p, authdata: %p\n",
			krb5ticket->enc_part2->caddrs,
			krb5ticket->enc_part2->authorization_data);
		printf("\tcaddrs:\n");
		for (i=0; paddr[i] != NULL; i++)
			{  krb5_data d;
			d.length=paddr[i]->length; d.data=paddr[i]->contents;
			print_krb5_data("\t\tIP: ", &d);
			}
		printf("\tstart/auth/end times: %d / %d / %d\n",
			krb5ticket->enc_part2->times.starttime,
			krb5ticket->enc_part2->times.authtime,
			krb5ticket->enc_part2->times.endtime);
		}
#endif	/* KSSL_DEBUG */
		}
1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381

	krb5rc = KRB5_NO_TKT_SUPPLIED;
	if (!krb5ticket  ||	!krb5ticket->enc_part2  ||
                !krb5ticket->enc_part2->client  ||
                !krb5ticket->enc_part2->client->data  ||
                !krb5ticket->enc_part2->session)
                {
                kssl_err_set(kssl_err, SSL_R_KRB5_S_BAD_TICKET,
                        "bad ticket from krb5_rd_req.\n");
		}
	else if (kssl_ctx_setprinc(kssl_ctx, KSSL_CLIENT,
                &krb5ticket->enc_part2->client->realm,
                krb5ticket->enc_part2->client->data))
                {
		kssl_err_set(kssl_err, SSL_R_KRB5_S_BAD_TICKET,
                        "kssl_ctx_setprinc() fails.\n");
		}
	else if (kssl_ctx_setkey(kssl_ctx, krb5ticket->enc_part2->session))
                {
		kssl_err_set(kssl_err, SSL_R_KRB5_S_BAD_TICKET,
                        "kssl_ctx_setkey() fails.\n");
		}
1382 1383 1384 1385 1386 1387
	else if (krb5ticket->enc_part2->flags & TKT_FLG_INVALID)
                {
		krb5rc = KRB5KRB_AP_ERR_TKT_INVALID;
                kssl_err_set(kssl_err, SSL_R_KRB5_S_BAD_TICKET,
                        "invalid ticket from krb5_rd_req.\n");
		}
1388 1389
	else	krb5rc = 0;

1390 1391 1392 1393 1394 1395
	kssl_ctx->enctype	= krb5ticket->enc_part.enctype;
	ttimes->authtime	= krb5ticket->enc_part2->times.authtime;
	ttimes->starttime	= krb5ticket->enc_part2->times.starttime;
	ttimes->endtime 	= krb5ticket->enc_part2->times.endtime;
	ttimes->renew_till	= krb5ticket->enc_part2->times.renew_till;

1396 1397 1398 1399 1400
 err:
#ifdef KSSL_DEBUG
	kssl_ctx_show(kssl_ctx);
#endif	/* KSSL_DEBUG */

1401
	if (asn1ticket) 	KRB5_TICKET_free((KRB5_TICKET *) asn1ticket);
1402
        if (krb5keytab)         krb5_kt_close(krb5context, krb5keytab);
1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425
	if (krb5ticket) 	krb5_free_ticket(krb5context, krb5ticket);
	if (krb5server) 	krb5_free_principal(krb5context, krb5server);
	return (krb5rc);
        }


/*	Allocate & return a new kssl_ctx struct.
*/
KSSL_CTX	*
kssl_ctx_new(void)
        {
	return ((KSSL_CTX *) calloc(1, sizeof(KSSL_CTX)));
        }


/*	Frees a kssl_ctx struct and any allocated memory it holds.
**	Returns NULL.
*/
KSSL_CTX	*
kssl_ctx_free(KSSL_CTX *kssl_ctx)
        {
	if (kssl_ctx == NULL)  return kssl_ctx;

1426 1427
	if (kssl_ctx->key)  		memset(kssl_ctx->key, 0,
							      kssl_ctx->length);
1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460
	if (kssl_ctx->key)  		free(kssl_ctx->key);
	if (kssl_ctx->client_princ) 	free(kssl_ctx->client_princ);
	if (kssl_ctx->service_host) 	free(kssl_ctx->service_host);
	if (kssl_ctx->service_name) 	free(kssl_ctx->service_name);
	if (kssl_ctx->keytab_file) 	free(kssl_ctx->keytab_file);

	free(kssl_ctx);
	return (KSSL_CTX *) NULL;
        }


/*	Given a (krb5_data *) entity (and optional realm),
**	set the plain (char *) client_princ or service_host member
**	of the kssl_ctx struct.
*/
krb5_error_code
kssl_ctx_setprinc(KSSL_CTX *kssl_ctx, int which,
        krb5_data *realm, krb5_data *entity)
        {
	char	**princ;
	int 	length;

	if (kssl_ctx == NULL  ||  entity == NULL)  return KSSL_CTX_ERR;

	switch (which)
                {
        case KSSL_CLIENT:	princ = &kssl_ctx->client_princ;	break;
        case KSSL_SERVER:	princ = &kssl_ctx->service_host;	break;
        default:		return KSSL_CTX_ERR;			break;
		}
	if (*princ)  free(*princ);

	length = entity->length + ((realm)? realm->length + 2: 1);
1461 1462
	if ((*princ = calloc(1, length)) == NULL)
		return KSSL_CTX_ERR;
1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478
	else
                {
		strncpy(*princ, entity->data, entity->length);
		if (realm)
                        {
			strcat (*princ, "@");
			(void) strncat(*princ, realm->data, realm->length);
			}
		}

	return KSSL_CTX_OK;
        }


/*	Set one of the plain (char *) string members of the kssl_ctx struct.
**	Default values should be:
1479 1480
**		which == KSSL_SERVICE	=>	"khost" (KRB5SVC)
**		which == KSSL_KEYTAB	=>	"/etc/krb5.keytab" (KRB5KEYTAB)
1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504
*/
krb5_error_code
kssl_ctx_setstring(KSSL_CTX *kssl_ctx, int which, char *text)
        {
	char	**string;

	if (!kssl_ctx)  return KSSL_CTX_ERR;

	switch (which)
                {
        case KSSL_SERVICE:	string = &kssl_ctx->service_name;	break;
        case KSSL_SERVER:	string = &kssl_ctx->service_host;	break;
        case KSSL_CLIENT:	string = &kssl_ctx->client_princ;	break;
        case KSSL_KEYTAB:	string = &kssl_ctx->keytab_file;	break;
        default:		return KSSL_CTX_ERR;			break;
		}
	if (*string)  free(*string);

	if (!text)
                {
		*string = '\0';
		return KSSL_CTX_OK;
		}

1505 1506 1507 1508
	if ((*string = calloc(1, strlen(text) + 1)) == NULL)
		return KSSL_CTX_ERR;
	else
		strcpy(*string, text);
1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519

	return KSSL_CTX_OK;
        }


/*	Copy the Kerberos session key from a (krb5_keyblock *) to a kssl_ctx
**	struct.  Clear kssl_ctx->key if Kerberos session key is NULL.
*/
krb5_error_code
kssl_ctx_setkey(KSSL_CTX *kssl_ctx, krb5_keyblock *session)
        {
1520 1521 1522 1523
	int 		length;
	krb5_enctype	enctype;
	krb5_octet FAR	*contents = NULL;

1524 1525 1526 1527 1528 1529 1530 1531 1532 1533
	if (!kssl_ctx)  return KSSL_CTX_ERR;

	if (kssl_ctx->key)
                {
		memset(kssl_ctx->key, 0, kssl_ctx->length);
		free(kssl_ctx->key);
		}

	if (session)
                {
1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545

#ifdef KRB5_HEIMDAL
		length = session->keyvalue->length;
		enctype = session->keytype;
		contents = session->keyvalue->contents;
#else
		length = session->length;
		enctype = session->enctype;
		contents = session->contents;
#endif
		kssl_ctx->enctype = enctype;
		kssl_ctx->length  = length;
1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559
		}
	else
                {
		kssl_ctx->enctype = ENCTYPE_UNKNOWN;
		kssl_ctx->length  = 0;
		return KSSL_CTX_OK;
		}

	if ((kssl_ctx->key =
                (krb5_octet FAR *) calloc(1, kssl_ctx->length)) == NULL)
                {
		kssl_ctx->length  = 0;
		return KSSL_CTX_ERR;
		}
1560
	else
1561
		memcpy(kssl_ctx->key, contents, length);
1562 1563 1564 1565 1566 1567 1568 1569 1570 1571

	return KSSL_CTX_OK;
        }


/*	Display contents of kssl_ctx struct
*/
void
kssl_ctx_show(KSSL_CTX *kssl_ctx)
        {
1572
	unsigned int 	i;
1573 1574 1575 1576 1577 1578 1579

	printf("kssl_ctx: ");
	if (kssl_ctx == NULL)
                {
		printf("NULL\n");
		return;
		}
1580 1581
	else
		printf("%p\n", kssl_ctx);
1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601

	printf("\tservice:\t%s\n",
                (kssl_ctx->service_name)? kssl_ctx->service_name: "NULL");
	printf("\tclient:\t%s\n",
                (kssl_ctx->client_princ)? kssl_ctx->client_princ: "NULL");
	printf("\tserver:\t%s\n",
                (kssl_ctx->service_host)? kssl_ctx->service_host: "NULL");
	printf("\tkeytab:\t%s\n",
                (kssl_ctx->keytab_file)? kssl_ctx->keytab_file: "NULL");
	printf("\tkey [%d:%d]:\t",
                kssl_ctx->enctype, kssl_ctx->length);

	for (i=0; i < kssl_ctx->length  &&  kssl_ctx->key; i++)
                {
		printf("%02x", kssl_ctx->key[i]);
		}
	printf("\n");
	return;
        }

1602
#if !defined(OPENSSL_SYS_WINDOWS) && !defined(OPENSSL_SYS_WIN32)
1603 1604 1605 1606 1607
void kssl_krb5_free_data_contents(krb5_context context, krb5_data *data)
	{
#ifdef KRB5_HEIMDAL
	data->length = 0;
	free(data->if (data->data) data);
1608 1609 1610 1611 1612
#elif defined(KRB5_MIT_OLD11)
	if (data->data)  {
		krb5_xfree(data->data);
		data->data = 0;
		}
1613 1614 1615 1616
#else
	krb5_free_data_contents(NULL, data);
#endif
	}
1617
#endif /* !OPENSSL_SYS_WINDOWS && !OPENSSL_SYS_WIN32 */
1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797

/*  Helper function for kssl_validate_times().
**  We need context->clockskew, but krb5_context is an opaque struct.
**  So we try to sneek the clockskew out through the replay cache.
**	If that fails just return a likely default (300 seconds).
*/
krb5_deltat	get_rc_clockskew(krb5_context context)
	{
	krb5_rcache 	rc;
	krb5_deltat 	clockskew;

	if (krb5_rc_default(context, &rc))  return KSSL_CLOCKSKEW;
	if (krb5_rc_initialize(context, rc, 0))  return KSSL_CLOCKSKEW;
	if (krb5_rc_get_lifespan(context, rc, &clockskew))  {
		clockskew = KSSL_CLOCKSKEW;
		}
	(void) krb5_rc_destroy(context, rc);
	return clockskew;
	}


/*  kssl_validate_times() combines (and more importantly exposes)
**  the MIT KRB5 internal function krb5_validate_times() and the
**  in_clock_skew() macro.  The authenticator client time is checked
**  to be within clockskew secs of the current time and the current
**  time is checked to be within the ticket start and expire times.
**  Either check may be omitted by supplying a NULL value.
**  Returns 0 for valid times, SSL_R_KRB5* error codes otherwise.
**  See Also: (Kerberos source)/krb5/lib/krb5/krb/valid_times.c
**  20010420 VRS
*/
krb5_error_code  kssl_validate_times(	krb5_timestamp atime,
					krb5_ticket_times *ttimes)
	{
	krb5_deltat 	skew;
	krb5_timestamp	start, now;
	krb5_error_code	rc;
	krb5_context	context;

	if ((rc = krb5_init_context(&context)))	 return SSL_R_KRB5_S_BAD_TICKET;
	skew = get_rc_clockskew(context); 
	if ((rc = krb5_timeofday(context,&now))) return SSL_R_KRB5_S_BAD_TICKET;
	krb5_free_context(context);

	if (atime  &&  labs(atime - now) >= skew)  return SSL_R_KRB5_S_TKT_SKEW;

	if (! ttimes)  return 0;

	start = (ttimes->starttime != 0)? ttimes->starttime: ttimes->authtime;
	if (start - now > skew)  return SSL_R_KRB5_S_TKT_NYV;
	if ((now - ttimes->endtime) > skew)  return SSL_R_KRB5_S_TKT_EXPIRED;

#ifdef KSSL_DEBUG
	printf("kssl_validate_times: %d |<-  | %d - %d | < %d  ->| %d\n",
		start, atime, now, skew, ttimes->endtime);
#endif	/* KSSL_DEBUG */

	return 0;
	}


/*  Decode and decrypt given DER-encoded authenticator, then pass
**  authenticator ctime back in *atimep (or 0 if time unavailable).
**  Returns krb5_error_code and kssl_err on error.  A NULL 
**  authenticator (authentp->length == 0) is not considered an error.
**  Note that kssl_check_authent() makes use of the KRB5 session key;
**  you must call kssl_sget_tkt() to get the key before calling this routine.
*/
krb5_error_code  kssl_check_authent(
			/* IN     */	KSSL_CTX	*kssl_ctx,
                        /* IN     */   	krb5_data	*authentp,
			/* OUT    */	krb5_timestamp	*atimep,
			/* OUT    */    KSSL_ERR	*kssl_err  )
	{
        krb5_error_code		krb5rc = 0;
	KRB5_ENCDATA		*dec_authent = NULL;
	KRB5_AUTHENTBODY	*auth = NULL;
	krb5_enctype		enctype;
	EVP_CIPHER_CTX		ciph_ctx;
	EVP_CIPHER		*enc = NULL;
	unsigned char		iv[EVP_MAX_IV_LENGTH];
	unsigned char		*p, *unenc_authent, *tbuf = NULL;
	int 			padl, outl, unencbufsize;

	*atimep = 0;
	kssl_err_set(kssl_err, 0, "");

#ifndef KRB5CHECKAUTH
	authentp = NULL;
#else
#if	KRB5CHECKAUTH == 0
	authentp = NULL;
#endif
#endif	/* KRB5CHECKAUTH */

	if (authentp == NULL  ||  authentp->length == 0)  return 0;

#ifdef KSSL_DEBUG
	printf("kssl_check_authent: authenticator[%d]:\n",authentp->length);
	p = authentp->data; 
	for (padl=0; padl < authentp->length; padl++)  printf("%02x ",p[padl]);
	printf("\n");
#endif	/* KSSL_DEBUG */

	unencbufsize = 2 * authentp->length;
	if ((unenc_authent = calloc(1, unencbufsize)) == NULL)
		{
		kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
			"Unable to allocate authenticator buffer.\n");
		krb5rc = KRB5KRB_ERR_GENERIC;
		goto err;
		}

	p = authentp->data;
	if ((dec_authent = d2i_KRB5_ENCDATA(NULL, &p,
					(long) authentp->length)) == NULL) 
		{
		kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
                        "Error decoding authenticator.\n");
		krb5rc = KRB5KRB_AP_ERR_BAD_INTEGRITY;
		goto err;
		}

	enctype = dec_authent->etype->data[0];	/* should = kssl_ctx->enctype */
	enc = kssl_map_enc(enctype);
	memset(iv, 0, EVP_MAX_IV_LENGTH);       /* per RFC 1510 */

	EVP_DecryptInit(&ciph_ctx, enc, kssl_ctx->key, iv);
	EVP_DecryptUpdate(&ciph_ctx, unenc_authent, &outl,
			dec_authent->cipher->data, dec_authent->cipher->length);
	if (outl > unencbufsize)
		{
		kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
                        "Buffer overflow decrypting authenticator.\n");
		krb5rc = KRB5KRB_AP_ERR_BAD_INTEGRITY;
		goto err;
		}
	EVP_DecryptFinal(&ciph_ctx, &(unenc_authent[outl]), &padl);
	outl += padl;
	if (outl > unencbufsize)
		{
		kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
                        "Buffer overflow decrypting authenticator.\n");
		krb5rc = KRB5KRB_AP_ERR_BAD_INTEGRITY;
		goto err;
		}
	EVP_CIPHER_CTX_cleanup(&ciph_ctx);

#ifdef KSSL_DEBUG
	printf("kssl_check_authent: decrypted authenticator[%d] =\n", outl);
	for (padl=0; padl < outl; padl++) printf("%02x ",unenc_authent[padl]);
	printf("\n");
#endif	/* KSSL_DEBUG */

	if ((p = kssl_skip_confound(enctype, unenc_authent)) == NULL)
		{
		kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
                        "confounded by authenticator.\n");
		krb5rc = KRB5KRB_AP_ERR_BAD_INTEGRITY;
		goto err;
		}
	outl -= p - unenc_authent;

	if ((auth = (KRB5_AUTHENTBODY *) d2i_KRB5_AUTHENT(NULL, &p,
							  (long) outl))==NULL)
		{
		kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
                        "Error decoding authenticator body.\n");
		krb5rc = KRB5KRB_AP_ERR_BAD_INTEGRITY;
		goto err;
		}
	if ((tbuf = calloc(1, auth->ctime->length + 1)) == NULL)
		{
		kssl_err_set(kssl_err, SSL_R_KRB5_S_INIT,
			"Unable to allocate atime buffer.\n");
		krb5rc = KRB5KRB_ERR_GENERIC;
		goto err;
		}
	else	strncpy(tbuf, auth->ctime->data, auth->ctime->length);
	
1798 1799
	if ( auth->ctime->length >= 9 && auth->ctime->length <= 14  )  
		/* tbuf == "%Y%m%d%H%M%S" */
1800
		{
1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831
		struct tm		tm_time, *tm_l, *tm_g;
		time_t			now, tl, tg, tr, tz_offset;
		int                     i;
		char                    *p = tbuf;
 
		memset(&tm_time,0,sizeof(struct tm));
		for ( i=0; 
		      i<4 && isdigit(*p);
		      i++, p++ )
			tm_time.tm_year = tm_time.tm_year*10 + (*p-'0');
		for ( i=0; 
		      i<2 && isdigit(*p) && tm_time.tm_mon <= 1; 
		      i++, p++ )
			tm_time.tm_mon = tm_time.tm_mon*10 + (*p-'0');
		for ( i=0; 
		      i<2 && isdigit(*p) && tm_time.tm_mday <= 3;
		      i++, p++ )
			tm_time.tm_mday = tm_time.tm_mday*10 + (*p-'0');
		for ( i=0; 
		      i<2 && isdigit(*p) && tm_time.tm_hour <= 2;
		      i++, p++ )
			tm_time.tm_hour = tm_time.tm_hour*10 + (*p-'0');
		for ( i=0;
		      i<2 && isdigit(*p) && tm_time.tm_min <= 6;
		      i++, p++ )
			tm_time.tm_min = tm_time.tm_min*10 + (*p-'0');
		for ( i=0; 
		      i<2 && isdigit(*p) && tm_time.tm_sec <= 6;
		      i++, p++ )
			tm_time.tm_sec = tm_time.tm_sec*10 + (*p-'0');

1832 1833 1834 1835
		now  = time(&now);
		tm_l = localtime(&now); 	tl = mktime(tm_l);
		tm_g = gmtime(&now);		tg = mktime(tm_g);
		tz_offset = tg - tl;
1836
		tr = mktime(&tm_time);
1837

1838 1839
		if (tr != (time_t)(-1))
			*atimep = mktime(&tm_time) - tz_offset;
1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901
		}
#ifdef KSSL_DEBUG
	printf("kssl_check_authent: client time %s = %d\n", tbuf, *atimep);
#endif	/* KSSL_DEBUG */

 err:
	if (tbuf)		free(tbuf);
	if (auth)		KRB5_AUTHENT_free((KRB5_AUTHENT *) auth);
	if (dec_authent)	KRB5_ENCDATA_free(dec_authent);
	if (unenc_authent)	free(unenc_authent);
	return krb5rc;
	}


/*  Replaces krb5_build_principal_ext(), with varargs length == 2 (svc, host),
**  because I dont't know how to stub varargs.
**  Returns krb5_error_code == ENOMEM on alloc error, otherwise
**  passes back newly constructed principal, which should be freed by caller.
*/
krb5_error_code  kssl_build_principal_2(
			/* UPDATE */	krb5_context	context,
			/* OUT    */	krb5_principal	*princ,
			/* IN     */	int rlen,  const char *realm,
			/* IN	  */	int slen,  const char *svc,
			/* IN	  */	int hlen,  const char *host)
	{
	krb5_data		*p_data = NULL;
	krb5_principal		new_p = NULL;
        char			*new_r = NULL;

	if ((p_data = (krb5_data *) calloc(2, sizeof(krb5_data))) == NULL  ||
	    (new_p = (krb5_principal) calloc(1, sizeof(krb5_principal_data)))
			== NULL)  goto err;
	new_p->length = 2;
	new_p->data = p_data;

	if ((new_r = calloc(1, rlen + 1)) == NULL)  goto err;
	memcpy(new_r, realm, rlen);
	krb5_princ_set_realm_length(context, new_p, rlen);
	krb5_princ_set_realm_data(context, new_p, new_r);

	if ((new_p->data[0].data = calloc(1, slen + 1)) == NULL)  goto err;
	memcpy(new_p->data[0].data, svc, slen);
	new_p->data[0].length = slen;

	if ((new_p->data[1].data = calloc(1, hlen + 1)) == NULL)  goto err;
	memcpy(new_p->data[1].data, host, hlen);
	new_p->data[1].length = hlen;
	
	krb5_princ_type(context, new_p) = KRB5_NT_UNKNOWN;
	*princ = new_p;
	return 0;

 err:
	if (new_p  &&  new_p[0].data)	free(new_p[0].data);
	if (new_p  &&  new_p[1].data)	free(new_p[1].data);
	if (new_p)	free(new_p);
	if (new_r)	free(new_r);
	return ENOMEM;
	}


1902
#else /* !OPENSSL_NO_KRB5 */
B
Ben Laurie 已提交
1903

1904
#if defined(PEDANTIC) || defined(OPENSSL_SYS_VMS)
B
Ben Laurie 已提交
1905 1906 1907
static int dummy=(int)&dummy;
#endif

1908
#endif	/* !OPENSSL_NO_KRB5	*/
1909