| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/tools/cert_verify_tool/verify_using_path_builder.h" | 5 #include "net/tools/cert_verify_tool/verify_using_path_builder.h" |
| 6 | 6 |
| 7 #include <iostream> | 7 #include <iostream> |
| 8 | 8 |
| 9 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
| 10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 42 net::der::GeneralizedTime result; | 42 net::der::GeneralizedTime result; |
| 43 result.year = exploded.year; | 43 result.year = exploded.year; |
| 44 result.month = exploded.month; | 44 result.month = exploded.month; |
| 45 result.day = exploded.day_of_month; | 45 result.day = exploded.day_of_month; |
| 46 result.hours = exploded.hour; | 46 result.hours = exploded.hour; |
| 47 result.minutes = exploded.minute; | 47 result.minutes = exploded.minute; |
| 48 result.seconds = exploded.second; | 48 result.seconds = exploded.second; |
| 49 return result; | 49 return result; |
| 50 } | 50 } |
| 51 | 51 |
| 52 bool AddPemEncodedCert(const net::ParsedCertificate* cert, |
| 53 std::vector<std::string>* pem_encoded_chain) { |
| 54 std::string der_cert; |
| 55 cert->der_cert().AsStringPiece().CopyToString(&der_cert); |
| 56 std::string pem; |
| 57 if (!net::X509Certificate::GetPEMEncodedFromDER(der_cert, &pem)) { |
| 58 std::cerr << "ERROR: GetPEMEncodedFromDER failed\n"; |
| 59 return false; |
| 60 } |
| 61 pem_encoded_chain->push_back(pem); |
| 62 return true; |
| 63 } |
| 64 |
| 52 // Dumps a chain of ParsedCertificate objects to a PEM file. | 65 // Dumps a chain of ParsedCertificate objects to a PEM file. |
| 53 bool DumpParsedCertificateChain( | 66 bool DumpParsedCertificateChain(const base::FilePath& file_path, |
| 54 const base::FilePath& file_path, | 67 const net::CertPath& chain) { |
| 55 const std::vector<scoped_refptr<net::ParsedCertificate>>& chain) { | |
| 56 std::vector<std::string> pem_encoded_chain; | 68 std::vector<std::string> pem_encoded_chain; |
| 57 for (const auto& cert : chain) { | 69 for (const auto& cert : chain.certs) { |
| 58 std::string der_cert; | 70 if (!AddPemEncodedCert(cert.get(), &pem_encoded_chain)) |
| 59 cert->der_cert().AsStringPiece().CopyToString(&der_cert); | |
| 60 std::string pem; | |
| 61 if (!net::X509Certificate::GetPEMEncodedFromDER(der_cert, &pem)) { | |
| 62 std::cerr << "ERROR: GetPEMEncodedFromDER failed\n"; | |
| 63 return false; | 71 return false; |
| 64 } | |
| 65 pem_encoded_chain.push_back(pem); | |
| 66 } | 72 } |
| 73 |
| 74 if (chain.trust_anchor && chain.trust_anchor->cert()) { |
| 75 if (!AddPemEncodedCert(chain.trust_anchor->cert().get(), |
| 76 &pem_encoded_chain)) |
| 77 return false; |
| 78 } |
| 79 |
| 67 return WriteToFile(file_path, base::JoinString(pem_encoded_chain, "")); | 80 return WriteToFile(file_path, base::JoinString(pem_encoded_chain, "")); |
| 68 } | 81 } |
| 69 | 82 |
| 70 // Returns a hex-encoded sha256 of the DER-encoding of |cert|. | 83 // Returns a hex-encoded sha256 of the DER-encoding of |cert|. |
| 71 std::string FingerPrintParsedCertificate(const net::ParsedCertificate* cert) { | 84 std::string FingerPrintParsedCertificate(const net::ParsedCertificate* cert) { |
| 72 std::string hash = crypto::SHA256HashString(cert->der_cert().AsStringPiece()); | 85 std::string hash = crypto::SHA256HashString(cert->der_cert().AsStringPiece()); |
| 73 return base::HexEncode(hash.data(), hash.size()); | 86 return base::HexEncode(hash.data(), hash.size()); |
| 74 } | 87 } |
| 75 | 88 |
| 76 // Returns a textual representation of the Subject of |cert|. | 89 std::string SubjectToString(const net::der::Input& subject_tlv) { |
| 77 std::string SubjectFromParsedCertificate(const net::ParsedCertificate* cert) { | |
| 78 net::RDNSequence subject, issuer; | 90 net::RDNSequence subject, issuer; |
| 79 if (!net::ParseName(cert->tbs().subject_tlv, &subject)) | 91 if (!net::ParseName(subject_tlv, &subject)) |
| 80 return std::string(); | 92 return std::string(); |
| 81 std::string subject_str; | 93 std::string subject_str; |
| 82 if (!net::ConvertToRFC2253(subject, &subject_str)) | 94 if (!net::ConvertToRFC2253(subject, &subject_str)) |
| 83 return std::string(); | 95 return std::string(); |
| 84 return subject_str; | 96 return subject_str; |
| 85 } | 97 } |
| 86 | 98 |
| 99 // Returns a textual representation of the Subject of |cert|. |
| 100 std::string SubjectFromParsedCertificate(const net::ParsedCertificate* cert) { |
| 101 return SubjectToString(cert->tbs().subject_tlv); |
| 102 } |
| 103 |
| 87 } // namespace | 104 } // namespace |
| 88 | 105 |
| 89 // Verifies |target_der_cert| using CertPathBuilder. | 106 // Verifies |target_der_cert| using CertPathBuilder. |
| 90 bool VerifyUsingPathBuilder( | 107 bool VerifyUsingPathBuilder( |
| 91 const CertInput& target_der_cert, | 108 const CertInput& target_der_cert, |
| 92 const std::vector<CertInput>& intermediate_der_certs, | 109 const std::vector<CertInput>& intermediate_der_certs, |
| 93 const std::vector<CertInput>& root_der_certs, | 110 const std::vector<CertInput>& root_der_certs, |
| 94 const base::Time at_time, | 111 const base::Time at_time, |
| 95 const base::FilePath& dump_prefix_path) { | 112 const base::FilePath& dump_prefix_path) { |
| 96 std::cout << "NOTE: CertPathBuilder does not currently use OS trust settings " | 113 std::cout << "NOTE: CertPathBuilder does not currently use OS trust settings " |
| 97 "(--roots must be specified).\n"; | 114 "(--roots must be specified).\n"; |
| 98 std::cerr << "WARNING: --hostname is not yet verified with CertPathBuilder\n"; | 115 std::cerr << "WARNING: --hostname is not yet verified with CertPathBuilder\n"; |
| 99 | 116 |
| 100 base::Time::Exploded exploded_time; | 117 base::Time::Exploded exploded_time; |
| 101 at_time.UTCExplode(&exploded_time); | 118 at_time.UTCExplode(&exploded_time); |
| 102 net::der::GeneralizedTime time = ConvertExplodedTime(exploded_time); | 119 net::der::GeneralizedTime time = ConvertExplodedTime(exploded_time); |
| 103 | 120 |
| 104 net::TrustStore trust_store; | 121 net::TrustStore trust_store; |
| 105 for (const auto& der_cert : root_der_certs) { | 122 for (const auto& der_cert : root_der_certs) { |
| 106 scoped_refptr<net::ParsedCertificate> cert = | 123 scoped_refptr<net::ParsedCertificate> cert = |
| 107 net::ParsedCertificate::CreateFromCertificateCopy(der_cert.der_cert, | 124 net::ParsedCertificate::CreateFromCertificateCopy(der_cert.der_cert, |
| 108 {}); | 125 {}); |
| 109 if (!cert) | 126 if (!cert) |
| 110 PrintCertError("ERROR: ParsedCertificate failed:", der_cert); | 127 PrintCertError("ERROR: ParsedCertificate failed:", der_cert); |
| 111 else | 128 else { |
| 112 trust_store.AddTrustedCertificate(cert); | 129 trust_store.AddTrustAnchor( |
| 130 net::TrustAnchor::CreateFromCertificateNoConstraints(cert)); |
| 131 } |
| 113 } | 132 } |
| 114 | 133 |
| 115 net::CertIssuerSourceStatic intermediate_cert_issuer_source; | 134 net::CertIssuerSourceStatic intermediate_cert_issuer_source; |
| 116 for (const auto& der_cert : intermediate_der_certs) { | 135 for (const auto& der_cert : intermediate_der_certs) { |
| 117 scoped_refptr<net::ParsedCertificate> cert = | 136 scoped_refptr<net::ParsedCertificate> cert = |
| 118 net::ParsedCertificate::CreateFromCertificateCopy(der_cert.der_cert, | 137 net::ParsedCertificate::CreateFromCertificateCopy(der_cert.der_cert, |
| 119 {}); | 138 {}); |
| 120 if (!cert) | 139 if (!cert) |
| 121 PrintCertError("ERROR: ParsedCertificate failed:", der_cert); | 140 PrintCertError("ERROR: ParsedCertificate failed:", der_cert); |
| 122 else | 141 else |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 165 DVLOG(1) << "async completed."; | 184 DVLOG(1) << "async completed."; |
| 166 } | 185 } |
| 167 | 186 |
| 168 std::cout << "CertPathBuilder best result: " | 187 std::cout << "CertPathBuilder best result: " |
| 169 << net::ErrorToShortString(result.error()) << "\n"; | 188 << net::ErrorToShortString(result.error()) << "\n"; |
| 170 | 189 |
| 171 for (size_t i = 0; i < result.paths.size(); ++i) { | 190 for (size_t i = 0; i < result.paths.size(); ++i) { |
| 172 std::cout << "path " << i << " " | 191 std::cout << "path " << i << " " |
| 173 << net::ErrorToShortString(result.paths[i]->error) | 192 << net::ErrorToShortString(result.paths[i]->error) |
| 174 << ((result.best_result_index == i) ? " (best)" : "") << "\n"; | 193 << ((result.best_result_index == i) ? " (best)" : "") << "\n"; |
| 175 for (const auto& cert : result.paths[i]->path) { | 194 for (const auto& cert : result.paths[i]->path.certs) { |
| 176 std::cout << " " << FingerPrintParsedCertificate(cert.get()) << " " | 195 std::cout << " " << FingerPrintParsedCertificate(cert.get()) << " " |
| 177 << SubjectFromParsedCertificate(cert.get()) << "\n"; | 196 << SubjectFromParsedCertificate(cert.get()) << "\n"; |
| 178 } | 197 } |
| 198 |
| 199 const auto& trust_anchor = result.paths[i]->path.trust_anchor; |
| 200 if (trust_anchor) { |
| 201 std::string trust_anchor_cert_fingerprint = "<no cert>"; |
| 202 if (trust_anchor->cert()) { |
| 203 trust_anchor_cert_fingerprint = |
| 204 FingerPrintParsedCertificate(trust_anchor->cert().get()); |
| 205 } |
| 206 std::cout << " " << trust_anchor_cert_fingerprint << " " |
| 207 << SubjectToString(trust_anchor->normalized_subject()) << "\n"; |
| 208 } |
| 179 } | 209 } |
| 180 | 210 |
| 181 // TODO(mattm): add flag to dump all paths, not just the final one? | 211 // TODO(mattm): add flag to dump all paths, not just the final one? |
| 182 if (!dump_prefix_path.empty() && result.paths.size()) { | 212 if (!dump_prefix_path.empty() && result.paths.size()) { |
| 183 if (!DumpParsedCertificateChain( | 213 if (!DumpParsedCertificateChain( |
| 184 dump_prefix_path.AddExtension( | 214 dump_prefix_path.AddExtension( |
| 185 FILE_PATH_LITERAL(".CertPathBuilder.pem")), | 215 FILE_PATH_LITERAL(".CertPathBuilder.pem")), |
| 186 result.paths[result.best_result_index]->path)) { | 216 result.paths[result.best_result_index]->path)) { |
| 187 return false; | 217 return false; |
| 188 } | 218 } |
| 189 } | 219 } |
| 190 | 220 |
| 191 return result.error() == net::OK; | 221 return result.error() == net::OK; |
| 192 } | 222 } |
| OLD | NEW |