verify.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. // Copyright 2011 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. //nolint:gocritic // code copied from crypto/x509
  5. package legacyx509
  6. import (
  7. "bytes"
  8. "strings"
  9. )
  10. // rfc2821Mailbox represents a “mailbox” (which is an email address to most
  11. // people) by breaking it into the “local” (i.e. before the '@') and “domain”
  12. // parts.
  13. type rfc2821Mailbox struct {
  14. local, domain string
  15. }
  16. // parseRFC2821Mailbox parses an email address into local and domain parts,
  17. // based on the ABNF for a “Mailbox” from RFC 2821. According to RFC 5280,
  18. // Section 4.2.1.6 that's correct for an rfc822Name from a certificate: “The
  19. // format of an rfc822Name is a "Mailbox" as defined in RFC 2821, Section 4.1.2”.
  20. func parseRFC2821Mailbox(in string) (mailbox rfc2821Mailbox, ok bool) {
  21. if len(in) == 0 {
  22. return mailbox, false
  23. }
  24. localPartBytes := make([]byte, 0, len(in)/2)
  25. if in[0] == '"' {
  26. // Quoted-string = DQUOTE *qcontent DQUOTE
  27. // non-whitespace-control = %d1-8 / %d11 / %d12 / %d14-31 / %d127
  28. // qcontent = qtext / quoted-pair
  29. // qtext = non-whitespace-control /
  30. // %d33 / %d35-91 / %d93-126
  31. // quoted-pair = ("\" text) / obs-qp
  32. // text = %d1-9 / %d11 / %d12 / %d14-127 / obs-text
  33. //
  34. // (Names beginning with “obs-” are the obsolete syntax from RFC 2822,
  35. // Section 4. Since it has been 16 years, we no longer accept that.)
  36. in = in[1:]
  37. QuotedString:
  38. for {
  39. if len(in) == 0 {
  40. return mailbox, false
  41. }
  42. c := in[0]
  43. in = in[1:]
  44. switch {
  45. case c == '"':
  46. break QuotedString
  47. case c == '\\':
  48. // quoted-pair
  49. if len(in) == 0 {
  50. return mailbox, false
  51. }
  52. if in[0] == 11 ||
  53. in[0] == 12 ||
  54. (1 <= in[0] && in[0] <= 9) ||
  55. (14 <= in[0] && in[0] <= 127) {
  56. localPartBytes = append(localPartBytes, in[0])
  57. in = in[1:]
  58. } else {
  59. return mailbox, false
  60. }
  61. case c == 11 ||
  62. c == 12 ||
  63. // Space (char 32) is not allowed based on the
  64. // BNF, but RFC 3696 gives an example that
  65. // assumes that it is. Several “verified”
  66. // errata continue to argue about this point.
  67. // We choose to accept it.
  68. c == 32 ||
  69. c == 33 ||
  70. c == 127 ||
  71. (1 <= c && c <= 8) ||
  72. (14 <= c && c <= 31) ||
  73. (35 <= c && c <= 91) ||
  74. (93 <= c && c <= 126):
  75. // qtext
  76. localPartBytes = append(localPartBytes, c)
  77. default:
  78. return mailbox, false
  79. }
  80. }
  81. } else {
  82. // Atom ("." Atom)*
  83. NextChar:
  84. for len(in) > 0 {
  85. // atext from RFC 2822, Section 3.2.4
  86. c := in[0]
  87. switch {
  88. case c == '\\':
  89. // Examples given in RFC 3696 suggest that
  90. // escaped characters can appear outside of a
  91. // quoted string. Several “verified” errata
  92. // continue to argue the point. We choose to
  93. // accept it.
  94. in = in[1:]
  95. if len(in) == 0 {
  96. return mailbox, false
  97. }
  98. fallthrough
  99. case ('0' <= c && c <= '9') ||
  100. ('a' <= c && c <= 'z') ||
  101. ('A' <= c && c <= 'Z') ||
  102. c == '!' || c == '#' || c == '$' || c == '%' ||
  103. c == '&' || c == '\'' || c == '*' || c == '+' ||
  104. c == '-' || c == '/' || c == '=' || c == '?' ||
  105. c == '^' || c == '_' || c == '`' || c == '{' ||
  106. c == '|' || c == '}' || c == '~' || c == '.':
  107. localPartBytes = append(localPartBytes, in[0])
  108. in = in[1:]
  109. default:
  110. break NextChar
  111. }
  112. }
  113. if len(localPartBytes) == 0 {
  114. return mailbox, false
  115. }
  116. // From RFC 3696, Section 3:
  117. // “period (".") may also appear, but may not be used to start
  118. // or end the local part, nor may two or more consecutive
  119. // periods appear.”
  120. twoDots := []byte{'.', '.'}
  121. if localPartBytes[0] == '.' ||
  122. localPartBytes[len(localPartBytes)-1] == '.' ||
  123. bytes.Contains(localPartBytes, twoDots) {
  124. return mailbox, false
  125. }
  126. }
  127. if len(in) == 0 || in[0] != '@' {
  128. return mailbox, false
  129. }
  130. in = in[1:]
  131. // The RFC species a format for domains, but that's known to be
  132. // violated in practice so we accept that anything after an '@' is the
  133. // domain part.
  134. if _, ok := domainToReverseLabels(in); !ok {
  135. return mailbox, false
  136. }
  137. mailbox.local = string(localPartBytes)
  138. mailbox.domain = in
  139. return mailbox, true
  140. }
  141. // domainToReverseLabels converts a textual domain name like foo.example.com to
  142. // the list of labels in reverse order, e.g. ["com", "example", "foo"].
  143. func domainToReverseLabels(domain string) (reverseLabels []string, ok bool) {
  144. for len(domain) > 0 {
  145. if i := strings.LastIndexByte(domain, '.'); i == -1 {
  146. reverseLabels = append(reverseLabels, domain)
  147. domain = ""
  148. } else {
  149. reverseLabels = append(reverseLabels, domain[i+1:])
  150. domain = domain[:i]
  151. if i == 0 { // domain == ""
  152. // domain is prefixed with an empty label, append an empty
  153. // string to reverseLabels to indicate this.
  154. reverseLabels = append(reverseLabels, "")
  155. }
  156. }
  157. }
  158. if len(reverseLabels) > 0 && len(reverseLabels[0]) == 0 {
  159. // An empty label at the end indicates an absolute value.
  160. return nil, false
  161. }
  162. for _, label := range reverseLabels {
  163. if len(label) == 0 {
  164. // Empty labels are otherwise invalid.
  165. return nil, false
  166. }
  167. for _, c := range label {
  168. if c < 33 || c > 126 {
  169. // Invalid character.
  170. return nil, false
  171. }
  172. }
  173. }
  174. return reverseLabels, true
  175. }