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