From b197807b5404c8c4724ea92c133e320ac0476d6e Mon Sep 17 00:00:00 2001 From: Jon Shallow Date: Sat, 18 Apr 2026 11:13:10 +0100 Subject: [PATCH] ASN1: Add in limit check for recursive tags --- src/coap_asn1.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/coap_asn1.c b/src/coap_asn1.c index 94c747e2..788387b5 100644 --- a/src/coap_asn1.c +++ b/src/coap_asn1.c @@ -83,10 +83,9 @@ asn1_tag_c(const uint8_t **ptr, size_t *plen, int *constructed, int *cls) { return tag; } -/* caller must free off returned coap_binary_t* */ -coap_binary_t * -get_asn1_tag(coap_asn1_tag_t ltag, const uint8_t *ptr, size_t tlen, - asn1_validate validate) { +static coap_binary_t * +get_asn1_tag_internal(coap_asn1_tag_t ltag, const uint8_t *ptr, size_t tlen, + asn1_validate validate, uint32_t recursive_check) { int constructed; int class; coap_asn1_tag_t tag = asn1_tag_c(&ptr, &tlen, &constructed, &class); @@ -117,7 +116,9 @@ get_asn1_tag(coap_asn1_tag_t ltag, const uint8_t *ptr, size_t tlen, } if (tag == 0x10 && constructed == 1) { /* SEQUENCE or SEQUENCE OF */ - tag_data = get_asn1_tag(ltag, ptr, len, validate); + if (recursive_check > 100) + return NULL; + tag_data = get_asn1_tag_internal(ltag, ptr, len, validate, recursive_check + 1); if (tag_data) return tag_data; } @@ -132,6 +133,13 @@ get_asn1_tag(coap_asn1_tag_t ltag, const uint8_t *ptr, size_t tlen, return NULL; } +/* caller must free off returned coap_binary_t* */ +coap_binary_t * +get_asn1_tag(coap_asn1_tag_t ltag, const uint8_t *ptr, size_t tlen, + asn1_validate validate) { + return get_asn1_tag_internal(ltag, ptr, tlen, validate, 0); +} + /* first part of Raw public key, this is the start of the Subject Public Key */ static const unsigned char cert_asn1_header1[] = { 0x30, 0x59, /* SEQUENCE, length 89 bytes */