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