/src/hostap/src/tls/asn1.h
Line | Count | Source (jump to first uncovered line) |
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 | 4.55k | #define ASN1_TAG_EOC 0x00 /* not used with DER */ |
13 | 24.7k | #define ASN1_TAG_BOOLEAN 0x01 |
14 | 3.05k | #define ASN1_TAG_INTEGER 0x02 |
15 | 2.96k | #define ASN1_TAG_BITSTRING 0x03 |
16 | 2.97k | #define ASN1_TAG_OCTETSTRING 0x04 |
17 | 24.3k | #define ASN1_TAG_NULL 0x05 |
18 | 5.94k | #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 | 2.76k | #define ASN1_TAG_REAL 0x09 /* not yet parsed */ |
22 | 2.76k | #define ASN1_TAG_ENUMERATED 0x0A /* not yet parsed */ |
23 | | #define ASN1_TAG_EMBEDDED_PDV 0x0B /* not yet parsed */ |
24 | 2.74k | #define ASN1_TAG_UTF8STRING 0x0C /* not yet parsed */ |
25 | 2.95k | #define ANS1_TAG_RELATIVE_OID 0x0D |
26 | 1.34k | #define ASN1_TAG_TIME 0x0E |
27 | 19.4k | #define ASN1_TAG_SEQUENCE 0x10 /* shall be constructed */ |
28 | 9.67k | #define ASN1_TAG_SET 0x11 |
29 | 2.75k | #define ASN1_TAG_NUMERICSTRING 0x12 /* not yet parsed */ |
30 | 2.98k | #define ASN1_TAG_PRINTABLESTRING 0x13 |
31 | 2.75k | #define ASN1_TAG_T61STRING 0x14 /* not yet parsed */ |
32 | 2.75k | #define ASN1_TAG_VIDEOTEXSTRING 0x15 /* not yet parsed */ |
33 | 2.94k | #define ASN1_TAG_IA5STRING 0x16 |
34 | 2.95k | #define ASN1_TAG_UTCTIME 0x17 |
35 | 2.73k | #define ASN1_TAG_GENERALIZEDTIME 0x18 /* not yet parsed */ |
36 | 2.74k | #define ASN1_TAG_GRAPHICSTRING 0x19 /* not yet parsed */ |
37 | 3.07k | #define ASN1_TAG_VISIBLESTRING 0x1A |
38 | 2.74k | #define ASN1_TAG_GENERALSTRING 0x1B /* not yet parsed */ |
39 | 2.74k | #define ASN1_TAG_UNIVERSALSTRING 0x1C /* not yet parsed */ |
40 | 2.74k | #define ASN1_TAG_CHARACTERSTRING 0x1D /* not yet parsed */ |
41 | 2.74k | #define ASN1_TAG_BMPSTRING 0x1E /* not yet parsed */ |
42 | | |
43 | 562k | #define ASN1_CLASS_UNIVERSAL 0 |
44 | 923 | #define ASN1_CLASS_APPLICATION 1 |
45 | 551k | #define ASN1_CLASS_CONTEXT_SPECIFIC 2 |
46 | 790k | #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 | 5.77k | #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 | 1.05k | { |
90 | 1.05k | return hdr->class == ASN1_CLASS_UNIVERSAL && |
91 | 1.05k | hdr->tag == ASN1_TAG_OID; |
92 | 1.05k | } |
93 | | |
94 | | static inline bool asn1_is_boolean(const struct asn1_hdr *hdr) |
95 | 0 | { |
96 | 0 | return hdr->class == ASN1_CLASS_UNIVERSAL && |
97 | 0 | hdr->tag == ASN1_TAG_BOOLEAN; |
98 | 0 | } |
99 | | |
100 | | static inline bool asn1_is_integer(const struct asn1_hdr *hdr) |
101 | 0 | { |
102 | 0 | return hdr->class == ASN1_CLASS_UNIVERSAL && |
103 | 0 | hdr->tag == ASN1_TAG_INTEGER; |
104 | 0 | } |
105 | | |
106 | | static inline bool asn1_is_enumerated(const struct asn1_hdr *hdr) |
107 | 0 | { |
108 | 0 | return hdr->class == ASN1_CLASS_UNIVERSAL && |
109 | 0 | hdr->tag == ASN1_TAG_ENUMERATED; |
110 | 0 | } |
111 | | |
112 | | static inline bool asn1_is_sequence(const struct asn1_hdr *hdr) |
113 | 0 | { |
114 | 0 | return hdr->class == ASN1_CLASS_UNIVERSAL && |
115 | 0 | hdr->tag == ASN1_TAG_SEQUENCE; |
116 | 0 | } |
117 | | |
118 | | static inline bool asn1_is_set(const struct asn1_hdr *hdr) |
119 | 0 | { |
120 | 0 | return hdr->class == ASN1_CLASS_UNIVERSAL && |
121 | 0 | hdr->tag == ASN1_TAG_SET; |
122 | 0 | } |
123 | | |
124 | | static inline bool asn1_is_octetstring(const struct asn1_hdr *hdr) |
125 | 0 | { |
126 | 0 | return hdr->class == ASN1_CLASS_UNIVERSAL && |
127 | 0 | hdr->tag == ASN1_TAG_OCTETSTRING; |
128 | 0 | } |
129 | | |
130 | | static inline bool asn1_is_bitstring(const struct asn1_hdr *hdr) |
131 | 0 | { |
132 | 0 | return hdr->class == ASN1_CLASS_UNIVERSAL && |
133 | 0 | hdr->tag == ASN1_TAG_BITSTRING; |
134 | 0 | } |
135 | | |
136 | | static inline bool asn1_is_utctime(const struct asn1_hdr *hdr) |
137 | 0 | { |
138 | 0 | return hdr->class == ASN1_CLASS_UNIVERSAL && |
139 | 0 | hdr->tag == ASN1_TAG_UTCTIME; |
140 | 0 | } |
141 | | |
142 | | static inline bool asn1_is_generalizedtime(const struct asn1_hdr *hdr) |
143 | 0 | { |
144 | 0 | return hdr->class == ASN1_CLASS_UNIVERSAL && |
145 | 0 | hdr->tag == ASN1_TAG_GENERALIZEDTIME; |
146 | 0 | } |
147 | | |
148 | | static inline bool asn1_is_string_type(const struct asn1_hdr *hdr) |
149 | 0 | { |
150 | 0 | if (hdr->class != ASN1_CLASS_UNIVERSAL || hdr->constructed) |
151 | 0 | return false; |
152 | 0 | return hdr->tag == ASN1_TAG_UTF8STRING || |
153 | 0 | hdr->tag == ASN1_TAG_NUMERICSTRING || |
154 | 0 | hdr->tag == ASN1_TAG_PRINTABLESTRING || |
155 | 0 | hdr->tag == ASN1_TAG_T61STRING || |
156 | 0 | hdr->tag == ASN1_TAG_VIDEOTEXSTRING || |
157 | 0 | hdr->tag == ASN1_TAG_IA5STRING || |
158 | 0 | hdr->tag == ASN1_TAG_GRAPHICSTRING || |
159 | 0 | hdr->tag == ASN1_TAG_VISIBLESTRING || |
160 | 0 | hdr->tag == ASN1_TAG_GENERALSTRING || |
161 | 0 | hdr->tag == ASN1_TAG_UNIVERSALSTRING || |
162 | 0 | hdr->tag == ASN1_TAG_CHARACTERSTRING || |
163 | 0 | hdr->tag == ASN1_TAG_BMPSTRING; |
164 | 0 | } |
165 | | |
166 | | static inline bool asn1_is_bmpstring(const struct asn1_hdr *hdr) |
167 | 0 | { |
168 | 0 | return hdr->class == ASN1_CLASS_UNIVERSAL && |
169 | 0 | hdr->tag == ASN1_TAG_BMPSTRING; |
170 | 0 | } |
171 | | |
172 | | static inline bool asn1_is_utf8string(const struct asn1_hdr *hdr) |
173 | 0 | { |
174 | 0 | return hdr->class == ASN1_CLASS_UNIVERSAL && |
175 | 0 | hdr->tag == ASN1_TAG_UTF8STRING; |
176 | 0 | } |
177 | | |
178 | | static inline bool asn1_is_null(const struct asn1_hdr *hdr) |
179 | 0 | { |
180 | 0 | return hdr->class == ASN1_CLASS_UNIVERSAL && |
181 | 0 | hdr->tag == ASN1_TAG_NULL; |
182 | 0 | } |
183 | | |
184 | | static inline bool asn1_is_cs_tag(const struct asn1_hdr *hdr, unsigned int tag) |
185 | 0 | { |
186 | 0 | return hdr->class == ASN1_CLASS_CONTEXT_SPECIFIC && |
187 | 0 | hdr->tag == tag; |
188 | 0 | } |
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 */ |