| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "remoting/host/token_validator_base.h" |
| 6 |
| 7 #include <vector> |
| 8 |
| 9 #include "base/atomic_sequence_num.h" |
| 10 #include "crypto/rsa_private_key.h" |
| 11 #include "net/cert/x509_util.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 |
| 14 namespace { |
| 15 |
| 16 const char kTokenUrl[] = "https://example.com/token"; |
| 17 const char kTokenValidationUrl[] = "https://example.com/validate"; |
| 18 const char kTokenValidationCertIssuer[] = "*"; |
| 19 |
| 20 base::StaticAtomicSequenceNumber g_serial_number; |
| 21 |
| 22 scoped_refptr<net::X509Certificate> CreateFakeCert(base::Time valid_start, |
| 23 base::Time valid_expiry) { |
| 24 std::unique_ptr<crypto::RSAPrivateKey> unused_key; |
| 25 std::string cert_der; |
| 26 net::x509_util::CreateKeyAndSelfSignedCert( |
| 27 "CN=subject", g_serial_number.GetNext(), valid_start, valid_expiry, |
| 28 &unused_key, &cert_der); |
| 29 return net::X509Certificate::CreateFromBytes(cert_der.data(), |
| 30 cert_der.size()); |
| 31 } |
| 32 |
| 33 } // namespace |
| 34 |
| 35 namespace remoting { |
| 36 |
| 37 class TestTokenValidator : TokenValidatorBase { |
| 38 public: |
| 39 explicit TestTokenValidator(const ThirdPartyAuthConfig& config); |
| 40 ~TestTokenValidator() override; |
| 41 |
| 42 void SelectCertificates(net::CertificateList* selected_certs); |
| 43 |
| 44 void ExpectContinueWithCertificate(net::X509Certificate* client_cert); |
| 45 |
| 46 protected: |
| 47 void ContinueWithCertificate(net::X509Certificate* client_cert, |
| 48 net::SSLPrivateKey* client_private_key) override; |
| 49 |
| 50 private: |
| 51 void StartValidateRequest(const std::string& token) override {} |
| 52 |
| 53 net::X509Certificate* expected_client_cert_ = nullptr; |
| 54 }; |
| 55 |
| 56 TestTokenValidator::TestTokenValidator(const ThirdPartyAuthConfig& config) : |
| 57 TokenValidatorBase(config, "", nullptr) { |
| 58 } |
| 59 |
| 60 TestTokenValidator::~TestTokenValidator() {} |
| 61 |
| 62 void TestTokenValidator::SelectCertificates( |
| 63 net::CertificateList* selected_certs) { |
| 64 OnCertificatesSelected(selected_certs, nullptr); |
| 65 } |
| 66 |
| 67 void TestTokenValidator::ExpectContinueWithCertificate( |
| 68 net::X509Certificate* client_cert) { |
| 69 expected_client_cert_ = client_cert; |
| 70 } |
| 71 |
| 72 void TestTokenValidator::ContinueWithCertificate( |
| 73 net::X509Certificate* client_cert, |
| 74 net::SSLPrivateKey* client_private_key) { |
| 75 EXPECT_EQ(expected_client_cert_, client_cert); |
| 76 } |
| 77 |
| 78 class TokenValidatorBaseTest : public testing::Test { |
| 79 public: |
| 80 void SetUp() override; |
| 81 protected: |
| 82 std::unique_ptr<TestTokenValidator> token_validator_; |
| 83 }; |
| 84 |
| 85 void TokenValidatorBaseTest::SetUp() { |
| 86 ThirdPartyAuthConfig config; |
| 87 config.token_url = GURL(kTokenUrl); |
| 88 config.token_validation_url = GURL(kTokenValidationUrl); |
| 89 config.token_validation_cert_issuer = kTokenValidationCertIssuer; |
| 90 token_validator_.reset(new TestTokenValidator(config)); |
| 91 } |
| 92 |
| 93 TEST_F(TokenValidatorBaseTest, TestSelectCertificate) { |
| 94 base::Time now = base::Time::Now(); |
| 95 |
| 96 scoped_refptr<net::X509Certificate> cert_expired_5_minutes_ago = |
| 97 CreateFakeCert(now - base::TimeDelta::FromMinutes(10), |
| 98 now - base::TimeDelta::FromMinutes(5)); |
| 99 |
| 100 scoped_refptr<net::X509Certificate> cert_start_5min_expire_5min = |
| 101 CreateFakeCert(now - base::TimeDelta::FromMinutes(5), |
| 102 now + base::TimeDelta::FromMinutes(5)); |
| 103 |
| 104 scoped_refptr<net::X509Certificate> cert_start_10min_expire_5min = |
| 105 CreateFakeCert(now - base::TimeDelta::FromMinutes(10), |
| 106 now + base::TimeDelta::FromMinutes(5)); |
| 107 |
| 108 scoped_refptr<net::X509Certificate> cert_start_5min_expire_10min = |
| 109 CreateFakeCert(now - base::TimeDelta::FromMinutes(5), |
| 110 now + base::TimeDelta::FromMinutes(10)); |
| 111 |
| 112 // No certificate. |
| 113 net::CertificateList certificates {}; |
| 114 token_validator_->ExpectContinueWithCertificate(nullptr); |
| 115 token_validator_->SelectCertificates(&certificates); |
| 116 |
| 117 // One invalid certificate. |
| 118 certificates = { cert_expired_5_minutes_ago }; |
| 119 token_validator_->ExpectContinueWithCertificate(nullptr); |
| 120 token_validator_->SelectCertificates(&certificates); |
| 121 |
| 122 // One valid certificate. |
| 123 certificates = { cert_start_5min_expire_5min }; |
| 124 token_validator_->ExpectContinueWithCertificate( |
| 125 cert_start_5min_expire_5min.get()); |
| 126 token_validator_->SelectCertificates(&certificates); |
| 127 |
| 128 // One valid one invalid. |
| 129 certificates = { cert_expired_5_minutes_ago, cert_start_5min_expire_5min }; |
| 130 token_validator_->ExpectContinueWithCertificate( |
| 131 cert_start_5min_expire_5min.get()); |
| 132 token_validator_->SelectCertificates(&certificates); |
| 133 |
| 134 // Two valid certs. Choose latest created. |
| 135 certificates = { cert_start_10min_expire_5min, cert_start_5min_expire_5min }; |
| 136 token_validator_->ExpectContinueWithCertificate( |
| 137 cert_start_5min_expire_5min.get()); |
| 138 token_validator_->SelectCertificates(&certificates); |
| 139 |
| 140 // Two valid certs. Choose latest expires. |
| 141 certificates = { cert_start_5min_expire_5min, cert_start_5min_expire_10min }; |
| 142 token_validator_->ExpectContinueWithCertificate( |
| 143 cert_start_5min_expire_10min.get()); |
| 144 token_validator_->SelectCertificates(&certificates); |
| 145 |
| 146 // Pick the best given all certificates. |
| 147 certificates = { cert_expired_5_minutes_ago, cert_start_5min_expire_5min, |
| 148 cert_start_5min_expire_10min, cert_start_10min_expire_5min }; |
| 149 token_validator_->ExpectContinueWithCertificate( |
| 150 cert_start_5min_expire_10min.get()); |
| 151 token_validator_->SelectCertificates(&certificates); |
| 152 } |
| 153 |
| 154 } // namespace remoting |
| OLD | NEW |