xref: /freebsd/crypto/heimdal/lib/asn1/der.h (revision 23f282aa31e9b6fceacd449020e936e98d6f2298)
1 /*
2  * Copyright (c) 1997, 1998, 1999 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.18 1999/12/02 17:05:01 joda 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 void 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_general_string (const unsigned char*, size_t,
91 			   general_string*, size_t*);
92 int decode_octet_string (const unsigned char*, size_t, octet_string*, size_t*);
93 int decode_generalized_time (const unsigned char*, size_t, time_t*, size_t*);
94 
95 int der_put_int (unsigned char *p, size_t len, int val, size_t*);
96 int der_put_length (unsigned char *p, size_t len, size_t val, size_t*);
97 int der_put_general_string (unsigned char *p, size_t len,
98 			    const general_string *str, size_t*);
99 int der_put_octet_string (unsigned char *p, size_t len,
100 			  const octet_string *data, size_t*);
101 int der_put_tag (unsigned char *p, size_t len, Der_class class, Der_type type,
102 		 int tag, size_t*);
103 int der_put_length_and_tag (unsigned char*, size_t, size_t,
104 			    Der_class, Der_type, int, size_t*);
105 
106 int encode_integer (unsigned char *p, size_t len,
107 		    const int *data, size_t*);
108 int encode_general_string (unsigned char *p, size_t len,
109 			   const general_string *data, size_t*);
110 int encode_octet_string (unsigned char *p, size_t len,
111 			 const octet_string *k, size_t*);
112 int encode_generalized_time (unsigned char *p, size_t len,
113 			     const time_t *t, size_t*);
114 
115 void free_integer (int *num);
116 void free_general_string (general_string *str);
117 void free_octet_string (octet_string *k);
118 void free_generalized_time (time_t *t);
119 
120 size_t length_len (size_t len);
121 size_t length_integer (const int *data);
122 size_t length_general_string (const general_string *data);
123 size_t length_octet_string (const octet_string *k);
124 size_t length_generalized_time (const time_t *t);
125 
126 int copy_general_string (const general_string *from, general_string *to);
127 int copy_octet_string (const octet_string *from, octet_string *to);
128 
129 int fix_dce(size_t reallen, size_t *len);
130 
131 #endif /* __DER_H__ */
132 
133