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 "extensions/browser/api/cast_channel/cast_auth_util.h" | 5 #include "extensions/browser/api/cast_channel/cast_auth_util.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/feature_list.h" | |
9 #include "base/logging.h" | 10 #include "base/logging.h" |
10 #include "base/macros.h" | 11 #include "base/macros.h" |
12 #include "base/metrics/histogram.h" | |
11 #include "base/strings/string_number_conversions.h" | 13 #include "base/strings/string_number_conversions.h" |
12 #include "base/strings/stringprintf.h" | 14 #include "base/strings/stringprintf.h" |
13 #include "components/cast_certificate/cast_cert_validator.h" | 15 #include "components/cast_certificate/cast_cert_validator.h" |
16 #include "components/cast_certificate/cast_crl.h" | |
14 #include "extensions/browser/api/cast_channel/cast_message_util.h" | 17 #include "extensions/browser/api/cast_channel/cast_message_util.h" |
15 #include "extensions/common/api/cast_channel/cast_channel.pb.h" | 18 #include "extensions/common/api/cast_channel/cast_channel.pb.h" |
16 #include "net/cert/x509_certificate.h" | 19 #include "net/cert/x509_certificate.h" |
17 #include "net/der/parse_values.h" | 20 #include "net/der/parse_values.h" |
18 | 21 |
19 namespace extensions { | 22 namespace extensions { |
20 namespace api { | 23 namespace api { |
21 namespace cast_channel { | 24 namespace cast_channel { |
22 namespace { | 25 namespace { |
23 | 26 |
24 const char* const kParseErrorPrefix = "Failed to parse auth message: "; | 27 const char* const kParseErrorPrefix = "Failed to parse auth message: "; |
25 | 28 |
26 // The maximum number of days a cert can live for. | 29 // The maximum number of days a cert can live for. |
27 const int kMaxSelfSignedCertLifetimeInDays = 4; | 30 const int kMaxSelfSignedCertLifetimeInDays = 4; |
28 | 31 |
32 // Enforce certificate revocation when enabled. | |
33 // If disabled, any revocation failures are ignored. | |
34 // | |
35 // This flags only controls the enforcement. Revocation is checked regardless. | |
36 // | |
37 // This flag tracks the changes necessary to fully enforce revocation. | |
38 const base::Feature kEnforceRevocationChecking{ | |
39 "CastCertificateRevocation", base::FEATURE_DISABLED_BY_DEFAULT}; | |
40 | |
29 namespace cast_crypto = ::cast_certificate; | 41 namespace cast_crypto = ::cast_certificate; |
30 | 42 |
31 // Extracts an embedded DeviceAuthMessage payload from an auth challenge reply | 43 // Extracts an embedded DeviceAuthMessage payload from an auth challenge reply |
32 // message. | 44 // message. |
33 AuthResult ParseAuthMessage(const CastMessage& challenge_reply, | 45 AuthResult ParseAuthMessage(const CastMessage& challenge_reply, |
34 DeviceAuthMessage* auth_message) { | 46 DeviceAuthMessage* auth_message) { |
35 if (challenge_reply.payload_type() != CastMessage_PayloadType_BINARY) { | 47 if (challenge_reply.payload_type() != CastMessage_PayloadType_BINARY) { |
36 return AuthResult::CreateWithParseError( | 48 return AuthResult::CreateWithParseError( |
37 "Wrong payload type in challenge reply", | 49 "Wrong payload type in challenge reply", |
38 AuthResult::ERROR_WRONG_PAYLOAD_TYPE); | 50 AuthResult::ERROR_WRONG_PAYLOAD_TYPE); |
(...skipping 17 matching lines...) Expand all Loading... | |
56 base::IntToString(auth_message->error().error_type()), | 68 base::IntToString(auth_message->error().error_type()), |
57 AuthResult::ERROR_MESSAGE_ERROR); | 69 AuthResult::ERROR_MESSAGE_ERROR); |
58 } | 70 } |
59 if (!auth_message->has_response()) { | 71 if (!auth_message->has_response()) { |
60 return AuthResult::CreateWithParseError( | 72 return AuthResult::CreateWithParseError( |
61 "Auth message has no response field", AuthResult::ERROR_NO_RESPONSE); | 73 "Auth message has no response field", AuthResult::ERROR_NO_RESPONSE); |
62 } | 74 } |
63 return AuthResult(); | 75 return AuthResult(); |
64 } | 76 } |
65 | 77 |
78 // Must match with histogram. | |
jwd
2016/10/17 20:55:11
Nit: mention it they shouldn't be reordered.
ryanchung
2016/10/17 23:58:56
Done.
| |
79 enum CertVerificationStatus { | |
80 CERT_STATUS_OK, | |
81 CERT_STATUS_INVALID_CRL, | |
82 CERT_STATUS_VERIFICATION_FAILED, | |
83 CERT_STATUS_REVOKED, | |
84 CERT_STATUS_COUNT, | |
85 }; | |
86 | |
66 } // namespace | 87 } // namespace |
67 | 88 |
68 AuthResult::AuthResult() | 89 AuthResult::AuthResult() |
69 : error_type(ERROR_NONE), channel_policies(POLICY_NONE) {} | 90 : error_type(ERROR_NONE), channel_policies(POLICY_NONE) {} |
70 | 91 |
71 AuthResult::AuthResult(const std::string& error_message, ErrorType error_type) | 92 AuthResult::AuthResult(const std::string& error_message, ErrorType error_type) |
72 : error_message(error_message), error_type(error_type) {} | 93 : error_message(error_message), error_type(error_type) {} |
73 | 94 |
74 AuthResult::~AuthResult() { | 95 AuthResult::~AuthResult() { |
75 } | 96 } |
(...skipping 27 matching lines...) Expand all Loading... | |
103 // structure is signed by the AuthResponse, so the validity field from X.509 | 124 // structure is signed by the AuthResponse, so the validity field from X.509 |
104 // is repurposed as this signature's expiration. | 125 // is repurposed as this signature's expiration. |
105 base::Time expiry = peer_cert.valid_expiry(); | 126 base::Time expiry = peer_cert.valid_expiry(); |
106 base::Time lifetime_limit = | 127 base::Time lifetime_limit = |
107 base::Time::Now() + | 128 base::Time::Now() + |
108 base::TimeDelta::FromDays(kMaxSelfSignedCertLifetimeInDays); | 129 base::TimeDelta::FromDays(kMaxSelfSignedCertLifetimeInDays); |
109 if (peer_cert.valid_start().is_null() || | 130 if (peer_cert.valid_start().is_null() || |
110 peer_cert.valid_start() > base::Time::Now()) { | 131 peer_cert.valid_start() > base::Time::Now()) { |
111 return AuthResult::CreateWithParseError( | 132 return AuthResult::CreateWithParseError( |
112 "Certificate's valid start date is in the future.", | 133 "Certificate's valid start date is in the future.", |
113 AuthResult::ERROR_VALID_START_DATE_IN_FUTURE); | 134 AuthResult::ERROR_TLS_CERT_VALID_START_DATE_IN_FUTURE); |
114 } | 135 } |
115 if (expiry.is_null() || peer_cert.HasExpired()) { | 136 if (expiry.is_null() || peer_cert.HasExpired()) { |
116 return AuthResult::CreateWithParseError("Certificate has expired.", | 137 return AuthResult::CreateWithParseError("Certificate has expired.", |
117 AuthResult::ERROR_CERT_EXPIRED); | 138 AuthResult::ERROR_TLS_CERT_EXPIRED); |
118 } | 139 } |
119 if (expiry > lifetime_limit) { | 140 if (expiry > lifetime_limit) { |
120 return AuthResult::CreateWithParseError( | 141 return AuthResult::CreateWithParseError( |
121 "Peer cert lifetime is too long.", | 142 "Peer cert lifetime is too long.", |
122 AuthResult::ERROR_VALIDITY_PERIOD_TOO_LONG); | 143 AuthResult::ERROR_TLS_CERT_VALIDITY_PERIOD_TOO_LONG); |
123 } | 144 } |
124 | 145 |
125 const AuthResponse& response = auth_message.response(); | 146 const AuthResponse& response = auth_message.response(); |
126 return VerifyCredentials(response, peer_cert_der); | 147 return VerifyCredentials(response, peer_cert_der); |
127 } | 148 } |
128 | 149 |
129 // This function does the following | 150 // This function does the following |
130 // | 151 // |
131 // * Verifies that the certificate chain |response.client_auth_certificate| + | 152 // * Verifies that the certificate chain |response.client_auth_certificate| + |
132 // |response.intermediate_certificate| is valid and chains to a trusted | 153 // |response.intermediate_certificate| is valid and chains to a trusted |
133 // Cast root. | 154 // Cast root. The list of trusted Cast roots can be overrided by providing a |
155 // non-nullptr |cast_trust_store|. The certificate is verified at | |
156 // |verification_time|. | |
157 // | |
158 // * Verifies that none of the certificates in the chain are revoked based on | |
159 // the CRL provided in the response |response.crl|. The CRL is verified to be | |
160 // valid and its issuer certificate chains to a trusted Cast CRL root. The | |
161 // list of trusted Cast CRL roots can be overrided by providing a non-nullptr | |
162 // |crl_trust_store|. If |crl_policy| is CRL_OPTIONAL then the result of | |
163 // revocation checking is ignored. The CRL is verified at | |
164 // |verification_time|. | |
134 // | 165 // |
135 // * Verifies that |response.signature| matches the signature | 166 // * Verifies that |response.signature| matches the signature |
136 // of |signature_input| by |response.client_auth_certificate|'s public | 167 // of |signature_input| by |response.client_auth_certificate|'s public |
137 // key. | 168 // key. |
138 AuthResult VerifyCredentials(const AuthResponse& response, | 169 AuthResult VerifyCredentialsImpl(const AuthResponse& response, |
139 const std::string& signature_input) { | 170 const std::string& signature_input, |
171 const cast_crypto::CRLPolicy& crl_policy, | |
172 net::TrustStore* cast_trust_store, | |
173 net::TrustStore* crl_trust_store, | |
174 const base::Time& verification_time) { | |
140 // Verify the certificate | 175 // Verify the certificate |
141 std::unique_ptr<cast_crypto::CertVerificationContext> verification_context; | 176 std::unique_ptr<cast_crypto::CertVerificationContext> verification_context; |
142 | 177 |
143 // Build a single vector containing the certificate chain. | 178 // Build a single vector containing the certificate chain. |
144 std::vector<std::string> cert_chain; | 179 std::vector<std::string> cert_chain; |
145 cert_chain.push_back(response.client_auth_certificate()); | 180 cert_chain.push_back(response.client_auth_certificate()); |
146 cert_chain.insert(cert_chain.end(), | 181 cert_chain.insert(cert_chain.end(), |
147 response.intermediate_certificate().begin(), | 182 response.intermediate_certificate().begin(), |
148 response.intermediate_certificate().end()); | 183 response.intermediate_certificate().end()); |
149 | 184 |
150 // Use the current time when checking certificate validity. | 185 // Parse the CRL. |
151 base::Time now = base::Time::Now(); | 186 std::unique_ptr<cast_crypto::CastCRL> crl = |
152 | 187 cast_crypto::ParseAndVerifyCRLUsingCustomTrustStore( |
153 // CRL should not be enforced until it is served. | 188 response.crl(), verification_time, crl_trust_store); |
154 cast_crypto::CastDeviceCertPolicy device_policy; | 189 if (!crl) { |
155 if (!cast_crypto::VerifyDeviceCert( | 190 // CRL is invalid. |
156 cert_chain, now, &verification_context, &device_policy, nullptr, | 191 UMA_HISTOGRAM_ENUMERATION("Cast.Channel.Certificate", |
157 cast_certificate::CRLPolicy::CRL_OPTIONAL)) { | 192 CERT_STATUS_INVALID_CRL, CERT_STATUS_COUNT); |
158 // TODO(eroman): The error information was lost; this error is ambiguous. | 193 if (crl_policy == cast_crypto::CRLPolicy::CRL_REQUIRED) { |
159 return AuthResult("Failed verifying cast device certificate", | 194 return AuthResult("Failed verifying Cast CRL.", |
160 AuthResult::ERROR_CERT_NOT_SIGNED_BY_TRUSTED_CA); | 195 AuthResult::ERROR_CRL_INVALID); |
196 } | |
161 } | 197 } |
162 | 198 |
199 cast_crypto::CastDeviceCertPolicy device_policy; | |
200 bool verification_success = | |
201 cast_crypto::VerifyDeviceCertUsingCustomTrustStore( | |
202 cert_chain, verification_time, &verification_context, &device_policy, | |
203 crl.get(), crl_policy, cast_trust_store); | |
204 if (!verification_success) { | |
205 // TODO(ryanchung): Once this feature is completely rolled-out, remove the | |
206 // reverification step and use error reporting to get verification errors | |
207 // for metrics. | |
208 bool verification_no_crl_success = | |
209 cast_crypto::VerifyDeviceCertUsingCustomTrustStore( | |
210 cert_chain, verification_time, &verification_context, | |
211 &device_policy, nullptr, cast_crypto::CRLPolicy::CRL_OPTIONAL, | |
212 cast_trust_store); | |
213 if (!verification_no_crl_success) { | |
214 // TODO(eroman): The error information was lost; this error is ambiguous. | |
215 UMA_HISTOGRAM_ENUMERATION("Cast.Channel.Certificate", | |
216 CERT_STATUS_VERIFICATION_FAILED, | |
217 CERT_STATUS_COUNT); | |
218 return AuthResult("Failed verifying cast device certificate", | |
219 AuthResult::ERROR_CERT_NOT_SIGNED_BY_TRUSTED_CA); | |
220 } | |
221 if (crl) { | |
222 // If CRL was not present, it should've been recorded as such. | |
223 UMA_HISTOGRAM_ENUMERATION("Cast.Channel.Certificate", CERT_STATUS_REVOKED, | |
224 CERT_STATUS_COUNT); | |
225 } | |
226 if (crl_policy == cast_crypto::CRLPolicy::CRL_REQUIRED) { | |
227 // Device is revoked. | |
228 return AuthResult("Failed certificate revocation check.", | |
229 AuthResult::ERROR_CERT_REVOKED); | |
230 } | |
231 } | |
232 // The certificate is verified at this point. | |
233 UMA_HISTOGRAM_ENUMERATION("Cast.Channel.Certificate", CERT_STATUS_OK, | |
234 CERT_STATUS_COUNT); | |
163 if (!verification_context->VerifySignatureOverData(response.signature(), | 235 if (!verification_context->VerifySignatureOverData(response.signature(), |
164 signature_input)) { | 236 signature_input)) { |
165 return AuthResult("Failed verifying signature over data", | 237 return AuthResult("Failed verifying signature over data", |
166 AuthResult::ERROR_SIGNED_BLOBS_MISMATCH); | 238 AuthResult::ERROR_SIGNED_BLOBS_MISMATCH); |
167 } | 239 } |
168 | 240 |
169 AuthResult success; | 241 AuthResult success; |
170 | 242 |
171 // Set the policy into the result. | 243 // Set the policy into the result. |
172 switch (device_policy) { | 244 switch (device_policy) { |
173 case cast_crypto::CastDeviceCertPolicy::AUDIO_ONLY: | 245 case cast_crypto::CastDeviceCertPolicy::AUDIO_ONLY: |
174 success.channel_policies = AuthResult::POLICY_AUDIO_ONLY; | 246 success.channel_policies = AuthResult::POLICY_AUDIO_ONLY; |
175 break; | 247 break; |
176 case cast_crypto::CastDeviceCertPolicy::NONE: | 248 case cast_crypto::CastDeviceCertPolicy::NONE: |
177 success.channel_policies = AuthResult::POLICY_NONE; | 249 success.channel_policies = AuthResult::POLICY_NONE; |
178 break; | 250 break; |
179 } | 251 } |
180 | 252 |
181 return success; | 253 return success; |
182 } | 254 } |
183 | 255 |
256 AuthResult VerifyCredentials(const AuthResponse& response, | |
257 const std::string& signature_input) { | |
258 base::Time now = base::Time::Now(); | |
259 cast_crypto::CRLPolicy policy = cast_crypto::CRLPolicy::CRL_REQUIRED; | |
260 if (!base::FeatureList::IsEnabled(kEnforceRevocationChecking)) { | |
261 policy = cast_crypto::CRLPolicy::CRL_OPTIONAL; | |
262 } | |
263 return VerifyCredentialsImpl(response, signature_input, policy, nullptr, | |
264 nullptr, now); | |
265 } | |
266 | |
267 AuthResult VerifyCredentialsForTest(const AuthResponse& response, | |
268 const std::string& signature_input, | |
269 const cast_crypto::CRLPolicy& crl_policy, | |
270 net::TrustStore* cast_trust_store, | |
271 net::TrustStore* crl_trust_store, | |
272 const base::Time& verification_time) { | |
273 return VerifyCredentialsImpl(response, signature_input, crl_policy, | |
274 cast_trust_store, crl_trust_store, | |
275 verification_time); | |
276 } | |
277 | |
184 } // namespace cast_channel | 278 } // namespace cast_channel |
185 } // namespace api | 279 } // namespace api |
186 } // namespace extensions | 280 } // namespace extensions |
OLD | NEW |