Chromium Code Reviews| 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/feature_list.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/macros.h" | 11 #include "base/macros.h" |
| 12 #include "base/memory/ptr_util.h" | |
| 13 #include "base/memory/singleton.h" | |
| 12 #include "base/metrics/histogram_macros.h" | 14 #include "base/metrics/histogram_macros.h" |
| 13 #include "base/strings/string_number_conversions.h" | 15 #include "base/strings/string_number_conversions.h" |
| 16 #include "base/strings/string_util.h" | |
| 14 #include "base/strings/stringprintf.h" | 17 #include "base/strings/stringprintf.h" |
| 15 #include "components/cast_certificate/cast_cert_validator.h" | 18 #include "components/cast_certificate/cast_cert_validator.h" |
| 16 #include "components/cast_certificate/cast_crl.h" | 19 #include "components/cast_certificate/cast_crl.h" |
| 20 #include "crypto/random.h" | |
| 17 #include "extensions/browser/api/cast_channel/cast_message_util.h" | 21 #include "extensions/browser/api/cast_channel/cast_message_util.h" |
| 18 #include "extensions/common/api/cast_channel/cast_channel.pb.h" | 22 #include "extensions/common/api/cast_channel/cast_channel.pb.h" |
| 19 #include "net/cert/x509_certificate.h" | 23 #include "net/cert/x509_certificate.h" |
| 20 #include "net/der/parse_values.h" | 24 #include "net/der/parse_values.h" |
| 21 | 25 |
| 22 namespace extensions { | 26 namespace extensions { |
| 23 namespace api { | 27 namespace api { |
| 24 namespace cast_channel { | 28 namespace cast_channel { |
| 25 namespace { | 29 namespace { |
| 26 | 30 |
| 27 const char kParseErrorPrefix[] = "Failed to parse auth message: "; | 31 const char kParseErrorPrefix[] = "Failed to parse auth message: "; |
| 28 | 32 |
| 29 // The maximum number of days a cert can live for. | 33 // The maximum number of days a cert can live for. |
| 30 const int kMaxSelfSignedCertLifetimeInDays = 4; | 34 const int kMaxSelfSignedCertLifetimeInDays = 4; |
| 31 | 35 |
| 36 // The size of the nonce challenge in bytes. | |
| 37 const int kNonceSizeInBytes = 16; | |
| 38 | |
| 39 // The number of hours after which a nonce is regenerated. | |
| 40 long kNonceExpirationTimeInHours = 24; | |
| 41 | |
| 32 // Enforce certificate revocation when enabled. | 42 // Enforce certificate revocation when enabled. |
| 33 // If disabled, any revocation failures are ignored. | 43 // If disabled, any revocation failures are ignored. |
| 34 // | 44 // |
| 35 // This flags only controls the enforcement. Revocation is checked regardless. | 45 // This flags only controls the enforcement. Revocation is checked regardless. |
| 36 // | 46 // |
| 37 // This flag tracks the changes necessary to fully enforce revocation. | 47 // This flag tracks the changes necessary to fully enforce revocation. |
| 38 const base::Feature kEnforceRevocationChecking{ | 48 const base::Feature kEnforceRevocationChecking{ |
| 39 "CastCertificateRevocation", base::FEATURE_DISABLED_BY_DEFAULT}; | 49 "CastCertificateRevocation", base::FEATURE_DISABLED_BY_DEFAULT}; |
| 40 | 50 |
| 51 // Enforce nonce checking when enabled. | |
| 52 // If disabled, the nonce value returned from the device is not checked against | |
| 53 // the one sent to the device. As a result, the nonce can be empty and omitted | |
| 54 // from the signature. This allows backwards compatibility with legacy Cast | |
| 55 // receivers. | |
| 56 | |
| 57 const base::Feature kEnforceNonceChecking{"CastNonceEnforced", | |
| 58 base::FEATURE_DISABLED_BY_DEFAULT}; | |
| 59 | |
| 41 namespace cast_crypto = ::cast_certificate; | 60 namespace cast_crypto = ::cast_certificate; |
| 42 | 61 |
| 43 // Extracts an embedded DeviceAuthMessage payload from an auth challenge reply | 62 // Extracts an embedded DeviceAuthMessage payload from an auth challenge reply |
| 44 // message. | 63 // message. |
| 45 AuthResult ParseAuthMessage(const CastMessage& challenge_reply, | 64 AuthResult ParseAuthMessage(const CastMessage& challenge_reply, |
| 46 DeviceAuthMessage* auth_message) { | 65 DeviceAuthMessage* auth_message) { |
| 47 if (challenge_reply.payload_type() != CastMessage_PayloadType_BINARY) { | 66 if (challenge_reply.payload_type() != CastMessage_PayloadType_BINARY) { |
| 48 return AuthResult::CreateWithParseError( | 67 return AuthResult::CreateWithParseError( |
| 49 "Wrong payload type in challenge reply", | 68 "Wrong payload type in challenge reply", |
| 50 AuthResult::ERROR_WRONG_PAYLOAD_TYPE); | 69 AuthResult::ERROR_WRONG_PAYLOAD_TYPE); |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 68 base::IntToString(auth_message->error().error_type()), | 87 base::IntToString(auth_message->error().error_type()), |
| 69 AuthResult::ERROR_MESSAGE_ERROR); | 88 AuthResult::ERROR_MESSAGE_ERROR); |
| 70 } | 89 } |
| 71 if (!auth_message->has_response()) { | 90 if (!auth_message->has_response()) { |
| 72 return AuthResult::CreateWithParseError( | 91 return AuthResult::CreateWithParseError( |
| 73 "Auth message has no response field", AuthResult::ERROR_NO_RESPONSE); | 92 "Auth message has no response field", AuthResult::ERROR_NO_RESPONSE); |
| 74 } | 93 } |
| 75 return AuthResult(); | 94 return AuthResult(); |
| 76 } | 95 } |
| 77 | 96 |
| 97 class CastNonce { | |
| 98 public: | |
| 99 static CastNonce* GetInstance() { | |
| 100 return base::Singleton<CastNonce, | |
| 101 base::LeakySingletonTraits<CastNonce>>::get(); | |
| 102 } | |
| 103 | |
| 104 static const std::string& Get() { | |
|
mark a. foltz
2017/03/16 23:23:01
static AuthContext
ryanchung
2017/03/17 02:46:06
I want to keep CastNonce has a simple singleton fo
| |
| 105 GetInstance()->EnsureNonceTimely(); | |
| 106 return GetInstance()->nonce_; | |
| 107 } | |
| 108 | |
| 109 private: | |
| 110 friend struct base::DefaultSingletonTraits<CastNonce>; | |
| 111 | |
| 112 CastNonce() { GenerateNonce(); } | |
| 113 void GenerateNonce() { | |
| 114 // Create a cryptographically secure nonce. | |
| 115 crypto::RandBytes(base::WriteInto(&nonce_, kNonceSizeInBytes + 1), | |
| 116 kNonceSizeInBytes); | |
| 117 nonce_generation_time_ = base::Time::Now(); | |
| 118 } | |
| 119 | |
| 120 void EnsureNonceTimely() { | |
| 121 if (base::Time::Now() > | |
| 122 (nonce_generation_time_ + | |
| 123 base::TimeDelta::FromHours(kNonceExpirationTimeInHours))) { | |
| 124 GenerateNonce(); | |
| 125 } | |
| 126 } | |
| 127 | |
| 128 // The nonce challenge to send to the Cast receiver. | |
| 129 // The nonce is updated daily. | |
| 130 std::string nonce_; | |
| 131 base::Time nonce_generation_time_; | |
| 132 }; | |
| 133 | |
| 78 // Must match with histogram enum CastCertificateStatus. | 134 // Must match with histogram enum CastCertificateStatus. |
| 79 // This should never be reordered. | 135 // This should never be reordered. |
| 80 enum CertVerificationStatus { | 136 enum CertVerificationStatus { |
| 81 CERT_STATUS_OK, | 137 CERT_STATUS_OK, |
| 82 CERT_STATUS_INVALID_CRL, | 138 CERT_STATUS_INVALID_CRL, |
| 83 CERT_STATUS_VERIFICATION_FAILED, | 139 CERT_STATUS_VERIFICATION_FAILED, |
| 84 CERT_STATUS_REVOKED, | 140 CERT_STATUS_REVOKED, |
| 85 CERT_STATUS_COUNT, | 141 CERT_STATUS_COUNT, |
| 86 }; | 142 }; |
| 87 | 143 |
| 144 enum NonceVerificationStatus { | |
| 145 NONCE_MATCH, | |
| 146 NONCE_MISMATCH, | |
| 147 NONCE_MISSING, | |
| 148 NONCE_COUNT, | |
| 149 }; | |
| 150 | |
| 88 } // namespace | 151 } // namespace |
| 89 | 152 |
| 90 AuthResult::AuthResult() | 153 AuthResult::AuthResult() |
| 91 : error_type(ERROR_NONE), channel_policies(POLICY_NONE) {} | 154 : error_type(ERROR_NONE), channel_policies(POLICY_NONE) {} |
| 92 | 155 |
| 93 AuthResult::AuthResult(const std::string& error_message, ErrorType error_type) | 156 AuthResult::AuthResult(const std::string& error_message, ErrorType error_type) |
| 94 : error_message(error_message), error_type(error_type) {} | 157 : error_message(error_message), error_type(error_type) {} |
| 95 | 158 |
| 96 AuthResult::~AuthResult() { | 159 AuthResult::~AuthResult() { |
| 97 } | 160 } |
| 98 | 161 |
| 99 // static | 162 // static |
| 100 AuthResult AuthResult::CreateWithParseError(const std::string& error_message, | 163 AuthResult AuthResult::CreateWithParseError(const std::string& error_message, |
| 101 ErrorType error_type) { | 164 ErrorType error_type) { |
| 102 return AuthResult(kParseErrorPrefix + error_message, error_type); | 165 return AuthResult(kParseErrorPrefix + error_message, error_type); |
| 103 } | 166 } |
| 104 | 167 |
| 105 AuthResult AuthenticateChallengeReply(const CastMessage& challenge_reply, | 168 AuthContext::AuthContext(const std::string& nonce) : nonce_(nonce) {} |
| 106 const net::X509Certificate& peer_cert) { | |
| 107 DeviceAuthMessage auth_message; | |
| 108 AuthResult result = ParseAuthMessage(challenge_reply, &auth_message); | |
| 109 if (!result.success()) { | |
| 110 return result; | |
| 111 } | |
| 112 | 169 |
| 170 AuthContext::~AuthContext() {} | |
| 171 | |
| 172 // Verifies the peer certificate and populates |peer_cert_der| with the DER | |
| 173 // encoded certificate. | |
| 174 AuthResult VerifyTLSCertificate(const net::X509Certificate& peer_cert, | |
| 175 std::string* peer_cert_der, | |
| 176 const base::Time& verification_time) { | |
| 113 // Get the DER-encoded form of the certificate. | 177 // Get the DER-encoded form of the certificate. |
| 114 std::string peer_cert_der; | |
| 115 if (!net::X509Certificate::GetDEREncoded(peer_cert.os_cert_handle(), | 178 if (!net::X509Certificate::GetDEREncoded(peer_cert.os_cert_handle(), |
| 116 &peer_cert_der) || | 179 peer_cert_der) || |
| 117 peer_cert_der.empty()) { | 180 peer_cert_der->empty()) { |
| 118 return AuthResult::CreateWithParseError( | 181 return AuthResult::CreateWithParseError( |
| 119 "Could not create DER-encoded peer cert.", | 182 "Could not create DER-encoded peer cert.", |
| 120 AuthResult::ERROR_CERT_PARSING_FAILED); | 183 AuthResult::ERROR_CERT_PARSING_FAILED); |
| 121 } | 184 } |
| 122 | 185 |
| 123 // Ensure the peer cert is valid and doesn't have an excessive remaining | 186 // Ensure the peer cert is valid and doesn't have an excessive remaining |
| 124 // lifetime. Although it is not verified as an X.509 certificate, the entire | 187 // lifetime. Although it is not verified as an X.509 certificate, the entire |
| 125 // structure is signed by the AuthResponse, so the validity field from X.509 | 188 // structure is signed by the AuthResponse, so the validity field from X.509 |
| 126 // is repurposed as this signature's expiration. | 189 // is repurposed as this signature's expiration. |
| 127 base::Time expiry = peer_cert.valid_expiry(); | 190 base::Time expiry = peer_cert.valid_expiry(); |
| 128 base::Time lifetime_limit = | 191 base::Time lifetime_limit = |
| 129 base::Time::Now() + | 192 verification_time + |
| 130 base::TimeDelta::FromDays(kMaxSelfSignedCertLifetimeInDays); | 193 base::TimeDelta::FromDays(kMaxSelfSignedCertLifetimeInDays); |
| 131 if (peer_cert.valid_start().is_null() || | 194 if (peer_cert.valid_start().is_null() || |
| 132 peer_cert.valid_start() > base::Time::Now()) { | 195 peer_cert.valid_start() > verification_time) { |
| 133 return AuthResult::CreateWithParseError( | 196 return AuthResult::CreateWithParseError( |
| 134 "Certificate's valid start date is in the future.", | 197 "Certificate's valid start date is in the future.", |
| 135 AuthResult::ERROR_TLS_CERT_VALID_START_DATE_IN_FUTURE); | 198 AuthResult::ERROR_TLS_CERT_VALID_START_DATE_IN_FUTURE); |
| 136 } | 199 } |
| 137 if (expiry.is_null() || peer_cert.HasExpired()) { | 200 if (expiry.is_null() || peer_cert.valid_expiry() < verification_time) { |
| 138 return AuthResult::CreateWithParseError("Certificate has expired.", | 201 return AuthResult::CreateWithParseError("Certificate has expired.", |
| 139 AuthResult::ERROR_TLS_CERT_EXPIRED); | 202 AuthResult::ERROR_TLS_CERT_EXPIRED); |
| 140 } | 203 } |
| 141 if (expiry > lifetime_limit) { | 204 if (expiry > lifetime_limit) { |
| 142 return AuthResult::CreateWithParseError( | 205 return AuthResult::CreateWithParseError( |
| 143 "Peer cert lifetime is too long.", | 206 "Peer cert lifetime is too long.", |
| 144 AuthResult::ERROR_TLS_CERT_VALIDITY_PERIOD_TOO_LONG); | 207 AuthResult::ERROR_TLS_CERT_VALIDITY_PERIOD_TOO_LONG); |
| 145 } | 208 } |
| 209 return AuthResult(); | |
| 210 } | |
| 211 | |
| 212 // Verifies the nonce received in the response is equivalent to the one sent. | |
| 213 AuthResult VerifySenderNonce(const std::string& nonce, | |
| 214 const std::string& nonce_response) { | |
| 215 if (nonce != nonce_response) { | |
| 216 if (nonce_response.empty()) { | |
| 217 UMA_HISTOGRAM_ENUMERATION("Cast.Channel.Nonce", NONCE_MISSING, | |
| 218 NONCE_COUNT); | |
| 219 } else { | |
| 220 UMA_HISTOGRAM_ENUMERATION("Cast.Channel.Nonce", NONCE_MISMATCH, | |
| 221 NONCE_COUNT); | |
| 222 } | |
| 223 if (base::FeatureList::IsEnabled(kEnforceNonceChecking)) { | |
| 224 return AuthResult("Sender nonce mismatched.", | |
| 225 AuthResult::ERROR_SENDER_NONCE_MISMATCH); | |
| 226 } | |
| 227 } else { | |
| 228 UMA_HISTOGRAM_ENUMERATION("Cast.Channel.Nonce", NONCE_MATCH, NONCE_COUNT); | |
| 229 } | |
| 230 return AuthResult(); | |
| 231 } | |
| 232 | |
| 233 AuthResult AuthenticateChallengeReply(const CastMessage& challenge_reply, | |
| 234 const net::X509Certificate& peer_cert, | |
| 235 const AuthContext& auth_context) { | |
| 236 DeviceAuthMessage auth_message; | |
| 237 AuthResult result = ParseAuthMessage(challenge_reply, &auth_message); | |
| 238 if (!result.success()) { | |
| 239 return result; | |
| 240 } | |
| 241 | |
| 242 std::string peer_cert_der; | |
| 243 result = VerifyTLSCertificate(peer_cert, &peer_cert_der, base::Time::Now()); | |
| 244 if (!result.success()) { | |
| 245 return result; | |
| 246 } | |
| 146 | 247 |
| 147 const AuthResponse& response = auth_message.response(); | 248 const AuthResponse& response = auth_message.response(); |
| 148 return VerifyCredentials(response, peer_cert_der); | 249 const std::string& nonce_response = response.sender_nonce(); |
| 250 | |
| 251 result = VerifySenderNonce(auth_context.nonce(), nonce_response); | |
|
mark a. foltz
2017/03/16 23:23:01
auth_context.VerifySenderNonce(nonce_response)
ryanchung
2017/03/17 02:46:06
Done.
| |
| 252 if (!result.success()) { | |
| 253 return result; | |
| 254 } | |
| 255 | |
| 256 return VerifyCredentials(response, nonce_response + peer_cert_der); | |
| 149 } | 257 } |
| 150 | 258 |
| 151 // This function does the following | 259 // This function does the following |
| 152 // | 260 // |
| 153 // * Verifies that the certificate chain |response.client_auth_certificate| + | 261 // * Verifies that the certificate chain |response.client_auth_certificate| + |
| 154 // |response.intermediate_certificate| is valid and chains to a trusted | 262 // |response.intermediate_certificate| is valid and chains to a trusted |
| 155 // Cast root. The list of trusted Cast roots can be overrided by providing a | 263 // Cast root. The list of trusted Cast roots can be overrided by providing a |
| 156 // non-nullptr |cast_trust_store|. The certificate is verified at | 264 // non-nullptr |cast_trust_store|. The certificate is verified at |
| 157 // |verification_time|. | 265 // |verification_time|. |
| 158 // | 266 // |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 269 const std::string& signature_input, | 377 const std::string& signature_input, |
| 270 const cast_crypto::CRLPolicy& crl_policy, | 378 const cast_crypto::CRLPolicy& crl_policy, |
| 271 net::TrustStore* cast_trust_store, | 379 net::TrustStore* cast_trust_store, |
| 272 net::TrustStore* crl_trust_store, | 380 net::TrustStore* crl_trust_store, |
| 273 const base::Time& verification_time) { | 381 const base::Time& verification_time) { |
| 274 return VerifyCredentialsImpl(response, signature_input, crl_policy, | 382 return VerifyCredentialsImpl(response, signature_input, crl_policy, |
| 275 cast_trust_store, crl_trust_store, | 383 cast_trust_store, crl_trust_store, |
| 276 verification_time); | 384 verification_time); |
| 277 } | 385 } |
| 278 | 386 |
| 387 AuthContext GetChallengeContext() { | |
| 388 return AuthContext(CastNonce::Get()); | |
| 389 } | |
| 390 | |
| 279 } // namespace cast_channel | 391 } // namespace cast_channel |
| 280 } // namespace api | 392 } // namespace api |
| 281 } // namespace extensions | 393 } // namespace extensions |
| OLD | NEW |