Skip to content

Commit 2260012

Browse files
authored
fix: Replace DER with ASN1 BER encoding when parsing distinguishedNames (#505)
* fix: Replace DER with ASN1 BER encoding when parsing distinguishedNames * Remove leftover comment
1 parent 9c14185 commit 2260012

File tree

4 files changed

+12
-26
lines changed

4 files changed

+12
-26
lines changed

dn.go

+5-12
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package ldap
22

33
import (
4-
"encoding/asn1"
54
"encoding/hex"
65
"errors"
76
"fmt"
7+
ber "github.com/go-asn1-ber/asn1-ber"
88
"sort"
99
"strings"
1010
"unicode"
@@ -35,9 +35,6 @@ func (a *AttributeTypeAndValue) setValue(s string) error {
3535
// AttributeValue is represented by an number sign ('#' U+0023)
3636
// character followed by the hexadecimal encoding of each of the octets
3737
// of the BER encoding of the X.500 AttributeValue.
38-
//
39-
// WARNING: we only support hex-encoded ASN.1 DER values here, not
40-
// BER encoding. This is a deviation from the RFC.
4138
if len(s) > 0 && s[0] == '#' {
4239
decodedString, err := decodeEncodedString(s[1:])
4340
if err != nil {
@@ -233,19 +230,15 @@ func encodeString(value string, isValue bool) string {
233230
func decodeEncodedString(str string) (string, error) {
234231
decoded, err := hex.DecodeString(str)
235232
if err != nil {
236-
return "", fmt.Errorf("failed to decode BER encoding: %s", err)
233+
return "", fmt.Errorf("failed to decode BER encoding: %w", err)
237234
}
238235

239-
var rawValue asn1.RawValue
240-
result, err := asn1.Unmarshal(decoded, &rawValue)
236+
packet, err := ber.DecodePacketErr(decoded)
241237
if err != nil {
242-
return "", fmt.Errorf("failed to unmarshal hex-encoded string: %s", err)
243-
}
244-
if len(result) != 0 {
245-
return "", errors.New("trailing data after unmarshalling hex-encoded string")
238+
return "", fmt.Errorf("failed to decode BER encoding: %w", err)
246239
}
247240

248-
return string(rawValue.Bytes), nil
241+
return packet.Data.String(), nil
249242
}
250243

251244
// ParseDN returns a distinguishedName or an error.

dn_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ func TestErrorDNParsing(t *testing.T) {
154154
"1.3.6.1.4.1.1466.0=test+": "DN ended with incomplete type, value pair",
155155
`1.3.6.1.4.1.1466.0=test;`: "DN ended with incomplete type, value pair",
156156
"1.3.6.1.4.1.1466.0=test+,": "incomplete type, value pair",
157-
"DF=#6666666666665006838820013100000746939546349182108463491821809FBFFFFFFFFF": "failed to unmarshal hex-encoded string: asn1: syntax error: data truncated",
157+
"DF=#6666666666665006838820013100000746939546349182108463491821809FBFFFFFFFFF": "failed to decode BER encoding: unexpected EOF",
158158
}
159159

160160
for test, answer := range testcases {

v3/dn.go

+5-12
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package ldap
22

33
import (
4-
"encoding/asn1"
54
"encoding/hex"
65
"errors"
76
"fmt"
7+
ber "github.com/go-asn1-ber/asn1-ber"
88
"sort"
99
"strings"
1010
"unicode"
@@ -35,9 +35,6 @@ func (a *AttributeTypeAndValue) setValue(s string) error {
3535
// AttributeValue is represented by an number sign ('#' U+0023)
3636
// character followed by the hexadecimal encoding of each of the octets
3737
// of the BER encoding of the X.500 AttributeValue.
38-
//
39-
// WARNING: we only support hex-encoded ASN.1 DER values here, not
40-
// BER encoding. This is a deviation from the RFC.
4138
if len(s) > 0 && s[0] == '#' {
4239
decodedString, err := decodeEncodedString(s[1:])
4340
if err != nil {
@@ -233,19 +230,15 @@ func encodeString(value string, isValue bool) string {
233230
func decodeEncodedString(str string) (string, error) {
234231
decoded, err := hex.DecodeString(str)
235232
if err != nil {
236-
return "", fmt.Errorf("failed to decode BER encoding: %s", err)
233+
return "", fmt.Errorf("failed to decode BER encoding: %w", err)
237234
}
238235

239-
var rawValue asn1.RawValue
240-
result, err := asn1.Unmarshal(decoded, &rawValue)
236+
packet, err := ber.DecodePacketErr(decoded)
241237
if err != nil {
242-
return "", fmt.Errorf("failed to unmarshal hex-encoded string: %s", err)
243-
}
244-
if len(result) != 0 {
245-
return "", errors.New("trailing data after unmarshalling hex-encoded string")
238+
return "", fmt.Errorf("failed to decode BER encoding: %w", err)
246239
}
247240

248-
return string(rawValue.Bytes), nil
241+
return packet.Data.String(), nil
249242
}
250243

251244
// ParseDN returns a distinguishedName or an error.

v3/dn_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ func TestErrorDNParsing(t *testing.T) {
154154
"1.3.6.1.4.1.1466.0=test+": "DN ended with incomplete type, value pair",
155155
`1.3.6.1.4.1.1466.0=test;`: "DN ended with incomplete type, value pair",
156156
"1.3.6.1.4.1.1466.0=test+,": "incomplete type, value pair",
157-
"DF=#6666666666665006838820013100000746939546349182108463491821809FBFFFFFFFFF": "failed to unmarshal hex-encoded string: asn1: syntax error: data truncated",
157+
"DF=#6666666666665006838820013100000746939546349182108463491821809FBFFFFFFFFF": "failed to decode BER encoding: unexpected EOF",
158158
}
159159

160160
for test, answer := range testcases {

0 commit comments

Comments
 (0)