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" |
11 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
| 12 #include "base/threading/thread.h" |
12 #include "crypto/sha2.h" | 13 #include "crypto/sha2.h" |
13 #include "net/base/test_completion_callback.h" | 14 #include "net/cert/cert_net_fetcher.h" |
14 #include "net/cert/internal/cert_issuer_source_aia.h" | 15 #include "net/cert/internal/cert_issuer_source_aia.h" |
15 #include "net/cert/internal/cert_issuer_source_static.h" | 16 #include "net/cert/internal/cert_issuer_source_static.h" |
16 #include "net/cert/internal/parse_name.h" | 17 #include "net/cert/internal/parse_name.h" |
17 #include "net/cert/internal/parsed_certificate.h" | 18 #include "net/cert/internal/parsed_certificate.h" |
18 #include "net/cert/internal/path_builder.h" | 19 #include "net/cert/internal/path_builder.h" |
19 #include "net/cert/internal/signature_policy.h" | 20 #include "net/cert/internal/signature_policy.h" |
20 #include "net/cert/internal/trust_store_collection.h" | 21 #include "net/cert/internal/trust_store_collection.h" |
21 #include "net/cert/internal/trust_store_in_memory.h" | 22 #include "net/cert/internal/trust_store_in_memory.h" |
22 #include "net/cert_net/cert_net_fetcher_impl.h" | 23 #include "net/cert_net/cert_net_fetcher_impl.h" |
23 #include "net/tools/cert_verify_tool/cert_verify_tool_util.h" | 24 #include "net/tools/cert_verify_tool/cert_verify_tool_util.h" |
24 #include "net/url_request/url_request_context.h" | 25 #include "net/url_request/url_request_context.h" |
25 #include "net/url_request/url_request_context_builder.h" | 26 #include "net/url_request/url_request_context_builder.h" |
| 27 #include "net/url_request/url_request_context_getter.h" |
26 | 28 |
27 #if defined(USE_NSS_CERTS) | 29 #if defined(USE_NSS_CERTS) |
28 #include "base/threading/thread_task_runner_handle.h" | 30 #include "base/threading/thread_task_runner_handle.h" |
29 #include "net/cert/internal/trust_store_nss.h" | 31 #include "net/cert/internal/trust_store_nss.h" |
30 #endif | 32 #endif |
31 | 33 |
32 #if defined(OS_LINUX) | 34 #if defined(OS_LINUX) |
33 #include "net/proxy/proxy_config.h" | 35 #include "net/proxy/proxy_config.h" |
34 #include "net/proxy/proxy_config_service_fixed.h" | 36 #include "net/proxy/proxy_config_service_fixed.h" |
35 #endif | 37 #endif |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
160 PrintCertError("ERROR: ParsedCertificate failed:", input); | 162 PrintCertError("ERROR: ParsedCertificate failed:", input); |
161 std::cout << errors.ToDebugString() << "\n"; | 163 std::cout << errors.ToDebugString() << "\n"; |
162 } | 164 } |
163 | 165 |
164 // TODO(crbug.com/634443): Print errors if there are any on success too (i.e. | 166 // TODO(crbug.com/634443): Print errors if there are any on success too (i.e. |
165 // warnings). | 167 // warnings). |
166 | 168 |
167 return cert; | 169 return cert; |
168 } | 170 } |
169 | 171 |
| 172 class URLRequestContextGetterForAia : public net::URLRequestContextGetter { |
| 173 public: |
| 174 URLRequestContextGetterForAia( |
| 175 scoped_refptr<base::SingleThreadTaskRunner> task_runner) |
| 176 : task_runner_(std::move(task_runner)) {} |
| 177 |
| 178 net::URLRequestContext* GetURLRequestContext() override { |
| 179 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 180 |
| 181 if (!context_) { |
| 182 // TODO(mattm): add command line flags to configure using |
| 183 // CertIssuerSourceAia |
| 184 // (similar to VERIFY_CERT_IO_ENABLED flag for CertVerifyProc). |
| 185 net::URLRequestContextBuilder url_request_context_builder; |
| 186 url_request_context_builder.set_user_agent(GetUserAgent()); |
| 187 #if defined(OS_LINUX) |
| 188 // On Linux, use a fixed ProxyConfigService, since the default one |
| 189 // depends on glib. |
| 190 // |
| 191 // TODO(akalin): Remove this once http://crbug.com/146421 is fixed. |
| 192 url_request_context_builder.set_proxy_config_service( |
| 193 base::MakeUnique<net::ProxyConfigServiceFixed>(net::ProxyConfig())); |
| 194 #endif |
| 195 context_ = url_request_context_builder.Build(); |
| 196 } |
| 197 |
| 198 return context_.get(); |
| 199 } |
| 200 |
| 201 void ShutDown() { |
| 202 GetNetworkTaskRunner()->PostTask( |
| 203 FROM_HERE, |
| 204 base::Bind(&URLRequestContextGetterForAia::ShutdownOnNetworkThread, |
| 205 this)); |
| 206 } |
| 207 |
| 208 scoped_refptr<base::SingleThreadTaskRunner> GetNetworkTaskRunner() |
| 209 const override { |
| 210 return task_runner_; |
| 211 } |
| 212 |
| 213 private: |
| 214 ~URLRequestContextGetterForAia() override { DCHECK(!context_); } |
| 215 |
| 216 void ShutdownOnNetworkThread() { context_.release(); } |
| 217 |
| 218 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
| 219 |
| 220 std::unique_ptr<net::URLRequestContext> context_; |
| 221 }; |
| 222 |
170 } // namespace | 223 } // namespace |
171 | 224 |
172 // Verifies |target_der_cert| using CertPathBuilder. | 225 // Verifies |target_der_cert| using CertPathBuilder. |
173 bool VerifyUsingPathBuilder( | 226 bool VerifyUsingPathBuilder( |
174 const CertInput& target_der_cert, | 227 const CertInput& target_der_cert, |
175 const std::vector<CertInput>& intermediate_der_certs, | 228 const std::vector<CertInput>& intermediate_der_certs, |
176 const std::vector<CertInput>& root_der_certs, | 229 const std::vector<CertInput>& root_der_certs, |
177 const base::Time at_time, | 230 const base::Time at_time, |
178 const base::FilePath& dump_prefix_path) { | 231 const base::FilePath& dump_prefix_path) { |
179 base::Time::Exploded exploded_time; | 232 base::Time::Exploded exploded_time; |
180 at_time.UTCExplode(&exploded_time); | 233 at_time.UTCExplode(&exploded_time); |
181 net::der::GeneralizedTime time = ConvertExplodedTime(exploded_time); | 234 net::der::GeneralizedTime time = ConvertExplodedTime(exploded_time); |
182 | 235 |
183 net::TrustStoreCollection trust_store; | 236 net::TrustStoreCollection trust_store; |
184 | 237 |
185 net::TrustStoreInMemory trust_store_in_memory; | 238 net::TrustStoreInMemory trust_store_in_memory; |
186 trust_store.AddTrustStoreSynchronousOnly(&trust_store_in_memory); | 239 trust_store.AddTrustStore(&trust_store_in_memory); |
187 for (const auto& der_cert : root_der_certs) { | 240 for (const auto& der_cert : root_der_certs) { |
188 scoped_refptr<net::ParsedCertificate> cert = ParseCertificate(der_cert); | 241 scoped_refptr<net::ParsedCertificate> cert = ParseCertificate(der_cert); |
189 if (cert) { | 242 if (cert) { |
190 trust_store_in_memory.AddTrustAnchor( | 243 trust_store_in_memory.AddTrustAnchor( |
191 net::TrustAnchor::CreateFromCertificateNoConstraints(cert)); | 244 net::TrustAnchor::CreateFromCertificateNoConstraints(cert)); |
192 } | 245 } |
193 } | 246 } |
194 | 247 |
195 #if defined(USE_NSS_CERTS) | 248 #if defined(USE_NSS_CERTS) |
196 net::TrustStoreNSS trust_store_nss(trustSSL, | 249 net::TrustStoreNSS trust_store_nss(trustSSL); |
197 base::ThreadTaskRunnerHandle::Get()); | 250 trust_store.AddTrustStore(&trust_store_nss); |
198 trust_store.SetPrimaryTrustStore(&trust_store_nss); | |
199 #else | 251 #else |
200 if (root_der_certs.empty()) { | 252 if (root_der_certs.empty()) { |
201 std::cerr << "NOTE: CertPathBuilder does not currently use OS trust " | 253 std::cerr << "NOTE: CertPathBuilder does not currently use OS trust " |
202 "settings (--roots must be specified).\n"; | 254 "settings (--roots must be specified).\n"; |
203 } | 255 } |
204 #endif | 256 #endif |
205 | 257 |
206 net::CertIssuerSourceStatic intermediate_cert_issuer_source; | 258 net::CertIssuerSourceStatic intermediate_cert_issuer_source; |
207 for (const auto& der_cert : intermediate_der_certs) { | 259 for (const auto& der_cert : intermediate_der_certs) { |
208 scoped_refptr<net::ParsedCertificate> cert = ParseCertificate(der_cert); | 260 scoped_refptr<net::ParsedCertificate> cert = ParseCertificate(der_cert); |
209 if (cert) | 261 if (cert) |
210 intermediate_cert_issuer_source.AddCert(cert); | 262 intermediate_cert_issuer_source.AddCert(cert); |
211 } | 263 } |
212 | 264 |
213 scoped_refptr<net::ParsedCertificate> target_cert = | 265 scoped_refptr<net::ParsedCertificate> target_cert = |
214 ParseCertificate(target_der_cert); | 266 ParseCertificate(target_der_cert); |
215 if (!target_cert) | 267 if (!target_cert) |
216 return false; | 268 return false; |
217 | 269 |
218 // Verify the chain. | 270 // Verify the chain. |
219 net::SimpleSignaturePolicy signature_policy(2048); | 271 net::SimpleSignaturePolicy signature_policy(2048); |
220 net::CertPathBuilder::Result result; | 272 net::CertPathBuilder::Result result; |
221 net::CertPathBuilder path_builder(target_cert, &trust_store, | 273 net::CertPathBuilder path_builder(target_cert, &trust_store, |
222 &signature_policy, time, &result); | 274 &signature_policy, time, &result); |
223 path_builder.AddCertIssuerSource(&intermediate_cert_issuer_source); | 275 path_builder.AddCertIssuerSource(&intermediate_cert_issuer_source); |
224 | 276 |
225 // TODO(mattm): add command line flags to configure using CertIssuerSourceAia | 277 // Initialize an AIA fetcher, that uses a separate thread for running the |
226 // (similar to VERIFY_CERT_IO_ENABLED flag for CertVerifyProc). | 278 // networking message loop. |
227 net::URLRequestContextBuilder url_request_context_builder; | 279 base::Thread::Options options(base::MessageLoop::TYPE_IO, 0); |
228 url_request_context_builder.set_user_agent(GetUserAgent()); | 280 base::Thread thread("network_thread"); |
229 #if defined(OS_LINUX) | 281 CHECK(thread.StartWithOptions(options)); |
230 // On Linux, use a fixed ProxyConfigService, since the default one | 282 scoped_refptr<URLRequestContextGetterForAia> url_request_context_getter( |
231 // depends on glib. | 283 new URLRequestContextGetterForAia(thread.task_runner())); |
232 // | 284 auto cert_net_fetcher = |
233 // TODO(akalin): Remove this once http://crbug.com/146421 is fixed. | 285 CreateCertNetFetcher(url_request_context_getter.get()); |
234 url_request_context_builder.set_proxy_config_service( | 286 net::CertIssuerSourceAia aia_cert_issuer_source(cert_net_fetcher.get()); |
235 base::MakeUnique<net::ProxyConfigServiceFixed>(net::ProxyConfig())); | |
236 #endif | |
237 std::unique_ptr<net::URLRequestContext> url_request_context = | |
238 url_request_context_builder.Build(); | |
239 net::CertNetFetcherImpl cert_net_fetcher(url_request_context.get()); | |
240 net::CertIssuerSourceAia aia_cert_issuer_source(&cert_net_fetcher); | |
241 path_builder.AddCertIssuerSource(&aia_cert_issuer_source); | 287 path_builder.AddCertIssuerSource(&aia_cert_issuer_source); |
242 | 288 |
243 net::TestClosure callback; | 289 // Run the path builder. |
244 net::CompletionStatus rv = path_builder.Run(callback.closure()); | 290 path_builder.Run(); |
245 | 291 |
246 if (rv == net::CompletionStatus::ASYNC) { | 292 // Stop the temporary network thread.. |
247 DVLOG(1) << "waiting for async completion..."; | 293 url_request_context_getter->ShutDown(); |
248 callback.WaitForResult(); | 294 thread.Stop(); |
249 DVLOG(1) << "async completed."; | |
250 } | |
251 | 295 |
252 // TODO(crbug.com/634443): Display any errors/warnings associated with path | 296 // TODO(crbug.com/634443): Display any errors/warnings associated with path |
253 // building that were not part of a particular | 297 // building that were not part of a particular |
254 // PathResult. | 298 // PathResult. |
255 std::cout << "CertPathBuilder result: " | 299 std::cout << "CertPathBuilder result: " |
256 << (result.HasValidPath() ? "SUCCESS" : "FAILURE") << "\n"; | 300 << (result.HasValidPath() ? "SUCCESS" : "FAILURE") << "\n"; |
257 | 301 |
258 for (size_t i = 0; i < result.paths.size(); ++i) { | 302 for (size_t i = 0; i < result.paths.size(); ++i) { |
259 PrintResultPath(result.paths[i].get(), i, i == result.best_result_index); | 303 PrintResultPath(result.paths[i].get(), i, i == result.best_result_index); |
260 } | 304 } |
261 | 305 |
262 // TODO(mattm): add flag to dump all paths, not just the final one? | 306 // TODO(mattm): add flag to dump all paths, not just the final one? |
263 if (!dump_prefix_path.empty() && result.paths.size()) { | 307 if (!dump_prefix_path.empty() && result.paths.size()) { |
264 if (!DumpParsedCertificateChain( | 308 if (!DumpParsedCertificateChain( |
265 dump_prefix_path.AddExtension( | 309 dump_prefix_path.AddExtension( |
266 FILE_PATH_LITERAL(".CertPathBuilder.pem")), | 310 FILE_PATH_LITERAL(".CertPathBuilder.pem")), |
267 result.paths[result.best_result_index]->path)) { | 311 result.paths[result.best_result_index]->path)) { |
268 return false; | 312 return false; |
269 } | 313 } |
270 } | 314 } |
271 | 315 |
272 return result.HasValidPath(); | 316 return result.HasValidPath(); |
273 } | 317 } |
OLD | NEW |