summaryrefslogtreecommitdiffstats
path: root/src/lib/krb5/asn.1/asn1buf.c
blob: 49ef84fbb61e87475db1ac5a9c3fc414a8edf707 (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
/* -*- mode: c; indent-tabs-mode: nil -*- */
/* Coding Buffer Implementation */

/*
  Implementation

    Encoding mode

    The encoding buffer is filled from bottom (lowest address) to top
    (highest address).  This makes it easier to expand the buffer,
    since realloc preserves the existing portion of the buffer.

    Note: Since ASN.1 encoding must be done in reverse, this means
    that you can't simply memcpy out the buffer data, since it will be
    backwards.  You need to reverse-iterate through it, instead.

    ***This decision may have been a mistake.  In practice, the
    implementation will probably be tuned such that reallocation is
    rarely necessary.  Also, the realloc probably has recopy the
    buffer itself, so we don't really gain that much by avoiding an
    explicit copy of the buffer.  --Keep this in mind for future reference.


    Decoding mode

    The decoding buffer is in normal order and is created by wrapping
    an asn1buf around a krb5_data structure.
  */

/* Abstraction Function

   Programs should use just pointers to asn1buf's (e.g. asn1buf *mybuf).
   These pointers must always point to a valid, allocated asn1buf
   structure or be NULL.

   The contents of the asn1buf represent an octet string.  This string
   begins at base and continues to the octet immediately preceding next.
   If next == base or mybuf == NULL, then the asn1buf represents an empty
   octet string. */

/* Representation Invariant

   Pointers to asn1buf's must always point to a valid, allocated
   asn1buf structure or be NULL.

   base points to a valid, allocated octet array or is NULL
   bound, if non-NULL, points to the last valid octet
   next >= base
   next <= bound+2  (i.e. next should be able to step just past the bound,
                     but no further.  (The bound should move out in response
                     to being crossed by next.)) */

#define ASN1BUF_OMIT_INLINE_FUNCS
#include "asn1buf.h"
#undef ASN1BUF_OMIT_INLINE_FUNCS
#include <stdio.h>
#include "asn1_get.h"

#define asn1_is_eoc(class, num, indef)  \
((class) == UNIVERSAL && !(num) && !(indef))

asn1_error_code asn1buf_create(asn1buf **buf)
{
    *buf = (asn1buf*)malloc(sizeof(asn1buf));
    if (*buf == NULL) return ENOMEM;
    (*buf)->base = NULL;
    (*buf)->bound = NULL;
    (*buf)->next = NULL;
    return 0;
}

asn1_error_code asn1buf_wrap_data(asn1buf *buf, const krb5_data *code)
{
    if (code == NULL || code->data == NULL) return ASN1_MISSING_FIELD;
    buf->next = buf->base = code->data;
    buf->bound = code->data + code->length - 1;
    return 0;
}

asn1_error_code asn1buf_imbed(asn1buf *subbuf, const asn1buf *buf, const unsigned int length, const int indef)
{
    subbuf->base = subbuf->next = buf->next;
    if (!indef) {
        subbuf->bound = subbuf->base + length - 1;
        if (subbuf->bound > buf->bound)
            return ASN1_OVERRUN;
    } else /* constructed indefinite */
        subbuf->bound = buf->bound;
    return 0;
}

asn1_error_code asn1buf_sync(asn1buf *buf, asn1buf *subbuf,
                             asn1_class asn1class, asn1_tagnum lasttag,
                             unsigned int length, int indef, int seqindef)
{
    asn1_error_code retval;

    if (!seqindef) {
        /* sequence was encoded as definite length */
        buf->next = subbuf->bound + 1;
    } else if (!asn1_is_eoc(asn1class, lasttag, indef)) {
        retval = asn1buf_skiptail(subbuf, length, indef);
        if (retval)
            return retval;
    } else {
        /* We have just read the EOC octets. */
        buf->next = subbuf->next;
    }
    return 0;
}

asn1_error_code asn1buf_skiptail(asn1buf *buf, const unsigned int length, const int indef)
{
    asn1_error_code retval;
    taginfo t;
    int nestlevel;

    nestlevel = 1 + indef;
    if (!indef) {
        if (length <= buf->bound - buf->next + 1)
            buf->next += length;
        else
            return ASN1_OVERRUN;
    }
    while (nestlevel > 0) {
        if (buf->bound - buf->next + 1 <= 0)
            return ASN1_OVERRUN;
        retval = asn1_get_tag_2(buf, &t);
        if (retval) return retval;
        if (!t.indef) {
            if (t.length <= buf->bound - buf->next + 1)
                buf->next += t.length;
            else
                return ASN1_OVERRUN;
        }
        if (t.indef)
            nestlevel++;
        if (asn1_is_eoc(t.asn1class, t.tagnum, t.indef))
            nestlevel--;                /* got an EOC encoding */
    }
    return 0;
}

asn1_error_code asn1buf_destroy(asn1buf **buf)
{
    if (*buf != NULL) {
        free((*buf)->base);
        free(*buf);
        *buf = NULL;
    }
    return 0;
}

#ifdef asn1buf_insert_octet
#undef asn1buf_insert_octet
#endif
asn1_error_code asn1buf_insert_octet(asn1buf *buf, const int o)
{
    asn1_error_code retval;

    retval = asn1buf_ensure_space(buf,1U);
    if (retval) return retval;
    *(buf->next) = (char)o;
    (buf->next)++;
    return 0;
}

asn1_error_code asn1buf_insert_octetstring(asn1buf *buf, const unsigned int len, const krb5_octet *s)
{
    asn1_error_code retval;
    unsigned int length;

    retval = asn1buf_ensure_space(buf,len);
    if (retval) return retval;
    for (length=1; length<=len; length++,(buf->next)++)
        *(buf->next) = (char)(s[len-length]);
    return 0;
}

asn1_error_code asn1buf_insert_charstring(asn1buf *buf, const unsigned int len, const char *s)
{
    asn1_error_code retval;
    unsigned int length;

    retval = asn1buf_ensure_space(buf,len);
    if (retval) return retval;
    for (length=1; length<=len; length++,(buf->next)++)
        *(buf->next) = (char)(s[len-length]);
    return 0;
}

#undef asn1buf_remove_octet
asn1_error_code asn1buf_remove_octet(asn1buf *buf, asn1_octet *o)
{
    if (buf->next > buf->bound) return ASN1_OVERRUN;
    *o = (asn1_octet)(*((buf->next)++));
    return 0;
}

asn1_error_code asn1buf_remove_octetstring(asn1buf *buf, const unsigned int len, asn1_octet **s)
{
    unsigned int i;

    if (len > buf->bound + 1 - buf->next) return ASN1_OVERRUN;
    if (len == 0) {
        *s = 0;
        return 0;
    }
    *s = (asn1_octet*)malloc(len*sizeof(asn1_octet));
    if (*s == NULL)
        return ENOMEM;
    for (i=0; i<len; i++)
        (*s)[i] = (asn1_octet)(buf->next)[i];
    buf->next += len;
    return 0;
}

asn1_error_code asn1buf_remove_charstring(asn1buf *buf, const unsigned int len, char **s)
{
    unsigned int i;

    if (len > buf->bound + 1 - buf->next) return ASN1_OVERRUN;
    if (len == 0) {
        *s = 0;
        return 0;
    }
    *s = (char*)malloc(len*sizeof(char));
    if (*s == NULL) return ENOMEM;
    for (i=0; i<len; i++)
        (*s)[i] = (char)(buf->next)[i];
    buf->next += len;
    return 0;
}

int asn1buf_remains(asn1buf *buf, int indef)
{
    int remain;
    if (buf == NULL || buf->base == NULL) return 0;
    remain = buf->bound - buf->next +1;
    if (remain <= 0) return remain;
    /*
     * Two 0 octets means the end of an indefinite encoding.
     */
    if (indef && remain >= 2 && !*(buf->next) && !*(buf->next + 1))
        return 0;
    else return remain;
}

asn1_error_code asn12krb5_buf(const asn1buf *buf, krb5_data **code)
{
    unsigned int i;
    *code = (krb5_data*)calloc(1,sizeof(krb5_data));
    if (*code == NULL) return ENOMEM;
    (*code)->magic = KV5M_DATA;
    (*code)->data = NULL;
    (*code)->length = 0;
    (*code)->length = asn1buf_len(buf);
    (*code)->data = (char*)malloc((((*code)->length)+1)*sizeof(char));
    if ((*code)->data == NULL) {
        free(*code);
        *code = NULL;
        return ENOMEM;
    }
    for (i=0; i < (*code)->length; i++)
        ((*code)->data)[i] = (buf->base)[((*code)->length)-i-1];
    ((*code)->data)[(*code)->length] = '\0';
    return 0;
}



/* These parse and unparse procedures should be moved out. They're
   useful only for debugging and superfluous in the production version. */

asn1_error_code asn1buf_unparse(const asn1buf *buf, char **s)
{
    free(*s);
    if (buf == NULL) {
        *s = malloc(sizeof("<NULL>"));
        if (*s == NULL) return ENOMEM;
        strcpy(*s,"<NULL>");
    } else if (buf->base == NULL) {
        *s = malloc(sizeof("<EMPTY>"));
        if (*s == NULL) return ENOMEM;
        strcpy(*s,"<EMPTY>");
    } else {
        unsigned int length = asn1buf_len(buf);
        unsigned int i;

        *s = calloc(length+1, sizeof(char));
        if (*s == NULL) return ENOMEM;
        (*s)[length] = '\0';
        for (i=0; i<length; i++) ;
/*      OLDDECLARG( (*s)[i] = , (buf->base)[length-i-1]) */
    }
    return 0;
}

asn1_error_code asn1buf_hex_unparse(const asn1buf *buf, char **s)
{
#define hexchar(d) ((d)<=9 ? ('0'+(d)) :        \
                    ((d)<=15 ? ('A'+(d)-10) :   \
                     'X'))

    free(*s);

    if (buf == NULL) {
        *s = malloc(sizeof("<NULL>"));
        if (*s == NULL) return ENOMEM;
        strcpy(*s,"<NULL>");
    } else if (buf->base == NULL) {
        *s = malloc(sizeof("<EMPTY>"));
        if (*s == NULL) return ENOMEM;
        strcpy(*s,"<EMPTY>");
    } else {
        unsigned int length = asn1buf_len(buf);
        int i;

        *s = malloc(3*length);
        if (*s == NULL) return ENOMEM;
        for (i = length-1; i >= 0; i--) {
            (*s)[3*(length-i-1)] = hexchar(((buf->base)[i]&0xF0)>>4);
            (*s)[3*(length-i-1)+1] = hexchar((buf->base)[i]&0x0F);
            (*s)[3*(length-i-1)+2] = ' ';
        }
        (*s)[3*length-1] = '\0';
    }
    return 0;
}

/****************************************************************/
/* Private Procedures */

#undef asn1buf_size
int asn1buf_size(const asn1buf *buf)
{
    if (buf == NULL || buf->base == NULL) return 0;
    return buf->bound - buf->base + 1;
}

#undef asn1buf_free
unsigned int asn1buf_free(const asn1buf *buf)
{
    if (buf == NULL || buf->base == NULL) return 0;
    else return buf->bound - buf->next + 1;
}

#undef asn1buf_ensure_space
asn1_error_code asn1buf_ensure_space(asn1buf *buf, const unsigned int amount)
{
    int avail = asn1buf_free(buf);
    if (avail < amount) {
        asn1_error_code retval = asn1buf_expand(buf, amount-avail);
        if (retval) return retval;
    }
    return 0;
}

asn1_error_code asn1buf_expand(asn1buf *buf, unsigned int inc)
{
#define STANDARD_INCREMENT 200
    int next_offset = buf->next - buf->base;
    int bound_offset;
    if (buf->base == NULL) bound_offset = -1;
    else bound_offset = buf->bound - buf->base;

    if (inc < STANDARD_INCREMENT)
        inc = STANDARD_INCREMENT;

    if (buf->base == NULL)
        buf->base = malloc((asn1buf_size(buf)+inc) * sizeof(asn1_octet));
    else
        buf->base = realloc(buf->base,
                            (asn1buf_size(buf)+inc) * sizeof(asn1_octet));
    if (buf->base == NULL) return ENOMEM;
    buf->bound = (buf->base) + bound_offset + inc;
    buf->next = (buf->base) + next_offset;
    return 0;
}

#undef asn1buf_len
int asn1buf_len(const asn1buf *buf)
{
    return buf->next - buf->base;
}