libnet  latest
A Portable Framework for Low-Level Network Packet Construction
libnet-asn1.h
1 /*
2  * $Id: libnet-asn1.h,v 1.3 2004/01/17 07:51:19 mike Exp $
3  *
4  * libnet-asn1.h - Network routine library ASN.1 header file
5  *
6  * Copyright (c) 1998 - 2004 Mike D. Schiffman <mike@infonexus.com>
7  * All rights reserved.
8  *
9  * Definitions for Abstract Syntax Notation One, ASN.1
10  * As defined in ISO/IS 8824 and ISO/IS 8825
11  *
12  * Copyright 1988, 1989 by Carnegie Mellon University
13  * All rights reserved.
14  *
15  * Permission to use, copy, modify, and distribute this software and its
16  * documentation for any purpose and without fee is hereby granted,
17  * provided that the above copyright notice appear in all copies and that
18  * both that copyright notice and this permission notice appear in
19  * supporting documentation, and that the name of CMU not be
20  * used in advertising or publicity pertaining to distribution of the
21  * software without specific, written prior permission.
22  *
23  * CMU DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
24  * ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
25  * CMU BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
26  * ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
27  * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
28  * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
29  * SOFTWARE.
30  *
31  * Copyright (c) 1998 - 2001 Mike D. Schiffman <mike@infonexus.com>
32  * All rights reserved.
33  *
34  * Redistribution and use in source and binary forms, with or without
35  * modification, are permitted provided that the following conditions
36  * are met:
37  * 1. Redistributions of source code must retain the above copyright
38  * notice, this list of conditions and the following disclaimer.
39  * 2. Redistributions in binary form must reproduce the above copyright
40  * notice, this list of conditions and the following disclaimer in the
41  * documentation and/or other materials provided with the distribution.
42  *
43  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
44  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
47  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
49  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
52  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53  * SUCH DAMAGE.
54  */
55 
56 #ifndef __LIBNET_ASN1_H
57 #define __LIBNET_ASN1_H
58 
59 #ifndef EIGHTBIT_SUBIDS
60 typedef uint32_t oid;
61 #define MAX_SUBID 0xFFFFFFFF
62 #else
63 typedef uint8_t oid;
64 #define MAX_SUBID 0xFF
65 #endif
66 
67 #define MAX_OID_LEN 64 /* max subid's in an oid */
68 
69 #define ASN_BOOLEAN (0x01)
70 #define ASN_INTEGER (0x02)
71 #define ASN_BIT_STR (0x03)
72 #define ASN_OCTET_STR (0x04)
73 #define ASN_NULL (0x05)
74 #define ASN_OBJECT_ID (0x06)
75 #define ASN_SEQUENCE (0x10)
76 #define ASN_SET (0x11)
77 
78 #define ASN_UNIVERSAL (0x00)
79 #define ASN_APPLICATION (0x40)
80 #define ASN_CONTEXT (0x80)
81 #define ASN_PRIVATE (0xC0)
82 
83 #define ASN_PRIMITIVE (0x00)
84 #define ASN_CONSTRUCTOR (0x20)
85 
86 #define ASN_LONG_LEN (0x80)
87 #define ASN_EXTENSION_ID (0x1F)
88 #define ASN_BIT8 (0x80)
89 
90 #define IS_CONSTRUCTOR(byte) ((byte) & ASN_CONSTRUCTOR)
91 #define IS_EXTENSION_ID(byte) (((byte) & ASN_EXTENSION_ID) = ASN_EXTENSION_ID)
92 
93 /*
94  * All of the build_asn1_* (build_asn1_length being an exception) functions
95  * take the same first 3 arguments:
96  *
97  * uint8_t *data: This is a pointer to the start of the data object to be
98  * manipulated.
99  * int *datalen: This is a pointer to the number of valid bytes following
100  * "data". This should be not be exceeded in any function.
101  * Upon exiting a function, this value will reflect the
102  * changed "data" and then refer to the new number of valid
103  * bytes until the end of "data".
104  * uint8_t type: The ASN.1 object type.
105  */
106 
107 
108 /*
109  * Builds an ASN object containing an integer.
110  *
111  * Returns NULL upon error or a pointer to the first byte past the end of
112  * this object (the start of the next object).
113  */
114 
115 uint8_t *
116 libnet_build_asn1_int(
117  uint8_t *, /* Pointer to the output buffer */
118  int *, /* Number of valid bytes left in the buffer */
119  uint8_t, /* ASN object type */
120  int32_t *, /* Pointer to a int32_t integer */
121  int /* Size of a int32_t integer */
122  );
123 
124 
125 /*
126  * Builds an ASN object containing an unsigned integer.
127  *
128  * Returns NULL upon error or a pointer to the first byte past the end of
129  * this object (the start of the next object).
130  */
131 
132 uint8_t *
133 libnet_build_asn1_uint(
134  uint8_t *, /* Pointer to the output buffer */
135  int *, /* Number of valid bytes left in the buffer */
136  uint8_t, /* ASN object type */
137  uint32_t *, /* Pointer to an unsigned int32_t integer */
138  int /* Size of a int32_t integer */
139  );
140 
141 
142 /*
143  * Builds an ASN object containing an octect string.
144  *
145  * Returns NULL upon error or a pointer to the first byte past the end of
146  * this object (the start of the next object).
147  */
148 
149 uint8_t *
150 libnet_build_asn1_string(
151  uint8_t *, /* Pointer to the output buffer */
152  int *, /* Number of valid bytes left in the buffer */
153  uint8_t, /* ASN object type */
154  uint8_t *, /* Pointer to a string to be built into an object */
155  int /* Size of the string */
156  );
157 
158 
159 /*
160  * Builds an ASN header for an object with the ID and length specified. This
161  * only works on data types < 30, i.e. no extension octets. The maximum
162  * length is 0xFFFF;
163  *
164  * Returns a pointer to the first byte of the contents of this object or
165  * NULL upon error
166  */
167 
168 uint8_t *
169 libnet_build_asn1_header(
170  uint8_t *, /* Pointer to the start of the object */
171  int *, /* Number of valid bytes left in buffer */
172  uint8_t, /* ASN object type */
173  int /* ASN object length */
174  );
175 
176 
177 uint8_t *
178 libnet_build_asn1_length(
179  uint8_t *, /* Pointer to start of object */
180  int *, /* Number of valid bytes in buffer */
181  int /* Length of object */
182  );
183 
184 
185 /*
186  * Builds an ASN header for a sequence with the ID and length specified.
187  *
188  * This only works on data types < 30, i.e. no extension octets.
189  * The maximum length is 0xFFFF;
190  *
191  * Returns a pointer to the first byte of the contents of this object.
192  * Returns NULL on any error.
193  */
194 
195 uint8_t *
196 libnet_build_asn1_sequence(
197  uint8_t *,
198  int *,
199  uint8_t,
200  int
201  );
202 
203 
204 /*
205  * Builds an ASN object identifier object containing the input string.
206  *
207  * Returns NULL upon error or a pointer to the first byte past the end of
208  * this object (the start of the next object).
209  */
210 
211 uint8_t *
212 libnet_build_asn1_objid(
213  uint8_t *,
214  int *,
215  uint8_t,
216  oid *,
217  int
218  );
219 
220 
221 /*
222  * Builds an ASN null object.
223  *
224  * Returns NULL upon error or a pointer to the first byte past the end of
225  * this object (the start of the next object).
226  */
227 
228 uint8_t *
229 libnet_build_asn1_null(
230  uint8_t *,
231  int *,
232  uint8_t
233  );
234 
235 
236 /*
237  * Builds an ASN bitstring.
238  *
239  * Returns NULL upon error or a pointer to the first byte past the end of
240  * this object (the start of the next object).
241  */
242 
243 uint8_t *
244 libnet_build_asn1_bitstring(
245  uint8_t *,
246  int *,
247  uint8_t,
248  uint8_t *, /* Pointer to the input buffer */
249  int /* Length of the input buffer */
250  );
251 
252 
253 #endif /* __LIBNET_ASN1_H */
254