dn.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // Copyright 2015 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. //
  5. // File contains DN parsing functionallity
  6. //
  7. // https://tools.ietf.org/html/rfc4514
  8. //
  9. // distinguishedName = [ relativeDistinguishedName
  10. // *( COMMA relativeDistinguishedName ) ]
  11. // relativeDistinguishedName = attributeTypeAndValue
  12. // *( PLUS attributeTypeAndValue )
  13. // attributeTypeAndValue = attributeType EQUALS attributeValue
  14. // attributeType = descr / numericoid
  15. // attributeValue = string / hexstring
  16. //
  17. // ; The following characters are to be escaped when they appear
  18. // ; in the value to be encoded: ESC, one of <escaped>, leading
  19. // ; SHARP or SPACE, trailing SPACE, and NULL.
  20. // string = [ ( leadchar / pair ) [ *( stringchar / pair )
  21. // ( trailchar / pair ) ] ]
  22. //
  23. // leadchar = LUTF1 / UTFMB
  24. // LUTF1 = %x01-1F / %x21 / %x24-2A / %x2D-3A /
  25. // %x3D / %x3F-5B / %x5D-7F
  26. //
  27. // trailchar = TUTF1 / UTFMB
  28. // TUTF1 = %x01-1F / %x21 / %x23-2A / %x2D-3A /
  29. // %x3D / %x3F-5B / %x5D-7F
  30. //
  31. // stringchar = SUTF1 / UTFMB
  32. // SUTF1 = %x01-21 / %x23-2A / %x2D-3A /
  33. // %x3D / %x3F-5B / %x5D-7F
  34. //
  35. // pair = ESC ( ESC / special / hexpair )
  36. // special = escaped / SPACE / SHARP / EQUALS
  37. // escaped = DQUOTE / PLUS / COMMA / SEMI / LANGLE / RANGLE
  38. // hexstring = SHARP 1*hexpair
  39. // hexpair = HEX HEX
  40. //
  41. // where the productions <descr>, <numericoid>, <COMMA>, <DQUOTE>,
  42. // <EQUALS>, <ESC>, <HEX>, <LANGLE>, <NULL>, <PLUS>, <RANGLE>, <SEMI>,
  43. // <SPACE>, <SHARP>, and <UTFMB> are defined in [RFC4512].
  44. //
  45. package ldap
  46. import (
  47. "bytes"
  48. enchex "encoding/hex"
  49. "errors"
  50. "fmt"
  51. "strings"
  52. ber "gopkg.in/asn1-ber.v1"
  53. )
  54. type AttributeTypeAndValue struct {
  55. Type string
  56. Value string
  57. }
  58. type RelativeDN struct {
  59. Attributes []*AttributeTypeAndValue
  60. }
  61. type DN struct {
  62. RDNs []*RelativeDN
  63. }
  64. func ParseDN(str string) (*DN, error) {
  65. dn := new(DN)
  66. dn.RDNs = make([]*RelativeDN, 0)
  67. rdn := new(RelativeDN)
  68. rdn.Attributes = make([]*AttributeTypeAndValue, 0)
  69. buffer := bytes.Buffer{}
  70. attribute := new(AttributeTypeAndValue)
  71. escaping := false
  72. for i := 0; i < len(str); i++ {
  73. char := str[i]
  74. if escaping {
  75. escaping = false
  76. switch char {
  77. case ' ', '"', '#', '+', ',', ';', '<', '=', '>', '\\':
  78. buffer.WriteByte(char)
  79. continue
  80. }
  81. // Not a special character, assume hex encoded octet
  82. if len(str) == i+1 {
  83. return nil, errors.New("Got corrupted escaped character")
  84. }
  85. dst := []byte{0}
  86. n, err := enchex.Decode([]byte(dst), []byte(str[i:i+2]))
  87. if err != nil {
  88. return nil, errors.New(
  89. fmt.Sprintf("Failed to decode escaped character: %s", err))
  90. } else if n != 1 {
  91. return nil, errors.New(
  92. fmt.Sprintf("Expected 1 byte when un-escaping, got %d", n))
  93. }
  94. buffer.WriteByte(dst[0])
  95. i++
  96. } else if char == '\\' {
  97. escaping = true
  98. } else if char == '=' {
  99. attribute.Type = buffer.String()
  100. buffer.Reset()
  101. // Special case: If the first character in the value is # the
  102. // following data is BER encoded so we can just fast forward
  103. // and decode.
  104. if len(str) > i+1 && str[i+1] == '#' {
  105. i += 2
  106. index := strings.IndexAny(str[i:], ",+")
  107. data := str
  108. if index > 0 {
  109. data = str[i : i+index]
  110. } else {
  111. data = str[i:]
  112. }
  113. raw_ber, err := enchex.DecodeString(data)
  114. if err != nil {
  115. return nil, errors.New(
  116. fmt.Sprintf("Failed to decode BER encoding: %s", err))
  117. }
  118. packet := ber.DecodePacket(raw_ber)
  119. buffer.WriteString(packet.Data.String())
  120. i += len(data) - 1
  121. }
  122. } else if char == ',' || char == '+' {
  123. // We're done with this RDN or value, push it
  124. attribute.Value = buffer.String()
  125. rdn.Attributes = append(rdn.Attributes, attribute)
  126. attribute = new(AttributeTypeAndValue)
  127. if char == ',' {
  128. dn.RDNs = append(dn.RDNs, rdn)
  129. rdn = new(RelativeDN)
  130. rdn.Attributes = make([]*AttributeTypeAndValue, 0)
  131. }
  132. buffer.Reset()
  133. } else {
  134. buffer.WriteByte(char)
  135. }
  136. }
  137. if buffer.Len() > 0 {
  138. if len(attribute.Type) == 0 {
  139. return nil, errors.New("DN ended with incomplete type, value pair")
  140. }
  141. attribute.Value = buffer.String()
  142. rdn.Attributes = append(rdn.Attributes, attribute)
  143. dn.RDNs = append(dn.RDNs, rdn)
  144. }
  145. return dn, nil
  146. }