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
|
/*
* lib/gssapi/krb5/ser_sctx.c
*
* Copyright 1995 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. Furthermore if you modify this software you must label
* your software as modified software and not distribute it in such a
* fashion that it might be confused with the original M.I.T. software.
* 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.
*
*/
/*
* ser_sctx.c - Handle [de]serialization of GSSAPI security context.
*/
#include "k5-int.h"
#include "gssapiP_krb5.h"
/*
* This module contains routines to [de]serialize
* krb5_gss_enc_desc and krb5_gss_ctx_id_t.
* XXX This whole serialization abstraction is unnecessary in a
* non-messaging environment, which krb5 is. Someday, this should
* all get redone without the extra level of indirection. I've done
* some of this work here, since adding new serializers is an internal
* krb5 interface, and I won't use those. There is some more
* deobfuscation (no longer anonymizing pointers, mostly) which could
* still be done. --marc
*/
static krb5_error_code
kg_oid_externalize(kcontext, arg, buffer, lenremain)
krb5_context kcontext;
krb5_pointer arg;
krb5_octet **buffer;
size_t *lenremain;
{
gss_OID oid = (gss_OID) arg;
(void) krb5_ser_pack_int32(KV5M_GSS_OID, buffer, lenremain);
(void) krb5_ser_pack_int32((krb5_int32) oid->length,
buffer, lenremain);
(void) krb5_ser_pack_bytes((krb5_octet *) oid->elements,
oid->length, buffer, lenremain);
(void) krb5_ser_pack_int32(KV5M_GSS_OID, buffer, lenremain);
return 0;
}
static krb5_error_code
kg_oid_internalize(kcontext, argp, buffer, lenremain)
krb5_context kcontext;
krb5_pointer *argp;
krb5_octet **buffer;
size_t *lenremain;
{
krb5_error_code kret;
gss_OID oid;
krb5_int32 ibuf;
krb5_octet *bp;
size_t remain;
bp = *buffer;
remain = *lenremain;
/* Read in and check our magic number */
if ((kret = krb5_ser_unpack_int32(&ibuf, &bp, &remain)))
return (EINVAL);
if (ibuf != KV5M_GSS_OID)
return (EINVAL);
oid = (gss_OID) malloc(sizeof(gss_OID_desc));
if (oid == NULL)
return ENOMEM;
(void) krb5_ser_unpack_int32(&ibuf, &bp, &remain);
oid->length = ibuf;
oid->elements = malloc(ibuf);
if (oid->elements == 0) {
free(oid);
return ENOMEM;
}
(void) krb5_ser_unpack_bytes((krb5_octet *) oid->elements,
oid->length, &bp, &remain);
/* Read in and check our trailing magic number */
if ((kret = krb5_ser_unpack_int32(&ibuf, &bp, &remain)))
return (EINVAL);
if (ibuf != KV5M_GSS_OID)
return (EINVAL);
*buffer = bp;
*lenremain = remain;
*argp = (krb5_pointer) oid;
return 0;
}
krb5_error_code
kg_oid_size(kcontext, arg, sizep)
krb5_context kcontext;
krb5_pointer arg;
size_t *sizep;
{
krb5_error_code kret;
gss_OID oid;
size_t required;
kret = EINVAL;
if ((oid = (gss_OID) arg)) {
required = 2*sizeof(krb5_int32); /* For the header and trailer */
required += sizeof(krb5_int32);
required += oid->length;
kret = 0;
*sizep += required;
}
return(kret);
}
static krb5_error_code
kg_queue_externalize(kcontext, arg, buffer, lenremain)
krb5_context kcontext;
krb5_pointer arg;
krb5_octet **buffer;
size_t *lenremain;
{
(void) krb5_ser_pack_int32(KV5M_GSS_QUEUE, buffer, lenremain);
g_queue_externalize(arg, buffer, lenremain);
(void) krb5_ser_pack_int32(KV5M_GSS_QUEUE, buffer, lenremain);
return 0;
}
static krb5_error_code
kg_queue_internalize(kcontext, argp, buffer, lenremain)
krb5_context kcontext;
krb5_pointer *argp;
krb5_octet **buffer;
size_t *lenremain;
{
krb5_error_code kret;
gss_OID oid;
krb5_int32 ibuf;
krb5_octet *bp;
size_t remain;
bp = *buffer;
remain = *lenremain;
/* Read in and check our magic number */
if ((kret = krb5_ser_unpack_int32(&ibuf, &bp, &remain)))
return (EINVAL);
if (ibuf != KV5M_GSS_QUEUE)
return (EINVAL);
g_queue_internalize(argp, &bp, &remain);
/* Read in and check our trailing magic number */
if ((kret = krb5_ser_unpack_int32(&ibuf, &bp, &remain)))
return (EINVAL);
if (ibuf != KV5M_GSS_QUEUE)
return (EINVAL);
*buffer = bp;
*lenremain = remain;
return 0;
}
krb5_error_code
kg_queue_size(kcontext, arg, sizep)
krb5_context kcontext;
krb5_pointer arg;
size_t *sizep;
{
krb5_error_code kret;
size_t required;
kret = EINVAL;
if (arg) {
required = 2*sizeof(krb5_int32); /* For the header and trailer */
g_queue_size(arg, &required);
kret = 0;
*sizep += required;
}
return(kret);
}
/*
* Determine the size required for this krb5_gss_ctx_id_rec.
*/
krb5_error_code
kg_ctx_size(kcontext, arg, sizep)
krb5_context kcontext;
krb5_pointer arg;
size_t *sizep;
{
krb5_error_code kret;
krb5_gss_ctx_id_rec *ctx;
size_t required;
/*
* krb5_gss_ctx_id_rec requires:
* krb5_int32 for KG_CONTEXT
* krb5_int32 for initiate.
* krb5_int32 for mutual.
* krb5_int32 for seed_init.
* sizeof(seed) for seed
* krb5_int32 for signalg.
* krb5_int32 for cksum_size.
* krb5_int32 for sealalg.
* krb5_int32 for endtime.
* krb5_int32 for flags.
* krb5_int32 for seq_send.
* krb5_int32 for seq_recv.
* krb5_int32 for established.
* krb5_int32 for big_endian.
* krb5_int32 for gsskrb5_version.
* krb5_int32 for nctypes.
* krb5_int32 for trailer.
*/
kret = EINVAL;
if ((ctx = (krb5_gss_ctx_id_rec *) arg)) {
required = 16*sizeof(krb5_int32);
required += sizeof(ctx->seed);
required += ctx->nctypes*sizeof(krb5_int32);
kret = 0;
if (!kret && ctx->here)
kret = krb5_size_opaque(kcontext,
KV5M_PRINCIPAL,
(krb5_pointer) ctx->here,
&required);
if (!kret && ctx->there)
kret = krb5_size_opaque(kcontext,
KV5M_PRINCIPAL,
(krb5_pointer) ctx->there,
&required);
if (!kret && ctx->subkey)
kret = krb5_size_opaque(kcontext,
KV5M_KEYBLOCK,
(krb5_pointer) ctx->subkey,
&required);
if (!kret && ctx->enc)
kret = krb5_size_opaque(kcontext,
KV5M_KEYBLOCK,
(krb5_pointer) ctx->enc,
&required);
if (!kret && ctx->seq)
kret = krb5_size_opaque(kcontext,
KV5M_KEYBLOCK,
(krb5_pointer) ctx->seq,
&required);
if (!kret)
kret = kg_oid_size(kcontext,
(krb5_pointer) ctx->mech_used,
&required);
if (!kret && ctx->seqstate)
kret = kg_queue_size(kcontext, ctx->seqstate, &required);
if (!kret)
kret = krb5_size_opaque(kcontext,
KV5M_AUTH_CONTEXT,
(krb5_pointer) ctx->auth_context,
&required);
if (!kret)
*sizep += required;
}
return(kret);
}
/*
* Externalize this krb5_gss_ctx_id_ret.
*/
krb5_error_code
kg_ctx_externalize(kcontext, arg, buffer, lenremain)
krb5_context kcontext;
krb5_pointer arg;
krb5_octet **buffer;
size_t *lenremain;
{
krb5_error_code kret;
krb5_gss_ctx_id_rec *ctx;
size_t required;
krb5_octet *bp;
size_t remain;
int i;
required = 0;
bp = *buffer;
remain = *lenremain;
kret = EINVAL;
if ((ctx = (krb5_gss_ctx_id_rec *) arg)) {
kret = ENOMEM;
if (!kg_ctx_size(kcontext, arg, &required) &&
(required <= remain)) {
/* Our identifier */
(void) krb5_ser_pack_int32(KG_CONTEXT, &bp, &remain);
/* Now static data */
(void) krb5_ser_pack_int32((krb5_int32) ctx->initiate,
&bp, &remain);
(void) krb5_ser_pack_int32((krb5_int32) ctx->gss_flags,
&bp, &remain);
(void) krb5_ser_pack_int32((krb5_int32) ctx->seed_init,
&bp, &remain);
(void) krb5_ser_pack_bytes((krb5_octet *) ctx->seed,
sizeof(ctx->seed),
&bp, &remain);
(void) krb5_ser_pack_int32((krb5_int32) ctx->signalg,
&bp, &remain);
(void) krb5_ser_pack_int32((krb5_int32) ctx->cksum_size,
&bp, &remain);
(void) krb5_ser_pack_int32((krb5_int32) ctx->sealalg,
&bp, &remain);
(void) krb5_ser_pack_int32((krb5_int32) ctx->endtime,
&bp, &remain);
(void) krb5_ser_pack_int32((krb5_int32) ctx->krb_flags,
&bp, &remain);
(void) krb5_ser_pack_int32((krb5_int32) ctx->seq_send,
&bp, &remain);
(void) krb5_ser_pack_int32((krb5_int32) ctx->seq_recv,
&bp, &remain);
(void) krb5_ser_pack_int32((krb5_int32) ctx->established,
&bp, &remain);
(void) krb5_ser_pack_int32((krb5_int32) ctx->big_endian,
&bp, &remain);
(void) krb5_ser_pack_int32((krb5_int32) ctx->gsskrb5_version,
&bp, &remain);
(void) krb5_ser_pack_int32((krb5_int32) ctx->nctypes,
&bp, &remain);
/* Now dynamic data */
kret = 0;
if (!kret && ctx->mech_used)
kret = kg_oid_externalize(kcontext, ctx->mech_used,
&bp, &remain);
if (!kret && ctx->here)
kret = krb5_externalize_opaque(kcontext,
KV5M_PRINCIPAL,
(krb5_pointer) ctx->here,
&bp, &remain);
if (!kret && ctx->there)
kret = krb5_externalize_opaque(kcontext,
KV5M_PRINCIPAL,
(krb5_pointer) ctx->there,
&bp, &remain);
if (!kret && ctx->subkey)
kret = krb5_externalize_opaque(kcontext,
KV5M_KEYBLOCK,
(krb5_pointer) ctx->subkey,
&bp, &remain);
if (!kret && ctx->enc)
kret = krb5_externalize_opaque(kcontext,
KV5M_KEYBLOCK,
(krb5_pointer) ctx->enc,
&bp, &remain);
if (!kret && ctx->seq)
kret = krb5_externalize_opaque(kcontext,
KV5M_KEYBLOCK,
(krb5_pointer) ctx->seq,
&bp, &remain);
if (!kret && ctx->seqstate)
kret = kg_queue_externalize(kcontext,
ctx->seqstate, &bp, &remain);
if (!kret)
kret = krb5_externalize_opaque(kcontext,
KV5M_AUTH_CONTEXT,
(krb5_pointer) ctx->auth_context,
&bp, &remain);
for (i=0; i<ctx->nctypes; i++) {
if (!kret) {
kret = krb5_ser_pack_int32((krb5_int32) ctx->ctypes[i],
&bp, &remain);
}
}
if (!kret) {
(void) krb5_ser_pack_int32(KG_CONTEXT, &bp, &remain);
*buffer = bp;
*lenremain = remain;
}
}
}
return(kret);
}
/*
* Internalize this krb5_gss_ctx_id_t.
*/
krb5_error_code
kg_ctx_internalize(kcontext, argp, buffer, lenremain)
krb5_context kcontext;
krb5_pointer *argp;
krb5_octet **buffer;
size_t *lenremain;
{
krb5_error_code kret;
krb5_gss_ctx_id_rec *ctx;
krb5_int32 ibuf;
krb5_octet *bp;
size_t remain;
int i;
bp = *buffer;
remain = *lenremain;
kret = EINVAL;
/* Read our magic number */
if (krb5_ser_unpack_int32(&ibuf, &bp, &remain))
ibuf = 0;
if (ibuf == KG_CONTEXT) {
kret = ENOMEM;
/* Get a context */
if ((remain >= ((10*sizeof(krb5_int32))+sizeof(ctx->seed))) &&
(ctx = (krb5_gss_ctx_id_rec *)
xmalloc(sizeof(krb5_gss_ctx_id_rec)))) {
memset(ctx, 0, sizeof(krb5_gss_ctx_id_rec));
/* Get static data */
(void) krb5_ser_unpack_int32(&ibuf, &bp, &remain);
ctx->initiate = (int) ibuf;
(void) krb5_ser_unpack_int32(&ibuf, &bp, &remain);
ctx->gss_flags = (int) ibuf;
(void) krb5_ser_unpack_int32(&ibuf, &bp, &remain);
ctx->seed_init = (int) ibuf;
(void) krb5_ser_unpack_bytes((krb5_octet *) ctx->seed,
sizeof(ctx->seed),
&bp, &remain);
(void) krb5_ser_unpack_int32(&ibuf, &bp, &remain);
ctx->signalg = (int) ibuf;
(void) krb5_ser_unpack_int32(&ibuf, &bp, &remain);
ctx->cksum_size = (int) ibuf;
(void) krb5_ser_unpack_int32(&ibuf, &bp, &remain);
ctx->sealalg = (int) ibuf;
(void) krb5_ser_unpack_int32(&ibuf, &bp, &remain);
ctx->endtime = (krb5_timestamp) ibuf;
(void) krb5_ser_unpack_int32(&ibuf, &bp, &remain);
ctx->krb_flags = (krb5_flags) ibuf;
(void) krb5_ser_unpack_int32(&ctx->seq_send, &bp, &remain);
(void) krb5_ser_unpack_int32(&ctx->seq_recv, &bp, &remain);
(void) krb5_ser_unpack_int32(&ibuf, &bp, &remain);
ctx->established = (int) ibuf;
(void) krb5_ser_unpack_int32(&ibuf, &bp, &remain);
ctx->big_endian = (int) ibuf;
(void) krb5_ser_unpack_int32(&ibuf, &bp, &remain);
ctx->gsskrb5_version = (int) ibuf;
(void) krb5_ser_unpack_int32(&ibuf, &bp, &remain);
ctx->nctypes = (int) ibuf;
if ((kret = kg_oid_internalize(kcontext, &ctx->mech_used, &bp,
&remain))) {
if (kret == EINVAL)
kret = 0;
}
/* Now get substructure data */
if ((kret = krb5_internalize_opaque(kcontext,
KV5M_PRINCIPAL,
(krb5_pointer *) &ctx->here,
&bp, &remain))) {
if (kret == EINVAL)
kret = 0;
}
if (!kret &&
(kret = krb5_internalize_opaque(kcontext,
KV5M_PRINCIPAL,
(krb5_pointer *) &ctx->there,
&bp, &remain))) {
if (kret == EINVAL)
kret = 0;
}
if (!kret &&
(kret = krb5_internalize_opaque(kcontext,
KV5M_KEYBLOCK,
(krb5_pointer *) &ctx->subkey,
&bp, &remain))) {
if (kret == EINVAL)
kret = 0;
}
if (!kret &&
(kret = krb5_internalize_opaque(kcontext,
KV5M_KEYBLOCK,
(krb5_pointer *) &ctx->enc,
&bp, &remain))) {
if (kret == EINVAL)
kret = 0;
}
if (!kret &&
(kret = krb5_internalize_opaque(kcontext,
KV5M_KEYBLOCK,
(krb5_pointer *) &ctx->seq,
&bp, &remain))) {
if (kret == EINVAL)
kret = 0;
}
if (!kret) {
kret = kg_queue_internalize(kcontext, &ctx->seqstate,
&bp, &remain);
if (kret == EINVAL)
kret = 0;
}
if (!kret)
kret = krb5_internalize_opaque(kcontext,
KV5M_AUTH_CONTEXT,
(krb5_pointer *) &ctx->auth_context,
&bp, &remain);
if (!kret) {
if (ctx->nctypes) {
if ((ctx->ctypes = (krb5_cksumtype *)
malloc(ctx->nctypes*sizeof(krb5_cksumtype))) == NULL){
kret = ENOMEM;
}
for (i=0; i<ctx->nctypes; i++) {
if (!kret) {
kret = krb5_ser_unpack_int32(&ibuf, &bp, &remain);
ctx->ctypes[i] = (krb5_cksumtype) ibuf;
}
}
}
}
/* Get trailer */
if (!kret &&
!(kret = krb5_ser_unpack_int32(&ibuf, &bp, &remain)) &&
(ibuf == KG_CONTEXT)) {
*buffer = bp;
*lenremain = remain;
*argp = (krb5_pointer) ctx;
}
else {
if (!kret && (ibuf != KG_CONTEXT))
kret = EINVAL;
if (ctx->seq)
krb5_free_keyblock(kcontext, ctx->seq);
if (ctx->enc)
krb5_free_keyblock(kcontext, ctx->enc);
if (ctx->subkey)
krb5_free_keyblock(kcontext, ctx->subkey);
if (ctx->there)
krb5_free_principal(kcontext, ctx->there);
if (ctx->here)
krb5_free_principal(kcontext, ctx->here);
xfree(ctx);
}
}
}
return(kret);
}
|