summaryrefslogtreecommitdiffstats
path: root/src/lib/krb5/krb/preauth.c
blob: 66459761787b3de870ffc66974e026fd9bdddef9 (plain)
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
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
482
/*
 * Copyright 1990,1991 by the Massachusetts Institute of Technology.
 * All Rights Reserved.
 *
 * Export of this software from the United States of America may
 *   require a specific license from the United States Government.
 *   It is the responsibility of any person or organization contemplating
 *   export to obtain such a license before exporting.
 *
 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
 * distribute this software and its documentation for any purpose and
 * without fee is hereby granted, provided that the above copyright
 * notice appear in all copies and that both that copyright notice and
 * this permission notice appear in supporting documentation, and that
 * the name of M.I.T. not be used in advertising or publicity pertaining
 * to distribution of the software without specific, written prior
 * permission.  M.I.T. makes no representations about the suitability of
 * this software for any purpose.  It is provided "as is" without express
 * or implied warranty.
 *
 * Sandia National Laboratories also makes no representations about the 
 * suitability of the modifications, or additions to this software for 
 * any purpose.  It is provided "as is" without express or implied warranty.
 *
 * Note: The internal interfaces to this routine are subject to change
 * and/or cleanup.  You should only expect the interfaces to
 * krb5_obtain_padata and krb5_verify_padata to have some chance of
 * staying stable.  [tytso:19920903.1544EDT]
 */

/*
 * This file contains routines for establishing, verifying, and any other
 * necessary functions, for utilizing the pre-authentication field of the 
 * kerberos kdc request, with various hardware/software verification devices.
 *
 * Note: At some point these functions may very well be split apart
 * into different files.... [tytso:19920903.1618EDT]
 */

#include "k5-int.h"
#include <stdio.h>
#include <time.h>
#include <syslog.h>
#ifdef _MSDOS
#define getpid _getpid
#include <process.h>
#endif

static krb5_preauth_ops preauth_systems[] = {
    {
	0,
        KRB5_PADATA_ENC_UNIX_TIME,
        KRB5_PREAUTH_FLAGS_ENCRYPT,
        get_unixtime_padata,
        verify_unixtime_padata,
    },
    {
	0,
	KRB5_PADATA_ENC_SANDIA_SECURID,
	KRB5_PREAUTH_FLAGS_ENCRYPT | KRB5_PREAUTH_FLAGS_HARDWARE,
	get_securid_padata,
	verify_securid_padata,
    },
    { -1,}
};

static krb5_error_code find_preauthenticator
    PROTOTYPE((int type, krb5_preauth_ops **Preauth_proc));

/*
 *   krb5_obtain_padata  is a glue routine which when passed in
 *   a preauthentication type, client principal, and src_addr, returns
 *   preauthentication data contained in data to be passed onto the KDC.
 *   
 *   If problems occur then a non zero value is returned...
 *
 *   Note: This is a first crack at what any preauthentication will need...
 */
krb5_error_code
krb5_obtain_padata(context, type, client, src_addr, encrypt_key, ret_data)
    krb5_context context;
    int type;			 	/*IN:  Preauth type */
    krb5_principal client;		/*IN:  requestor */
    krb5_address **src_addr;            /*IN:  array of ptrs to addresses */
    krb5_keyblock *encrypt_key;		/*IN:  encryption key */
    krb5_pa_data **ret_data;			/*OUT: Returned padata */
{
    krb5_error_code	retval;
    krb5_preauth_ops	*p_system;
    krb5_encrypt_block	eblock;
    krb5_data		scratch;
    krb5_pa_data	*data;

    if (!ret_data)
	return EINVAL;
    *ret_data = 0;
    
    if (type == KRB5_PADATA_NONE ) 
	return(0);

    data = (krb5_pa_data *) malloc(sizeof(krb5_pa_data));
    if (!data)
	return ENOMEM;
    
    data->length = 0;
    data->contents = 0;
    data->pa_type = type;

    /* Find appropriate preauthenticator */
    retval = find_preauthenticator(type, &p_system);
    if (retval)
	goto error_out;

    retval = (*p_system->obtain)(context, client, src_addr, data );
    if (retval)
	goto error_out;

    /* Check to see if we need to encrypt padata */
    if (p_system->flags & KRB5_PREAUTH_FLAGS_ENCRYPT) {
	/* If we dont have a encryption key we are out of luck */
	if (!encrypt_key) {
	    retval = KRB5_PREAUTH_NO_KEY;
	    goto error_out;
	}
        krb5_use_keytype(context, &eblock, encrypt_key->keytype);

	/* do any necessay key pre-processing */
	retval = krb5_process_key(context, &eblock, encrypt_key);
	if (retval)
	    goto error_out;
	
	/*
	 * Set up scratch data and length for encryption 
	 * Must allocate more space for checksum and confounder
	 * We also leave space for an uncrypted size field.
	 */
	scratch.length = krb5_encrypt_size(data->length,
					   eblock.crypto_entry) + 4;
	
	if(!(scratch.data = malloc(scratch.length))){
	    (void) krb5_finish_key(context, &eblock);
	    retval = ENOMEM;
	    goto error_out;
	}
	
	scratch.data[0] = data->length >> 24;
	scratch.data[1] = data->length >> 16;
	scratch.data[2] = data->length >> 8;
	scratch.data[3] = data->length;

	/* Encrypt preauth data in encryption key */
	if ((retval = krb5_encrypt(context, (krb5_pointer) data->contents,
				   (char *) scratch.data + 4,
				   data->length, &eblock, 0))) {
	    (void) krb5_finish_key(context, &eblock);
	    free(scratch.data);
	    goto error_out;
	}
	(void) krb5_finish_key(context, &eblock);
	    
	free(data->contents);
	data->length = scratch.length;
	data->contents = (unsigned char *) scratch.data;
    }

    *ret_data = data;
    return 0;
    
error_out:
    free(data);
    return retval;
}

/*
 *   krb5_verify_padata  is a glue routine which when passed in
 *   the client, src_addr and padata verifies it with the appropriate 
 *   verify function.
 *  
 *   If problems occur then a non zero value is returned...
 *   else returns zero if padata verifies, and returns a "unique" id.
 *
 *   Note: This is a first crack at what any preauthentication will need...
 */

krb5_error_code
krb5_verify_padata(context, data,client,src_addr, decrypt_key, req_id, flags)
    krb5_context context;
    krb5_pa_data *data;                 /*IN: padata */
    krb5_principal client;              /*IN: requestor */
    krb5_address **src_addr;            /*IN: array of ptrs to addresses */
    krb5_keyblock *decrypt_key;		/*IN: decryption key */
    int * req_id;			/*OUT: identifier */
    int * flags;			/*OUT: flags  */
{
    krb5_preauth_ops	*p_system;
    krb5_encrypt_block 	eblock;
    krb5_data		scratch;
    int 		free_scratch = 0;
    krb5_checksum	cksum;
    krb5_error_code	retval;

    if (!data)
	return(EINVAL);

    /* Find appropriate preauthenticator */
    retval = find_preauthenticator((int) data->pa_type, &p_system);
    if (retval)
	return retval;

    /* Check to see if we need to decrypt padata */
    if (p_system->flags & KRB5_PREAUTH_FLAGS_ENCRYPT) {

	/* If we dont have a decryption key we are out of luck */
	if (!decrypt_key)
	    return(EINVAL);

        krb5_use_keytype(context, &eblock, decrypt_key->keytype);

        scratch.length = data->length;
        if (!(scratch.data = (char *)malloc(scratch.length))) {
           return(ENOMEM);
        }

	/* do any necessay key pre-processing */
	retval = krb5_process_key(context, &eblock,decrypt_key);
	if (retval) {
           free(scratch.data);
           return(retval);
        }

	/* Decrypt data */
	retval = krb5_decrypt(context, (char *) data->contents + 4,
			      (krb5_pointer) scratch.data,
			      scratch.length - 4, &eblock, 0);
	if (retval) {
           (void) krb5_finish_key(context, &eblock);
           free(scratch.data);
           return(retval);
	}

	scratch.length  = (((int) ((unsigned char *)data->contents)[0] << 24)
			   + ((int) ((unsigned char *)data->contents)[1] << 16)
			   + ((int) ((unsigned char *)data->contents)[2] << 8)
			   + (int) ((unsigned char *)data->contents)[3]);
	free_scratch++;
    } else {
	scratch.data = (char *) data->contents;
	scratch.length = data->length;
    }

    retval = (*p_system->verify)(context, client, src_addr, &scratch);
    if (free_scratch)
	free(scratch.data);
    if (retval)
	return retval;
    if (flags)
	*flags = p_system->flags;

    /* Generate a request id by crc32ing the (encrypted) preauth data. */
    /* Note: The idea behind req_id is that it is dependant upon
             the information in data. This could then be used for
             replay detection. */
    /* MUST malloc cksum.contents */
    cksum.contents = (krb5_octet *)calloc(1,
				krb5_checksum_size(context, CKSUMTYPE_CRC32));
    if (!cksum.contents) return(1);

    if (krb5_calculate_checksum(context, CKSUMTYPE_CRC32,
			data->contents,
			data->length,
                        0, /* seed is ignored */
                        0, /* seed length is ignored */
                        &cksum )) {
        *req_id = 0;
    } else {
        /* Checksum length should be 32 bits, so truncation should never
           take place */
        if ( cksum.length > sizeof(*req_id)) cksum.length = sizeof(*req_id);

        /* Offset req_id for 64 bit systems */
        memcpy((char *)req_id + (sizeof(*req_id) - cksum.length),
                cksum.contents,cksum.length);
    } 
    free(cksum.contents);
    return(0);
}

static krb5_error_code
find_preauthenticator(type, preauth)
    int			type;
    krb5_preauth_ops	**preauth;
{
    krb5_preauth_ops *ap = preauth_systems;
    
    while ((ap->type != -1) && (ap->type != type))
	ap++;
    if (ap->type == -1)
	return(KRB5_PREAUTH_BAD_TYPE);
    *preauth = ap;
    return 0;
} 

/*
 * Format is:   8 bytes of random confounder,
 *              1 byte version number (currently 0),
 *              4 bytes: number of seconds since Jan 1, 1970, in MSB order.
 */
int seeded = 0 ; /* Used by srand below */

krb5_error_code
get_unixtime_padata(context, client, src_addr, pa_data)
    krb5_context context;
    krb5_principal client;
    krb5_address **src_addr;
    krb5_pa_data *pa_data;
{
    unsigned char *tmp;
    krb5_error_code     retval;
    krb5_timestamp kdc_time;
    int         i;

    pa_data->length = 13;
    tmp = pa_data->contents = (unsigned char *) malloc(pa_data->length);
    if (!tmp) 
        return(ENOMEM);

    retval = krb5_timeofday(context, &kdc_time);
    if (retval)
        return retval;
    if ( !seeded) {
	seeded = (int) kdc_time + getpid();
	srand(seeded);
    }

    for (i=0; i < 8; i++)
        *tmp++ = rand() & 255;

    *tmp++ = (unsigned char) 0;
    *tmp++ = (unsigned char) ((kdc_time >> 24) & 255);
    *tmp++ = (unsigned char) ((kdc_time >> 16) & 255);
    *tmp++ = (unsigned char) ((kdc_time >> 8) & 255);
    *tmp++ = (unsigned char) (kdc_time & 255);

    return(0);
}

krb5_error_code
verify_unixtime_padata(context, client, src_addr, data)
    krb5_context context;
    krb5_principal client;
    krb5_address **src_addr;
    krb5_data *data;
{
    unsigned char       *tmp;
    krb5_error_code     retval;
    krb5_timestamp      currenttime, patime;
    extern krb5_deltat  krb5_clockskew;
#define in_clock_skew(date) (labs((date)-currenttime) < krb5_clockskew)

    tmp = (unsigned char *) data->data;
    if (tmp[8] != 0)
        return KRB5_PREAUTH_FAILED;
    patime = (int) tmp[9] << 24;
    patime += (int) tmp[10] << 16;
    patime += (int) tmp[11] << 8;
    patime += tmp[12];

    retval = krb5_timeofday(context, &currenttime);
    if (retval)
        return retval;

    if (!in_clock_skew(patime))
        return KRB5_PREAUTH_FAILED;

    return 0;
}

#ifdef KRBCONF_SECUREID
#include "sdcli.h"
#include "sdconf.c"

krb5_error_code
verify_securid_padata(client, src_addr, data)
    krb5_principal client;
    krb5_address **src_addr;
    krb5_data *data;
{
   extern perform_hw;

   if (perform_hw) {
        krb5_error_code	retval;
        char username[255];
        struct SD_CLIENT sd;

	memset((char *)&sd,0, sizeof (sd));
   	memset((char *) username, 0, sizeof(username));
        memcpy((char *) username, krb5_princ_component(context, client,0)->data,
                        	  krb5_princ_component(context, client,0)->length);
        /* If Instance then Append */
 	if (krb5_princ_size(context, client) > 1 ) {
	    if (strncmp(krb5_princ_realm(context, client)->data,
			krb5_princ_component(context, client,1)->data,
			krb5_princ_component(context, client,1)->length) ||
		        krb5_princ_realm(context, client)->length != 
			krb5_princ_component(context, client,1)->length) {
		strncat(username,"/",1);
		strncat(username,krb5_princ_component(context, client,1)->data,
				 krb5_princ_component(context, client,1)->length);
	    }
	}
        if (retval = sd_check(data->data,username,&sd) != ACM_OK) {
		syslog(LOG_INFO, 
		    "%s - Invalid Securid Authentication Data sd_check Code %d",
			username, retval);
		return(KRB5_PREAUTH_FAILED);
	}
	return(0);
    } else {
        char *username = 0;

	krb5_unparse_name(context, client,&username);
	syslog(LOG_INFO, 
	    "%s Provided Securid but this KDC does not support Securid",
		username);
	free(username);
	return(KRB5_PREAUTH_FAILED);
    }
}
#else
krb5_error_code
verify_securid_padata(context, client, src_addr, data)
    krb5_context context;
    krb5_principal client;
    krb5_address **src_addr;
    krb5_data *data;
{
 char *username = 0;
	krb5_unparse_name(context, client,&username);
	syslog(LOG_INFO, 
	    "%s Provided Securid but this KDC does not support Securid",
		username);
	free(username);
	return(KRB5_PREAUTH_FAILED);
}

#endif


/*
static char *krb5_SecureId_prompt = "\nEnter Your SecurId Access Code Prepended with Your PIN\n (or a \'#\'if Your PIN is entered on the card keypad)\n or Type return <CR> if You Do NOT Use a SecurId Card: ";
 */
static char *krb5_SecureId_prompt = "\nEnter Your SecurId Access Code Prepended with Your PIN\n (or a \'#\'if Your PIN is entered on the card keypad): ";

krb5_error_code
get_securid_padata(context, client,src_addr,pa_data)
    krb5_context context;
    krb5_principal client;
    krb5_address **src_addr;
    krb5_pa_data *pa_data;
{

 char temp[MAX_PREAUTH_SIZE];   
 int tempsize;
 int retval = 0;

    tempsize = sizeof(temp) - 1;
    if (krb5_read_password(context, krb5_SecureId_prompt, 0, temp, &tempsize))
        return(KRB5_PARSE_ILLCHAR);
    temp[tempsize] = '\0';

    if (temp[0] == '\0') 
	return(KRB5_PARSE_ILLCHAR);
    pa_data->length = strlen(temp) + 1;
    pa_data->contents = (krb5_octet *) calloc(1,pa_data->length);
    if (pa_data->contents) {
        memcpy(pa_data->contents,temp,pa_data->length);
	retval = 0;
    }
    else retval = ENOMEM;
    memset(temp,0,pa_data->length);
    return(retval);
}