1 /* $OpenBSD: bs_ber.c,v 1.7 2015/06/17 07:20:39 doug Exp $ */
3 * Copyright (c) 2014, Google Inc.
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
12 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
14 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
15 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
19 #include <openssl/opensslconf.h>
21 #include "bytestring.h"
24 * kMaxDepth is a just a sanity limit. The code should be such that the length
25 * of the input being processes always decreases. None the less, a very large
26 * input could otherwise cause the stack to overflow.
28 static const unsigned int kMaxDepth
= 2048;
30 /* Non-strict version that allows a relaxed DER with indefinite form. */
32 cbs_nonstrict_get_any_asn1_element(CBS
*cbs
, CBS
*out
, unsigned int *out_tag
,
33 size_t *out_header_len
)
35 return cbs_get_any_asn1_element_internal(cbs
, out
,
36 out_tag
, out_header_len
, 0);
40 * cbs_find_indefinite walks an ASN.1 structure in |orig_in| and sets
41 * |*indefinite_found| depending on whether an indefinite length element was
42 * found. The value of |orig_in| is not modified.
44 * Returns one on success (i.e. |*indefinite_found| was set) and zero on error.
47 cbs_find_indefinite(const CBS
*orig_in
, char *indefinite_found
,
52 if (depth
> kMaxDepth
)
55 CBS_init(&in
, CBS_data(orig_in
), CBS_len(orig_in
));
57 while (CBS_len(&in
) > 0) {
62 if (!cbs_nonstrict_get_any_asn1_element(&in
, &contents
, &tag
,
66 /* Indefinite form not allowed by DER. */
67 if (CBS_len(&contents
) == header_len
&& header_len
> 0 &&
68 CBS_data(&contents
)[header_len
- 1] == 0x80) {
69 *indefinite_found
= 1;
72 if (tag
& CBS_ASN1_CONSTRUCTED
) {
73 if (!CBS_skip(&contents
, header_len
) ||
74 !cbs_find_indefinite(&contents
, indefinite_found
,
80 *indefinite_found
= 0;
85 * is_primitive_type returns true if |tag| likely a primitive type. Normally
86 * one can just test the "constructed" bit in the tag but, in BER, even
87 * primitive tags can have the constructed bit if they have indefinite
91 is_primitive_type(unsigned int tag
)
93 return (tag
& 0xc0) == 0 &&
94 (tag
& 0x1f) != (CBS_ASN1_SEQUENCE
& 0x1f) &&
95 (tag
& 0x1f) != (CBS_ASN1_SET
& 0x1f);
99 * is_eoc returns true if |header_len| and |contents|, as returned by
100 * |cbs_nonstrict_get_any_asn1_element|, indicate an "end of contents" (EOC)
104 is_eoc(size_t header_len
, CBS
*contents
)
106 return header_len
== 2 && CBS_mem_equal(contents
, "\x00\x00", 2);
110 * cbs_convert_indefinite reads data with DER encoding (but relaxed to allow
111 * indefinite form) from |in| and writes definite form DER data to |out|. If
112 * |squash_header| is set then the top-level of elements from |in| will not
113 * have their headers written. This is used when concatenating the fragments of
114 * an indefinite length, primitive value. If |looking_for_eoc| is set then any
115 * EOC elements found will cause the function to return after consuming it.
116 * It returns one on success and zero on error.
119 cbs_convert_indefinite(CBS
*in
, CBB
*out
, char squash_header
,
120 char looking_for_eoc
, unsigned int depth
)
122 if (depth
> kMaxDepth
)
125 while (CBS_len(in
) > 0) {
129 CBB
*out_contents
, out_contents_storage
;
131 if (!cbs_nonstrict_get_any_asn1_element(in
, &contents
, &tag
,
137 if (CBS_len(&contents
) == header_len
) {
138 if (is_eoc(header_len
, &contents
))
139 return looking_for_eoc
;
141 if (header_len
> 0 &&
142 CBS_data(&contents
)[header_len
- 1] == 0x80) {
144 * This is an indefinite length element. If
145 * it's a SEQUENCE or SET then we just need to
146 * write the out the contents as normal, but
147 * with a concrete length prefix.
149 * If it's a something else then the contents
150 * will be a series of DER elements of the same
151 * type which need to be concatenated.
153 const char context_specific
= (tag
& 0xc0)
155 char squash_child_headers
=
156 is_primitive_type(tag
);
159 * This is a hack, but it sufficies to handle
160 * NSS's output. If we find an indefinite
161 * length, context-specific tag with a definite,
162 * primtive tag inside it, then we assume that
163 * the context-specific tag is implicit and the
164 * tags within are fragments of a primitive type
165 * that need to be concatenated.
167 if (context_specific
&&
168 (tag
& CBS_ASN1_CONSTRUCTED
)) {
169 CBS in_copy
, inner_contents
;
170 unsigned int inner_tag
;
171 size_t inner_header_len
;
173 CBS_init(&in_copy
, CBS_data(in
),
175 if (!cbs_nonstrict_get_any_asn1_element(
176 &in_copy
, &inner_contents
,
177 &inner_tag
, &inner_header_len
))
180 if (CBS_len(&inner_contents
) >
182 is_primitive_type(inner_tag
))
183 squash_child_headers
= 1;
186 if (!squash_header
) {
187 unsigned int out_tag
= tag
;
189 if (squash_child_headers
)
191 ~CBS_ASN1_CONSTRUCTED
;
193 if (!CBB_add_asn1(out
,
194 &out_contents_storage
, out_tag
))
197 out_contents
= &out_contents_storage
;
200 if (!cbs_convert_indefinite(in
, out_contents
,
201 squash_child_headers
,
202 1 /* looking for eoc */, depth
+ 1))
205 if (out_contents
!= out
&& !CBB_flush(out
))
212 if (!squash_header
) {
213 if (!CBB_add_asn1(out
, &out_contents_storage
, tag
))
216 out_contents
= &out_contents_storage
;
219 if (!CBS_skip(&contents
, header_len
))
222 if (tag
& CBS_ASN1_CONSTRUCTED
) {
223 if (!cbs_convert_indefinite(&contents
, out_contents
,
224 0 /* don't squash header */,
225 0 /* not looking for eoc */, depth
+ 1))
228 if (!CBB_add_bytes(out_contents
, CBS_data(&contents
),
233 if (out_contents
!= out
&& !CBB_flush(out
))
237 return looking_for_eoc
== 0;
241 CBS_asn1_indefinite_to_definite(CBS
*in
, uint8_t **out
, size_t *out_len
)
246 * First, do a quick walk to find any indefinite-length elements. Most
247 * of the time we hope that there aren't any and thus we can quickly
250 char conversion_needed
;
251 if (!cbs_find_indefinite(in
, &conversion_needed
, 0))
254 if (!conversion_needed
) {
260 if (!CBB_init(&cbb
, CBS_len(in
)))
262 if (!cbs_convert_indefinite(in
, &cbb
, 0, 0, 0)) {
267 return CBB_finish(&cbb
, out
, out_len
);