and another
[strongswan.git] / src / libstrongswan / asn1 / asn1.h
1 /*
2 * Copyright (C) 2006 Martin Will
3 * Copyright (C) 2000-2008 Andreas Steffen
4 *
5 * Hochschule fuer Technik Rapperswil
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 * for more details.
16 *
17 * $Id$
18 */
19
20 /**
21 * @defgroup asn1 asn1
22 * @{ @ingroup asn1
23 */
24
25 #ifndef ASN1_H_
26 #define ASN1_H_
27
28 #include <stdarg.h>
29 #include <gmp.h>
30
31 #include <library.h>
32 #include <asn1/oid.h>
33
34
35 /**
36 * Definition of some primitive ASN1 types
37 */
38 typedef enum {
39 ASN1_EOC = 0x00,
40 ASN1_BOOLEAN = 0x01,
41 ASN1_INTEGER = 0x02,
42 ASN1_BIT_STRING = 0x03,
43 ASN1_OCTET_STRING = 0x04,
44 ASN1_NULL = 0x05,
45 ASN1_OID = 0x06,
46 ASN1_ENUMERATED = 0x0A,
47 ASN1_UTF8STRING = 0x0C,
48 ASN1_NUMERICSTRING = 0x12,
49 ASN1_PRINTABLESTRING = 0x13,
50 ASN1_T61STRING = 0x14,
51 ASN1_VIDEOTEXSTRING = 0x15,
52 ASN1_IA5STRING = 0x16,
53 ASN1_UTCTIME = 0x17,
54 ASN1_GENERALIZEDTIME = 0x18,
55 ASN1_GRAPHICSTRING = 0x19,
56 ASN1_VISIBLESTRING = 0x1A,
57 ASN1_GENERALSTRING = 0x1B,
58 ASN1_UNIVERSALSTRING = 0x1C,
59 ASN1_BMPSTRING = 0x1E,
60
61 ASN1_CONSTRUCTED = 0x20,
62
63 ASN1_SEQUENCE = 0x30,
64
65 ASN1_SET = 0x31,
66
67 ASN1_CONTEXT_S_0 = 0x80,
68 ASN1_CONTEXT_S_1 = 0x81,
69 ASN1_CONTEXT_S_2 = 0x82,
70 ASN1_CONTEXT_S_3 = 0x83,
71 ASN1_CONTEXT_S_4 = 0x84,
72 ASN1_CONTEXT_S_5 = 0x85,
73 ASN1_CONTEXT_S_6 = 0x86,
74 ASN1_CONTEXT_S_7 = 0x87,
75 ASN1_CONTEXT_S_8 = 0x88,
76
77 ASN1_CONTEXT_C_0 = 0xA0,
78 ASN1_CONTEXT_C_1 = 0xA1,
79 ASN1_CONTEXT_C_2 = 0xA2,
80 ASN1_CONTEXT_C_3 = 0xA3,
81 ASN1_CONTEXT_C_4 = 0xA4,
82 ASN1_CONTEXT_C_5 = 0xA5
83 } asn1_t;
84
85 /* Definition of ASN1 flags */
86 #define ASN1_NONE 0x00
87 #define ASN1_DEF 0x01
88 #define ASN1_OPT 0x02
89 #define ASN1_LOOP 0x04
90 #define ASN1_END 0x08
91 #define ASN1_OBJ 0x10
92 #define ASN1_BODY 0x20
93 #define ASN1_RAW 0x40
94
95 #define ASN1_INVALID_LENGTH 0xffffffff
96
97 /* definition of an ASN.1 object */
98 typedef struct {
99 u_int level;
100 const u_char *name;
101 asn1_t type;
102 u_char flags;
103 } asn1Object_t;
104
105 #define ASN1_MAX_LEVEL 10
106
107 typedef struct {
108 bool implicit;
109 bool private;
110 u_int level0;
111 u_int loopAddr[ASN1_MAX_LEVEL+1];
112 chunk_t blobs[ASN1_MAX_LEVEL+2];
113 } asn1_ctx_t;
114
115 /* some common prefabricated ASN.1 constants */
116 extern const chunk_t ASN1_INTEGER_0;
117 extern const chunk_t ASN1_INTEGER_1;
118 extern const chunk_t ASN1_INTEGER_2;
119
120 /* returns some popular algorithmIdentifiers */
121 extern chunk_t asn1_algorithmIdentifier(int oid);
122
123 extern int known_oid(chunk_t object);
124 extern u_int asn1_length(chunk_t *blob);
125 extern bool is_printablestring(chunk_t str);
126 extern time_t asn1totime(const chunk_t *utctime, asn1_t type);
127 extern chunk_t timetoasn1(const time_t *time, asn1_t type);
128 extern void asn1_init(asn1_ctx_t *ctx, chunk_t blob, u_int level0, bool implicit, bool private);
129 extern bool extract_object(asn1Object_t const *objects, u_int *objectID, chunk_t *object, u_int *level, asn1_ctx_t *ctx);
130 extern bool parse_asn1_simple_object(chunk_t *object, asn1_t type, u_int level, const char* name);
131 extern int parse_algorithmIdentifier(chunk_t blob, int level0, chunk_t *parameters);
132 extern time_t parse_time(chunk_t blob, int level0);
133
134 extern bool is_asn1(chunk_t blob);
135
136 extern void code_asn1_length(size_t length, chunk_t *code);
137 extern u_char* build_asn1_object(chunk_t *object, asn1_t type, size_t datalen);
138 extern chunk_t asn1_simple_object(asn1_t tag, chunk_t content);
139 extern chunk_t asn1_bitstring(const char *mode, chunk_t content);
140 extern chunk_t asn1_wrap(asn1_t type, const char *mode, ...);
141
142 #endif /* ASN1_H_ @}*/