| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/asn1_util.h" | 5 #include "net/cert/asn1_util.h" |
| 6 | 6 |
| 7 #include "net/der/input.h" | 7 #include "net/der/input.h" |
| 8 #include "net/der/parser.h" | 8 #include "net/der/parser.h" |
| 9 | 9 |
| 10 namespace net { | 10 namespace net { |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 63 return false; | 63 return false; |
| 64 // validity | 64 // validity |
| 65 if (!tbs_certificate->SkipTag(der::kSequence)) | 65 if (!tbs_certificate->SkipTag(der::kSequence)) |
| 66 return false; | 66 return false; |
| 67 // subject | 67 // subject |
| 68 if (!tbs_certificate->SkipTag(der::kSequence)) | 68 if (!tbs_certificate->SkipTag(der::kSequence)) |
| 69 return false; | 69 return false; |
| 70 return true; | 70 return true; |
| 71 } | 71 } |
| 72 | 72 |
| 73 der::Input InputFromStringPiece(base::StringPiece in) { | |
| 74 return der::Input(reinterpret_cast<const uint8_t*>(in.data()), in.length()); | |
| 75 } | |
| 76 | |
| 77 base::StringPiece StringPieceFromInput(der::Input in) { | |
| 78 return base::StringPiece(reinterpret_cast<const char*>(in.UnsafeData()), | |
| 79 in.Length()); | |
| 80 } | |
| 81 | |
| 82 } // namespace | 73 } // namespace |
| 83 | 74 |
| 84 bool ExtractSPKIFromDERCert(base::StringPiece cert, | 75 bool ExtractSPKIFromDERCert(base::StringPiece cert, |
| 85 base::StringPiece* spki_out) { | 76 base::StringPiece* spki_out) { |
| 86 der::Parser parser; | 77 der::Parser parser; |
| 87 if (!SeekToSPKI(InputFromStringPiece(cert), &parser)) | 78 if (!SeekToSPKI(der::Input(cert), &parser)) |
| 88 return false; | 79 return false; |
| 89 der::Input spki; | 80 der::Input spki; |
| 90 if (!parser.ReadRawTLV(&spki)) | 81 if (!parser.ReadRawTLV(&spki)) |
| 91 return false; | 82 return false; |
| 92 *spki_out = StringPieceFromInput(spki); | 83 *spki_out = spki.AsStringPiece(); |
| 93 return true; | 84 return true; |
| 94 } | 85 } |
| 95 | 86 |
| 96 bool ExtractSubjectPublicKeyFromSPKI(base::StringPiece spki, | 87 bool ExtractSubjectPublicKeyFromSPKI(base::StringPiece spki, |
| 97 base::StringPiece* spk_out) { | 88 base::StringPiece* spk_out) { |
| 98 // From RFC 5280, Section 4.1 | 89 // From RFC 5280, Section 4.1 |
| 99 // SubjectPublicKeyInfo ::= SEQUENCE { | 90 // SubjectPublicKeyInfo ::= SEQUENCE { |
| 100 // algorithm AlgorithmIdentifier, | 91 // algorithm AlgorithmIdentifier, |
| 101 // subjectPublicKey BIT STRING } | 92 // subjectPublicKey BIT STRING } |
| 102 // | 93 // |
| 103 // AlgorithmIdentifier ::= SEQUENCE { | 94 // AlgorithmIdentifier ::= SEQUENCE { |
| 104 // algorithm OBJECT IDENTIFIER, | 95 // algorithm OBJECT IDENTIFIER, |
| 105 // parameters ANY DEFINED BY algorithm OPTIONAL } | 96 // parameters ANY DEFINED BY algorithm OPTIONAL } |
| 106 | 97 |
| 107 // Step into SubjectPublicKeyInfo sequence. | 98 // Step into SubjectPublicKeyInfo sequence. |
| 108 der::Parser parser(InputFromStringPiece(spki)); | 99 der::Parser parser((der::Input(spki))); |
| 109 der::Parser spki_parser; | 100 der::Parser spki_parser; |
| 110 if (!parser.ReadSequence(&spki_parser)) | 101 if (!parser.ReadSequence(&spki_parser)) |
| 111 return false; | 102 return false; |
| 112 | 103 |
| 113 // Step over algorithm field (a SEQUENCE). | 104 // Step over algorithm field (a SEQUENCE). |
| 114 if (!spki_parser.SkipTag(der::kSequence)) | 105 if (!spki_parser.SkipTag(der::kSequence)) |
| 115 return false; | 106 return false; |
| 116 | 107 |
| 117 // Extract the subjectPublicKey field. | 108 // Extract the subjectPublicKey field. |
| 118 der::Input spk; | 109 der::Input spk; |
| 119 if (!spki_parser.ReadTag(der::kBitString, &spk)) | 110 if (!spki_parser.ReadTag(der::kBitString, &spk)) |
| 120 return false; | 111 return false; |
| 121 *spk_out = StringPieceFromInput(spk); | 112 *spk_out = spk.AsStringPiece(); |
| 122 return true; | 113 return true; |
| 123 } | 114 } |
| 124 | 115 |
| 125 | 116 |
| 126 bool ExtractCRLURLsFromDERCert(base::StringPiece cert, | 117 bool ExtractCRLURLsFromDERCert(base::StringPiece cert, |
| 127 std::vector<base::StringPiece>* urls_out) { | 118 std::vector<base::StringPiece>* urls_out) { |
| 128 urls_out->clear(); | 119 urls_out->clear(); |
| 129 std::vector<base::StringPiece> tmp_urls_out; | 120 std::vector<base::StringPiece> tmp_urls_out; |
| 130 | 121 |
| 131 bool present; | 122 bool present; |
| 132 der::Parser tbs_cert_parser; | 123 der::Parser tbs_cert_parser; |
| 133 if (!SeekToSPKI(InputFromStringPiece(cert), &tbs_cert_parser)) | 124 if (!SeekToSPKI(der::Input(cert), &tbs_cert_parser)) |
| 134 return false; | 125 return false; |
| 135 | 126 |
| 136 // From RFC 5280, section 4.1 | 127 // From RFC 5280, section 4.1 |
| 137 // TBSCertificate ::= SEQUENCE { | 128 // TBSCertificate ::= SEQUENCE { |
| 138 // ... | 129 // ... |
| 139 // subjectPublicKeyInfo SubjectPublicKeyInfo, | 130 // subjectPublicKeyInfo SubjectPublicKeyInfo, |
| 140 // issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL, | 131 // issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL, |
| 141 // subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL, | 132 // subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL, |
| 142 // extensions [3] EXPLICIT Extensions OPTIONAL } | 133 // extensions [3] EXPLICIT Extensions OPTIONAL } |
| 143 | 134 |
| (...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 274 // ... } | 265 // ... } |
| 275 der::Parser general_names_parser(general_names); | 266 der::Parser general_names_parser(general_names); |
| 276 while (general_names_parser.HasMore()) { | 267 while (general_names_parser.HasMore()) { |
| 277 der::Input url; | 268 der::Input url; |
| 278 if (!general_names_parser.ReadOptionalTag(der::kTagContextSpecific | 6, | 269 if (!general_names_parser.ReadOptionalTag(der::kTagContextSpecific | 6, |
| 279 &url, &present)) { | 270 &url, &present)) { |
| 280 return false; | 271 return false; |
| 281 } | 272 } |
| 282 if (present) { | 273 if (present) { |
| 283 // This does not validate that |url| is a valid IA5String. | 274 // This does not validate that |url| is a valid IA5String. |
| 284 tmp_urls_out.push_back(StringPieceFromInput(url)); | 275 tmp_urls_out.push_back(url.AsStringPiece()); |
| 285 } else { | 276 } else { |
| 286 der::Tag unused_tag; | 277 der::Tag unused_tag; |
| 287 der::Input unused_value; | 278 der::Input unused_value; |
| 288 if (!general_names_parser.ReadTagAndValue(&unused_tag, | 279 if (!general_names_parser.ReadTagAndValue(&unused_tag, |
| 289 &unused_value)) { | 280 &unused_value)) { |
| 290 return false; | 281 return false; |
| 291 } | 282 } |
| 292 } | 283 } |
| 293 } | 284 } |
| 294 } | 285 } |
| 295 } | 286 } |
| 296 | 287 |
| 297 urls_out->swap(tmp_urls_out); | 288 urls_out->swap(tmp_urls_out); |
| 298 return true; | 289 return true; |
| 299 } | 290 } |
| 300 | 291 |
| 301 } // namespace asn1 | 292 } // namespace asn1 |
| 302 | 293 |
| 303 } // namespace net | 294 } // namespace net |
| OLD | NEW |