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