Debian Codegraph @

internal/legacy/x509/verify.go

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