6c80842ff807c70ec3bcd90668afcee0050ae6bd
[samba.git] / source / heimdal / lib / asn1 / der.h
1 /*
2  * Copyright (c) 1997 - 2001 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.28 2005/05/29 14:23:00 lha Exp $ */
35
36 #ifndef __DER_H__
37 #define __DER_H__
38
39 #include <time.h>
40
41 typedef enum {
42     ASN1_C_UNIV = 0,
43     ASN1_C_APPL = 1,
44     ASN1_C_CONTEXT = 2 ,
45     ASN1_C_PRIVATE = 3
46 } Der_class;
47
48 typedef enum {PRIM = 0, CONS = 1} Der_type;
49
50 /* Universal tags */
51
52 enum {
53      UT_Boolean         = 1,
54      UT_Integer         = 2,
55      UT_BitString       = 3,
56      UT_OctetString     = 4,
57      UT_Null            = 5,
58      UT_OID             = 6,
59      UT_Enumerated      = 10,
60      UT_UTF8String      = 12,
61      UT_Sequence        = 16,
62      UT_Set             = 17,
63      UT_PrintableString = 19,
64      UT_IA5String       = 22,
65      UT_UTCTime         = 23,
66      UT_GeneralizedTime = 24,
67      UT_VisibleString   = 26,
68      UT_GeneralString   = 27
69 };
70
71 #define ASN1_INDEFINITE 0xdce0deed
72
73 #ifndef HAVE_TIMEGM
74 time_t timegm (struct tm *);
75 #endif
76
77 int time2generalizedtime (time_t t, heim_octet_string *s);
78
79 int der_get_int (const unsigned char *p, size_t len, int *ret, size_t *size);
80 int der_get_length (const unsigned char *p, size_t len,
81                     size_t *val, size_t *size);
82 int der_get_boolean (const unsigned char *p, size_t len,
83                      int *data, size_t *size);
84 int der_get_general_string (const unsigned char *p, size_t len, 
85                             heim_general_string *str, size_t *size);
86 int der_get_octet_string (const unsigned char *p, size_t len, 
87                           heim_octet_string *data, size_t *size);
88 int der_get_oid (const unsigned char *p, size_t len,
89                  heim_oid *data, size_t *size);
90 int der_get_tag (const unsigned char *p, size_t len, 
91                  Der_class *class, Der_type *type,
92                  int *tag, size_t *size);
93
94 int der_match_tag (const unsigned char *p, size_t len, 
95                    Der_class class, Der_type type,
96                    int tag, size_t *size);
97 int der_match_tag_and_length (const unsigned char *p, size_t len,
98                               Der_class class, Der_type type, int tag,
99                               size_t *length_ret, size_t *size);
100
101 int decode_boolean (const unsigned char*, size_t, int*, size_t*);
102 int decode_integer (const unsigned char*, size_t, int*, size_t*);
103 int decode_unsigned (const unsigned char*, size_t, unsigned*, size_t*);
104 int decode_enumerated (const unsigned char*, size_t, unsigned*, size_t*);
105 int decode_general_string (const unsigned char*, size_t,
106                            heim_general_string*, size_t*);
107 int decode_oid (const unsigned char *p, size_t len, 
108                 heim_oid *k, size_t *size);
109 int decode_octet_string (const unsigned char*, size_t, 
110                          heim_octet_string*, size_t*);
111 int decode_generalized_time (const unsigned char*, size_t, time_t*, size_t*);
112 int decode_nulltype (const unsigned char*, size_t, size_t*);
113 int decode_utf8string (const unsigned char*, size_t, 
114                        heim_utf8_string*, size_t*);
115
116 int der_put_int (unsigned char *p, size_t len, int val, size_t*);
117 int der_put_length (unsigned char *p, size_t len, size_t val, size_t*);
118 int der_put_boolean (unsigned char *p, size_t len, const int *data, size_t*);
119 int der_put_general_string (unsigned char *p, size_t len,
120                             const heim_general_string *str, size_t*);
121 int der_put_octet_string (unsigned char *p, size_t len,
122                           const heim_octet_string *data, size_t*);
123 int der_put_oid (unsigned char *p, size_t len,
124                  const heim_oid *data, size_t *size);
125 int der_put_tag (unsigned char *p, size_t len, Der_class class, Der_type type,
126                  int tag, size_t*);
127 int der_put_length_and_tag (unsigned char*, size_t, size_t, 
128                             Der_class, Der_type, int, size_t*);
129
130 int encode_boolean (unsigned char *p, size_t len,
131                     const int *data, size_t*);
132 int encode_integer (unsigned char *p, size_t len,
133                     const int *data, size_t*);
134 int encode_unsigned (unsigned char *p, size_t len,
135                      const unsigned *data, size_t*);
136 int encode_enumerated (unsigned char *p, size_t len,
137                        const unsigned *data, size_t*);
138 int encode_general_string (unsigned char *p, size_t len, 
139                            const heim_general_string *data, size_t*);
140 int encode_octet_string (unsigned char *p, size_t len,
141                          const heim_octet_string *k, size_t*);
142 int encode_oid (unsigned char *p, size_t len,
143                 const heim_oid *k, size_t*);
144 int encode_generalized_time (unsigned char *p, size_t len,
145                              const time_t *t, size_t*);
146 int encode_nulltype (unsigned char*, size_t, size_t*);
147 int encode_utf8string (unsigned char*, size_t, 
148                        const heim_utf8_string*, size_t*);
149
150 void free_integer (int *num);
151 void free_general_string (heim_general_string *str);
152 void free_octet_string (heim_octet_string *k);
153 void free_oid (heim_oid *k);
154 void free_generalized_time (time_t *t);
155 void free_utf8string (heim_utf8_string*);
156
157 size_t length_len (size_t len);
158 size_t length_boolean (const int *data);
159 size_t length_integer (const int *data);
160 size_t length_unsigned (const unsigned *data);
161 size_t length_enumerated (const unsigned *data);
162 size_t length_general_string (const heim_general_string *data);
163 size_t length_octet_string (const heim_octet_string *k);
164 size_t length_oid (const heim_oid *k);
165 size_t length_generalized_time (const time_t *t);
166 size_t length_nulltype (void);
167 size_t length_utf8string (const heim_utf8_string*);
168
169 int copy_general_string (const heim_general_string *, heim_general_string *);
170 int copy_octet_string (const heim_octet_string *, heim_octet_string *);
171 int copy_oid (const heim_oid *from, heim_oid *to);
172 int copy_nulltype (void *, void *);
173 int copy_utf8string (const heim_utf8_string*, heim_utf8_string*);
174
175 int heim_oid_cmp(const heim_oid *, const heim_oid *);
176 int heim_octet_string_cmp(const heim_octet_string *,const heim_octet_string *);
177
178 int fix_dce(size_t reallen, size_t *len);
179
180 #endif /* __DER_H__ */