From 7cda110d40ba91141b76287d34a04c3a1d0fc9fd Mon Sep 17 00:00:00 2001 From: InfiniteLoopSpace <35842605+InfiniteLoopSpace@users.noreply.github.com> Date: Wed, 14 Nov 2018 12:41:27 +0100 Subject: [PATCH] Add support for indefinite length encoding. --- asn1/asn1.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/asn1/asn1.go b/asn1/asn1.go index 91893c7..cade975 100644 --- a/asn1/asn1.go +++ b/asn1/asn1.go @@ -22,6 +22,7 @@ package asn1 import ( "errors" "fmt" + "log" "math" "math/big" "reflect" @@ -534,6 +535,34 @@ func parseTagAndLength(bytes []byte, initOffset int) (ret tagAndLength, offset i // Bottom 7 bits give the number of length bytes to follow. numBytes := int(b & 0x7f) if numBytes == 0 { + if ret.isCompound { + reTag, reoffset, _ := parseTagAndLength(bytes, offset) + for reoffset <= len(bytes) { + if bytes[reoffset-2] == 0x00 && bytes[reoffset-1] == 0x00 { + //delete indefinite termination + bytes = append(bytes[:reoffset-2], bytes[reoffset:]...) + + ret.length = reoffset - 2 - offset + + var encLen []byte + if ret.length >= 128 { + l := lengthLength(ret.length) + encLen = append(encLen, 0x80|byte(l)) + encLen = appendLength(encLen, ret.length) + } else { + encLen = append(encLen, byte(ret.length)) + } + + if ret.length > 0 { + bytes = append(bytes[:offset-1], append(encLen, bytes[offset:]...)...) + offset = offset + len(encLen) - 1 + } + return + } + reTag, reoffset, _ = parseTagAndLength(bytes, reoffset+reTag.length) + } + log.Println("indefinite length found (not DER)") + } err = SyntaxError{"indefinite length found (not DER)"} return }