139beb93cSSam Leffler /*
239beb93cSSam Leffler * ASN.1 DER parsing
339beb93cSSam Leffler * Copyright (c) 2006, Jouni Malinen <j@w1.fi>
439beb93cSSam Leffler *
5f05cddf9SRui Paulo * This software may be distributed under the terms of the BSD license.
6f05cddf9SRui Paulo * See README for more details.
739beb93cSSam Leffler */
839beb93cSSam Leffler
939beb93cSSam Leffler #ifndef ASN1_H
1039beb93cSSam Leffler #define ASN1_H
1139beb93cSSam Leffler
1239beb93cSSam Leffler #define ASN1_TAG_EOC 0x00 /* not used with DER */
1339beb93cSSam Leffler #define ASN1_TAG_BOOLEAN 0x01
1439beb93cSSam Leffler #define ASN1_TAG_INTEGER 0x02
1539beb93cSSam Leffler #define ASN1_TAG_BITSTRING 0x03
1639beb93cSSam Leffler #define ASN1_TAG_OCTETSTRING 0x04
1739beb93cSSam Leffler #define ASN1_TAG_NULL 0x05
1839beb93cSSam Leffler #define ASN1_TAG_OID 0x06
1939beb93cSSam Leffler #define ASN1_TAG_OBJECT_DESCRIPTOR 0x07 /* not yet parsed */
2039beb93cSSam Leffler #define ASN1_TAG_EXTERNAL 0x08 /* not yet parsed */
2139beb93cSSam Leffler #define ASN1_TAG_REAL 0x09 /* not yet parsed */
2239beb93cSSam Leffler #define ASN1_TAG_ENUMERATED 0x0A /* not yet parsed */
23780fb4a2SCy Schubert #define ASN1_TAG_EMBEDDED_PDV 0x0B /* not yet parsed */
2439beb93cSSam Leffler #define ASN1_TAG_UTF8STRING 0x0C /* not yet parsed */
2539beb93cSSam Leffler #define ANS1_TAG_RELATIVE_OID 0x0D
26*c1d255d3SCy Schubert #define ASN1_TAG_TIME 0x0E
2739beb93cSSam Leffler #define ASN1_TAG_SEQUENCE 0x10 /* shall be constructed */
2839beb93cSSam Leffler #define ASN1_TAG_SET 0x11
2939beb93cSSam Leffler #define ASN1_TAG_NUMERICSTRING 0x12 /* not yet parsed */
3039beb93cSSam Leffler #define ASN1_TAG_PRINTABLESTRING 0x13
31*c1d255d3SCy Schubert #define ASN1_TAG_T61STRING 0x14 /* not yet parsed */
3239beb93cSSam Leffler #define ASN1_TAG_VIDEOTEXSTRING 0x15 /* not yet parsed */
3339beb93cSSam Leffler #define ASN1_TAG_IA5STRING 0x16
3439beb93cSSam Leffler #define ASN1_TAG_UTCTIME 0x17
3539beb93cSSam Leffler #define ASN1_TAG_GENERALIZEDTIME 0x18 /* not yet parsed */
3639beb93cSSam Leffler #define ASN1_TAG_GRAPHICSTRING 0x19 /* not yet parsed */
3739beb93cSSam Leffler #define ASN1_TAG_VISIBLESTRING 0x1A
3839beb93cSSam Leffler #define ASN1_TAG_GENERALSTRING 0x1B /* not yet parsed */
3939beb93cSSam Leffler #define ASN1_TAG_UNIVERSALSTRING 0x1C /* not yet parsed */
40780fb4a2SCy Schubert #define ASN1_TAG_CHARACTERSTRING 0x1D /* not yet parsed */
41780fb4a2SCy Schubert #define ASN1_TAG_BMPSTRING 0x1E /* not yet parsed */
4239beb93cSSam Leffler
4339beb93cSSam Leffler #define ASN1_CLASS_UNIVERSAL 0
4439beb93cSSam Leffler #define ASN1_CLASS_APPLICATION 1
4539beb93cSSam Leffler #define ASN1_CLASS_CONTEXT_SPECIFIC 2
4639beb93cSSam Leffler #define ASN1_CLASS_PRIVATE 3
4739beb93cSSam Leffler
4839beb93cSSam Leffler
4939beb93cSSam Leffler struct asn1_hdr {
5039beb93cSSam Leffler const u8 *payload;
5139beb93cSSam Leffler u8 identifier, class, constructed;
5239beb93cSSam Leffler unsigned int tag, length;
5339beb93cSSam Leffler };
5439beb93cSSam Leffler
5539beb93cSSam Leffler #define ASN1_MAX_OID_LEN 20
5639beb93cSSam Leffler struct asn1_oid {
5739beb93cSSam Leffler unsigned long oid[ASN1_MAX_OID_LEN];
5839beb93cSSam Leffler size_t len;
5939beb93cSSam Leffler };
6039beb93cSSam Leffler
6139beb93cSSam Leffler
6239beb93cSSam Leffler int asn1_get_next(const u8 *buf, size_t len, struct asn1_hdr *hdr);
63*c1d255d3SCy Schubert void asn1_print_hdr(const struct asn1_hdr *hdr, const char *title);
64*c1d255d3SCy Schubert void asn1_unexpected(const struct asn1_hdr *hdr, const char *title);
65e28a4053SRui Paulo int asn1_parse_oid(const u8 *buf, size_t len, struct asn1_oid *oid);
6639beb93cSSam Leffler int asn1_get_oid(const u8 *buf, size_t len, struct asn1_oid *oid,
6739beb93cSSam Leffler const u8 **next);
685b9c547cSRui Paulo void asn1_oid_to_str(const struct asn1_oid *oid, char *buf, size_t len);
6939beb93cSSam Leffler unsigned long asn1_bit_string_to_long(const u8 *buf, size_t len);
705b9c547cSRui Paulo int asn1_oid_equal(const struct asn1_oid *a, const struct asn1_oid *b);
71*c1d255d3SCy Schubert int asn1_get_integer(const u8 *buf, size_t len, int *integer, const u8 **next);
72*c1d255d3SCy Schubert int asn1_get_sequence(const u8 *buf, size_t len, struct asn1_hdr *hdr,
73*c1d255d3SCy Schubert const u8 **next);
74*c1d255d3SCy Schubert int asn1_get_alg_id(const u8 *buf, size_t len, struct asn1_oid *oid,
75*c1d255d3SCy Schubert const u8 **params, size_t *params_len, const u8 **next);
76*c1d255d3SCy Schubert void asn1_put_integer(struct wpabuf *buf, int val);
77*c1d255d3SCy Schubert void asn1_put_octet_string(struct wpabuf *buf, const struct wpabuf *val);
78*c1d255d3SCy Schubert void asn1_put_oid(struct wpabuf *buf, const struct asn1_oid *oid);
79*c1d255d3SCy Schubert void asn1_put_hdr(struct wpabuf *buf, u8 class, int constructed, u8 tag,
80*c1d255d3SCy Schubert size_t len);
81*c1d255d3SCy Schubert void asn1_put_sequence(struct wpabuf *buf, const struct wpabuf *payload);
82*c1d255d3SCy Schubert void asn1_put_set(struct wpabuf *buf, const struct wpabuf *payload);
83*c1d255d3SCy Schubert void asn1_put_utf8string(struct wpabuf *buf, const char *val);
84*c1d255d3SCy Schubert struct wpabuf * asn1_build_alg_id(const struct asn1_oid *oid,
85*c1d255d3SCy Schubert const struct wpabuf *params);
86*c1d255d3SCy Schubert struct wpabuf * asn1_encaps(struct wpabuf *buf, u8 class, u8 tag);
875b9c547cSRui Paulo
asn1_is_oid(const struct asn1_hdr * hdr)88*c1d255d3SCy Schubert static inline bool asn1_is_oid(const struct asn1_hdr *hdr)
89*c1d255d3SCy Schubert {
90*c1d255d3SCy Schubert return hdr->class == ASN1_CLASS_UNIVERSAL &&
91*c1d255d3SCy Schubert hdr->tag == ASN1_TAG_OID;
92*c1d255d3SCy Schubert }
93*c1d255d3SCy Schubert
asn1_is_boolean(const struct asn1_hdr * hdr)94*c1d255d3SCy Schubert static inline bool asn1_is_boolean(const struct asn1_hdr *hdr)
95*c1d255d3SCy Schubert {
96*c1d255d3SCy Schubert return hdr->class == ASN1_CLASS_UNIVERSAL &&
97*c1d255d3SCy Schubert hdr->tag == ASN1_TAG_BOOLEAN;
98*c1d255d3SCy Schubert }
99*c1d255d3SCy Schubert
asn1_is_integer(const struct asn1_hdr * hdr)100*c1d255d3SCy Schubert static inline bool asn1_is_integer(const struct asn1_hdr *hdr)
101*c1d255d3SCy Schubert {
102*c1d255d3SCy Schubert return hdr->class == ASN1_CLASS_UNIVERSAL &&
103*c1d255d3SCy Schubert hdr->tag == ASN1_TAG_INTEGER;
104*c1d255d3SCy Schubert }
105*c1d255d3SCy Schubert
asn1_is_enumerated(const struct asn1_hdr * hdr)106*c1d255d3SCy Schubert static inline bool asn1_is_enumerated(const struct asn1_hdr *hdr)
107*c1d255d3SCy Schubert {
108*c1d255d3SCy Schubert return hdr->class == ASN1_CLASS_UNIVERSAL &&
109*c1d255d3SCy Schubert hdr->tag == ASN1_TAG_ENUMERATED;
110*c1d255d3SCy Schubert }
111*c1d255d3SCy Schubert
asn1_is_sequence(const struct asn1_hdr * hdr)112*c1d255d3SCy Schubert static inline bool asn1_is_sequence(const struct asn1_hdr *hdr)
113*c1d255d3SCy Schubert {
114*c1d255d3SCy Schubert return hdr->class == ASN1_CLASS_UNIVERSAL &&
115*c1d255d3SCy Schubert hdr->tag == ASN1_TAG_SEQUENCE;
116*c1d255d3SCy Schubert }
117*c1d255d3SCy Schubert
asn1_is_set(const struct asn1_hdr * hdr)118*c1d255d3SCy Schubert static inline bool asn1_is_set(const struct asn1_hdr *hdr)
119*c1d255d3SCy Schubert {
120*c1d255d3SCy Schubert return hdr->class == ASN1_CLASS_UNIVERSAL &&
121*c1d255d3SCy Schubert hdr->tag == ASN1_TAG_SET;
122*c1d255d3SCy Schubert }
123*c1d255d3SCy Schubert
asn1_is_octetstring(const struct asn1_hdr * hdr)124*c1d255d3SCy Schubert static inline bool asn1_is_octetstring(const struct asn1_hdr *hdr)
125*c1d255d3SCy Schubert {
126*c1d255d3SCy Schubert return hdr->class == ASN1_CLASS_UNIVERSAL &&
127*c1d255d3SCy Schubert hdr->tag == ASN1_TAG_OCTETSTRING;
128*c1d255d3SCy Schubert }
129*c1d255d3SCy Schubert
asn1_is_bitstring(const struct asn1_hdr * hdr)130*c1d255d3SCy Schubert static inline bool asn1_is_bitstring(const struct asn1_hdr *hdr)
131*c1d255d3SCy Schubert {
132*c1d255d3SCy Schubert return hdr->class == ASN1_CLASS_UNIVERSAL &&
133*c1d255d3SCy Schubert hdr->tag == ASN1_TAG_BITSTRING;
134*c1d255d3SCy Schubert }
135*c1d255d3SCy Schubert
asn1_is_utctime(const struct asn1_hdr * hdr)136*c1d255d3SCy Schubert static inline bool asn1_is_utctime(const struct asn1_hdr *hdr)
137*c1d255d3SCy Schubert {
138*c1d255d3SCy Schubert return hdr->class == ASN1_CLASS_UNIVERSAL &&
139*c1d255d3SCy Schubert hdr->tag == ASN1_TAG_UTCTIME;
140*c1d255d3SCy Schubert }
141*c1d255d3SCy Schubert
asn1_is_generalizedtime(const struct asn1_hdr * hdr)142*c1d255d3SCy Schubert static inline bool asn1_is_generalizedtime(const struct asn1_hdr *hdr)
143*c1d255d3SCy Schubert {
144*c1d255d3SCy Schubert return hdr->class == ASN1_CLASS_UNIVERSAL &&
145*c1d255d3SCy Schubert hdr->tag == ASN1_TAG_GENERALIZEDTIME;
146*c1d255d3SCy Schubert }
147*c1d255d3SCy Schubert
asn1_is_string_type(const struct asn1_hdr * hdr)148*c1d255d3SCy Schubert static inline bool asn1_is_string_type(const struct asn1_hdr *hdr)
149*c1d255d3SCy Schubert {
150*c1d255d3SCy Schubert if (hdr->class != ASN1_CLASS_UNIVERSAL || hdr->constructed)
151*c1d255d3SCy Schubert return false;
152*c1d255d3SCy Schubert return hdr->tag == ASN1_TAG_UTF8STRING ||
153*c1d255d3SCy Schubert hdr->tag == ASN1_TAG_NUMERICSTRING ||
154*c1d255d3SCy Schubert hdr->tag == ASN1_TAG_PRINTABLESTRING ||
155*c1d255d3SCy Schubert hdr->tag == ASN1_TAG_T61STRING ||
156*c1d255d3SCy Schubert hdr->tag == ASN1_TAG_VIDEOTEXSTRING ||
157*c1d255d3SCy Schubert hdr->tag == ASN1_TAG_IA5STRING ||
158*c1d255d3SCy Schubert hdr->tag == ASN1_TAG_GRAPHICSTRING ||
159*c1d255d3SCy Schubert hdr->tag == ASN1_TAG_VISIBLESTRING ||
160*c1d255d3SCy Schubert hdr->tag == ASN1_TAG_GENERALSTRING ||
161*c1d255d3SCy Schubert hdr->tag == ASN1_TAG_UNIVERSALSTRING ||
162*c1d255d3SCy Schubert hdr->tag == ASN1_TAG_CHARACTERSTRING ||
163*c1d255d3SCy Schubert hdr->tag == ASN1_TAG_BMPSTRING;
164*c1d255d3SCy Schubert }
165*c1d255d3SCy Schubert
asn1_is_bmpstring(const struct asn1_hdr * hdr)166*c1d255d3SCy Schubert static inline bool asn1_is_bmpstring(const struct asn1_hdr *hdr)
167*c1d255d3SCy Schubert {
168*c1d255d3SCy Schubert return hdr->class == ASN1_CLASS_UNIVERSAL &&
169*c1d255d3SCy Schubert hdr->tag == ASN1_TAG_BMPSTRING;
170*c1d255d3SCy Schubert }
171*c1d255d3SCy Schubert
asn1_is_utf8string(const struct asn1_hdr * hdr)172*c1d255d3SCy Schubert static inline bool asn1_is_utf8string(const struct asn1_hdr *hdr)
173*c1d255d3SCy Schubert {
174*c1d255d3SCy Schubert return hdr->class == ASN1_CLASS_UNIVERSAL &&
175*c1d255d3SCy Schubert hdr->tag == ASN1_TAG_UTF8STRING;
176*c1d255d3SCy Schubert }
177*c1d255d3SCy Schubert
asn1_is_null(const struct asn1_hdr * hdr)178*c1d255d3SCy Schubert static inline bool asn1_is_null(const struct asn1_hdr *hdr)
179*c1d255d3SCy Schubert {
180*c1d255d3SCy Schubert return hdr->class == ASN1_CLASS_UNIVERSAL &&
181*c1d255d3SCy Schubert hdr->tag == ASN1_TAG_NULL;
182*c1d255d3SCy Schubert }
183*c1d255d3SCy Schubert
asn1_is_cs_tag(const struct asn1_hdr * hdr,unsigned int tag)184*c1d255d3SCy Schubert static inline bool asn1_is_cs_tag(const struct asn1_hdr *hdr, unsigned int tag)
185*c1d255d3SCy Schubert {
186*c1d255d3SCy Schubert return hdr->class == ASN1_CLASS_CONTEXT_SPECIFIC &&
187*c1d255d3SCy Schubert hdr->tag == tag;
188*c1d255d3SCy Schubert }
189*c1d255d3SCy Schubert
190*c1d255d3SCy Schubert extern const struct asn1_oid asn1_sha1_oid;
191*c1d255d3SCy Schubert extern const struct asn1_oid asn1_sha256_oid;
192*c1d255d3SCy Schubert extern const struct asn1_oid asn1_ec_public_key_oid;
193*c1d255d3SCy Schubert extern const struct asn1_oid asn1_prime256v1_oid;
194*c1d255d3SCy Schubert extern const struct asn1_oid asn1_secp384r1_oid;
195*c1d255d3SCy Schubert extern const struct asn1_oid asn1_secp521r1_oid;
196*c1d255d3SCy Schubert extern const struct asn1_oid asn1_brainpoolP256r1_oid;
197*c1d255d3SCy Schubert extern const struct asn1_oid asn1_brainpoolP384r1_oid;
198*c1d255d3SCy Schubert extern const struct asn1_oid asn1_brainpoolP512r1_oid;
199*c1d255d3SCy Schubert extern const struct asn1_oid asn1_aes_siv_cmac_aead_256_oid;
200*c1d255d3SCy Schubert extern const struct asn1_oid asn1_aes_siv_cmac_aead_384_oid;
201*c1d255d3SCy Schubert extern const struct asn1_oid asn1_aes_siv_cmac_aead_512_oid;
202*c1d255d3SCy Schubert extern const struct asn1_oid asn1_aes_siv_cmac_aead_256_oid;
203*c1d255d3SCy Schubert extern const struct asn1_oid asn1_aes_siv_cmac_aead_384_oid;
204*c1d255d3SCy Schubert extern const struct asn1_oid asn1_aes_siv_cmac_aead_512_oid;
205*c1d255d3SCy Schubert extern const struct asn1_oid asn1_pbkdf2_oid;
206*c1d255d3SCy Schubert extern const struct asn1_oid asn1_pbkdf2_hmac_sha256_oid;
207*c1d255d3SCy Schubert extern const struct asn1_oid asn1_pbkdf2_hmac_sha384_oid;
208*c1d255d3SCy Schubert extern const struct asn1_oid asn1_pbkdf2_hmac_sha512_oid;
209*c1d255d3SCy Schubert extern const struct asn1_oid asn1_dpp_config_params_oid;
210*c1d255d3SCy Schubert extern const struct asn1_oid asn1_dpp_asymmetric_key_package_oid;
21139beb93cSSam Leffler
21239beb93cSSam Leffler #endif /* ASN1_H */
213