From 7ff30e1caf5d2a4ceffde956bbee1966919ea0c3 Mon Sep 17 00:00:00 2001 From: Christopher Puschmann Date: Sun, 7 Apr 2024 11:09:18 +0200 Subject: [PATCH 1/2] fix: Replace DER with ASN1 BER encoding when parsing distinguishedNames --- dn.go | 14 +++++--------- dn_test.go | 2 +- v3/dn.go | 14 +++++--------- v3/dn_test.go | 2 +- 4 files changed, 12 insertions(+), 20 deletions(-) diff --git a/dn.go b/dn.go index 39b6833..e9ec4d0 100644 --- a/dn.go +++ b/dn.go @@ -1,10 +1,10 @@ package ldap import ( - "encoding/asn1" "encoding/hex" "errors" "fmt" + ber "github.com/go-asn1-ber/asn1-ber" "sort" "strings" "unicode" @@ -233,19 +233,15 @@ func encodeString(value string, isValue bool) string { func decodeEncodedString(str string) (string, error) { decoded, err := hex.DecodeString(str) if err != nil { - return "", fmt.Errorf("failed to decode BER encoding: %s", err) + return "", fmt.Errorf("failed to decode BER encoding: %w", err) } - var rawValue asn1.RawValue - result, err := asn1.Unmarshal(decoded, &rawValue) + packet, err := ber.DecodePacketErr(decoded) if err != nil { - return "", fmt.Errorf("failed to unmarshal hex-encoded string: %s", err) - } - if len(result) != 0 { - return "", errors.New("trailing data after unmarshalling hex-encoded string") + return "", fmt.Errorf("failed to decode BER encoding: %w", err) } - return string(rawValue.Bytes), nil + return packet.Data.String(), nil } // ParseDN returns a distinguishedName or an error. diff --git a/dn_test.go b/dn_test.go index f02d4c1..da367d8 100644 --- a/dn_test.go +++ b/dn_test.go @@ -154,7 +154,7 @@ func TestErrorDNParsing(t *testing.T) { "1.3.6.1.4.1.1466.0=test+": "DN ended with incomplete type, value pair", `1.3.6.1.4.1.1466.0=test;`: "DN ended with incomplete type, value pair", "1.3.6.1.4.1.1466.0=test+,": "incomplete type, value pair", - "DF=#6666666666665006838820013100000746939546349182108463491821809FBFFFFFFFFF": "failed to unmarshal hex-encoded string: asn1: syntax error: data truncated", + "DF=#6666666666665006838820013100000746939546349182108463491821809FBFFFFFFFFF": "failed to decode BER encoding: unexpected EOF", } for test, answer := range testcases { diff --git a/v3/dn.go b/v3/dn.go index 39b6833..e9ec4d0 100644 --- a/v3/dn.go +++ b/v3/dn.go @@ -1,10 +1,10 @@ package ldap import ( - "encoding/asn1" "encoding/hex" "errors" "fmt" + ber "github.com/go-asn1-ber/asn1-ber" "sort" "strings" "unicode" @@ -233,19 +233,15 @@ func encodeString(value string, isValue bool) string { func decodeEncodedString(str string) (string, error) { decoded, err := hex.DecodeString(str) if err != nil { - return "", fmt.Errorf("failed to decode BER encoding: %s", err) + return "", fmt.Errorf("failed to decode BER encoding: %w", err) } - var rawValue asn1.RawValue - result, err := asn1.Unmarshal(decoded, &rawValue) + packet, err := ber.DecodePacketErr(decoded) if err != nil { - return "", fmt.Errorf("failed to unmarshal hex-encoded string: %s", err) - } - if len(result) != 0 { - return "", errors.New("trailing data after unmarshalling hex-encoded string") + return "", fmt.Errorf("failed to decode BER encoding: %w", err) } - return string(rawValue.Bytes), nil + return packet.Data.String(), nil } // ParseDN returns a distinguishedName or an error. diff --git a/v3/dn_test.go b/v3/dn_test.go index f02d4c1..da367d8 100644 --- a/v3/dn_test.go +++ b/v3/dn_test.go @@ -154,7 +154,7 @@ func TestErrorDNParsing(t *testing.T) { "1.3.6.1.4.1.1466.0=test+": "DN ended with incomplete type, value pair", `1.3.6.1.4.1.1466.0=test;`: "DN ended with incomplete type, value pair", "1.3.6.1.4.1.1466.0=test+,": "incomplete type, value pair", - "DF=#6666666666665006838820013100000746939546349182108463491821809FBFFFFFFFFF": "failed to unmarshal hex-encoded string: asn1: syntax error: data truncated", + "DF=#6666666666665006838820013100000746939546349182108463491821809FBFFFFFFFFF": "failed to decode BER encoding: unexpected EOF", } for test, answer := range testcases { From 24a4046e0c06bdca27a8147cbddc893d922ea34d Mon Sep 17 00:00:00 2001 From: Christopher Puschmann Date: Sun, 7 Apr 2024 11:15:10 +0200 Subject: [PATCH 2/2] Remove leftover comment --- dn.go | 3 --- v3/dn.go | 3 --- 2 files changed, 6 deletions(-) diff --git a/dn.go b/dn.go index e9ec4d0..6520b8e 100644 --- a/dn.go +++ b/dn.go @@ -35,9 +35,6 @@ func (a *AttributeTypeAndValue) setValue(s string) error { // AttributeValue is represented by an number sign ('#' U+0023) // character followed by the hexadecimal encoding of each of the octets // of the BER encoding of the X.500 AttributeValue. - // - // WARNING: we only support hex-encoded ASN.1 DER values here, not - // BER encoding. This is a deviation from the RFC. if len(s) > 0 && s[0] == '#' { decodedString, err := decodeEncodedString(s[1:]) if err != nil { diff --git a/v3/dn.go b/v3/dn.go index e9ec4d0..6520b8e 100644 --- a/v3/dn.go +++ b/v3/dn.go @@ -35,9 +35,6 @@ func (a *AttributeTypeAndValue) setValue(s string) error { // AttributeValue is represented by an number sign ('#' U+0023) // character followed by the hexadecimal encoding of each of the octets // of the BER encoding of the X.500 AttributeValue. - // - // WARNING: we only support hex-encoded ASN.1 DER values here, not - // BER encoding. This is a deviation from the RFC. if len(s) > 0 && s[0] == '#' { decodedString, err := decodeEncodedString(s[1:]) if err != nil {