der.h revision 72445
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
41typedef enum {UNIV = 0, APPL = 1, CONTEXT = 2 , PRIVATE = 3} Der_class;
42
43typedef enum {PRIM = 0, CONS = 1} Der_type;
44
45/* Universal tags */
46
47enum {
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
66time_t timegm (struct tm *);
67#endif
68
69int time2generalizedtime (time_t t, octet_string *s);
70
71int der_get_int (const unsigned char *p, size_t len, int *ret, size_t *size);
72int der_get_length (const unsigned char *p, size_t len,
73		    size_t *val, size_t *size);
74int der_get_general_string (const unsigned char *p, size_t len,
75			    general_string *str, size_t *size);
76int der_get_octet_string (const unsigned char *p, size_t len,
77			  octet_string *data, size_t *size);
78int der_get_tag (const unsigned char *p, size_t len,
79		 Der_class *class, Der_type *type,
80		 int *tag, size_t *size);
81
82int der_match_tag (const unsigned char *p, size_t len,
83		   Der_class class, Der_type type,
84		   int tag, size_t *size);
85int 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
89int decode_integer (const unsigned char*, size_t, int*, size_t*);
90int decode_unsigned (const unsigned char*, size_t, unsigned*, size_t*);
91int decode_general_string (const unsigned char*, size_t,
92			   general_string*, size_t*);
93int decode_octet_string (const unsigned char*, size_t, octet_string*, size_t*);
94int decode_generalized_time (const unsigned char*, size_t, time_t*, size_t*);
95
96int der_put_int (unsigned char *p, size_t len, int val, size_t*);
97int der_put_length (unsigned char *p, size_t len, size_t val, size_t*);
98int der_put_general_string (unsigned char *p, size_t len,
99			    const general_string *str, size_t*);
100int der_put_octet_string (unsigned char *p, size_t len,
101			  const octet_string *data, size_t*);
102int der_put_tag (unsigned char *p, size_t len, Der_class class, Der_type type,
103		 int tag, size_t*);
104int der_put_length_and_tag (unsigned char*, size_t, size_t,
105			    Der_class, Der_type, int, size_t*);
106
107int encode_integer (unsigned char *p, size_t len,
108		    const int *data, size_t*);
109int encode_unsigned (unsigned char *p, size_t len,
110		     const unsigned *data, size_t*);
111int encode_general_string (unsigned char *p, size_t len,
112			   const general_string *data, size_t*);
113int encode_octet_string (unsigned char *p, size_t len,
114			 const octet_string *k, size_t*);
115int encode_generalized_time (unsigned char *p, size_t len,
116			     const time_t *t, size_t*);
117
118void free_integer (int *num);
119void free_general_string (general_string *str);
120void free_octet_string (octet_string *k);
121void free_generalized_time (time_t *t);
122
123size_t length_len (size_t len);
124size_t length_integer (const int *data);
125size_t length_unsigned (const unsigned *data);
126size_t length_general_string (const general_string *data);
127size_t length_octet_string (const octet_string *k);
128size_t length_generalized_time (const time_t *t);
129
130int copy_general_string (const general_string *from, general_string *to);
131int copy_octet_string (const octet_string *from, octet_string *to);
132
133int fix_dce(size_t reallen, size_t *len);
134
135#endif /* __DER_H__ */
136
137