r9221: Try to merge Heimdal across from lorikeet-heimdal to samba4.
[kai/samba.git] / source / heimdal / lib / asn1 / der.h
1 /*
2  * Copyright (c) 1997 - 2005 Kungliga Tekniska Högskolan
3  * (Royal Institute of Technology, Stockholm, Sweden). 
4  * All rights reserved. 
5  *
6  * Redistribution and use in source and binary forms, with or without 
7  * modification, are permitted provided that the following conditions 
8  * are met: 
9  *
10  * 1. Redistributions of source code must retain the above copyright 
11  *    notice, this list of conditions and the following disclaimer. 
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright 
14  *    notice, this list of conditions and the following disclaimer in the 
15  *    documentation and/or other materials provided with the distribution. 
16  *
17  * 3. Neither the name of the Institute nor the names of its contributors 
18  *    may be used to endorse or promote products derived from this software 
19  *    without specific prior written permission. 
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 
31  * SUCH DAMAGE. 
32  */
33
34 /* $Id: der.h,v 1.29 2005/07/12 06:27:19 lha Exp $ */
35
36 #ifndef __DER_H__
37 #define __DER_H__
38
39 typedef enum {
40     ASN1_C_UNIV = 0,
41     ASN1_C_APPL = 1,
42     ASN1_C_CONTEXT = 2,
43     ASN1_C_PRIVATE = 3
44 } Der_class;
45
46 typedef enum {PRIM = 0, CONS = 1} Der_type;
47
48 #define MAKE_TAG(CLASS, TYPE, TAG)  (((CLASS) << 6) | ((TYPE) << 5) | (TAG))
49
50 /* Universal tags */
51
52 enum {
53     UT_EndOfContent     = 0,
54     UT_Boolean          = 1,
55     UT_Integer          = 2,    
56     UT_BitString        = 3,
57     UT_OctetString      = 4,
58     UT_Null             = 5,
59     UT_OID              = 6,
60     UT_Enumerated       = 10,
61     UT_UTF8String       = 12,
62     UT_Sequence         = 16,
63     UT_Set              = 17,
64     UT_PrintableString  = 19,
65     UT_IA5String        = 22,
66     UT_UTCTime          = 23,
67     UT_GeneralizedTime  = 24,
68     UT_VisibleString    = 26,
69     UT_GeneralString    = 27,
70     /* unsupported types */
71     UT_ObjectDescriptor = 7,
72     UT_External         = 8,
73     UT_Real             = 9,
74     UT_EmbeddedPDV      = 11,
75     UT_RelativeOID      = 13,
76     UT_NumericString    = 18,
77     UT_TeletexString    = 20,
78     UT_VideotexString   = 21,
79     UT_GraphicString    = 25,
80     UT_UniversalString  = 25,
81     UT_BMPString        = 30,
82 };
83
84 #define ASN1_INDEFINITE 0xdce0deed
85
86 typedef struct asn1_der_time_t {
87     time_t dt_sec;
88     unsigned long dt_nsec;
89 } asn1_der_time_t;
90
91 typedef struct asn1_ber_time_t {
92     time_t bt_sec;
93     unsigned bt_nsec;
94     int bt_zone;
95 } asn1_ber_time_t;
96
97 int der_get_unsigned (const unsigned char *p, size_t len,
98                       unsigned *ret, size_t *size);
99 int der_get_integer (const unsigned char *p, size_t len,
100                      int *ret, size_t *size);
101 int der_get_heim_integer (const unsigned char *p, size_t len,
102                           heim_integer *ret, size_t *size);
103 int der_get_boolean(const unsigned char *p, size_t len,
104                     int *data, size_t *size);
105 int der_get_length (const unsigned char *p, size_t len,
106                     size_t *val, size_t *size);
107 int der_get_general_string (const unsigned char *p, size_t len, 
108                             heim_general_string *str, size_t *size);
109 int der_get_utf8string (const unsigned char *p, size_t len, 
110                             heim_utf8_string *str, size_t *size);
111 int der_get_universal_string (const unsigned char *p, size_t len, 
112                              heim_universal_string *str, size_t *size);
113 int der_get_bmp_string (const unsigned char *p, size_t len, 
114                         heim_bmp_string *str, size_t *size);
115 int der_get_printable_string (const unsigned char *p, size_t len, 
116                             heim_printable_string *str, size_t *size);
117 int der_get_ia5_string (const unsigned char *p, size_t len,
118                         heim_ia5_string *str, size_t *size);
119 int der_get_octet_string (const unsigned char *p, size_t len, 
120                           heim_octet_string *data, size_t *size);
121 int der_get_generalized_time (const unsigned char *p, size_t len, 
122                               time_t *data, size_t *size);
123 int der_get_generalized_time_der (const unsigned char *p, size_t len, 
124                                   asn1_der_time_t *data, size_t *size);
125 int der_get_generalized_time_ber (const unsigned char *p, size_t len, 
126                                   asn1_ber_time_t *data, size_t *size);
127 int der_get_utctime (const unsigned char *p, size_t len, 
128                      time_t *data, size_t *size);
129 int der_get_oid (const unsigned char *p, size_t len,
130                  heim_oid *data, size_t *size);
131 int der_get_bit_string (const unsigned char *p, size_t len,
132                         heim_bit_string *data, size_t *size);
133 int der_get_tag (const unsigned char *p, size_t len, 
134                  Der_class *class, Der_type *type,
135                  unsigned int *tag, size_t *size);
136
137 int der_match_tag (const unsigned char *p, size_t len, 
138                    Der_class class, Der_type type,
139                    unsigned int tag, size_t *size);
140 int der_match_tag_and_length (const unsigned char *p, size_t len,
141                               Der_class class, Der_type type, unsigned int tag,
142                               size_t *length_ret, size_t *size);
143
144 int der_put_unsigned (unsigned char *p, size_t len, const unsigned *val, size_t*);
145 int der_put_integer (unsigned char *p, size_t len, const int *val, size_t*);
146 int der_put_heim_integer (unsigned char *p, size_t len, 
147                           const heim_integer *val, size_t*);
148 int der_put_boolean (unsigned char *p, size_t len, const int *val, size_t*);
149
150 int der_put_length (unsigned char *p, size_t len, size_t val, size_t*);
151 int der_put_general_string (unsigned char *p, size_t len,
152                             const heim_general_string *str, size_t*);
153 int der_put_utf8string (unsigned char *p, size_t len,
154                         const heim_utf8_string *str, size_t*);
155 int der_put_universal_string (unsigned char *p, size_t len,
156                               const heim_universal_string *str, size_t*);
157 int der_put_bmp_string (unsigned char *p, size_t len,
158                             const heim_bmp_string *str, size_t*);
159 int der_put_printable_string (unsigned char *p, size_t len,
160                             const heim_printable_string *str, size_t*);
161 int der_put_ia5_string (unsigned char *p, size_t len,
162                         const heim_ia5_string *str, size_t*);
163 int der_put_octet_string (unsigned char *p, size_t len,
164                           const heim_octet_string *data, size_t*);
165 int der_put_generalized_time (unsigned char *p, size_t len, 
166                               const time_t *data, size_t *size);
167 int der_put_utctime (unsigned char *p, size_t len, 
168                      const time_t *data, size_t *size);
169 int der_put_oid (unsigned char *p, size_t len,
170                  const heim_oid *data, size_t *size);
171 int der_put_bit_string (unsigned char *p, size_t len,
172                         const heim_bit_string *data, size_t *size);
173 int der_put_tag (unsigned char *p, size_t len, Der_class class, Der_type type,
174                  unsigned int tag, size_t*);
175 int der_put_length_and_tag (unsigned char*, size_t, size_t, 
176                             Der_class, Der_type, unsigned int, size_t*);
177
178 void free_integer (int *num);
179 void free_heim_integer (heim_integer *num);
180 void free_octet_string (heim_octet_string *k);
181 void free_general_string (heim_general_string *str);
182 void free_octet_string (heim_octet_string *k);
183 void free_oid (heim_oid *k);
184 void free_bit_string (heim_bit_string *k);
185 void free_generalized_time (time_t *t);
186 void free_utctime (time_t *t);
187 void free_utf8string (heim_utf8_string*);
188 void free_printable_string (heim_printable_string*);
189 void free_ia5_string (heim_ia5_string*);
190 void free_universal_string (heim_universal_string*);
191 void free_bmp_string (heim_bmp_string*);
192
193 size_t length_len (size_t len);
194 size_t length_integer (const int *data);
195 size_t length_heim_integer (const heim_integer *data);
196 size_t length_unsigned (const unsigned *data);
197 size_t length_enumerated (const unsigned *data);
198 size_t length_general_string (const heim_general_string *data);
199 size_t length_octet_string (const heim_octet_string *k);
200 size_t length_oid (const heim_oid *k);
201 size_t length_bit_string (const heim_bit_string *k);
202 size_t length_generalized_time (const time_t *t);
203 size_t length_utctime (const time_t *t);
204 size_t length_utf8string (const heim_utf8_string*);
205 size_t length_printable_string (const heim_printable_string*);
206 size_t length_ia5_string (const heim_ia5_string*);
207 size_t length_bmp_string (const heim_bmp_string*);
208 size_t length_universal_string (const heim_universal_string*);
209 size_t length_boolean (const int*);
210
211 int copy_heim_integer (const heim_integer *, heim_integer *);
212 int copy_general_string (const heim_general_string *, heim_general_string *);
213 int copy_octet_string (const heim_octet_string *, heim_octet_string *);
214 int copy_oid (const heim_oid *from, heim_oid *to);
215 int copy_bit_string (const heim_bit_string *from, heim_bit_string *to);
216 int copy_utf8string (const heim_utf8_string*, heim_utf8_string*);
217 int copy_printable_string (const heim_printable_string*,heim_printable_string*);
218 int copy_ia5_string (const heim_ia5_string*,heim_ia5_string*);
219 int copy_universal_string(const heim_universal_string*,heim_universal_string*);
220 int copy_bmp_string (const heim_bmp_string*,heim_bmp_string*);
221
222 int heim_oid_cmp(const heim_oid *, const heim_oid *);
223 int heim_octet_string_cmp(const heim_octet_string *,const heim_octet_string *);
224 int heim_bit_string_cmp(const heim_bit_string *, const heim_bit_string *);
225 int heim_integer_cmp(const heim_integer *, const heim_integer *);
226 int heim_bmp_string_cmp(const heim_bmp_string *, const heim_bmp_string *);
227 int heim_universal_string_cmp(const heim_universal_string *, 
228                               const heim_universal_string *);
229
230 int der_parse_oid(const char *, heim_oid *);
231
232 int _heim_fix_dce(size_t reallen, size_t *len);
233 int _heim_der_set_sort(const void *, const void *);
234 int _heim_time2generalizedtime (time_t, heim_octet_string *, int);
235
236 const char *    der_get_class_name(unsigned);
237 int             der_get_class_num(const char *);
238 const char *    der_get_type_name(unsigned);
239 int             der_get_type_num(const char *);
240 const char *    der_get_tag_name(unsigned);
241 int             der_get_tag_num(const char *);
242
243 #endif /* __DER_H__ */