aboutsummaryrefslogtreecommitdiff
path: root/src/lib/krb5/asn.1/ldap_key_seq.c
blob: 92104ba2c8cb5e9e1521794e07b6ef99e9bb812c (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
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/* ... copyright ... */

/*
 * Novell key-format scheme:
 *
 * KrbKeySet ::= SEQUENCE {
 * attribute-major-vno       [0] UInt16,
 * attribute-minor-vno       [1] UInt16,
 * kvno                      [2] UInt32,
 * mkvno                     [3] UInt32 OPTIONAL,
 * keys                      [4] SEQUENCE OF KrbKey,
 * ...
 * }
 *
 * KrbKey ::= SEQUENCE {
 * salt      [0] KrbSalt OPTIONAL,
 * key       [1] EncryptionKey,
 * s2kparams [2] OCTET STRING OPTIONAL,
 *  ...
 * }
 *
 * KrbSalt ::= SEQUENCE {
 * type      [0] Int32,
 * salt      [1] OCTET STRING OPTIONAL
 * }
 *
 * EncryptionKey ::= SEQUENCE {
 * keytype   [0] Int32,
 * keyvalue  [1] OCTET STRING
 * }
 *
 */

#include <k5-int.h>
#include <kdb.h>

#include "krbasn1.h"
#include "asn1_encode.h"
#include "asn1_decode.h"
#include "asn1_make.h"
#include "asn1_get.h"
#include "asn1_k_encode.h"

#ifdef ENABLE_LDAP

/************************************************************************/
/* Encode the Principal's keys                                          */
/************************************************************************/

/*
 * Imports from asn1_k_encode.c.
 * XXX Must be manually synchronized for now.
 */
IMPORT_TYPE(octetstring, unsigned char *);
IMPORT_TYPE(int32, krb5_int32);

DEFINTTYPE(int16, krb5_int16);
DEFINTTYPE(ui_2, krb5_ui_2);

static const struct field_info krbsalt_fields[] = {
    FIELDOF_NORM(krb5_key_data, int16, key_data_type[1], 0),
    FIELDOF_OPTSTRINGL(krb5_key_data, octetstring, key_data_contents[1],
                       ui_2, key_data_length[1], 1, 1),
};
static unsigned int
optional_krbsalt (const void *p)
{
    const krb5_key_data *k = p;
    unsigned int optional = 0;

    if (k->key_data_length[1] > 0)
        optional |= (1u << 1);

    return optional;
}
DEFSEQTYPE(krbsalt, krb5_key_data, krbsalt_fields, optional_krbsalt);
static const struct field_info encryptionkey_fields[] = {
    FIELDOF_NORM(krb5_key_data, int16, key_data_type[0], 0),
    FIELDOF_STRINGL(krb5_key_data, octetstring, key_data_contents[0],
                    ui_2, key_data_length[0], 1),
};
DEFSEQTYPE(encryptionkey, krb5_key_data, encryptionkey_fields, 0);

static const struct field_info key_data_fields[] = {
    FIELDOF_ENCODEAS(krb5_key_data, krbsalt, 0),
    FIELDOF_ENCODEAS(krb5_key_data, encryptionkey, 1),
#if 0 /* We don't support this field currently.  */
    FIELDOF_blah(krb5_key_data, s2kparams, ...),
#endif
};
DEFSEQTYPE(key_data, krb5_key_data, key_data_fields, 0);
DEFPTRTYPE(ptr_key_data, key_data);

DEFFIELDTYPE(key_data_kvno, krb5_key_data,
             FIELDOF_NORM(krb5_key_data, int16, key_data_kvno, -1));
DEFPTRTYPE(ptr_key_data_kvno, key_data_kvno);

static const struct field_info ldap_key_seq_fields[] = {
    FIELD_INT_IMM(1, 0),
    FIELD_INT_IMM(1, 1),
    FIELDOF_NORM(ldap_seqof_key_data, ptr_key_data_kvno, key_data, 2),
    FIELDOF_NORM(ldap_seqof_key_data, int32, mkvno, 3), /* mkvno */
    FIELDOF_SEQOF_LEN(ldap_seqof_key_data, ptr_key_data, key_data, n_key_data,
                      int16, 4),
};
DEFSEQTYPE(ldap_key_seq, ldap_seqof_key_data, ldap_key_seq_fields, 0);

/* Export a function to do the whole encoding.  */
MAKE_FULL_ENCODER(krb5int_ldap_encode_sequence_of_keys, ldap_key_seq);

/************************************************************************/
/* Decode the Principal's keys                                          */
/************************************************************************/

#define cleanup(err)                            \
    {                                           \
        ret = err;                              \
        goto last;                              \
    }

#define checkerr                                \
    if (ret != 0)                               \
        goto last

#define safe_syncbuf(outer,inner,buflen)                \
    if (! ((inner)->next == (inner)->bound + 1 &&       \
           (inner)->next == (outer)->next + buflen))    \
        cleanup (ASN1_BAD_LENGTH);                      \
    asn1buf_sync((outer), (inner), 0, 0, 0, 0, 0);

static asn1_error_code
decode_tagged_integer (asn1buf *buf, asn1_tagnum expectedtag, long *val)
{
    int buflen;
    asn1_error_code ret = 0;
    asn1buf tmp, subbuf;
    taginfo t;

    /* Work on a copy of 'buf' */
    ret = asn1buf_imbed(&tmp, buf, 0, 1); checkerr;
    ret = asn1_get_tag_2(&tmp, &t); checkerr;
    if (t.tagnum != expectedtag)
        cleanup (ASN1_MISSING_FIELD);

    buflen = t.length;
    ret = asn1buf_imbed(&subbuf, &tmp, t.length, 0); checkerr;
    ret = asn1_decode_integer(&subbuf, val); checkerr;

    safe_syncbuf(&tmp, &subbuf, buflen);
    *buf = tmp;

last:
    return ret;
}

#if 0 /* not currently used */
static asn1_error_code
decode_tagged_unsigned_integer (asn1buf *buf, int expectedtag, unsigned long *val)
{
    int buflen;
    asn1_error_code ret = 0;
    asn1buf tmp, subbuf;
    taginfo t;

    /* Work on a copy of 'buf' */
    ret = asn1buf_imbed(&tmp, buf, 0, 1); checkerr;
    ret = asn1_get_tag_2(&tmp, &t); checkerr;
    if (t.tagnum != expectedtag)
        cleanup (ASN1_MISSING_FIELD);

    buflen = t.length;
    ret = asn1buf_imbed(&subbuf, &tmp, t.length, 0); checkerr;
    ret = asn1_decode_unsigned_integer(&subbuf, val); checkerr;

    safe_syncbuf(&tmp, &subbuf, buflen);
    *buf = tmp;

last:
    return ret;
}
#endif

static asn1_error_code
decode_tagged_octetstring (asn1buf *buf, asn1_tagnum expectedtag,
                           unsigned int *len,
                           asn1_octet **val)
{
    int buflen;
    asn1_error_code ret = 0;
    asn1buf tmp, subbuf;
    taginfo t;

    *val = NULL;

    /* Work on a copy of 'buf' */
    ret = asn1buf_imbed(&tmp, buf, 0, 1); checkerr;
    ret = asn1_get_tag_2(&tmp, &t); checkerr;
    if (t.tagnum != expectedtag)
        cleanup (ASN1_MISSING_FIELD);

    buflen = t.length;
    ret = asn1buf_imbed(&subbuf, &tmp, t.length, 0); checkerr;
    ret = asn1_decode_octetstring (&subbuf, len, val); checkerr;

    safe_syncbuf(&tmp, &subbuf, buflen);
    *buf = tmp;

last:
    if (ret != 0)
        free (*val);
    return ret;
}

static asn1_error_code
asn1_decode_key(asn1buf *buf, krb5_key_data *key)
{
    int full_buflen, seqindef;
    unsigned int length;
    asn1_error_code ret;
    asn1buf subbuf;
    taginfo t;

    key->key_data_contents[0] = NULL;
    key->key_data_contents[1] = NULL;

    ret = asn1_get_sequence(buf, &length, &seqindef); checkerr;
    full_buflen = length;
    ret = asn1buf_imbed(&subbuf, buf, length, seqindef); checkerr;

    asn1_get_tag_2(&subbuf, &t);
    /* Salt */
    if (t.tagnum == 0) {
        int salt_buflen;
        asn1buf slt;
        long keytype;
        unsigned int keylen;

        key->key_data_ver = 2;
        asn1_get_sequence(&subbuf, &length, &seqindef);
        salt_buflen = length;
        asn1buf_imbed(&slt, &subbuf, length, seqindef);

        ret = decode_tagged_integer (&slt, 0, &keytype);
        key->key_data_type[1] = keytype; /* XXX range check?? */
        checkerr;

        if (asn1buf_remains(&slt, 0) != 0) { /* Salt value is optional */
            ret = decode_tagged_octetstring (&slt, 1, &keylen,
                                             &key->key_data_contents[1]);
            checkerr;
        } else
            keylen = 0;
        safe_syncbuf (&subbuf, &slt, salt_buflen);
        key->key_data_length[1] = keylen; /* XXX range check?? */

        ret = asn1_get_tag_2(&subbuf, &t); checkerr;
    } else
        key->key_data_ver = 1;

    /* Key */
    {
        int key_buflen;
        asn1buf kbuf;
        long lval;
        unsigned int ival;

        if (t.tagnum != 1)
            cleanup (ASN1_MISSING_FIELD);

        ret = asn1_get_sequence(&subbuf, &length, &seqindef); checkerr;
        key_buflen = length;
        ret = asn1buf_imbed(&kbuf, &subbuf, length, seqindef); checkerr;

        ret = decode_tagged_integer (&kbuf, 0, &lval);
        checkerr;
        key->key_data_type[0] = lval; /* XXX range check? */

        ret = decode_tagged_octetstring (&kbuf, 1, &ival,
                                         &key->key_data_contents[0]); checkerr;
        key->key_data_length[0] = ival; /* XXX range check? */

        safe_syncbuf (&subbuf, &kbuf, key_buflen);
    }

    safe_syncbuf (buf, &subbuf, full_buflen);

last:
    if (ret != 0) {
        free (key->key_data_contents[0]);
        key->key_data_contents[0] = NULL;
        free (key->key_data_contents[1]);
        key->key_data_contents[1] = NULL;
    }
    return ret;
}

krb5_error_code
krb5int_ldap_decode_sequence_of_keys (krb5_data *in, ldap_seqof_key_data **rep)
{
    ldap_seqof_key_data *repval;
    krb5_key_data **out;
    krb5_int16 *n_key_data;
    int *mkvno;

    asn1_error_code ret;
    asn1buf buf, subbuf;
    int seqindef;
    unsigned int length;
    taginfo t;
    int kvno, maj, min;
    long lval;

    repval = calloc(1,sizeof(ldap_seqof_key_data));
    *rep = repval;
    out = &repval->key_data;
    n_key_data = &repval->n_key_data;
    mkvno = &repval->mkvno;

    *n_key_data = 0;
    *out = NULL;

    ret = asn1buf_wrap_data(&buf, in); checkerr;

    ret = asn1_get_sequence(&buf, &length, &seqindef); checkerr;
    ret = asn1buf_imbed(&subbuf, &buf, length, seqindef); checkerr;

    /* attribute-major-vno */
    ret = decode_tagged_integer (&subbuf, 0, &lval); checkerr;
    maj = lval;                 /* XXX range check? */

    /* attribute-minor-vno */
    ret = decode_tagged_integer (&subbuf, 1, &lval); checkerr;
    min = lval;                 /* XXX range check? */

    if (maj != 1 || min != 1)
        cleanup (ASN1_BAD_FORMAT);

    /* kvno (assuming all keys in array have same version) */
    ret = decode_tagged_integer (&subbuf, 2, &lval); checkerr;
    kvno = lval;                /* XXX range check? */

    /* mkvno (optional) */
    ret = decode_tagged_integer (&subbuf, 3, &lval); checkerr;
    *mkvno = lval;              /* XXX range check? */

    ret = asn1_get_tag_2(&subbuf, &t); checkerr;

    /* Sequence of keys */
    {
        int i, seq_buflen;
        asn1buf keyseq;
        if (t.tagnum != 4)
            cleanup (ASN1_MISSING_FIELD);
        ret = asn1_get_sequence(&subbuf, &length, &seqindef); checkerr;
        seq_buflen = length;
        ret = asn1buf_imbed(&keyseq, &subbuf, length, seqindef); checkerr;
        for (i = 1, *out = NULL; ; i++) {
            krb5_key_data *tmp;
            tmp = (krb5_key_data *) realloc (*out, i * sizeof (krb5_key_data));
            if (tmp == NULL)
                cleanup (ENOMEM);
            *out = tmp;
            (*out)[i - 1].key_data_kvno = kvno;
            ret = asn1_decode_key(&keyseq, &(*out)[i - 1]); checkerr;
            (*n_key_data)++;
            if (asn1buf_remains(&keyseq, 0) == 0)
                break; /* Not freeing the last key structure */
        }
        safe_syncbuf (&subbuf, &keyseq, seq_buflen);
    }

    /*
     * There could be other data inside the outermost sequence ... tags we don't
     * know about. So, not invoking "safe_syncbuf(&buf,&subbuf)"
     */

last:
    if (ret != 0) {
        int i;
        for (i = 0; i < *n_key_data; i++) {
            free ((*out)[i].key_data_contents[0]);
            free ((*out)[i].key_data_contents[1]);
        }
        free (*out);
        *out = NULL;
    }

    return ret;
}
#endif