| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "components/cast_certificate/cast_cert_validator.h" | 5 #include "components/cast_certificate/cast_cert_validator.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <algorithm> | 10 #include <algorithm> |
| 11 #include <memory> | 11 #include <memory> |
| 12 #include <utility> | 12 #include <utility> |
| 13 | 13 |
| 14 #include "base/memory/ptr_util.h" | 14 #include "base/memory/ptr_util.h" |
| 15 #include "base/memory/singleton.h" | 15 #include "base/memory/singleton.h" |
| 16 #include "net/cert/internal/cert_issuer_source_static.h" | 16 #include "net/cert/internal/cert_issuer_source_static.h" |
| 17 #include "components/cast_certificate/cast_crl.h" | |
| 18 #include "net/cert/internal/certificate_policies.h" | 17 #include "net/cert/internal/certificate_policies.h" |
| 19 #include "net/cert/internal/extended_key_usage.h" | 18 #include "net/cert/internal/extended_key_usage.h" |
| 20 #include "net/cert/internal/parse_certificate.h" | 19 #include "net/cert/internal/parse_certificate.h" |
| 21 #include "net/cert/internal/parse_name.h" | 20 #include "net/cert/internal/parse_name.h" |
| 22 #include "net/cert/internal/parsed_certificate.h" | 21 #include "net/cert/internal/parsed_certificate.h" |
| 23 #include "net/cert/internal/path_builder.h" | 22 #include "net/cert/internal/path_builder.h" |
| 24 #include "net/cert/internal/signature_algorithm.h" | 23 #include "net/cert/internal/signature_algorithm.h" |
| 25 #include "net/cert/internal/signature_policy.h" | 24 #include "net/cert/internal/signature_policy.h" |
| 26 #include "net/cert/internal/trust_store.h" | 25 #include "net/cert/internal/trust_store.h" |
| 27 #include "net/cert/internal/verify_signed_data.h" | 26 #include "net/cert/internal/verify_signed_data.h" |
| 28 #include "net/der/encode_values.h" | |
| 29 #include "net/der/input.h" | 27 #include "net/der/input.h" |
| 30 | 28 |
| 31 namespace cast_certificate { | 29 namespace cast_certificate { |
| 32 namespace { | 30 namespace { |
| 33 | 31 |
| 34 // ------------------------------------------------------------------------- | 32 // ------------------------------------------------------------------------- |
| 35 // Cast trust anchors. | 33 // Cast trust anchors. |
| 36 // ------------------------------------------------------------------------- | 34 // ------------------------------------------------------------------------- |
| 37 | 35 |
| 38 // There are two trusted roots for Cast certificate chains: | 36 // There are two trusted roots for Cast certificate chains: |
| (...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 228 // Get the Common Name for the certificate. | 226 // Get the Common Name for the certificate. |
| 229 std::string common_name; | 227 std::string common_name; |
| 230 if (!GetCommonNameFromSubject(cert->tbs().subject_tlv, &common_name)) | 228 if (!GetCommonNameFromSubject(cert->tbs().subject_tlv, &common_name)) |
| 231 return false; | 229 return false; |
| 232 | 230 |
| 233 context->reset( | 231 context->reset( |
| 234 new CertVerificationContextImpl(cert->tbs().spki_tlv, common_name)); | 232 new CertVerificationContextImpl(cert->tbs().spki_tlv, common_name)); |
| 235 return true; | 233 return true; |
| 236 } | 234 } |
| 237 | 235 |
| 236 // Converts a base::Time::Exploded to a net::der::GeneralizedTime. |
| 237 net::der::GeneralizedTime ConvertExplodedTime( |
| 238 const base::Time::Exploded& exploded) { |
| 239 net::der::GeneralizedTime result; |
| 240 result.year = exploded.year; |
| 241 result.month = exploded.month; |
| 242 result.day = exploded.day_of_month; |
| 243 result.hours = exploded.hour; |
| 244 result.minutes = exploded.minute; |
| 245 result.seconds = exploded.second; |
| 246 return result; |
| 247 } |
| 248 |
| 238 // Returns the parsing options used for Cast certificates. | 249 // Returns the parsing options used for Cast certificates. |
| 239 net::ParseCertificateOptions GetCertParsingOptions() { | 250 net::ParseCertificateOptions GetCertParsingOptions() { |
| 240 net::ParseCertificateOptions options; | 251 net::ParseCertificateOptions options; |
| 241 | 252 |
| 242 // Some cast intermediate certificates contain serial numbers that are | 253 // Some cast intermediate certificates contain serial numbers that are |
| 243 // 21 octets long, and might also not use valid DER encoding for an | 254 // 21 octets long, and might also not use valid DER encoding for an |
| 244 // INTEGER (non-minimal encoding). | 255 // INTEGER (non-minimal encoding). |
| 245 // | 256 // |
| 246 // Allow these sorts of serial numbers. | 257 // Allow these sorts of serial numbers. |
| 247 // | 258 // |
| 248 // TODO(eroman): At some point in the future this workaround will no longer be | 259 // TODO(eroman): At some point in the future this workaround will no longer be |
| 249 // necessary. Should revisit this for removal in 2017 if not earlier. | 260 // necessary. Should revisit this for removal in 2017 if not earlier. |
| 250 options.allow_invalid_serial_numbers = true; | 261 options.allow_invalid_serial_numbers = true; |
| 251 return options; | 262 return options; |
| 252 } | 263 } |
| 253 | 264 |
| 254 } // namespace | 265 } // namespace |
| 255 | 266 |
| 256 bool VerifyDeviceCert(const std::vector<std::string>& certs, | 267 bool VerifyDeviceCert(const std::vector<std::string>& certs, |
| 257 const base::Time& time, | 268 const base::Time::Exploded& time, |
| 258 std::unique_ptr<CertVerificationContext>* context, | 269 std::unique_ptr<CertVerificationContext>* context, |
| 259 CastDeviceCertPolicy* policy, | 270 CastDeviceCertPolicy* policy) { |
| 260 const CastCRL* crl, | |
| 261 CRLPolicy crl_policy) { | |
| 262 if (certs.empty()) | 271 if (certs.empty()) |
| 263 return false; | 272 return false; |
| 264 | 273 |
| 265 // No reference to these ParsedCertificates is kept past the end of this | 274 // No reference to these ParsedCertificates is kept past the end of this |
| 266 // function, so using EXTERNAL_REFERENCE here is safe. | 275 // function, so using EXTERNAL_REFERENCE here is safe. |
| 267 scoped_refptr<net::ParsedCertificate> target_cert; | 276 scoped_refptr<net::ParsedCertificate> target_cert; |
| 268 net::CertIssuerSourceStatic intermediate_cert_issuer_source; | 277 net::CertIssuerSourceStatic intermediate_cert_issuer_source; |
| 269 for (size_t i = 0; i < certs.size(); ++i) { | 278 for (size_t i = 0; i < certs.size(); ++i) { |
| 270 scoped_refptr<net::ParsedCertificate> cert( | 279 scoped_refptr<net::ParsedCertificate> cert( |
| 271 net::ParsedCertificate::CreateFromCertificateData( | 280 net::ParsedCertificate::CreateFromCertificateData( |
| 272 reinterpret_cast<const uint8_t*>(certs[i].data()), certs[i].size(), | 281 reinterpret_cast<const uint8_t*>(certs[i].data()), certs[i].size(), |
| 273 net::ParsedCertificate::DataSource::EXTERNAL_REFERENCE, | 282 net::ParsedCertificate::DataSource::EXTERNAL_REFERENCE, |
| 274 GetCertParsingOptions())); | 283 GetCertParsingOptions())); |
| 275 if (!cert) | 284 if (!cert) |
| 276 return false; | 285 return false; |
| 277 | 286 |
| 278 if (i == 0) | 287 if (i == 0) |
| 279 target_cert = std::move(cert); | 288 target_cert = std::move(cert); |
| 280 else | 289 else |
| 281 intermediate_cert_issuer_source.AddCert(std::move(cert)); | 290 intermediate_cert_issuer_source.AddCert(std::move(cert)); |
| 282 } | 291 } |
| 283 | 292 |
| 284 // Use a signature policy compatible with Cast's PKI. | 293 // Use a signature policy compatible with Cast's PKI. |
| 285 auto signature_policy = CreateCastSignaturePolicy(); | 294 auto signature_policy = CreateCastSignaturePolicy(); |
| 286 | 295 |
| 287 // Do path building and RFC 5280 compatible certificate verification using the | 296 // Do path building and RFC 5280 compatible certificate verification using the |
| 288 // two Cast trust anchors and Cast signature policy. | 297 // two Cast trust anchors and Cast signature policy. |
| 289 net::der::GeneralizedTime verification_time; | |
| 290 if (!net::der::EncodeTimeAsGeneralizedTime(time, &verification_time)) | |
| 291 return false; | |
| 292 net::CertPathBuilder::Result result; | 298 net::CertPathBuilder::Result result; |
| 293 net::CertPathBuilder path_builder(target_cert.get(), &CastTrustStore::Get(), | 299 net::CertPathBuilder path_builder(target_cert.get(), &CastTrustStore::Get(), |
| 294 signature_policy.get(), verification_time, | 300 signature_policy.get(), |
| 295 &result); | 301 ConvertExplodedTime(time), &result); |
| 296 path_builder.AddCertIssuerSource(&intermediate_cert_issuer_source); | 302 path_builder.AddCertIssuerSource(&intermediate_cert_issuer_source); |
| 297 net::CompletionStatus rv = path_builder.Run(base::Closure()); | 303 net::CompletionStatus rv = path_builder.Run(base::Closure()); |
| 298 DCHECK_EQ(rv, net::CompletionStatus::SYNC); | 304 DCHECK_EQ(rv, net::CompletionStatus::SYNC); |
| 299 if (!result.is_success()) | 305 if (!result.is_success()) |
| 300 return false; | 306 return false; |
| 301 | 307 |
| 302 // Check properties of the leaf certificate (key usage, policy), and construct | 308 // Check properties of the leaf certificate (key usage, policy), and construct |
| 303 // a CertVerificationContext that uses its public key. | 309 // a CertVerificationContext that uses its public key. |
| 304 if (!CheckTargetCertificate(target_cert.get(), context, policy)) | 310 return CheckTargetCertificate(target_cert.get(), context, policy); |
| 305 return false; | |
| 306 | |
| 307 // Check if a CRL is available. | |
| 308 if (!crl) { | |
| 309 if (crl_policy == CRLPolicy::CRL_REQUIRED) { | |
| 310 return false; | |
| 311 } | |
| 312 } else { | |
| 313 if (result.paths.empty() || | |
| 314 !result.paths[result.best_result_index]->is_success()) | |
| 315 return false; | |
| 316 | |
| 317 if (!crl->CheckRevocation(result.paths[result.best_result_index]->path, | |
| 318 time)) { | |
| 319 return false; | |
| 320 } | |
| 321 } | |
| 322 return true; | |
| 323 } | 311 } |
| 324 | 312 |
| 325 std::unique_ptr<CertVerificationContext> CertVerificationContextImplForTest( | 313 std::unique_ptr<CertVerificationContext> CertVerificationContextImplForTest( |
| 326 const base::StringPiece& spki) { | 314 const base::StringPiece& spki) { |
| 327 // Use a bogus CommonName, since this is just exposed for testing signature | 315 // Use a bogus CommonName, since this is just exposed for testing signature |
| 328 // verification by unittests. | 316 // verification by unittests. |
| 329 return base::WrapUnique( | 317 return base::WrapUnique( |
| 330 new CertVerificationContextImpl(net::der::Input(spki), "CommonName")); | 318 new CertVerificationContextImpl(net::der::Input(spki), "CommonName")); |
| 331 } | 319 } |
| 332 | 320 |
| 333 bool SetTrustAnchorForTest(const std::string& cert) { | 321 bool AddTrustAnchorForTest(const uint8_t* data, size_t length) { |
| 334 scoped_refptr<net::ParsedCertificate> anchor( | 322 scoped_refptr<net::ParsedCertificate> anchor( |
| 335 net::ParsedCertificate::CreateFromCertificateCopy( | 323 net::ParsedCertificate::CreateFromCertificateData( |
| 336 cert, GetCertParsingOptions())); | 324 data, length, net::ParsedCertificate::DataSource::EXTERNAL_REFERENCE, |
| 325 GetCertParsingOptions())); |
| 337 if (!anchor) | 326 if (!anchor) |
| 338 return false; | 327 return false; |
| 339 CastTrustStore::Get().Clear(); | |
| 340 CastTrustStore::Get().AddTrustedCertificate(std::move(anchor)); | 328 CastTrustStore::Get().AddTrustedCertificate(std::move(anchor)); |
| 341 return true; | 329 return true; |
| 342 } | 330 } |
| 343 | 331 |
| 344 } // namespace cast_certificate | 332 } // namespace cast_certificate |
| OLD | NEW |