xref: /freebsd/crypto/heimdal/lib/asn1/der.h (revision 1b6c76a2fe091c74f08427e6c870851025a9cf67)
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.20 2001/01/29 08:31:27 assar Exp $ */
35 
36 #ifndef __DER_H__
37 #define __DER_H__
38 
39 #include <time.h>
40 
41 typedef enum {UNIV = 0, APPL = 1, CONTEXT = 2 , PRIVATE = 3} Der_class;
42 
43 typedef enum {PRIM = 0, CONS = 1} Der_type;
44 
45 /* Universal tags */
46 
47 enum {
48      UT_Integer		= 2,
49      UT_BitString	= 3,
50      UT_OctetString	= 4,
51      UT_Null		= 5,
52      UT_ObjID		= 6,
53      UT_Sequence	= 16,
54      UT_Set		= 17,
55      UT_PrintableString	= 19,
56      UT_IA5String	= 22,
57      UT_UTCTime		= 23,
58      UT_GeneralizedTime	= 24,
59      UT_VisibleString	= 26,
60      UT_GeneralString	= 27
61 };
62 
63 #define ASN1_INDEFINITE 0xdce0deed
64 
65 #ifndef HAVE_TIMEGM
66 time_t timegm (struct tm *);
67 #endif
68 
69 int time2generalizedtime (time_t t, octet_string *s);
70 
71 int der_get_int (const unsigned char *p, size_t len, int *ret, size_t *size);
72 int der_get_length (const unsigned char *p, size_t len,
73 		    size_t *val, size_t *size);
74 int der_get_general_string (const unsigned char *p, size_t len,
75 			    general_string *str, size_t *size);
76 int der_get_octet_string (const unsigned char *p, size_t len,
77 			  octet_string *data, size_t *size);
78 int der_get_tag (const unsigned char *p, size_t len,
79 		 Der_class *class, Der_type *type,
80 		 int *tag, size_t *size);
81 
82 int der_match_tag (const unsigned char *p, size_t len,
83 		   Der_class class, Der_type type,
84 		   int tag, size_t *size);
85 int der_match_tag_and_length (const unsigned char *p, size_t len,
86 			      Der_class class, Der_type type, int tag,
87 			      size_t *length_ret, size_t *size);
88 
89 int decode_integer (const unsigned char*, size_t, int*, size_t*);
90 int decode_unsigned (const unsigned char*, size_t, unsigned*, size_t*);
91 int decode_general_string (const unsigned char*, size_t,
92 			   general_string*, size_t*);
93 int decode_octet_string (const unsigned char*, size_t, octet_string*, size_t*);
94 int decode_generalized_time (const unsigned char*, size_t, time_t*, size_t*);
95 
96 int der_put_int (unsigned char *p, size_t len, int val, size_t*);
97 int der_put_length (unsigned char *p, size_t len, size_t val, size_t*);
98 int der_put_general_string (unsigned char *p, size_t len,
99 			    const general_string *str, size_t*);
100 int der_put_octet_string (unsigned char *p, size_t len,
101 			  const octet_string *data, size_t*);
102 int der_put_tag (unsigned char *p, size_t len, Der_class class, Der_type type,
103 		 int tag, size_t*);
104 int der_put_length_and_tag (unsigned char*, size_t, size_t,
105 			    Der_class, Der_type, int, size_t*);
106 
107 int encode_integer (unsigned char *p, size_t len,
108 		    const int *data, size_t*);
109 int encode_unsigned (unsigned char *p, size_t len,
110 		     const unsigned *data, size_t*);
111 int encode_general_string (unsigned char *p, size_t len,
112 			   const general_string *data, size_t*);
113 int encode_octet_string (unsigned char *p, size_t len,
114 			 const octet_string *k, size_t*);
115 int encode_generalized_time (unsigned char *p, size_t len,
116 			     const time_t *t, size_t*);
117 
118 void free_integer (int *num);
119 void free_general_string (general_string *str);
120 void free_octet_string (octet_string *k);
121 void free_generalized_time (time_t *t);
122 
123 size_t length_len (size_t len);
124 size_t length_integer (const int *data);
125 size_t length_unsigned (const unsigned *data);
126 size_t length_general_string (const general_string *data);
127 size_t length_octet_string (const octet_string *k);
128 size_t length_generalized_time (const time_t *t);
129 
130 int copy_general_string (const general_string *from, general_string *to);
131 int copy_octet_string (const octet_string *from, octet_string *to);
132 
133 int fix_dce(size_t reallen, size_t *len);
134 
135 #endif /* __DER_H__ */
136 
137