xref: /openbsd-src/lib/libcrypto/bytestring/bs_ber.c (revision ae014d6e3f9ccf3d9970baaca440e60715a6e7f5)
1*ae014d6eStb /*	$OpenBSD: bs_ber.c,v 1.3 2024/05/25 15:12:47 tb Exp $	*/
23c46923dSjsing /*
33c46923dSjsing  * Copyright (c) 2014, Google Inc.
43c46923dSjsing  *
53c46923dSjsing  * Permission to use, copy, modify, and/or distribute this software for any
63c46923dSjsing  * purpose with or without fee is hereby granted, provided that the above
73c46923dSjsing  * copyright notice and this permission notice appear in all copies.
83c46923dSjsing  *
93c46923dSjsing  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
103c46923dSjsing  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
113c46923dSjsing  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
123c46923dSjsing  * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
133c46923dSjsing  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
143c46923dSjsing  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
153c46923dSjsing  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
163c46923dSjsing  */
173c46923dSjsing 
18*ae014d6eStb #include <stdint.h>
193c46923dSjsing #include <string.h>
203c46923dSjsing 
213c46923dSjsing #include "bytestring.h"
223c46923dSjsing 
233c46923dSjsing /*
243c46923dSjsing  * kMaxDepth is a just a sanity limit. The code should be such that the length
253c46923dSjsing  * of the input being processes always decreases. None the less, a very large
263c46923dSjsing  * input could otherwise cause the stack to overflow.
273c46923dSjsing  */
283c46923dSjsing static const unsigned int kMaxDepth = 2048;
293c46923dSjsing 
303c46923dSjsing /* Non-strict version that allows a relaxed DER with indefinite form. */
313c46923dSjsing static int
cbs_nonstrict_get_any_asn1_element(CBS * cbs,CBS * out,unsigned int * out_tag,size_t * out_header_len)323c46923dSjsing cbs_nonstrict_get_any_asn1_element(CBS *cbs, CBS *out, unsigned int *out_tag,
333c46923dSjsing     size_t *out_header_len)
343c46923dSjsing {
353c46923dSjsing 	return cbs_get_any_asn1_element_internal(cbs, out,
363c46923dSjsing 	    out_tag, out_header_len, 0);
373c46923dSjsing }
383c46923dSjsing 
393c46923dSjsing /*
403c46923dSjsing  * cbs_find_indefinite walks an ASN.1 structure in |orig_in| and sets
413c46923dSjsing  * |*indefinite_found| depending on whether an indefinite length element was
423c46923dSjsing  * found. The value of |orig_in| is not modified.
433c46923dSjsing  *
443c46923dSjsing  * Returns one on success (i.e. |*indefinite_found| was set) and zero on error.
453c46923dSjsing  */
463c46923dSjsing static int
cbs_find_indefinite(const CBS * orig_in,char * indefinite_found,unsigned int depth)473c46923dSjsing cbs_find_indefinite(const CBS *orig_in, char *indefinite_found,
483c46923dSjsing     unsigned int depth)
493c46923dSjsing {
503c46923dSjsing 	CBS in;
513c46923dSjsing 
523c46923dSjsing 	if (depth > kMaxDepth)
533c46923dSjsing 		return 0;
543c46923dSjsing 
553c46923dSjsing 	CBS_init(&in, CBS_data(orig_in), CBS_len(orig_in));
563c46923dSjsing 
573c46923dSjsing 	while (CBS_len(&in) > 0) {
583c46923dSjsing 		CBS contents;
593c46923dSjsing 		unsigned int tag;
603c46923dSjsing 		size_t header_len;
613c46923dSjsing 
623c46923dSjsing 		if (!cbs_nonstrict_get_any_asn1_element(&in, &contents, &tag,
633c46923dSjsing 		    &header_len))
643c46923dSjsing 			return 0;
653c46923dSjsing 
663c46923dSjsing 		/* Indefinite form not allowed by DER. */
673c46923dSjsing 		if (CBS_len(&contents) == header_len && header_len > 0 &&
683c46923dSjsing 		    CBS_data(&contents)[header_len - 1] == 0x80) {
693c46923dSjsing 			*indefinite_found = 1;
703c46923dSjsing 			return 1;
713c46923dSjsing 		}
723c46923dSjsing 		if (tag & CBS_ASN1_CONSTRUCTED) {
733c46923dSjsing 			if (!CBS_skip(&contents, header_len) ||
743c46923dSjsing 			    !cbs_find_indefinite(&contents, indefinite_found,
753c46923dSjsing 			    depth + 1))
763c46923dSjsing 				return 0;
773c46923dSjsing 		}
783c46923dSjsing 	}
793c46923dSjsing 
803c46923dSjsing 	*indefinite_found = 0;
813c46923dSjsing 	return 1;
823c46923dSjsing }
833c46923dSjsing 
843c46923dSjsing /*
853c46923dSjsing  * is_primitive_type returns true if |tag| likely a primitive type. Normally
863c46923dSjsing  * one can just test the "constructed" bit in the tag but, in BER, even
873c46923dSjsing  * primitive tags can have the constructed bit if they have indefinite
883c46923dSjsing  * length.
893c46923dSjsing  */
903c46923dSjsing static char
is_primitive_type(unsigned int tag)913c46923dSjsing is_primitive_type(unsigned int tag)
923c46923dSjsing {
933c46923dSjsing 	return (tag & 0xc0) == 0 &&
943c46923dSjsing 	    (tag & 0x1f) != (CBS_ASN1_SEQUENCE & 0x1f) &&
953c46923dSjsing 	    (tag & 0x1f) != (CBS_ASN1_SET & 0x1f);
963c46923dSjsing }
973c46923dSjsing 
983c46923dSjsing /*
993c46923dSjsing  * is_eoc returns true if |header_len| and |contents|, as returned by
1003c46923dSjsing  * |cbs_nonstrict_get_any_asn1_element|, indicate an "end of contents" (EOC)
1013c46923dSjsing  * value.
1023c46923dSjsing  */
1033c46923dSjsing static char
is_eoc(size_t header_len,CBS * contents)1043c46923dSjsing is_eoc(size_t header_len, CBS *contents)
1053c46923dSjsing {
1063c46923dSjsing 	const unsigned char eoc[] = {0x0, 0x0};
1073c46923dSjsing 
1083c46923dSjsing 	return header_len == 2 && CBS_mem_equal(contents, eoc, 2);
1093c46923dSjsing }
1103c46923dSjsing 
1113c46923dSjsing /*
1123c46923dSjsing  * cbs_convert_indefinite reads data with DER encoding (but relaxed to allow
1133c46923dSjsing  * indefinite form) from |in| and writes definite form DER data to |out|. If
1143c46923dSjsing  * |squash_header| is set then the top-level of elements from |in| will not
1153c46923dSjsing  * have their headers written. This is used when concatenating the fragments of
1163c46923dSjsing  * an indefinite length, primitive value. If |looking_for_eoc| is set then any
1173c46923dSjsing  * EOC elements found will cause the function to return after consuming it.
1183c46923dSjsing  * It returns one on success and zero on error.
1193c46923dSjsing  */
1203c46923dSjsing static int
cbs_convert_indefinite(CBS * in,CBB * out,char squash_header,char looking_for_eoc,unsigned int depth)1213c46923dSjsing cbs_convert_indefinite(CBS *in, CBB *out, char squash_header,
1223c46923dSjsing     char looking_for_eoc, unsigned int depth)
1233c46923dSjsing {
1243c46923dSjsing 	if (depth > kMaxDepth)
1253c46923dSjsing 		return 0;
1263c46923dSjsing 
1273c46923dSjsing 	while (CBS_len(in) > 0) {
1283c46923dSjsing 		CBS contents;
1293c46923dSjsing 		unsigned int tag;
1303c46923dSjsing 		size_t header_len;
1313c46923dSjsing 		CBB *out_contents, out_contents_storage;
1323c46923dSjsing 
1333c46923dSjsing 		if (!cbs_nonstrict_get_any_asn1_element(in, &contents, &tag,
1343c46923dSjsing 		    &header_len))
1353c46923dSjsing 			return 0;
1363c46923dSjsing 
1373c46923dSjsing 		out_contents = out;
1383c46923dSjsing 
1393c46923dSjsing 		if (CBS_len(&contents) == header_len) {
1403c46923dSjsing 			if (is_eoc(header_len, &contents))
1413c46923dSjsing 				return looking_for_eoc;
1423c46923dSjsing 
1433c46923dSjsing 			if (header_len > 0 &&
1443c46923dSjsing 			    CBS_data(&contents)[header_len - 1] == 0x80) {
1453c46923dSjsing 				/*
1463c46923dSjsing 				 * This is an indefinite length element. If
1473c46923dSjsing 				 * it's a SEQUENCE or SET then we just need to
1483c46923dSjsing 				 * write the out the contents as normal, but
1493c46923dSjsing 				 * with a concrete length prefix.
1503c46923dSjsing 				 *
1513c46923dSjsing 				 * If it's a something else then the contents
1523c46923dSjsing 				 * will be a series of DER elements of the same
1533c46923dSjsing 				 * type which need to be concatenated.
1543c46923dSjsing 				 */
1553c46923dSjsing 				const char context_specific = (tag & 0xc0)
1563c46923dSjsing 				    == 0x80;
1573c46923dSjsing 				char squash_child_headers =
1583c46923dSjsing 				    is_primitive_type(tag);
1593c46923dSjsing 
1603c46923dSjsing 				/*
1613c46923dSjsing 				 * This is a hack, but it sufficies to handle
1623c46923dSjsing 				 * NSS's output. If we find an indefinite
1633c46923dSjsing 				 * length, context-specific tag with a definite,
1643c46923dSjsing 				 * primtive tag inside it, then we assume that
1653c46923dSjsing 				 * the context-specific tag is implicit and the
1663c46923dSjsing 				 * tags within are fragments of a primitive type
1673c46923dSjsing 				 * that need to be concatenated.
1683c46923dSjsing 				 */
1693c46923dSjsing 				if (context_specific &&
1703c46923dSjsing 				    (tag & CBS_ASN1_CONSTRUCTED)) {
1713c46923dSjsing 					CBS in_copy, inner_contents;
1723c46923dSjsing 					unsigned int inner_tag;
1733c46923dSjsing 					size_t inner_header_len;
1743c46923dSjsing 
1753c46923dSjsing 					CBS_init(&in_copy, CBS_data(in),
1763c46923dSjsing 					    CBS_len(in));
1773c46923dSjsing 					if (!cbs_nonstrict_get_any_asn1_element(
1783c46923dSjsing 					    &in_copy, &inner_contents,
1793c46923dSjsing 					    &inner_tag, &inner_header_len))
1803c46923dSjsing 						return 0;
1813c46923dSjsing 
1823c46923dSjsing 					if (CBS_len(&inner_contents) >
1833c46923dSjsing 					    inner_header_len &&
1843c46923dSjsing 					    is_primitive_type(inner_tag))
1853c46923dSjsing 						squash_child_headers = 1;
1863c46923dSjsing 				}
1873c46923dSjsing 
1883c46923dSjsing 				if (!squash_header) {
1893c46923dSjsing 					unsigned int out_tag = tag;
1903c46923dSjsing 
1913c46923dSjsing 					if (squash_child_headers)
1923c46923dSjsing 						out_tag &=
1933c46923dSjsing 						    ~CBS_ASN1_CONSTRUCTED;
1943c46923dSjsing 
1953c46923dSjsing 					if (!CBB_add_asn1(out,
1963c46923dSjsing 					    &out_contents_storage, out_tag))
1973c46923dSjsing 						return 0;
1983c46923dSjsing 
1993c46923dSjsing 					out_contents = &out_contents_storage;
2003c46923dSjsing 				}
2013c46923dSjsing 
2023c46923dSjsing 				if (!cbs_convert_indefinite(in, out_contents,
2033c46923dSjsing 				    squash_child_headers,
2043c46923dSjsing 				    1 /* looking for eoc */, depth + 1))
2053c46923dSjsing 					return 0;
2063c46923dSjsing 
2073c46923dSjsing 				if (out_contents != out && !CBB_flush(out))
2083c46923dSjsing 					return 0;
2093c46923dSjsing 
2103c46923dSjsing 				continue;
2113c46923dSjsing 			}
2123c46923dSjsing 		}
2133c46923dSjsing 
2143c46923dSjsing 		if (!squash_header) {
2153c46923dSjsing 			if (!CBB_add_asn1(out, &out_contents_storage, tag))
2163c46923dSjsing 				return 0;
2173c46923dSjsing 
2183c46923dSjsing 			out_contents = &out_contents_storage;
2193c46923dSjsing 		}
2203c46923dSjsing 
2213c46923dSjsing 		if (!CBS_skip(&contents, header_len))
2223c46923dSjsing 			return 0;
2233c46923dSjsing 
2243c46923dSjsing 		if (tag & CBS_ASN1_CONSTRUCTED) {
2253c46923dSjsing 			if (!cbs_convert_indefinite(&contents, out_contents,
2263c46923dSjsing 			    0 /* don't squash header */,
2273c46923dSjsing 			    0 /* not looking for eoc */, depth + 1))
2283c46923dSjsing 				return 0;
2293c46923dSjsing 		} else {
2303c46923dSjsing 			if (!CBB_add_bytes(out_contents, CBS_data(&contents),
2313c46923dSjsing 			    CBS_len(&contents)))
2323c46923dSjsing 				return 0;
2333c46923dSjsing 		}
2343c46923dSjsing 
2353c46923dSjsing 		if (out_contents != out && !CBB_flush(out))
2363c46923dSjsing 			return 0;
2373c46923dSjsing 	}
2383c46923dSjsing 
2393c46923dSjsing 	return looking_for_eoc == 0;
2403c46923dSjsing }
2413c46923dSjsing 
2423c46923dSjsing int
CBS_asn1_indefinite_to_definite(CBS * in,uint8_t ** out,size_t * out_len)2433c46923dSjsing CBS_asn1_indefinite_to_definite(CBS *in, uint8_t **out, size_t *out_len)
2443c46923dSjsing {
2453c46923dSjsing 	CBB cbb;
2463c46923dSjsing 
2473c46923dSjsing 	/*
2483c46923dSjsing 	 * First, do a quick walk to find any indefinite-length elements. Most
2493c46923dSjsing 	 * of the time we hope that there aren't any and thus we can quickly
2503c46923dSjsing 	 * return.
2513c46923dSjsing 	 */
2523c46923dSjsing 	char conversion_needed;
2533c46923dSjsing 	if (!cbs_find_indefinite(in, &conversion_needed, 0))
2543c46923dSjsing 		return 0;
2553c46923dSjsing 
2563c46923dSjsing 	if (!conversion_needed) {
2573c46923dSjsing 		*out = NULL;
2583c46923dSjsing 		*out_len = 0;
2593c46923dSjsing 		return 1;
2603c46923dSjsing 	}
2613c46923dSjsing 
2623c46923dSjsing 	if (!CBB_init(&cbb, CBS_len(in)))
2633c46923dSjsing 		return 0;
2643c46923dSjsing 	if (!cbs_convert_indefinite(in, &cbb, 0, 0, 0)) {
2653c46923dSjsing 		CBB_cleanup(&cbb);
2663c46923dSjsing 		return 0;
2673c46923dSjsing 	}
2683c46923dSjsing 
2693c46923dSjsing 	return CBB_finish(&cbb, out, out_len);
2703c46923dSjsing }
271