| Index: extensions/browser/api/cast_channel/cast_auth_util.cc
|
| diff --git a/extensions/browser/api/cast_channel/cast_auth_util.cc b/extensions/browser/api/cast_channel/cast_auth_util.cc
|
| index 6ed1cdc78fdab1e546371d2d6bc1d19724588caa..5e3fc84ab957e11b9de6fd4630c0c5b9bb036e29 100644
|
| --- a/extensions/browser/api/cast_channel/cast_auth_util.cc
|
| +++ b/extensions/browser/api/cast_channel/cast_auth_util.cc
|
| @@ -38,6 +38,15 @@ const int kMaxSelfSignedCertLifetimeInDays = 4;
|
| const base::Feature kEnforceRevocationChecking{
|
| "CastCertificateRevocation", base::FEATURE_DISABLED_BY_DEFAULT};
|
|
|
| +// Enforce nonce checking when enabled.
|
| +// If disabled, the nonce value returned from the device is not checked against
|
| +// the one sent to the device. As a result, the nonce can be empty and omitted
|
| +// from the signature. This allows backwards compatibility with legacy Cast
|
| +// receivers.
|
| +
|
| +const base::Feature kEnforceNonceChecking{"CastNonceEnforced",
|
| + base::FEATURE_DISABLED_BY_DEFAULT};
|
| +
|
| namespace cast_crypto = ::cast_certificate;
|
|
|
| // Extracts an embedded DeviceAuthMessage payload from an auth challenge reply
|
| @@ -85,6 +94,13 @@ enum CertVerificationStatus {
|
| CERT_STATUS_COUNT,
|
| };
|
|
|
| +enum NonceVerificationStatus {
|
| + NONCE_MATCH,
|
| + NONCE_MISMATCH,
|
| + NONCE_MISSING,
|
| + NONCE_COUNT,
|
| +};
|
| +
|
| } // namespace
|
|
|
| AuthResult::AuthResult()
|
| @@ -102,19 +118,15 @@ AuthResult AuthResult::CreateWithParseError(const std::string& error_message,
|
| return AuthResult(kParseErrorPrefix + error_message, error_type);
|
| }
|
|
|
| -AuthResult AuthenticateChallengeReply(const CastMessage& challenge_reply,
|
| - const net::X509Certificate& peer_cert) {
|
| - DeviceAuthMessage auth_message;
|
| - AuthResult result = ParseAuthMessage(challenge_reply, &auth_message);
|
| - if (!result.success()) {
|
| - return result;
|
| - }
|
| -
|
| +// Verifies the peer certificate and populates |peer_cert_der| with the DER
|
| +// encoded certificate.
|
| +AuthResult VerifyTLSCertificate(const net::X509Certificate& peer_cert,
|
| + std::string* peer_cert_der,
|
| + const base::Time& verification_time) {
|
| // Get the DER-encoded form of the certificate.
|
| - std::string peer_cert_der;
|
| if (!net::X509Certificate::GetDEREncoded(peer_cert.os_cert_handle(),
|
| - &peer_cert_der) ||
|
| - peer_cert_der.empty()) {
|
| + peer_cert_der) ||
|
| + peer_cert_der->empty()) {
|
| return AuthResult::CreateWithParseError(
|
| "Could not create DER-encoded peer cert.",
|
| AuthResult::ERROR_CERT_PARSING_FAILED);
|
| @@ -126,15 +138,15 @@ AuthResult AuthenticateChallengeReply(const CastMessage& challenge_reply,
|
| // is repurposed as this signature's expiration.
|
| base::Time expiry = peer_cert.valid_expiry();
|
| base::Time lifetime_limit =
|
| - base::Time::Now() +
|
| + verification_time +
|
| base::TimeDelta::FromDays(kMaxSelfSignedCertLifetimeInDays);
|
| if (peer_cert.valid_start().is_null() ||
|
| - peer_cert.valid_start() > base::Time::Now()) {
|
| + peer_cert.valid_start() > verification_time) {
|
| return AuthResult::CreateWithParseError(
|
| "Certificate's valid start date is in the future.",
|
| AuthResult::ERROR_TLS_CERT_VALID_START_DATE_IN_FUTURE);
|
| }
|
| - if (expiry.is_null() || peer_cert.HasExpired()) {
|
| + if (expiry.is_null() || peer_cert.valid_expiry() < verification_time) {
|
| return AuthResult::CreateWithParseError("Certificate has expired.",
|
| AuthResult::ERROR_TLS_CERT_EXPIRED);
|
| }
|
| @@ -143,9 +155,54 @@ AuthResult AuthenticateChallengeReply(const CastMessage& challenge_reply,
|
| "Peer cert lifetime is too long.",
|
| AuthResult::ERROR_TLS_CERT_VALIDITY_PERIOD_TOO_LONG);
|
| }
|
| + return AuthResult();
|
| +}
|
| +
|
| +// Verifies the nonce received in the response is equivalent to the one sent.
|
| +AuthResult VerifySenderNonce(const std::string& nonce,
|
| + const std::string& nonce_response) {
|
| + if (nonce != nonce_response) {
|
| + if (nonce_response.empty()) {
|
| + UMA_HISTOGRAM_ENUMERATION("Cast.Channel.Nonce", NONCE_MISSING,
|
| + NONCE_COUNT);
|
| + } else {
|
| + UMA_HISTOGRAM_ENUMERATION("Cast.Channel.Nonce", NONCE_MISMATCH,
|
| + NONCE_COUNT);
|
| + }
|
| + if (base::FeatureList::IsEnabled(kEnforceNonceChecking)) {
|
| + return AuthResult("Sender nonce mismatched.",
|
| + AuthResult::ERROR_SENDER_NONCE_MISMATCH);
|
| + }
|
| + } else {
|
| + UMA_HISTOGRAM_ENUMERATION("Cast.Channel.Nonce", NONCE_MATCH, NONCE_COUNT);
|
| + }
|
| + return AuthResult();
|
| +}
|
| +
|
| +AuthResult AuthenticateChallengeReply(const CastMessage& challenge_reply,
|
| + const net::X509Certificate& peer_cert,
|
| + const std::string& nonce) {
|
| + DeviceAuthMessage auth_message;
|
| + AuthResult result = ParseAuthMessage(challenge_reply, &auth_message);
|
| + if (!result.success()) {
|
| + return result;
|
| + }
|
| +
|
| + std::string peer_cert_der;
|
| + result = VerifyTLSCertificate(peer_cert, &peer_cert_der, base::Time::Now());
|
| + if (!result.success()) {
|
| + return result;
|
| + }
|
|
|
| const AuthResponse& response = auth_message.response();
|
| - return VerifyCredentials(response, peer_cert_der);
|
| + const std::string& nonce_response = response.sender_nonce();
|
| +
|
| + result = VerifySenderNonce(nonce, nonce_response);
|
| + if (!result.success()) {
|
| + return result;
|
| + }
|
| +
|
| + return VerifyCredentials(response, nonce_response + peer_cert_der);
|
| }
|
|
|
| // This function does the following
|
|
|