From Chris Wilson:
[obnox/wireshark/wip.git] / asn1.h
1 /* asn1.h
2  * Definitions for ASN.1 BER dissection
3  *
4  * $Id: asn1.h,v 1.15 2003/11/09 22:57:52 guy Exp $
5  *
6  * Ethereal - Network traffic analyzer
7  * By Gerald Combs <gerald@ethereal.com>
8  *
9  * Based on "g_asn1.h" from:
10  *
11  * GXSNMP -- An snmp mangament application
12  * Copyright (C) 1998 Gregory McLean & Jochen Friedrich
13  * Beholder RMON ethernet network monitor,Copyright (C) 1993 DNPAP group
14  *
15  * This program is free software; you can redistribute it and/or modify
16  * it under the terms of the GNU General Public License as published by
17  * the Free Software Foundation; either version 2 of the License, or
18  * (at your option) any later version.
19  *
20  * This program is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  * GNU General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with this program; if not, write to the Free Software
27  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28  *
29  */
30 #ifndef __ASN1_H__
31 #define __ASN1_H__
32
33 #define ASN1_UNI       0     /* Universal   */
34 #define ASN1_APL       1     /* Application */
35 #define ASN1_CTX       2     /* Context     */
36 #define ASN1_PRV       3     /* Private     */
37
38                              /* Tag                */
39 #define ASN1_EOC       0     /* End Of Contents    */
40 #define ASN1_BOL       1     /* Boolean            */
41 #define ASN1_INT       2     /* Integer            */
42 #define ASN1_BTS       3     /* Bit String         */
43 #define ASN1_OTS       4     /* Octet String       */
44 #define ASN1_NUL       5     /* Null               */
45 #define ASN1_OJI       6     /* Object Identifier  */
46 #define ASN1_OJD       7     /* Object Description */
47 #define ASN1_EXT       8     /* External           */
48 #define ASN1_REAL      9     /* Real               */
49 #define ASN1_ENUM      10    /* Enumerated         */
50 #define ASN1_SEQ       16    /* Sequence           */
51 #define ASN1_SET       17    /* Set                */
52 #define ASN1_NUMSTR    18    /* Numerical String   */
53 #define ASN1_PRNSTR    19    /* Printable String   */
54 #define ASN1_TEXSTR    20    /* Teletext String    */
55 #define ASN1_VIDSTR    21    /* Video String       */
56 #define ASN1_IA5STR    22    /* IA5 String         */
57 #define ASN1_UNITIM    23    /* Universal Time     */
58 #define ASN1_GENTIM    24    /* General Time       */
59 #define ASN1_GRASTR    25    /* Graphical String   */
60 #define ASN1_VISSTR    26    /* Visible String     */
61 #define ASN1_GENSTR    27    /* General String     */
62
63                              /* Primitive / Constructed */
64 #define ASN1_PRI     0       /* Primitive               */
65 #define ASN1_CON     1       /* Constructed             */
66
67 /*
68  * Oh, this is hellish.
69  *
70  * The CMU SNMP library defines an OID as a sequence of "u_int"s,
71  * unless EIGHTBIT_SUBIDS is defined, in which case it defines
72  * an OID as a sequence of "u_char"s.  None of its header files
73  * define EIGHTBIT_SUBIDS, and if a program defines it, that's
74  * not going to change the library to treat OIDs as sequences
75  * of "u_chars", so I'll assume that it'll be "u_int"s.
76  *
77  * The UCD SNMP library does the same, except it defines an OID
78  * as a sequence of "u_long"s, by default.
79  *
80  * "libsmi" defines it as a sequence of "unsigned int"s.
81  *
82  * I don't want to oblige all users of ASN.1 to include the SNMP
83  * library header files, so I'll assume none of the SNMP libraries
84  * will rudely surprise me by changing the definition; if they
85  * do, there will be compiler warnings, so we'll at least be able
86  * to catch it.
87  *
88  * This requires that, if you're going to use "asn1_subid_decode()",
89  * "asn1_oid_value_decode()", or "asn1_oid_decode()", you include
90  * "config.h", to get the right #defines defined, so that we properly
91  * typedef "subid_t".
92  */
93 #if defined(HAVE_SOME_SNMP)
94 typedef gulong  subid_t;        /* Net-SNMP or UCD SNMP */
95 #else
96 typedef guint   subid_t;        /* CMU SNMP, libsmi, or nothing */
97 #endif
98
99 #define ASN1_ERR_NOERROR                0       /* no error */
100 #define ASN1_ERR_EOC_MISMATCH           1
101 #define ASN1_ERR_WRONG_TYPE             2       /* type not right */
102 #define ASN1_ERR_LENGTH_NOT_DEFINITE    3       /* length should be definite */
103 #define ASN1_ERR_LENGTH_MISMATCH        4
104 #define ASN1_ERR_WRONG_LENGTH_FOR_TYPE  5       /* length wrong for type */
105
106 typedef struct _ASN1_SCK ASN1_SCK;
107
108 struct _ASN1_SCK
109 {                           /* ASN1 socket                         */
110     tvbuff_t *tvb;          /* Tvbuff whence the data comes        */
111     int offset;             /* Current offset in tvbuff            */
112 };
113
114 extern void asn1_open (ASN1_SCK *asn1, tvbuff_t *tvb, int offset);
115 extern void asn1_close (ASN1_SCK *asn1, int *offset);
116 extern int asn1_octet_decode (ASN1_SCK *asn1, guchar *ch);
117 extern int asn1_tag_decode (ASN1_SCK *asn1, guint *tag);
118 extern int asn1_id_decode (ASN1_SCK *asn1, guint *cls, guint *con, guint *tag);
119 extern int asn1_id_decode1 (ASN1_SCK *asn1, guint *tag);
120 extern int asn1_length_decode (ASN1_SCK *asn1, gboolean *def, guint *len);
121 extern int asn1_header_decode(ASN1_SCK *asn1, guint *cls, guint *con,
122                               guint *tag, gboolean *defp, guint *lenp);
123 extern int asn1_eoc (ASN1_SCK *asn1, int eoc);
124 extern int asn1_eoc_decode (ASN1_SCK *asn1, int eoc);
125 extern int asn1_null_decode (ASN1_SCK *asn1, int enc_len);
126 extern int asn1_bool_decode (ASN1_SCK *asn1, int enc_len, gboolean *boolean);
127 extern int asn1_int32_value_decode (ASN1_SCK *asn1, int enc_len,
128                                     gint32 *integer);
129 extern int asn1_int32_decode (ASN1_SCK *asn1, gint32 *integer, guint *nbytes);
130 extern int asn1_uint32_value_decode (ASN1_SCK *asn1, int enc_len,
131                                      guint32 *integer);
132 extern int asn1_uint32_decode (ASN1_SCK *asn1, guint32 *integer, guint *nbytes);
133 extern int asn1_bits_decode (ASN1_SCK *asn1, int enc_len, guchar **bits,
134                              guint *len, guchar *unused);
135 extern int asn1_string_value_decode (ASN1_SCK *asn1, int enc_len,
136                                      guchar **octets);
137 extern int asn1_string_decode (ASN1_SCK *asn1, guchar **octets, guint *str_len,
138                                guint *nbytes, guint expected_tag);
139 extern int asn1_octet_string_decode (ASN1_SCK *asn1, guchar **octets,
140                                      guint *str_len, guint *nbytes);
141 extern int asn1_subid_decode (ASN1_SCK *asn1, subid_t *subid);
142 extern int asn1_oid_value_decode (ASN1_SCK *asn1, int enc_len, subid_t **oid,
143                                   guint *len);
144 extern int asn1_oid_decode (ASN1_SCK *asn1, subid_t **oid, guint *len,
145                             guint *nbytes);
146 extern int asn1_sequence_decode (ASN1_SCK *asn1, guint *seq_len, guint *nbytes);
147
148 extern char *asn1_err_to_str (int err);
149
150 #endif