~ubuntu-branches/ubuntu/gutsy/net-snmp/gutsy-security

« back to all changes in this revision

Viewing changes to include/net-snmp/library/asn1.h

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2004-09-13 12:06:21 UTC
  • Revision ID: james.westby@ubuntu.com-20040913120621-g952ntonlleihcvm
Tags: upstream-5.1.1
ImportĀ upstreamĀ versionĀ 5.1.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#ifndef ASN1_H
 
2
#define ASN1_H
 
3
 
 
4
#ifdef __cplusplus
 
5
extern          "C" {
 
6
#endif
 
7
 
 
8
#define PARSE_PACKET    0
 
9
#define DUMP_PACKET     1
 
10
 
 
11
    /*
 
12
     * Definitions for Abstract Syntax Notation One, ASN.1
 
13
     * As defined in ISO/IS 8824 and ISO/IS 8825
 
14
     *
 
15
     *
 
16
     */
 
17
/***********************************************************
 
18
        Copyright 1988, 1989 by Carnegie Mellon University
 
19
 
 
20
                      All Rights Reserved
 
21
 
 
22
Permission to use, copy, modify, and distribute this software and its 
 
23
documentation for any purpose and without fee is hereby granted, 
 
24
provided that the above copyright notice appear in all copies and that
 
25
both that copyright notice and this permission notice appear in 
 
26
supporting documentation, and that the name of CMU not be
 
27
used in advertising or publicity pertaining to distribution of the
 
28
software without specific, written prior permission.  
 
29
 
 
30
CMU DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
 
31
ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
 
32
CMU BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
 
33
ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
 
34
WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
 
35
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
 
36
SOFTWARE.
 
37
******************************************************************/
 
38
 
 
39
 
 
40
#ifndef MAX_SUBID               /* temporary - duplicate definition protection */
 
41
#ifndef EIGHTBIT_SUBIDS
 
42
    typedef u_long  oid;
 
43
#define MAX_SUBID   0xFFFFFFFF
 
44
#else
 
45
    typedef u_char  oid;
 
46
#define MAX_SUBID   0xFF
 
47
#endif
 
48
#endif
 
49
 
 
50
#define MIN_OID_LEN         2
 
51
#define MAX_OID_LEN         128 /* max subid's in an oid */
 
52
#ifndef MAX_NAME_LEN            /* conflicts with some libraries */
 
53
#define MAX_NAME_LEN        MAX_OID_LEN /* obsolete. use MAX_OID_LEN */
 
54
#endif
 
55
 
 
56
#define OID_LENGTH(x)  (sizeof(x)/sizeof(oid))
 
57
 
 
58
 
 
59
#define ASN_BOOLEAN         ((u_char)0x01)
 
60
#define ASN_INTEGER         ((u_char)0x02)
 
61
#define ASN_BIT_STR         ((u_char)0x03)
 
62
#define ASN_OCTET_STR       ((u_char)0x04)
 
63
#define ASN_NULL            ((u_char)0x05)
 
64
#define ASN_OBJECT_ID       ((u_char)0x06)
 
65
#define ASN_SEQUENCE        ((u_char)0x10)
 
66
#define ASN_SET             ((u_char)0x11)
 
67
 
 
68
#define ASN_UNIVERSAL       ((u_char)0x00)
 
69
#define ASN_APPLICATION     ((u_char)0x40)
 
70
#define ASN_CONTEXT         ((u_char)0x80)
 
71
#define ASN_PRIVATE         ((u_char)0xC0)
 
72
 
 
73
#define ASN_PRIMITIVE       ((u_char)0x00)
 
74
#define ASN_CONSTRUCTOR     ((u_char)0x20)
 
75
 
 
76
#define ASN_LONG_LEN        (0x80)
 
77
#define ASN_EXTENSION_ID    (0x1F)
 
78
#define ASN_BIT8            (0x80)
 
79
 
 
80
#define IS_CONSTRUCTOR(byte)    ((byte) & ASN_CONSTRUCTOR)
 
81
#define IS_EXTENSION_ID(byte)   (((byte) & ASN_EXTENSION_ID) == ASN_EXTENSION_ID)
 
82
 
 
83
    struct counter64 {
 
84
        u_long          high;
 
85
        u_long          low;
 
86
    };
 
87
 
 
88
#ifdef OPAQUE_SPECIAL_TYPES
 
89
    typedef struct counter64 integer64;
 
90
    typedef struct counter64 unsigned64;
 
91
 
 
92
    /*
 
93
     * The BER inside an OPAQUE is an context specific with a value of 48 (0x30)
 
94
     * plus the "normal" tag. For a Counter64, the tag is 0x46 (i.e., an
 
95
     * applications specific tag with value 6). So the value for a 64 bit
 
96
     * counter is 0x46 + 0x30, or 0x76 (118 base 10). However, values
 
97
     * greater than 30 can not be encoded in one octet. So the first octet
 
98
     * has the class, in this case context specific (ASN_CONTEXT), and
 
99
     * the special value (i.e., 31) to indicate that the real value follows
 
100
     * in one or more octets. The high order bit of each following octet
 
101
     * indicates if the value is encoded in additional octets. A high order
 
102
     * bit of zero, indicates the last. For this "hack", only one octet
 
103
     * will be used for the value. 
 
104
     */
 
105
 
 
106
    /*
 
107
     * first octet of the tag 
 
108
     */
 
109
#define ASN_OPAQUE_TAG1 (ASN_CONTEXT | ASN_EXTENSION_ID)
 
110
    /*
 
111
     * base value for the second octet of the tag - the
 
112
     * second octet was the value for the tag 
 
113
     */
 
114
#define ASN_OPAQUE_TAG2 ((u_char)0x30)
 
115
 
 
116
#define ASN_OPAQUE_TAG2U ((u_char)0x2f) /* second octet of tag for union */
 
117
 
 
118
    /*
 
119
     * All the ASN.1 types for SNMP "should have been" defined in this file,
 
120
     * but they were not. (They are defined in snmp_impl.h)  Thus, the tag for
 
121
     * Opaque and Counter64 is defined, again, here with a different names. 
 
122
     */
 
123
#define ASN_APP_OPAQUE (ASN_APPLICATION | 4)
 
124
#define ASN_APP_COUNTER64 (ASN_APPLICATION | 6)
 
125
#define ASN_APP_FLOAT (ASN_APPLICATION | 8)
 
126
#define ASN_APP_DOUBLE (ASN_APPLICATION | 9)
 
127
#define ASN_APP_I64 (ASN_APPLICATION | 10)
 
128
#define ASN_APP_U64 (ASN_APPLICATION | 11)
 
129
#define ASN_APP_UNION (ASN_PRIVATE | 1) /* or ASN_PRIV_UNION ? */
 
130
 
 
131
    /*
 
132
     * value for Counter64 
 
133
     */
 
134
#define ASN_OPAQUE_COUNTER64 (ASN_OPAQUE_TAG2 + ASN_APP_COUNTER64)
 
135
    /*
 
136
     * max size of BER encoding of Counter64 
 
137
     */
 
138
#define ASN_OPAQUE_COUNTER64_MX_BER_LEN 12
 
139
 
 
140
    /*
 
141
     * value for Float 
 
142
     */
 
143
#define ASN_OPAQUE_FLOAT (ASN_OPAQUE_TAG2 + ASN_APP_FLOAT)
 
144
    /*
 
145
     * size of BER encoding of Float 
 
146
     */
 
147
#define ASN_OPAQUE_FLOAT_BER_LEN 7
 
148
 
 
149
    /*
 
150
     * value for Double 
 
151
     */
 
152
#define ASN_OPAQUE_DOUBLE (ASN_OPAQUE_TAG2 + ASN_APP_DOUBLE)
 
153
    /*
 
154
     * size of BER encoding of Double 
 
155
     */
 
156
#define ASN_OPAQUE_DOUBLE_BER_LEN 11
 
157
 
 
158
    /*
 
159
     * value for Integer64 
 
160
     */
 
161
#define ASN_OPAQUE_I64 (ASN_OPAQUE_TAG2 + ASN_APP_I64)
 
162
    /*
 
163
     * max size of BER encoding of Integer64 
 
164
     */
 
165
#define ASN_OPAQUE_I64_MX_BER_LEN 11
 
166
 
 
167
    /*
 
168
     * value for Unsigned64 
 
169
     */
 
170
#define ASN_OPAQUE_U64 (ASN_OPAQUE_TAG2 + ASN_APP_U64)
 
171
    /*
 
172
     * max size of BER encoding of Unsigned64 
 
173
     */
 
174
#define ASN_OPAQUE_U64_MX_BER_LEN 12
 
175
 
 
176
#endif                          /* OPAQUE_SPECIAL_TYPES */
 
177
 
 
178
 
 
179
#define ASN_PRIV_INCL_RANGE (ASN_PRIVATE | 2)
 
180
#define ASN_PRIV_EXCL_RANGE (ASN_PRIVATE | 3)
 
181
#define ASN_PRIV_DELEGATED  (ASN_PRIVATE | 5)
 
182
#define ASN_PRIV_IMPLIED_OCTET_STR  (ASN_PRIVATE | ASN_OCTET_STR)       /* 4 */
 
183
#define ASN_PRIV_IMPLIED_OBJECT_ID  (ASN_PRIVATE | ASN_OBJECT_ID)       /* 6 */
 
184
#define ASN_PRIV_RETRY      (ASN_PRIVATE | 7)
 
185
#define IS_DELEGATED(x)   ((x) == ASN_PRIV_DELEGATED)
 
186
 
 
187
 
 
188
    int             asn_check_packet(u_char *, size_t);
 
189
    u_char         *asn_parse_int(u_char *, size_t *, u_char *, long *,
 
190
                                  size_t);
 
191
    u_char         *asn_build_int(u_char *, size_t *, u_char, long *,
 
192
                                  size_t);
 
193
    u_char         *asn_parse_unsigned_int(u_char *, size_t *, u_char *,
 
194
                                           u_long *, size_t);
 
195
    u_char         *asn_build_unsigned_int(u_char *, size_t *, u_char,
 
196
                                           u_long *, size_t);
 
197
    u_char         *asn_parse_string(u_char *, size_t *, u_char *,
 
198
                                     u_char *, size_t *);
 
199
    u_char         *asn_build_string(u_char *, size_t *, u_char,
 
200
                                     const u_char *, size_t);
 
201
    u_char         *asn_parse_header(u_char *, size_t *, u_char *);
 
202
    u_char         *asn_parse_sequence(u_char *, size_t *, u_char *, u_char expected_type,      /* must be this type */
 
203
                                       const char *estr);       /* error message prefix */
 
204
    u_char         *asn_build_header(u_char *, size_t *, u_char, size_t);
 
205
    u_char         *asn_build_sequence(u_char *, size_t *, u_char, size_t);
 
206
    u_char         *asn_parse_length(u_char *, u_long *);
 
207
    u_char         *asn_build_length(u_char *, size_t *, size_t);
 
208
    u_char         *asn_parse_objid(u_char *, size_t *, u_char *, oid *,
 
209
                                    size_t *);
 
210
    u_char         *asn_build_objid(u_char *, size_t *, u_char, oid *,
 
211
                                    size_t);
 
212
    u_char         *asn_parse_null(u_char *, size_t *, u_char *);
 
213
    u_char         *asn_build_null(u_char *, size_t *, u_char);
 
214
    u_char         *asn_parse_bitstring(u_char *, size_t *, u_char *,
 
215
                                        u_char *, size_t *);
 
216
    u_char         *asn_build_bitstring(u_char *, size_t *, u_char,
 
217
                                        u_char *, size_t);
 
218
    u_char         *asn_parse_unsigned_int64(u_char *, size_t *, u_char *,
 
219
                                             struct counter64 *, size_t);
 
220
    u_char         *asn_build_unsigned_int64(u_char *, size_t *, u_char,
 
221
                                             struct counter64 *, size_t);
 
222
    u_char         *asn_parse_signed_int64(u_char *, size_t *, u_char *,
 
223
                                           struct counter64 *, size_t);
 
224
    u_char         *asn_build_signed_int64(u_char *, size_t *, u_char,
 
225
                                           struct counter64 *, size_t);
 
226
    u_char         *asn_build_float(u_char *, size_t *, u_char, float *,
 
227
                                    size_t);
 
228
    u_char         *asn_parse_float(u_char *, size_t *, u_char *, float *,
 
229
                                    size_t);
 
230
    u_char         *asn_build_double(u_char *, size_t *, u_char, double *,
 
231
                                     size_t);
 
232
    u_char         *asn_parse_double(u_char *, size_t *, u_char *,
 
233
                                     double *, size_t);
 
234
 
 
235
#ifdef USE_REVERSE_ASNENCODING
 
236
 
 
237
    /*
 
238
     * Re-allocator function for below.  
 
239
     */
 
240
 
 
241
    int             asn_realloc(u_char **, size_t *);
 
242
 
 
243
    /*
 
244
     * Re-allocating reverse ASN.1 encoder functions.  Synopsis:
 
245
     * 
 
246
     * u_char *buf = (u_char*)malloc(100);
 
247
     * u_char type = (ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_INTEGER);
 
248
     * size_t buf_len = 100, offset = 0;
 
249
     * long data = 12345;
 
250
     * int allow_realloc = 1;
 
251
     * 
 
252
     * if (asn_realloc_rbuild_int(&buf, &buf_len, &offset, allow_realloc,
 
253
     * type, &data, sizeof(long)) == 0) {
 
254
     * error;
 
255
     * }
 
256
     * 
 
257
     * NOTE WELL: after calling one of these functions with allow_realloc
 
258
     * non-zero, buf might have moved, buf_len might have grown and
 
259
     * offset will have increased by the size of the encoded data.
 
260
     * You should **NEVER** do something like this:
 
261
     * 
 
262
     * u_char *buf = (u_char *)malloc(100), *ptr;
 
263
     * u_char type = (ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_INTEGER);
 
264
     * size_t buf_len = 100, offset = 0;
 
265
     * long data1 = 1234, data2 = 5678;
 
266
     * int rc = 0, allow_realloc = 1;
 
267
     * 
 
268
     * rc  = asn_realloc_rbuild_int(&buf, &buf_len, &offset, allow_realloc,
 
269
     * type, &data1, sizeof(long));
 
270
     * ptr = buf[buf_len - offset];   / * points at encoding of data1 * /
 
271
     * if (rc == 0) {
 
272
     * error;
 
273
     * }
 
274
     * rc  = asn_realloc_rbuild_int(&buf, &buf_len, &offset, allow_realloc,
 
275
     * type, &data2, sizeof(long));
 
276
     * make use of ptr here;
 
277
     * 
 
278
     * 
 
279
     * ptr is **INVALID** at this point.  In general, you should store the
 
280
     * offset value and compute pointers when you need them:
 
281
     * 
 
282
     * 
 
283
     * 
 
284
     * u_char *buf = (u_char *)malloc(100), *ptr;
 
285
     * u_char type = (ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_INTEGER);
 
286
     * size_t buf_len = 100, offset = 0, ptr_offset;
 
287
     * long data1 = 1234, data2 = 5678;
 
288
     * int rc = 0, allow_realloc = 1;
 
289
     * 
 
290
     * rc  = asn_realloc_rbuild_int(&buf, &buf_len, &offset, allow_realloc,
 
291
     * type, &data1, sizeof(long));
 
292
     * ptr_offset = offset;
 
293
     * if (rc == 0) {
 
294
     * error;
 
295
     * }
 
296
     * rc  = asn_realloc_rbuild_int(&buf, &buf_len, &offset, allow_realloc,
 
297
     * type, &data2, sizeof(long));
 
298
     * ptr = buf + buf_len - ptr_offset
 
299
     * make use of ptr here;
 
300
     * 
 
301
     * 
 
302
     * 
 
303
     * Here, you can see that ptr will be a valid pointer even if the block of
 
304
     * memory has been moved, as it may well have been.  Plenty of examples of
 
305
     * usage all over asn1.c, snmp_api.c, snmpusm.c.
 
306
     * 
 
307
     * The other thing you should **NEVER** do is to pass a pointer to a buffer
 
308
     * on the stack as the first argument when allow_realloc is non-zero, unless
 
309
     * you really know what you are doing and your machine/compiler allows you to
 
310
     * free non-heap memory.  There are rumours that such things exist, but many
 
311
     * consider them no more than the wild tales of a fool.
 
312
     * 
 
313
     * Of course, you can pass allow_realloc as zero, to indicate that you do not
 
314
     * wish the packet buffer to be reallocated for some reason; perhaps because
 
315
     * it is on the stack.  This may be useful to emulate the functionality of
 
316
     * the old API:
 
317
     * 
 
318
     * u_char my_static_buffer[100], *cp = NULL;
 
319
     * size_t my_static_buffer_len = 100;
 
320
     * float my_pi = (float)22/(float)7;
 
321
     * 
 
322
     * cp = asn_rbuild_float(my_static_buffer, &my_static_buffer_len,
 
323
     * ASN_OPAQUE_FLOAT, &my_pi, sizeof(float));
 
324
     * if (cp == NULL) {
 
325
     * error;
 
326
     * }
 
327
     * 
 
328
     * 
 
329
     * IS EQUIVALENT TO:
 
330
     * 
 
331
     * 
 
332
     * u_char my_static_buffer[100];
 
333
     * size_t my_static_buffer_len = 100, my_offset = 0;
 
334
     * float my_pi = (float)22/(float)7;
 
335
     * int rc = 0;
 
336
     * 
 
337
     * rc = asn_realloc_rbuild_float(&my_static_buffer, &my_static_buffer_len,
 
338
     * &my_offset, 0,
 
339
     * ASN_OPAQUE_FLOAT, &my_pi, sizeof(float));
 
340
     * if (rc == 0) {
 
341
     * error;
 
342
     * }
 
343
     * 
 
344
     * 
 
345
     */
 
346
 
 
347
 
 
348
    int             asn_realloc_rbuild_int(u_char ** pkt, size_t * pkt_len,
 
349
                                           size_t * offset,
 
350
                                           int allow_realloc, u_char type,
 
351
                                           long *data, size_t data_size);
 
352
 
 
353
    int             asn_realloc_rbuild_string(u_char ** pkt,
 
354
                                              size_t * pkt_len,
 
355
                                              size_t * offset,
 
356
                                              int allow_realloc,
 
357
                                              u_char type,
 
358
                                              const u_char * data,
 
359
                                              size_t data_size);
 
360
 
 
361
    int             asn_realloc_rbuild_unsigned_int(u_char ** pkt,
 
362
                                                    size_t * pkt_len,
 
363
                                                    size_t * offset,
 
364
                                                    int allow_realloc,
 
365
                                                    u_char type,
 
366
                                                    u_long * data,
 
367
                                                    size_t data_size);
 
368
 
 
369
    int             asn_realloc_rbuild_header(u_char ** pkt,
 
370
                                              size_t * pkt_len,
 
371
                                              size_t * offset,
 
372
                                              int allow_realloc,
 
373
                                              u_char type,
 
374
                                              size_t data_size);
 
375
 
 
376
    int             asn_realloc_rbuild_sequence(u_char ** pkt,
 
377
                                                size_t * pkt_len,
 
378
                                                size_t * offset,
 
379
                                                int allow_realloc,
 
380
                                                u_char type,
 
381
                                                size_t data_size);
 
382
 
 
383
    int             asn_realloc_rbuild_length(u_char ** pkt,
 
384
                                              size_t * pkt_len,
 
385
                                              size_t * offset,
 
386
                                              int allow_realloc,
 
387
                                              size_t data_size);
 
388
 
 
389
    int             asn_realloc_rbuild_objid(u_char ** pkt,
 
390
                                             size_t * pkt_len,
 
391
                                             size_t * offset,
 
392
                                             int allow_realloc,
 
393
                                             u_char type, const oid *,
 
394
                                             size_t);
 
395
 
 
396
    int             asn_realloc_rbuild_null(u_char ** pkt,
 
397
                                            size_t * pkt_len,
 
398
                                            size_t * offset,
 
399
                                            int allow_realloc,
 
400
                                            u_char type);
 
401
 
 
402
    int             asn_realloc_rbuild_bitstring(u_char ** pkt,
 
403
                                                 size_t * pkt_len,
 
404
                                                 size_t * offset,
 
405
                                                 int allow_realloc,
 
406
                                                 u_char type,
 
407
                                                 u_char * data,
 
408
                                                 size_t data_size);
 
409
 
 
410
    int             asn_realloc_rbuild_unsigned_int64(u_char ** pkt,
 
411
                                                      size_t * pkt_len,
 
412
                                                      size_t * offset,
 
413
                                                      int allow_realloc,
 
414
                                                      u_char type,
 
415
                                                      struct counter64
 
416
                                                      *data, size_t);
 
417
 
 
418
    int             asn_realloc_rbuild_signed_int64(u_char ** pkt,
 
419
                                                    size_t * pkt_len,
 
420
                                                    size_t * offset,
 
421
                                                    int allow_realloc,
 
422
                                                    u_char type,
 
423
                                                    struct counter64 *data,
 
424
                                                    size_t);
 
425
 
 
426
    int             asn_realloc_rbuild_float(u_char ** pkt,
 
427
                                             size_t * pkt_len,
 
428
                                             size_t * offset,
 
429
                                             int allow_realloc,
 
430
                                             u_char type, float *data,
 
431
                                             size_t data_size);
 
432
 
 
433
    int             asn_realloc_rbuild_double(u_char ** pkt,
 
434
                                              size_t * pkt_len,
 
435
                                              size_t * offset,
 
436
                                              int allow_realloc,
 
437
                                              u_char type, double *data,
 
438
                                              size_t data_size);
 
439
#endif
 
440
 
 
441
#ifdef __cplusplus
 
442
}
 
443
#endif
 
444
#endif                          /* ASN1_H */