blob: a4d1be473521b3ffb5e2e6b2065ab2e12dd64a86 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * ASN.1 DER parsing
3 * Copyright (c) 2006, Jouni Malinen <j@w1.fi>
4 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 */
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 */
Dmitry Shmidt1b467752015-12-14 12:45:46 -080023#define ASN1_TAG_EMBEDDED_PDV 0x0B /* not yet parsed */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070024#define ASN1_TAG_UTF8STRING 0x0C /* not yet parsed */
25#define ANS1_TAG_RELATIVE_OID 0x0D
Hai Shaloma20dcd72022-02-04 13:43:00 -080026#define ASN1_TAG_TIME 0x0E
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070027#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
Hai Shaloma20dcd72022-02-04 13:43:00 -080031#define ASN1_TAG_T61STRING 0x14 /* not yet parsed */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070032#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 */
Dmitry Shmidt1b467752015-12-14 12:45:46 -080040#define ASN1_TAG_CHARACTERSTRING 0x1D /* not yet parsed */
41#define ASN1_TAG_BMPSTRING 0x1E /* not yet parsed */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070042
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
49struct 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
56struct asn1_oid {
57 unsigned long oid[ASN1_MAX_OID_LEN];
58 size_t len;
59};
60
61
62int asn1_get_next(const u8 *buf, size_t len, struct asn1_hdr *hdr);
Hai Shaloma20dcd72022-02-04 13:43:00 -080063void asn1_print_hdr(const struct asn1_hdr *hdr, const char *title);
64void asn1_unexpected(const struct asn1_hdr *hdr, const char *title);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070065int asn1_parse_oid(const u8 *buf, size_t len, struct asn1_oid *oid);
66int asn1_get_oid(const u8 *buf, size_t len, struct asn1_oid *oid,
67 const u8 **next);
Dmitry Shmidt50b691d2014-05-21 14:01:45 -070068void asn1_oid_to_str(const struct asn1_oid *oid, char *buf, size_t len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070069unsigned long asn1_bit_string_to_long(const u8 *buf, size_t len);
Dmitry Shmidt50b691d2014-05-21 14:01:45 -070070int asn1_oid_equal(const struct asn1_oid *a, const struct asn1_oid *b);
Hai Shalomfdcde762020-04-02 11:19:20 -070071int asn1_get_integer(const u8 *buf, size_t len, int *integer, const u8 **next);
72int asn1_get_sequence(const u8 *buf, size_t len, struct asn1_hdr *hdr,
73 const u8 **next);
74int 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);
76void asn1_put_integer(struct wpabuf *buf, int val);
77void asn1_put_octet_string(struct wpabuf *buf, const struct wpabuf *val);
78void asn1_put_oid(struct wpabuf *buf, const struct asn1_oid *oid);
79void asn1_put_hdr(struct wpabuf *buf, u8 class, int constructed, u8 tag,
80 size_t len);
81void asn1_put_sequence(struct wpabuf *buf, const struct wpabuf *payload);
82void asn1_put_set(struct wpabuf *buf, const struct wpabuf *payload);
83void asn1_put_utf8string(struct wpabuf *buf, const char *val);
84struct wpabuf * asn1_build_alg_id(const struct asn1_oid *oid,
85 const struct wpabuf *params);
86struct wpabuf * asn1_encaps(struct wpabuf *buf, u8 class, u8 tag);
Dmitry Shmidt50b691d2014-05-21 14:01:45 -070087
Hai Shaloma20dcd72022-02-04 13:43:00 -080088static 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
94static 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
100static 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
106static 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
112static 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
118static 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
124static 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
130static 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
136static 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
142static 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
148static 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
166static 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
172static 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
178static 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
184static 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
Hai Shalomfdcde762020-04-02 11:19:20 -0700190extern const struct asn1_oid asn1_sha1_oid;
191extern const struct asn1_oid asn1_sha256_oid;
192extern const struct asn1_oid asn1_ec_public_key_oid;
193extern const struct asn1_oid asn1_prime256v1_oid;
194extern const struct asn1_oid asn1_secp384r1_oid;
195extern const struct asn1_oid asn1_secp521r1_oid;
196extern const struct asn1_oid asn1_brainpoolP256r1_oid;
197extern const struct asn1_oid asn1_brainpoolP384r1_oid;
198extern const struct asn1_oid asn1_brainpoolP512r1_oid;
199extern const struct asn1_oid asn1_aes_siv_cmac_aead_256_oid;
200extern const struct asn1_oid asn1_aes_siv_cmac_aead_384_oid;
201extern const struct asn1_oid asn1_aes_siv_cmac_aead_512_oid;
202extern const struct asn1_oid asn1_aes_siv_cmac_aead_256_oid;
203extern const struct asn1_oid asn1_aes_siv_cmac_aead_384_oid;
204extern const struct asn1_oid asn1_aes_siv_cmac_aead_512_oid;
205extern const struct asn1_oid asn1_pbkdf2_oid;
206extern const struct asn1_oid asn1_pbkdf2_hmac_sha256_oid;
207extern const struct asn1_oid asn1_pbkdf2_hmac_sha384_oid;
208extern const struct asn1_oid asn1_pbkdf2_hmac_sha512_oid;
209extern const struct asn1_oid asn1_dpp_config_params_oid;
210extern const struct asn1_oid asn1_dpp_asymmetric_key_package_oid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700211
212#endif /* ASN1_H */