xref: /freebsd/contrib/wpa/src/tls/asn1.h (revision b077aed33b7b6aefca7b17ddb250cf521f938613)
1 /*
2  * ASN.1 DER parsing
3  * Copyright (c) 2006, Jouni Malinen <j@w1.fi>
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8 
9 #ifndef ASN1_H
10 #define ASN1_H
11 
12 #define ASN1_TAG_EOC		0x00 /* not used with DER */
13 #define ASN1_TAG_BOOLEAN	0x01
14 #define ASN1_TAG_INTEGER	0x02
15 #define ASN1_TAG_BITSTRING	0x03
16 #define ASN1_TAG_OCTETSTRING	0x04
17 #define ASN1_TAG_NULL		0x05
18 #define ASN1_TAG_OID		0x06
19 #define ASN1_TAG_OBJECT_DESCRIPTOR	0x07 /* not yet parsed */
20 #define ASN1_TAG_EXTERNAL	0x08 /* not yet parsed */
21 #define ASN1_TAG_REAL		0x09 /* not yet parsed */
22 #define ASN1_TAG_ENUMERATED	0x0A /* not yet parsed */
23 #define ASN1_TAG_EMBEDDED_PDV	0x0B /* not yet parsed */
24 #define ASN1_TAG_UTF8STRING	0x0C /* not yet parsed */
25 #define ANS1_TAG_RELATIVE_OID	0x0D
26 #define ASN1_TAG_TIME		0x0E
27 #define ASN1_TAG_SEQUENCE	0x10 /* shall be constructed */
28 #define ASN1_TAG_SET		0x11
29 #define ASN1_TAG_NUMERICSTRING	0x12 /* not yet parsed */
30 #define ASN1_TAG_PRINTABLESTRING	0x13
31 #define ASN1_TAG_T61STRING	0x14 /* not yet parsed */
32 #define ASN1_TAG_VIDEOTEXSTRING	0x15 /* not yet parsed */
33 #define ASN1_TAG_IA5STRING	0x16
34 #define ASN1_TAG_UTCTIME	0x17
35 #define ASN1_TAG_GENERALIZEDTIME	0x18 /* not yet parsed */
36 #define ASN1_TAG_GRAPHICSTRING	0x19 /* not yet parsed */
37 #define ASN1_TAG_VISIBLESTRING	0x1A
38 #define ASN1_TAG_GENERALSTRING	0x1B /* not yet parsed */
39 #define ASN1_TAG_UNIVERSALSTRING	0x1C /* not yet parsed */
40 #define ASN1_TAG_CHARACTERSTRING	0x1D /* not yet parsed */
41 #define ASN1_TAG_BMPSTRING	0x1E /* not yet parsed */
42 
43 #define ASN1_CLASS_UNIVERSAL		0
44 #define ASN1_CLASS_APPLICATION		1
45 #define ASN1_CLASS_CONTEXT_SPECIFIC	2
46 #define ASN1_CLASS_PRIVATE		3
47 
48 
49 struct asn1_hdr {
50 	const u8 *payload;
51 	u8 identifier, class, constructed;
52 	unsigned int tag, length;
53 };
54 
55 #define ASN1_MAX_OID_LEN 20
56 struct asn1_oid {
57 	unsigned long oid[ASN1_MAX_OID_LEN];
58 	size_t len;
59 };
60 
61 
62 int asn1_get_next(const u8 *buf, size_t len, struct asn1_hdr *hdr);
63 void asn1_print_hdr(const struct asn1_hdr *hdr, const char *title);
64 void asn1_unexpected(const struct asn1_hdr *hdr, const char *title);
65 int asn1_parse_oid(const u8 *buf, size_t len, struct asn1_oid *oid);
66 int asn1_get_oid(const u8 *buf, size_t len, struct asn1_oid *oid,
67 		 const u8 **next);
68 void asn1_oid_to_str(const struct asn1_oid *oid, char *buf, size_t len);
69 unsigned long asn1_bit_string_to_long(const u8 *buf, size_t len);
70 int asn1_oid_equal(const struct asn1_oid *a, const struct asn1_oid *b);
71 int asn1_get_integer(const u8 *buf, size_t len, int *integer, const u8 **next);
72 int asn1_get_sequence(const u8 *buf, size_t len, struct asn1_hdr *hdr,
73 		      const u8 **next);
74 int asn1_get_alg_id(const u8 *buf, size_t len, struct asn1_oid *oid,
75 		    const u8 **params, size_t *params_len, const u8 **next);
76 void asn1_put_integer(struct wpabuf *buf, int val);
77 void asn1_put_octet_string(struct wpabuf *buf, const struct wpabuf *val);
78 void asn1_put_oid(struct wpabuf *buf, const struct asn1_oid *oid);
79 void asn1_put_hdr(struct wpabuf *buf, u8 class, int constructed, u8 tag,
80 		  size_t len);
81 void asn1_put_sequence(struct wpabuf *buf, const struct wpabuf *payload);
82 void asn1_put_set(struct wpabuf *buf, const struct wpabuf *payload);
83 void asn1_put_utf8string(struct wpabuf *buf, const char *val);
84 struct wpabuf * asn1_build_alg_id(const struct asn1_oid *oid,
85 				  const struct wpabuf *params);
86 struct wpabuf * asn1_encaps(struct wpabuf *buf, u8 class, u8 tag);
87 
88 static inline bool asn1_is_oid(const struct asn1_hdr *hdr)
89 {
90 	return hdr->class == ASN1_CLASS_UNIVERSAL &&
91 		hdr->tag == ASN1_TAG_OID;
92 }
93 
94 static inline bool asn1_is_boolean(const struct asn1_hdr *hdr)
95 {
96 	return hdr->class == ASN1_CLASS_UNIVERSAL &&
97 		hdr->tag == ASN1_TAG_BOOLEAN;
98 }
99 
100 static inline bool asn1_is_integer(const struct asn1_hdr *hdr)
101 {
102 	return hdr->class == ASN1_CLASS_UNIVERSAL &&
103 		hdr->tag == ASN1_TAG_INTEGER;
104 }
105 
106 static inline bool asn1_is_enumerated(const struct asn1_hdr *hdr)
107 {
108 	return hdr->class == ASN1_CLASS_UNIVERSAL &&
109 		hdr->tag == ASN1_TAG_ENUMERATED;
110 }
111 
112 static inline bool asn1_is_sequence(const struct asn1_hdr *hdr)
113 {
114 	return hdr->class == ASN1_CLASS_UNIVERSAL &&
115 		hdr->tag == ASN1_TAG_SEQUENCE;
116 }
117 
118 static inline bool asn1_is_set(const struct asn1_hdr *hdr)
119 {
120 	return hdr->class == ASN1_CLASS_UNIVERSAL &&
121 		hdr->tag == ASN1_TAG_SET;
122 }
123 
124 static inline bool asn1_is_octetstring(const struct asn1_hdr *hdr)
125 {
126 	return hdr->class == ASN1_CLASS_UNIVERSAL &&
127 		hdr->tag == ASN1_TAG_OCTETSTRING;
128 }
129 
130 static inline bool asn1_is_bitstring(const struct asn1_hdr *hdr)
131 {
132 	return hdr->class == ASN1_CLASS_UNIVERSAL &&
133 		hdr->tag == ASN1_TAG_BITSTRING;
134 }
135 
136 static inline bool asn1_is_utctime(const struct asn1_hdr *hdr)
137 {
138 	return hdr->class == ASN1_CLASS_UNIVERSAL &&
139 		hdr->tag == ASN1_TAG_UTCTIME;
140 }
141 
142 static inline bool asn1_is_generalizedtime(const struct asn1_hdr *hdr)
143 {
144 	return hdr->class == ASN1_CLASS_UNIVERSAL &&
145 		hdr->tag == ASN1_TAG_GENERALIZEDTIME;
146 }
147 
148 static inline bool asn1_is_string_type(const struct asn1_hdr *hdr)
149 {
150 	if (hdr->class != ASN1_CLASS_UNIVERSAL || hdr->constructed)
151 		return false;
152 	return hdr->tag == ASN1_TAG_UTF8STRING ||
153 		hdr->tag == ASN1_TAG_NUMERICSTRING ||
154 		hdr->tag == ASN1_TAG_PRINTABLESTRING ||
155 		hdr->tag == ASN1_TAG_T61STRING ||
156 		hdr->tag == ASN1_TAG_VIDEOTEXSTRING ||
157 		hdr->tag == ASN1_TAG_IA5STRING ||
158 		hdr->tag == ASN1_TAG_GRAPHICSTRING ||
159 		hdr->tag == ASN1_TAG_VISIBLESTRING ||
160 		hdr->tag == ASN1_TAG_GENERALSTRING ||
161 		hdr->tag == ASN1_TAG_UNIVERSALSTRING ||
162 		hdr->tag == ASN1_TAG_CHARACTERSTRING ||
163 		hdr->tag == ASN1_TAG_BMPSTRING;
164 }
165 
166 static inline bool asn1_is_bmpstring(const struct asn1_hdr *hdr)
167 {
168 	return hdr->class == ASN1_CLASS_UNIVERSAL &&
169 		hdr->tag == ASN1_TAG_BMPSTRING;
170 }
171 
172 static inline bool asn1_is_utf8string(const struct asn1_hdr *hdr)
173 {
174 	return hdr->class == ASN1_CLASS_UNIVERSAL &&
175 		hdr->tag == ASN1_TAG_UTF8STRING;
176 }
177 
178 static inline bool asn1_is_null(const struct asn1_hdr *hdr)
179 {
180 	return hdr->class == ASN1_CLASS_UNIVERSAL &&
181 		hdr->tag == ASN1_TAG_NULL;
182 }
183 
184 static inline bool asn1_is_cs_tag(const struct asn1_hdr *hdr, unsigned int tag)
185 {
186 	return hdr->class == ASN1_CLASS_CONTEXT_SPECIFIC &&
187 		hdr->tag == tag;
188 }
189 
190 extern const struct asn1_oid asn1_sha1_oid;
191 extern const struct asn1_oid asn1_sha256_oid;
192 extern const struct asn1_oid asn1_ec_public_key_oid;
193 extern const struct asn1_oid asn1_prime256v1_oid;
194 extern const struct asn1_oid asn1_secp384r1_oid;
195 extern const struct asn1_oid asn1_secp521r1_oid;
196 extern const struct asn1_oid asn1_brainpoolP256r1_oid;
197 extern const struct asn1_oid asn1_brainpoolP384r1_oid;
198 extern const struct asn1_oid asn1_brainpoolP512r1_oid;
199 extern const struct asn1_oid asn1_aes_siv_cmac_aead_256_oid;
200 extern const struct asn1_oid asn1_aes_siv_cmac_aead_384_oid;
201 extern const struct asn1_oid asn1_aes_siv_cmac_aead_512_oid;
202 extern const struct asn1_oid asn1_aes_siv_cmac_aead_256_oid;
203 extern const struct asn1_oid asn1_aes_siv_cmac_aead_384_oid;
204 extern const struct asn1_oid asn1_aes_siv_cmac_aead_512_oid;
205 extern const struct asn1_oid asn1_pbkdf2_oid;
206 extern const struct asn1_oid asn1_pbkdf2_hmac_sha256_oid;
207 extern const struct asn1_oid asn1_pbkdf2_hmac_sha384_oid;
208 extern const struct asn1_oid asn1_pbkdf2_hmac_sha512_oid;
209 extern const struct asn1_oid asn1_dpp_config_params_oid;
210 extern const struct asn1_oid asn1_dpp_asymmetric_key_package_oid;
211 
212 #endif /* ASN1_H */
213