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