removed unused gmp.h to build libstrongswan without libgmp
[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
30 #include <library.h>
31 #include <asn1/oid.h>
32
33
34 /**
35 * Definition of some primitive ASN1 types
36 */
37 typedef enum {
38 ASN1_EOC = 0x00,
39 ASN1_BOOLEAN = 0x01,
40 ASN1_INTEGER = 0x02,
41 ASN1_BIT_STRING = 0x03,
42 ASN1_OCTET_STRING = 0x04,
43 ASN1_NULL = 0x05,
44 ASN1_OID = 0x06,
45 ASN1_ENUMERATED = 0x0A,
46 ASN1_UTF8STRING = 0x0C,
47 ASN1_NUMERICSTRING = 0x12,
48 ASN1_PRINTABLESTRING = 0x13,
49 ASN1_T61STRING = 0x14,
50 ASN1_VIDEOTEXSTRING = 0x15,
51 ASN1_IA5STRING = 0x16,
52 ASN1_UTCTIME = 0x17,
53 ASN1_GENERALIZEDTIME = 0x18,
54 ASN1_GRAPHICSTRING = 0x19,
55 ASN1_VISIBLESTRING = 0x1A,
56 ASN1_GENERALSTRING = 0x1B,
57 ASN1_UNIVERSALSTRING = 0x1C,
58 ASN1_BMPSTRING = 0x1E,
59
60 ASN1_CONSTRUCTED = 0x20,
61
62 ASN1_SEQUENCE = 0x30,
63
64 ASN1_SET = 0x31,
65
66 ASN1_CONTEXT_S_0 = 0x80,
67 ASN1_CONTEXT_S_1 = 0x81,
68 ASN1_CONTEXT_S_2 = 0x82,
69 ASN1_CONTEXT_S_3 = 0x83,
70 ASN1_CONTEXT_S_4 = 0x84,
71 ASN1_CONTEXT_S_5 = 0x85,
72 ASN1_CONTEXT_S_6 = 0x86,
73 ASN1_CONTEXT_S_7 = 0x87,
74 ASN1_CONTEXT_S_8 = 0x88,
75
76 ASN1_CONTEXT_C_0 = 0xA0,
77 ASN1_CONTEXT_C_1 = 0xA1,
78 ASN1_CONTEXT_C_2 = 0xA2,
79 ASN1_CONTEXT_C_3 = 0xA3,
80 ASN1_CONTEXT_C_4 = 0xA4,
81 ASN1_CONTEXT_C_5 = 0xA5
82 } asn1_t;
83
84 /* Definition of ASN1 flags */
85 #define ASN1_NONE 0x00
86 #define ASN1_DEF 0x01
87 #define ASN1_OPT 0x02
88 #define ASN1_LOOP 0x04
89 #define ASN1_END 0x08
90 #define ASN1_OBJ 0x10
91 #define ASN1_BODY 0x20
92 #define ASN1_RAW 0x40
93
94 #define ASN1_INVALID_LENGTH 0xffffffff
95
96 /* definition of an ASN.1 object */
97 typedef struct {
98 u_int level;
99 const u_char *name;
100 asn1_t type;
101 u_char flags;
102 } asn1Object_t;
103
104 #define ASN1_MAX_LEVEL 10
105
106 typedef struct {
107 bool implicit;
108 bool private;
109 u_int level0;
110 u_int loopAddr[ASN1_MAX_LEVEL+1];
111 chunk_t blobs[ASN1_MAX_LEVEL+2];
112 } asn1_ctx_t;
113
114 /* some common prefabricated ASN.1 constants */
115 extern const chunk_t ASN1_INTEGER_0;
116 extern const chunk_t ASN1_INTEGER_1;
117 extern const chunk_t ASN1_INTEGER_2;
118
119 /* returns some popular algorithmIdentifiers */
120 extern chunk_t asn1_algorithmIdentifier(int oid);
121
122 extern int known_oid(chunk_t object);
123 extern u_int asn1_length(chunk_t *blob);
124 extern bool is_printablestring(chunk_t str);
125 extern time_t asn1totime(const chunk_t *utctime, asn1_t type);
126 extern chunk_t timetoasn1(const time_t *time, asn1_t type);
127 extern void asn1_init(asn1_ctx_t *ctx, chunk_t blob, u_int level0, bool implicit, bool private);
128 extern bool extract_object(asn1Object_t const *objects, u_int *objectID, chunk_t *object, u_int *level, asn1_ctx_t *ctx);
129 extern bool parse_asn1_simple_object(chunk_t *object, asn1_t type, u_int level, const char* name);
130 extern int parse_algorithmIdentifier(chunk_t blob, int level0, chunk_t *parameters);
131 extern time_t parse_time(chunk_t blob, int level0);
132
133 extern bool is_asn1(chunk_t blob);
134
135 extern void code_asn1_length(size_t length, chunk_t *code);
136 extern u_char* build_asn1_object(chunk_t *object, asn1_t type, size_t datalen);
137 extern chunk_t asn1_simple_object(asn1_t tag, chunk_t content);
138 extern chunk_t asn1_bitstring(const char *mode, chunk_t content);
139 extern chunk_t asn1_wrap(asn1_t type, const char *mode, ...);
140
141 #endif /* ASN1_H_ @}*/