dn.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. // AttributeTypeAndValue represents an attributeTypeAndValue from https://tools.ietf.org/html/rfc4514
  55. type AttributeTypeAndValue struct {
  56. // Type is the attribute type
  57. Type string
  58. // Value is the attribute value
  59. Value string
  60. }
  61. // RelativeDN represents a relativeDistinguishedName from https://tools.ietf.org/html/rfc4514
  62. type RelativeDN struct {
  63. Attributes []*AttributeTypeAndValue
  64. }
  65. // DN represents a distinguishedName from https://tools.ietf.org/html/rfc4514
  66. type DN struct {
  67. RDNs []*RelativeDN
  68. }
  69. // ParseDN returns a distinguishedName or an error
  70. func ParseDN(str string) (*DN, error) {
  71. dn := new(DN)
  72. dn.RDNs = make([]*RelativeDN, 0)
  73. rdn := new(RelativeDN)
  74. rdn.Attributes = make([]*AttributeTypeAndValue, 0)
  75. buffer := bytes.Buffer{}
  76. attribute := new(AttributeTypeAndValue)
  77. escaping := false
  78. for i := 0; i < len(str); i++ {
  79. char := str[i]
  80. if escaping {
  81. escaping = false
  82. switch char {
  83. case ' ', '"', '#', '+', ',', ';', '<', '=', '>', '\\':
  84. buffer.WriteByte(char)
  85. continue
  86. }
  87. // Not a special character, assume hex encoded octet
  88. if len(str) == i+1 {
  89. return nil, errors.New("Got corrupted escaped character")
  90. }
  91. dst := []byte{0}
  92. n, err := enchex.Decode([]byte(dst), []byte(str[i:i+2]))
  93. if err != nil {
  94. return nil, fmt.Errorf("Failed to decode escaped character: %s", err)
  95. } else if n != 1 {
  96. return nil, fmt.Errorf("Expected 1 byte when un-escaping, got %d", n)
  97. }
  98. buffer.WriteByte(dst[0])
  99. i++
  100. } else if char == '\\' {
  101. escaping = true
  102. } else if char == '=' {
  103. attribute.Type = buffer.String()
  104. buffer.Reset()
  105. // Special case: If the first character in the value is # the
  106. // following data is BER encoded so we can just fast forward
  107. // and decode.
  108. if len(str) > i+1 && str[i+1] == '#' {
  109. i += 2
  110. index := strings.IndexAny(str[i:], ",+")
  111. data := str
  112. if index > 0 {
  113. data = str[i : i+index]
  114. } else {
  115. data = str[i:]
  116. }
  117. rawBER, err := enchex.DecodeString(data)
  118. if err != nil {
  119. return nil, fmt.Errorf("Failed to decode BER encoding: %s", err)
  120. }
  121. packet := ber.DecodePacket(rawBER)
  122. buffer.WriteString(packet.Data.String())
  123. i += len(data) - 1
  124. }
  125. } else if char == ',' || char == '+' {
  126. // We're done with this RDN or value, push it
  127. attribute.Value = buffer.String()
  128. rdn.Attributes = append(rdn.Attributes, attribute)
  129. attribute = new(AttributeTypeAndValue)
  130. if char == ',' {
  131. dn.RDNs = append(dn.RDNs, rdn)
  132. rdn = new(RelativeDN)
  133. rdn.Attributes = make([]*AttributeTypeAndValue, 0)
  134. }
  135. buffer.Reset()
  136. } else {
  137. buffer.WriteByte(char)
  138. }
  139. }
  140. if buffer.Len() > 0 {
  141. if len(attribute.Type) == 0 {
  142. return nil, errors.New("DN ended with incomplete type, value pair")
  143. }
  144. attribute.Value = buffer.String()
  145. rdn.Attributes = append(rdn.Attributes, attribute)
  146. dn.RDNs = append(dn.RDNs, rdn)
  147. }
  148. return dn, nil
  149. }