OSDN Git Service

2013.10.24
[uclinux-h8/uClinux-dist.git] / freeswan / pluto / asn1.h
1 /* Simple ASN.1 parser
2  * Copyright (C) 2000-2002 Andreas Steffen, Zuercher Hochschule Winterthur
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License as published by the
6  * Free Software Foundation; either version 2 of the License, or (at your
7  * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * for more details.
13  *
14  * RCSID $Id: asn1.h,v 0.1 2002/04/12 00:00:00 as Exp $
15  */
16
17 /* Defines some primitive ASN1 types */
18
19 typedef enum {
20     ASN1_EOC =                  0x00,
21     ASN1_BOOLEAN =              0x01,
22     ASN1_INTEGER =              0x02,
23     ASN1_BIT_STRING =           0x03,
24     ASN1_OCTET_STRING =         0x04,
25     ASN1_NULL =                 0x05,
26     ASN1_OID =                  0x06,
27     ASN1_UTF8STRING =           0x0C,
28     ASN1_NUMERICSTRING =        0x12,
29     ASN1_PRINTABLESTRING =      0x13,
30     ASN1_T61STRING =            0x14,
31     ASN1_VIDEOTEXSTRING =       0x15,
32     ASN1_IA5STRING =            0x16,
33     ASN1_UTCTIME =              0x17,
34     ASN1_GENERALIZEDTIME =      0x18,
35     ASN1_GRAPHICSTRING =        0x19,
36     ASN1_VISIBLESTRING =        0x1A,
37     ASN1_GENERALSTRING =        0x1B,
38     ASN1_UNIVERSALSTRING =      0x1C,
39     ASN1_BMPSTRING =            0x1E,
40
41     ASN1_CONSTRUCTED =          0x20,
42
43     ASN1_SEQUENCE =             0x30,
44
45     ASN1_SET =                  0x31,
46
47     ASN1_CONTEXT_S_0 =          0x80,
48     ASN1_CONTEXT_S_1 =          0x81,
49     ASN1_CONTEXT_S_2 =          0x82,
50     ASN1_CONTEXT_S_3 =          0x83,
51     ASN1_CONTEXT_S_4 =          0x84,
52     ASN1_CONTEXT_S_5 =          0x85,
53     ASN1_CONTEXT_S_6 =          0x86,
54     ASN1_CONTEXT_S_7 =          0x87,
55     ASN1_CONTEXT_S_8 =          0x88,
56
57     ASN1_CONTEXT_C_0 =          0xA0,
58     ASN1_CONTEXT_C_1 =          0xA1,
59     ASN1_CONTEXT_C_2 =          0xA2,
60     ASN1_CONTEXT_C_3 =          0xA3,
61     ASN1_CONTEXT_C_4 =          0xA4,
62     ASN1_CONTEXT_C_5 =          0xA5
63 } asn1_t;
64
65 /* Definition of ASN1 flags */
66
67 #define ASN1_NONE       0x00
68 #define ASN1_DEF        0x01
69 #define ASN1_OPT        0x02
70 #define ASN1_LOOP       0x04
71 #define ASN1_END        0x08
72 #define ASN1_OBJ        0x10
73 #define ASN1_BODY       0x20
74
75 #define ASN1_INVALID_LENGTH     0xffffffff
76
77 /* definition of an ASN.1 object */
78
79 typedef struct {
80     u_int   level;
81     u_char  *name;
82     asn1_t  type;
83     u_char  flags;
84 } asn1Object_t;
85
86 /* defines a node in a the hierarchical OID tree */
87
88 typedef struct {
89     u_char digit;
90     u_int  next;
91     u_int  down;
92     u_char *name;
93 } oid_t;
94
95 /* Some well known object identifiers (OIDs) */
96
97 extern const oid_t oid_names[];
98
99 #define OID_SUBJECT_ALT_NAME            19
100 #define OID_BASIC_CONSTRAINTS           21
101 #define OID_CRL_DISTRIBUTION_POINTS     23
102 #define OID_RSA_ENCRYPTION              35
103 #define OID_MD2_WITH_RSA                36
104 #define OID_MD5_WITH_RSA                37
105 #define OID_SHA1_WITH_RSA               38
106 #define OID_SHA256_WITH_RSA             39
107 #define OID_SHA384_WITH_RSA             40
108 #define OID_SHA512_WITH_RSA             41
109 #define OID_PKCS7_DATA                  43
110 #define OID_PKCS7_SIGNED_DATA           44
111 #define OID_PKCS9_EMAIL                 50
112 #define OID_MD2                         53
113 #define OID_MD5                         54
114 #define OID_SHA1                        70
115
116 /* internal context of ASN.1 parser */
117
118 #define ASN1_MAX_LEVEL  20
119
120 typedef struct {
121     bool  implicit;
122     u_int cond;
123     u_int level0;
124     u_int loopAddr[ASN1_MAX_LEVEL+1];
125     chunk_t  blobs[ASN1_MAX_LEVEL+2];
126 } asn1_ctx_t;
127
128 extern int known_oid(chunk_t object);
129 extern u_int asn1_length(chunk_t *blob);
130 extern bool is_printablestring(chunk_t str);
131 extern time_t asn1totime(const chunk_t *utctime, asn1_t type);
132 extern void asn1_init(asn1_ctx_t *ctx, chunk_t blob,
133     u_int level0, bool implicit, u_int cond);
134 extern bool extract_object(asn1Object_t const *objects, 
135     u_int *objectID, chunk_t *object, asn1_ctx_t *ctx);
136 extern bool load_asn1_file(const char* filename, const char* passphrase,
137     const char* type, chunk_t *blob);
138