-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathticket2cert.c
667 lines (617 loc) · 17.3 KB
/
ticket2cert.c
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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
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
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
/* ticket2cert.c -- Read the current TGT and produce an X.509 cert for it.
*
* From: Rick van Rein <[email protected]>
*/
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <time.h>
#include <asn1buf.h>
#include <krbasn1.h>
#include <krb5.h>
#include <openssl/sha.h>
/* Unkeyed SHA1 is in the IANA registry, but maybe not yet in MIT krb5 */
/* TODO: There should be more secure unkeyed hashes for this application */
/* TODO: Hashes shouldn't be hard-coded of course */
#ifndef CKSUMTYPE_SHA1
#define CKSUMTYPE_SHA1 14
#endif
/* OID definitions */
//TODO// decoding to wrong OID values
const uint8_t OID_KEY_TICKET [] = {
// 1.3.6.1.4.1.44469.666.509.88.1.1.1
0x2B, 0x06, 0x01, 0x04, 0x01, 0x82, 0xDB, 0x35, 0x85, 0x1A,
0x83, 0x7D, 0x58, 0x01, 0x01, 0x01
};
#define OID_KEY_TICKET_LEN sizeof (OID_KEY_TICKET)
const uint8_t OID_SIG_AUTHENTICATOR_SHA1 [] = {
// 1.3.6.1.4.1.44469.666.509.88.1.1.2.1.3.14.3.2.26
0x2B, 0x06, 0x01, 0x04, 0x01, 0x82, 0xDB, 0x35, 0x85, 0x1A,
0x83, 0x7D, 0x58, 0x01, 0x01, 0x02,
0x01, 0x03, 0x0e, 0x03, 0x02, 0x1a
};
#define OID_SIG_AUTHENTICATOR_SHA1_LEN sizeof (OID_SIG_AUTHENTICATOR_SHA1)
/* Find a ticket in the credentials cache
*/
krb5_error_code find_ticket (krb5_context ctx, krb5_ccache cache, char *servicename, krb5_creds **ticket) {
krb5_error_code kerrno = 0;
krb5_principal cli;
int have_cli = 0;
krb5_principal svc;
int have_svc = 0;
krb5_creds match;
int have_ticket = 0;
int i;
//
// Get the default principal describing the client
if (!kerrno) {
kerrno = krb5_cc_get_principal (ctx, cache, &cli);
have_cli = (kerrno == 0);
}
//
// Possibly build a default principal for the service
if (!kerrno) {
if (servicename != NULL) {
kerrno = krb5_parse_name (ctx, servicename, &svc);
} else {
kerrno = krb5_build_principal_ext (ctx, &svc,
cli->realm.length, cli->realm.data,
6, "krbtgt",
cli->realm.length, cli->realm.data,
0 /* end marker */ );
}
have_svc = (kerrno == 0);
}
//
// Retrieve a ticket for (cli,svc)
if (!kerrno) {
memset (&match, 0, sizeof (match));
match.magic = 0; /*TODO*/
match.client = cli;
match.server = svc;
// kerrno = krb5_cc_retrieve_cred (ctx, cache, 0, &match, ticket);
kerrno = krb5_get_credentials (ctx, 0, cache, &match, ticket);
have_ticket = (kerrno == 0);
}
if (!kerrno) {
//TODO// kerrno = krb5int_validate_times (ctx, (*ticket)->times);
//TODO// use KRB5_TC_MATCH_TIMES to enforce match's lifetimes
}
//
// Print ticket descriptive information
if (have_cli) {
printf ("-----BEGIN CLIENT PRINCIPAL-----\n");
printf ("NameType: %d\n", cli->type);
for (i=0; i<cli->length; i++) {
printf ("Name_%d: %.*s\n", i, cli->data [i].length, cli->data [i].data);
}
printf ("Realm: %.*s\n", svc->realm.length, svc->realm.data);
printf ("-----END CLIENT PRINCIPAL-----\n");
}
printf ("\n");
if (have_svc) {
printf ("-----BEGIN SERVICE PRINCIPAL-----\n");
printf ("NameType: %d\n", svc->type);
for (i=0; i<svc->length; i++) {
printf ("Name_%d: %.*s\n", i, svc->data [i].length, svc->data [i].data);
}
printf ("Realm: %.*s\n", svc->realm.length, svc->realm.data);
printf ("-----END SERVICE PRINCIPAL-----\n");
}
printf ("\n");
if (have_ticket) {
printf ("-----BEGIN TICKET HEXDUMP-----\n");
i = 0;
while (i < (*ticket)->ticket.length) {
char sep = (((i & 15) != 15) && (i != (*ticket)->ticket.length - 1))? ' ': '\n';
printf ("%02x%c", (uint8_t) (*ticket)->ticket.data [i++], sep);
}
printf ("-----END TICKET HEXDUMP-----\n");
}
//
// Cleanup
if (kerrno && have_ticket) {
krb5_free_cred_contents (ctx, *ticket);
*ticket = NULL;
have_ticket = 0;
}
if (have_svc) {
krb5_free_principal (ctx, svc);
have_svc = 0;
}
if (have_cli) {
krb5_free_principal (ctx, cli);
have_cli = 0;
}
//
// Return the overall result
return kerrno;
}
/* Construct an Authenticator object with the given checksum
*/
krb5_error_code construct_enc_authenticator (krb5_context ctx, krb5_creds *ticket, krb5_checksum *csum_opt, krb5_enc_data *enc_authenticator) {
krb5_error_code kerrno = 0;
krb5_authenticator auth;
krb5_data *plain_asn1 = NULL;
krb5_enc_data *enc_asn1 = NULL;
int i;
//
// Fill the authenticator fields
memset (&auth, 0, sizeof (auth));
auth.magic = 0; /*TODO*/
auth.client = ticket->client;
if (csum_opt) {
auth.checksum = csum_opt;
}
if (!kerrno) {
kerrno = krb5_us_timeofday (ctx, &auth.ctime, &auth.cusec);
}
//
// Represent the authenticator in ASN.1 / DER
if (!kerrno) {
plain_asn1 = malloc (sizeof (*plain_asn1));
if (plain_asn1 == NULL) {
perror ("Allocating plaintext authenticator structure");
//TODO// Setup as kerrno
exit (1);
}
}
if (!kerrno) {
memset (plain_asn1, 0, sizeof (plain_asn1));
plain_asn1->magic = 0; /*TODO*/
kerrno = encode_krb5_authenticator (&auth, &plain_asn1);
}
//
// Encrypt the authenticator
//TODO// Build up to EncryptedData?
if (!kerrno) {
enc_asn1 = malloc (sizeof (*enc_asn1));
if (enc_asn1 == NULL) {
perror ("Allocating encrypted authenticator structure");
//TODO// Setup as kerrno value
exit (1);
}
}
if (!kerrno) {
size_t enclen = 0;
enc_asn1->magic = 0; /*TODO*/
kerrno = krb5_c_encrypt_length (ctx, ticket->keyblock.enctype, plain_asn1->length, &enclen);
enc_asn1->ciphertext.length = enclen;
if (!kerrno) {
enc_asn1->ciphertext.data = malloc (enc_asn1->ciphertext.length);
if (!enc_asn1->ciphertext.data) {
perror ("Allocating encrypted authenticator block");
//TODO// Setup as kerrno value
exit (1);
}
}
}
if (!kerrno) {
kerrno = krb5_c_encrypt (ctx, &ticket->keyblock, KRB5_KEYUSAGE_AP_REQ_AUTH /*TODO:STOLEN11:OR10ADDS:_CKSUM*/, NULL, plain_asn1, enc_asn1);
}
//
// Print representations
printf ("\n");
if (plain_asn1) {
printf ("-----BEGIN AUTHENTICATOR HEXDUMP-----\n");
i = 0;
while (i < plain_asn1->length) {
char sep = (((i & 15) != 15) && (i != plain_asn1->length - 1))? ' ': '\n';
printf ("%02x%c", (uint8_t) plain_asn1->data [i++], sep);
}
printf ("-----END AUTHENTICATOR HEXDUMP-----\n");
}
printf ("\n");
if (enc_asn1) {
printf ("-----BEGIN ENCAUTHENTICATOR HEXDUMP-----\n");
i = 0;
while (i < enc_asn1->ciphertext.length) {
char sep = (((i & 15) != 15) && (i != enc_asn1->ciphertext.length - 1))? ' ': '\n';
printf ("%02x%c", (uint8_t) enc_asn1->ciphertext.data [i++], sep);
}
printf ("-----END ENCAUTHENTICATOR HEXDUMP-----\n");
}
//
// Cleanup
if (plain_asn1->data) {
free (plain_asn1->data);
plain_asn1->length = 0;
}
if (kerrno && enc_asn1 && enc_asn1->ciphertext.data) {
free (enc_asn1->ciphertext.data);
enc_asn1->ciphertext.data = NULL;
enc_asn1->ciphertext.length = 0;
}
if (kerrno && enc_asn1) {
free (enc_asn1);
enc_asn1 = NULL;
}
//
// Return the overall result
if (!kerrno) {
*enc_authenticator = *enc_asn1;
free (enc_asn1);
}
return kerrno;
}
asn1_error_code encode_x509 (krb5_context ctx, struct timeval *timestamp_opt, krb5_creds *ticket, krb5_data **x509) {
asn1_error_code aerrno = 0;
asn1buf *tbsbuf;
int have_tbsbuf = 0;
krb5_data *tbscert;
int have_tbscert = 0;
asn1buf *x509buf;
int have_x509buf = 0;
unsigned int certlen = 0, tbslen = 0, sublen, seqlen, seq2len;
time_t sigtime;
int i;
krb5_data *encauthbits;
int have_encauthbits = 0;
uint8_t *tbshash = NULL;
int have_tbshash = 0;
krb5_enc_data enc_authenticator;
int have_enc_authenticator = 0;
krb5_checksum checksum;
//
// Initialise working context
if (!aerrno) {
aerrno = asn1buf_create (&x509buf);
have_x509buf = (aerrno == 0);
}
if (!aerrno) {
aerrno = asn1buf_create (&tbsbuf);
have_tbsbuf = (aerrno == 0);
}
if (timestamp_opt) {
sigtime = timestamp_opt->tv_usec;
} else {
if (time (&sigtime) == (time_t) -1) {
perror ("Failed to read the time");
//TODO// Setup as aerrno
exit (1);
}
}
//
// Fill out the TBSCertificate structure [RFC3280]:
//
// TBSCertificate ::= SEQUENCE {
// version [0] EXPLICIT Version DEFAULT v1,
// serialNumber CertificateSerialNumber,
// signature AlgorithmIdentifier,
// issuer Name,
// validity Validity,
// subject Name,
// subjectPublicKeyInfo SubjectPublicKeyInfo,
// issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
// -- If present, version MUST be v2 or v3
// subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
// -- If present, version MUST be v2 or v3
// extensions [3] EXPLICIT Extensions OPTIONAL
// -- If present, version MUST be v3
// }
//
// The buildup is in reverse order!
// SKIP extensions
// SKIP subjectUniqueID
// SKIP issuerUniqueID
if (!aerrno) {
// subjectPublicKeyInfo_pre2 <-- BITSTRING (ticket)
aerrno = asn1_encode_bitstring (tbsbuf, ticket->ticket.length, ticket->ticket.data, &sublen);
seqlen = sublen;
}
if (!aerrno) {
// subjectPublicKeyInfo_pre12 (parameters) absent, not used here
// subjectPublicKeyInfo_pre11 <-- OID for Kerberos Ticket
aerrno = asn1_encode_oid (tbsbuf, OID_KEY_TICKET_LEN, OID_KEY_TICKET, &sublen);
seq2len = sublen;
}
if (!aerrno) {
// subjectPublicKeyInfo_pre1 <-- AlgorithmIdentifier {_pre11,_pre12}
aerrno = asn1_make_sequence (tbsbuf, seq2len, &sublen);
seqlen += seq2len + sublen;
}
if (!aerrno) {
// subjectPublicKeyInfo <-- SEQUENCE {_pre1, _pre2}
aerrno = asn1_make_sequence (tbsbuf, seqlen, &sublen);
tbslen += seqlen + sublen;
}
if (!aerrno) {
// subject <-- CHOICE (RDNSequence (empty))
aerrno = asn1_make_sequence (tbsbuf, 0, &sublen);
tbslen += 0 + sublen;
}
if (!aerrno) {
// validity_pre2 <-- CHOICE (GeneralTime (now + 3 min))
aerrno = asn1_encode_generaltime (tbsbuf, sigtime + 180, &sublen);
seqlen = sublen;
}
if (!aerrno) {
// validity_pre1 <-- CHOICE (GeneralTime (now - 2 min))
aerrno = asn1_encode_generaltime (tbsbuf, sigtime - 120, &sublen);
seqlen += sublen;
}
if (!aerrno) {
// validity <-- SEQUENCE {validity_pre1, validity_pre2}
aerrno = asn1_make_sequence (tbsbuf, seqlen, &sublen);
tbslen += seqlen + sublen;
}
if (!aerrno) {
// issuer <-- CHOICE (RDNSequence (empty))
aerrno = asn1_make_sequence (tbsbuf, 0, &sublen);
tbslen += 0 + sublen;
}
if (!aerrno) {
// signature_pre2 (parameters) absent, not used here
// signature_pre1 <-- OID for Kerberos signing
aerrno = asn1_encode_oid (tbsbuf, OID_SIG_AUTHENTICATOR_SHA1_LEN, OID_SIG_AUTHENTICATOR_SHA1, &sublen);
seqlen = sublen;
}
if (!aerrno) {
// signature <-- AlgorithmIdentifier {signature_pre1, signature_pre2}
aerrno = asn1_make_sequence (tbsbuf, seqlen, &sublen);
tbslen += seqlen + sublen;
}
if (!aerrno) {
// serialNumber <-- INTEGER timestamp (or 0)
asn1_intmax ts;
if (timestamp_opt) {
//TODO// Subtract ticket signtime start
ts = 1000000 * timestamp_opt->tv_sec
+ timestamp_opt->tv_usec;
if (ts / 1000000 < timestamp_opt->tv_sec) {
fprintf (stderr, "Warning: Timestamp range clipped\n");
}
} else {
ts = 0;
}
aerrno = asn1_encode_integer (tbsbuf, ts, &sublen);
tbslen += sublen;
}
if (!aerrno) {
// version_pre <-- INTEGER { v3(2) }
aerrno = asn1_encode_integer (tbsbuf, 2, &sublen);
seqlen = sublen;
}
if (!aerrno) {
// version <-- [0] EXPLICIT version_pre
aerrno = asn1_make_tag (tbsbuf, CONTEXT_SPECIFIC, CONSTRUCTED, 0 /*tagnum*/, seqlen, &sublen);
tbslen += seqlen + sublen;
}
//
// SEQUENCE all the TBSCertificate components together
if (!aerrno) {
// TBSCertificate <-- SEQUENCE {all_of_the_above}
aerrno = asn1_make_sequence (tbsbuf, tbslen, &sublen);
//TODO// Length seems to be off
tbslen += sublen;
}
//
// Now form a temporary data buffer for the TBSCertificate
if (!aerrno) {
aerrno = asn12krb5_buf (tbsbuf, &tbscert);
have_tbscert = (aerrno == 0);
}
//
// Now hash the TBSCertificate and fill the krb5_checksum
if (!aerrno) {
int ok = 1;
SHA_CTX hctx;
tbshash = malloc (20);
ok = ok && (tbshash != NULL);
ok = ok && SHA1_Init (&hctx);
ok = ok && SHA1_Update (&hctx, tbscert->data, tbscert->length);
ok = ok && SHA1_Final (tbshash, &hctx);
if (!ok) {
if (tbshash != NULL) {
free (tbshash);
aerrno = ENOMEM; //TODO// Better one?
} else {
aerrno = ENOMEM;
}
}
have_tbshash = (ok == 1);
}
if (!aerrno) {
memset (&checksum, 0, sizeof (checksum));
checksum.magic = 0; //TODO// Better value
checksum.checksum_type = CKSUMTYPE_SHA1;
checksum.length = 20;
checksum.contents = tbshash;
}
//
// Construct an authenticator for the ticket
if (!aerrno) {
aerrno = construct_enc_authenticator (ctx, ticket, &checksum, &enc_authenticator);
//TODO// Really krb5_error_code
have_enc_authenticator = (aerrno == 0);
}
//
// Construct the Certificate [RFC3280]:
//
// Certificate ::= SEQUENCE {
// tbsCertificate TBSCertificate,
// signatureAlgorithm AlgorithmIdentifier,
// signatureValue BIT STRING
// }
//
// Buildup is in reverse order!
if (!aerrno) {
assert (have_enc_authenticator);
// authenticator_pre1 <-- EncryptedData {Authenticator}
//TODO// Function really returns a krb5_error_code
aerrno = encode_krb5_enc_data (&enc_authenticator, &encauthbits);
//TODO// Possible problem: optional kvno field not included
have_encauthbits = (aerrno == 0);
}
if (!aerrno) {
// authenticator_pre1 <-- 0:authenticator_pre11
assert (have_encauthbits);
aerrno = asn1_encode_bitstring (x509buf, encauthbits->length, encauthbits->data, &sublen);
certlen += sublen;
}
if (!aerrno) {
// signatureAlgorithm_pre1 <-- OID for Kerberos Signing
// signatureAlgorithm_pre2 (parameters) absent, not used here
aerrno = asn1_encode_oid (x509buf, OID_SIG_AUTHENTICATOR_SHA1_LEN, OID_SIG_AUTHENTICATOR_SHA1, &sublen);
seqlen = sublen;
}
if (!aerrno) {
// signatureAlgorithm <-- SEQUENCE {_pre1,_pre2}
aerrno = asn1_make_sequence (x509buf, seqlen, &sublen);
certlen += seqlen + sublen;
}
if (!aerrno) {
// tbsCertificate <-- Prepared tbscert buffer
assert (have_tbscert);
aerrno = asn1buf_insert_bytestring (x509buf, tbscert->length, tbscert->data);
certlen += tbscert->length;
}
//
// Pull all the Certificate parts together
if (!aerrno) {
// Certificate <-- SEQUENCE {all_of_the_above}
aerrno = asn1_make_sequence (x509buf, certlen, &sublen);
//TODO// Length seems to be off
certlen += sublen;
}
//
// Export the ASN.1 buffer data to a krb5_data structure
if (!aerrno) {
aerrno = asn12krb5_buf (x509buf, x509);
if (aerrno) {
krb5_free_data (ctx, *x509);
}
}
//
// Print the result for debugging
printf ("\n");
if (!aerrno) {
printf ("-----BEGIN TO-BE-SIGNED-CERTIFICATE HEXDUMP-----\n");
i = 0;
while (i < tbscert->length) {
char sep = (((i & 15) != 15) && (i != tbscert->length - 1))? ' ': '\n';
printf ("%02x%c", (uint8_t) tbscert->data [i++], sep);
}
printf ("-----END TO-BE-SIGNED-CERTIFICATE HEXDUMP-----\n");
}
printf ("\n");
if (tbshash != NULL) {
printf ("SHA1(tbscert) =");
for (i=0; i<20; i++) {
printf (" %02x", tbshash [i]);
}
printf ("\n");
}
printf ("\n");
if (!aerrno) {
printf ("-----BEGIN CERTIFICATE HEXDUMP-----\n");
i = 0;
while (i < (*x509)->length) {
char sep = (((i & 15) != 15) && (i != (*x509)->length - 1))? ' ': '\n';
printf ("%02x%c", (uint8_t) (*x509)->data [i++], sep);
}
printf ("-----END CERTIFICATE HEXDUMP-----\n");
}
//
// Cleanup
if (have_enc_authenticator) {
free (enc_authenticator.ciphertext.data);
have_enc_authenticator = 0;
}
if (have_encauthbits) {
krb5_free_data (ctx, encauthbits);
have_encauthbits = 0;
}
if (have_tbshash) {
free (tbshash);
have_tbshash = 0;
}
if (have_tbscert) {
krb5_free_data (ctx, tbscert);
have_tbscert = 0;
}
if (have_tbsbuf) {
asn1buf_destroy (&tbsbuf);
have_tbsbuf = 0;
}
if (have_x509buf) {
asn1buf_destroy (&x509buf);
have_x509buf = 0;
}
//
// Return the success or failure value
return aerrno;
}
/* Main routine
*/
int main (int argc, char *argv []) {
krb5_error_code kerrno = 0;
krb5_context ctx;
int have_ctx = 0;
krb5_ccache cache;
int have_cache = 0;
krb5_creds *ticket;
int have_ticket = 0;
krb5_data *x509;
int have_x509 = 0;
char *servicename = NULL;
//
// Parse commandline
if (argc > 2) {
fprintf (stderr, "Usage: %s [servicename]\n", argv [0]);
exit (1);
} else if (argc == 2) {
servicename = argv [1];
}
//
// Allocate and Initialise resources
if (!kerrno) {
kerrno = krb5_init_context (&ctx);
have_ctx = (kerrno == 0);
}
if (!kerrno) {
kerrno = krb5_cc_default (ctx, &cache);
have_cache = (kerrno == 0);
}
//
// Obtain a ticket
if (!kerrno) {
kerrno = find_ticket (ctx, cache, servicename, &ticket);
have_ticket = (kerrno == 0);
}
//
// Using the ticket and authenticator, build an X.509 certificate
if (!kerrno) {
//TODO// kerrno is actually asn1_error_code aerrno
kerrno = encode_x509 (ctx, NULL, ticket, &x509);
have_x509 = (kerrno != 0);
}
//
// Error reporting and Cleanup
if (kerrno) {
const char *errmsg = krb5_get_error_message (ctx, kerrno);
fprintf (stderr, "Error in Kerberos: %s\n", errmsg);
krb5_free_error_message (ctx, errmsg);
}
if (have_x509) {
krb5_free_data (ctx, x509);
have_x509 = 0;
}
if (have_ticket) {
krb5_free_creds (ctx, ticket);
have_ticket = 0;
}
if (have_cache) {
krb5_cc_close (ctx, cache);
have_cache = 0;
}
if (have_ctx) {
krb5_free_context (ctx);
have_ctx = 0;
}
//
// Exit with the krb5_error_code (0 for success)
exit (kerrno);
}