Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(458)

Side by Side Diff: net/cert/internal/verify_name_match.cc

Issue 1125333005: RFC 2459 name comparison. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: changes for comment #31 Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | net/cert/internal/verify_name_match_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "net/cert/internal/verify_name_match.h" 5 #include "net/cert/internal/verify_name_match.h"
6
7 #include <string.h>
8
9 #include "base/stl_util.h"
10 #include "base/strings/string16.h"
11 #include "base/strings/string_util.h"
12 #include "base/strings/utf_string_conversion_utils.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "base/sys_byteorder.h"
15 #include "base/third_party/icu/icu_utf.h"
16 #include "base/tuple.h"
6 #include "net/der/input.h" 17 #include "net/der/input.h"
18 #include "net/der/parser.h"
19 #include "net/der/tag.h"
7 20
8 namespace net { 21 namespace net {
9 22
23 namespace {
24
25 // Types of character set checking that NormalizeDirectoryString can perform.
26 enum CharsetEnforcement {
27 NO_ENFORCEMENT,
28 ENFORCE_PRINTABLE_STRING,
29 ENFORCE_ASCII,
30 };
31
32 // Normalizes |output|, a UTF-8 encoded string, as if it contained
33 // only ASCII characters.
34 //
35 // This could be considered a partial subset of RFC 5280 rules, and
36 // is compatible with RFC 2459/3280.
37 //
38 // In particular, RFC 5280, Section 7.1 describes how UTF8String
39 // and PrintableString should be compared - using the LDAP StringPrep
40 // profile of RFC 4518, with case folding and whitespace compression.
41 // However, because it is optional for implementations and because
Ryan Sleevi 2015/07/30 02:30:12 s/optional for implementations/optional for 2459/3
mattm 2015/08/04 05:02:06 Done.
42 // it's desirable to avoid the size cost of the StringPrep tables,
43 // this function treats |output| as if it was composed of ASCII.
44 //
45 // That is, rather than folding all whitespace characters, it only
46 // folds ' '. Rather than case folding using locale-aware handling,
47 // it only folds A-Z to a-z.
48 //
49 // This gives better results than outright rejecting (due to mismatched
50 // encodings), or from doing a strict binary comparison (the minimum
51 // required by RFC 3280), and is sufficient for those certificates
52 // publicly deployed.
53 //
54 // If |charset_enforcement| is not NO_ENFORCEMENT and |output| contains any
55 // characters not allowed in the specified charset, returns false.
56 //
57 // NOTE: |output| will be modified regardless of the return.
58 WARN_UNUSED_RESULT bool NormalizeDirectoryString(
59 CharsetEnforcement charset_enforcement,
60 std::string* output) {
61 // Normalized version will always be equal or shorter than input.
62 // Normalize in place and then truncate the output if necessary.
63 std::string::const_iterator read_iter = output->begin();
64 std::string::iterator write_iter = output->begin();
65
66 for (; read_iter != output->end() && *read_iter == ' '; ++read_iter) {
67 // Ignore leading whitespace.
68 }
69
70 for (; read_iter != output->end(); ++read_iter) {
71 const unsigned char c = *read_iter;
72 if (c == ' ') {
73 // If there are non-whitespace characters remaining in input, compress
74 // multiple whitespace chars to a single space, otherwise ignore trailing
75 // whitespace.
76 std::string::const_iterator next_iter = read_iter + 1;
77 if (next_iter != output->end() && *next_iter != ' ')
78 *(write_iter++) = ' ';
79 } else if (c >= 'A' && c <= 'Z') {
80 // Fold case.
81 *(write_iter++) = c + ('a' - 'A');
82 } else {
83 // Note that these checks depend on the characters allowed by earlier
84 // conditions also being valid for the enforced charset.
85 switch (charset_enforcement) {
86 case ENFORCE_PRINTABLE_STRING:
87 if (!((c >= 'a' && c <= 'z') || (c >= '\'' && c <= ':') || c == '=' ||
88 c == '?'))
Ryan Sleevi 2015/07/30 02:30:12 Probably worth a comment about // See NormalizePr
mattm 2015/08/04 05:02:06 Done.
89 return false;
90 break;
91 case ENFORCE_ASCII:
92 if (c > 0x7F)
93 return false;
94 break;
95 case NO_ENFORCEMENT:
96 break;
97 }
98 *(write_iter++) = c;
Ryan Sleevi 2015/07/30 02:30:12 Just documenting a review note: I had wondered if
mattm 2015/08/04 05:02:06 Acknowledged. I was also assuming that it's faster
99 }
100 }
101 if (write_iter != output->end())
102 output->erase(write_iter, output->end());
103 return true;
104 }
105
106 // Normalizes the DER-encoded PrintableString value |in| according to
107 // RFC 2459, Section 4.1.2.4
108 //
109 // Briefly, normalization involves removing leading and trailing
110 // whitespace, folding multiple whitespace characters into a single
111 // whitespace character, and normalizing on case (this function
112 // normalizes to lowercase).
113 //
114 // During normalization, this function also validates that |in|
115 // is properly encoded - that is, that it restricts to the character
116 // set defined in X.680 (2008), Section 41.4, Table 10. X.680 defines
117 // the valid characters as
118 // a-z A-Z 0-9 (space) ' ( ) + , - . / : = ?
119 //
120 // However, due to an old OpenSSL encoding bug, a number of
121 // certificates have also included '*', which has historically been
122 // allowed by implementations, and so is also allowed here.
123 //
124 // If |in| can be normalized, returns true and sets |output| to the
125 // case folded, normalized value. If |in| is invalid, returns false.
126 // NOTE: |output| will be modified regardless of the return.
127 WARN_UNUSED_RESULT bool NormalizePrintableStringValue(const der::Input& in,
128 std::string* output) {
129 output->assign(in.AsString());
130 return NormalizeDirectoryString(ENFORCE_PRINTABLE_STRING, output);
131 }
132
133 // Normalized a UTF8String value. See the comment for NormalizeDirectoryString
134 // for details.
135 //
136 // If |in| can be normalized, returns true and sets |output| to the
137 // case folded, normalized value. If |in| is invalid, returns false.
138 // NOTE: |output| will be modified regardless of the return.
139 WARN_UNUSED_RESULT bool NormalizeUtf8StringValue(const der::Input& in,
140 std::string* output) {
141 output->assign(in.AsString());
142 return NormalizeDirectoryString(NO_ENFORCEMENT, output);
143 }
144
145 // IA5String is ISO/IEC Registrations 1 and 6 from the ISO
146 // "International Register of Coded Character Sets to be used
147 // with Escape Sequences", plus space and delete. That's just the
148 // polite way of saying 0x00 - 0x7F, aka ASCII (or, more formally,
149 // ISO/IEC 646)
150 //
151 // If |in| can be normalized, returns true and sets |output| to the case folded,
152 // normalized value. If |in| is invalid, returns false.
153 // NOTE: |output| will be modified regardless of the return.
154 WARN_UNUSED_RESULT bool NormalizeIA5StringValue(const der::Input& in,
155 std::string* output) {
156 output->assign(in.AsString());
157 return NormalizeDirectoryString(ENFORCE_ASCII, output);
158 }
159
160 // Converts BMPString value to UTF-8 and then normalizes it. See the comment for
161 // NormalizeDirectoryString for details.
162 //
163 // If |in| can be normalized, returns true and sets |output| to the case folded,
164 // normalized value. If |in| is invalid, returns false.
165 // NOTE: |output| will be modified regardless of the return.
166 WARN_UNUSED_RESULT bool NormalizeBmpStringValue(const der::Input& in,
167 std::string* output) {
168 if (in.Length() % 2 != 0)
169 return false;
170
171 base::string16 in_16bit;
172 if (in.Length()) {
173 memcpy(base::WriteInto(&in_16bit, in.Length() / 2 + 1), in.UnsafeData(),
174 in.Length());
175 }
176 for (base::char16& c : in_16bit) {
177 // BMPString is UCS-2 in big-endian order.
178 c = base::NetToHost16(c);
179
180 // BMPString only supports codepoints in the Basic Multilingual Plane;
181 // surrogates are not allowed.
182 if (CBU_IS_SURROGATE(c))
183 return false;
184 }
185 if (!base::UTF16ToUTF8(in_16bit.data(), in_16bit.size(), output))
186 return false;
187 return NormalizeDirectoryString(NO_ENFORCEMENT, output);
188 }
189
190 // Converts UniversalString value to UTF-8 and then normalizes it. See the
191 // comment for NormalizeDirectoryString for details.
192 //
193 // If |in| can be normalized, returns true and sets |output| to the case folded,
194 // normalized value. If |in| is invalid, returns false.
195 // NOTE: |output| will be modified regardless of the return.
196 WARN_UNUSED_RESULT bool NormalizeUniversalStringValue(const der::Input& in,
197 std::string* output) {
198 if (in.Length() % 4 != 0)
199 return false;
200
201 std::vector<uint32_t> in_32bit(in.Length() / 4);
202 if (in.Length())
203 memcpy(vector_as_array(&in_32bit), in.UnsafeData(), in.Length());
204 for (const uint32_t c : in_32bit) {
205 // UniversalString is UCS-4 in big-endian order.
206 uint32_t codepoint = base::NetToHost32(c);
207 if (!CBU_IS_UNICODE_CHAR(codepoint))
208 return false;
209
210 base::WriteUnicodeCharacter(codepoint, output);
Ryan Sleevi 2015/07/30 02:30:12 Hrm, we may want to rope in brettw@ on this, since
mattm 2015/08/04 05:02:06 I just pinged Brett about it. He was okay with usi
211 }
212 return NormalizeDirectoryString(NO_ENFORCEMENT, output);
213 }
214
215 // Converts the string |value| to UTF-8, normalizes it, and stores in |output|.
216 // |tag| must one of the types for which IsNormalizableDirectoryString is true.
217 //
218 // If |value| can be normalized, returns true and sets |output| to the case
219 // folded, normalized value. If |value| is invalid, returns false.
220 // NOTE: |output| will be modified regardless of the return.
221 WARN_UNUSED_RESULT bool NormalizeValue(const der::Tag tag,
222 const der::Input& value,
223 std::string* output) {
224 switch (tag) {
225 case der::kPrintableString:
226 return NormalizePrintableStringValue(value, output);
227 case der::kUtf8String:
228 return NormalizeUtf8StringValue(value, output);
229 case der::kIA5String:
230 return NormalizeIA5StringValue(value, output);
231 case der::kUniversalString:
232 return NormalizeUniversalStringValue(value, output);
233 case der::kBmpString:
234 return NormalizeBmpStringValue(value, output);
235 default:
236 NOTREACHED();
237 return false;
238 }
239 }
240
241 // Returns true if |tag| is a string type that NormalizeValue can handle.
242 bool IsNormalizableDirectoryString(der::Tag tag) {
243 switch (tag) {
244 case der::kPrintableString:
245 case der::kUtf8String:
246 // RFC 5280 only requires handling IA5String for comparing domainComponent
247 // values, but handling it here avoids the need to special case anything.
248 case der::kIA5String:
249 case der::kUniversalString:
250 case der::kBmpString:
251 return true;
252 // TeletexString isn't normalized. Section 8 of RFC 5280 briefly
253 // describes the historical confusion between treating TeletexString
254 // as Latin1String vs T.61, and there are even incompatibilities within
255 // T.61 implementations. As this time is virtually unused, simply
256 // treat it with a binary comparison, as permitted by RFC 3280/5280.
257 default:
258 return false;
259 }
260 }
261
262 // Returns true if the AttributeValue (|a_tag|, |a_value|) matches (|b_tag|,
263 // |b_value|).
264 bool VerifyValueMatch(const der::Tag a_tag,
265 const der::Input& a_value,
266 const der::Tag b_tag,
267 const der::Input& b_value) {
268 if (IsNormalizableDirectoryString(a_tag) &&
269 IsNormalizableDirectoryString(b_tag)) {
270 std::string a_normalized, b_normalized;
271 if (!NormalizeValue(a_tag, a_value, &a_normalized) ||
272 !NormalizeValue(b_tag, b_value, &b_normalized))
273 return false;
274 return a_normalized == b_normalized;
275 }
276 // Attributes encoded with different types may be assumed to be unequal.
277 if (a_tag != b_tag)
278 return false;
279 // All other types use binary comparison.
280 return a_value.Equals(b_value);
281 }
282
283 struct AttributeTypeAndValue {
284 AttributeTypeAndValue(der::Input in_type,
285 der::Tag in_value_tag,
286 der::Input in_value)
287 : type(in_type), value_tag(in_value_tag), value(in_value) {}
288 der::Input type;
289 der::Tag value_tag;
290 der::Input value;
291 };
292
293 // Parses all the ASN.1 AttributeTypeAndValue elements in |parser| and stores
294 // each as an AttributeTypeAndValue object in |out|.
295 //
296 // AttributeTypeAndValue is defined in RFC 5280 section 4.1.2.4:
297 //
298 // AttributeTypeAndValue ::= SEQUENCE {
299 // type AttributeType,
300 // value AttributeValue }
301 //
302 // AttributeType ::= OBJECT IDENTIFIER
303 //
304 // AttributeValue ::= ANY -- DEFINED BY AttributeType
305 //
306 // DirectoryString ::= CHOICE {
307 // teletexString TeletexString (SIZE (1..MAX)),
308 // printableString PrintableString (SIZE (1..MAX)),
309 // universalString UniversalString (SIZE (1..MAX)),
310 // utf8String UTF8String (SIZE (1..MAX)),
311 // bmpString BMPString (SIZE (1..MAX)) }
312 //
313 // The type of the component AttributeValue is determined by the AttributeType;
314 // in general it will be a DirectoryString.
315 WARN_UNUSED_RESULT bool ReadRdn(der::Parser* parser,
316 std::vector<AttributeTypeAndValue>* out) {
317 while (parser->HasMore()) {
318 der::Parser attr_type_and_value;
319 if (!parser->ReadSequence(&attr_type_and_value))
320 return false;
321 // Read the attribute type, which must be an OBJECT IDENTIFIER.
322 der::Input type;
323 if (!attr_type_and_value.ReadTag(der::kOid, &type))
324 return false;
325
326 // Read the attribute value.
327 der::Tag tag;
328 der::Input value;
329 if (!attr_type_and_value.ReadTagAndValue(&tag, &value))
330 return false;
331
332 // There should be no more elements in the sequence after reading the
333 // attribute type and value.
334 if (attr_type_and_value.HasMore())
335 return false;
336
337 out->push_back(AttributeTypeAndValue(type, tag, value));
338 }
339 return true;
340 }
341
342 // Verifies that |a_parser| and |b_parser| are the same length and that every
343 // AttributeTypeAndValue in |a_parser| has a matching AttributeTypeAndValue in
344 // |b_parser|.
345 bool VerifyRdnMatch(der::Parser* a_parser, der::Parser* b_parser) {
346 std::vector<AttributeTypeAndValue> a_type_and_values, b_type_and_values;
347 if (!ReadRdn(a_parser, &a_type_and_values) ||
348 !ReadRdn(b_parser, &b_type_and_values))
349 return false;
350
351 // RFC 5280 section 4.1.2.4
352 // RelativeDistinguishedName ::= SET SIZE (1..MAX) OF AttributeTypeAndValue
353 if (a_type_and_values.empty() || b_type_and_values.empty())
354 return false;
355
356 // RFC 5280 section 7.1:
357 // Two relative distinguished names RDN1 and RDN2 match if they have the same
358 // number of naming attributes and for each naming attribute in RDN1 there is
359 // a matching naming attribute in RDN2.
360 if (a_type_and_values.size() != b_type_and_values.size())
361 return false;
362
363 // The ordering of elements may differ due to denormalized values sorting
364 // differently in the DER encoding. Since the number of elements should be
365 // small, a naive linear search for each element should be fine. (Hostile
366 // certificates already have ways to provoke pathological behavior.)
367 for (const auto& a : a_type_and_values) {
368 bool matched = false;
369 for (const auto& b : b_type_and_values) {
370 if (a.type.Equals(b.type) &&
371 VerifyValueMatch(a.value_tag, a.value, b.value_tag, b.value)) {
372 matched = true;
373 break;
374 }
375 }
376 if (!matched)
377 return false;
378 }
379
380 // Every element in |a_type_and_values| had a matching element in
381 // |b_type_and_values|.
382 return true;
383 }
384
385 } // namespace
386
387 // |a| and |b| are ASN.1 Name structures, defined in RFC 5280 section 4.1.2.4:
388 //
389 // Name ::= CHOICE { -- only one possibility for now --
390 // rdnSequence RDNSequence }
391 //
392 // RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
393 //
394 // RelativeDistinguishedName ::=
395 // SET SIZE (1..MAX) OF AttributeTypeAndValue
10 bool VerifyNameMatch(const der::Input& a, const der::Input& b) { 396 bool VerifyNameMatch(const der::Input& a, const der::Input& b) {
Ryan Sleevi 2015/07/30 02:30:12 I suspect as this matures (but no need in this CL)
mattm 2015/08/04 05:02:06 Acknowledged.
11 // TODO(mattm): use normalization as specified in RFC 5280 section 7. 397 der::Parser a_parser(a);
12 return a.Equals(b); 398 der::Parser b_parser(b);
399 der::Parser a_rdn_sequence;
400 der::Parser b_rdn_sequence;
401
402 if (!a_parser.ReadSequence(&a_rdn_sequence) ||
403 !b_parser.ReadSequence(&b_rdn_sequence)) {
404 return false;
405 }
406
407 // No data should remain in the inputs after the RDN sequence.
408 if (a_parser.HasMore() || b_parser.HasMore())
409 return false;
410
411 // Empty Names are allowed. RFC 5280 section 4.1.2.4 requires "The issuer
412 // field MUST contain a non-empty distinguished name (DN)", while section
413 // 4.1.2.6 allows for the Subject to be empty in certain cases. The caller is
414 // assumed to have verified those conditions.
Ryan Sleevi 2015/07/30 02:30:12 Because of this, does it make sense to expect the
mattm 2015/08/04 05:02:06 Yeah, I had actually made that change in the follo
415
416 // RFC 5280 section 7.1:
417 // Two distinguished names DN1 and DN2 match if they have the same number of
418 // RDNs, for each RDN in DN1 there is a matching RDN in DN2, and the matching
419 // RDNs appear in the same order in both DNs.
420 while (a_rdn_sequence.HasMore() && b_rdn_sequence.HasMore()) {
421 der::Parser a_rdn, b_rdn;
422 if (!a_rdn_sequence.ReadConstructed(der::kSet, &a_rdn) ||
423 !b_rdn_sequence.ReadConstructed(der::kSet, &b_rdn)) {
424 return false;
425 }
426 if (!VerifyRdnMatch(&a_rdn, &b_rdn))
427 return false;
428 }
429
430 // If one of the sequences has more elements than the other, not a match.
431 if (a_rdn_sequence.HasMore() || b_rdn_sequence.HasMore())
Ryan Sleevi 2015/07/30 02:30:12 On an efficiency note, I'm wondering whether or no
mattm 2015/08/04 05:02:06 Seems plausible. I've changed it, though I don't h
432 return false;
433
434 return true;
13 } 435 }
14 436
15 } // namespace net 437 } // namespace net
OLDNEW
« no previous file with comments | « no previous file | net/cert/internal/verify_name_match_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698