| 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> |
| 8 |
| 7 #include "base/logging.h" | 9 #include "base/logging.h" |
| 8 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
| 9 #include "base/strings/stringprintf.h" | 11 #include "base/strings/stringprintf.h" |
| 10 #include "extensions/browser/api/cast_channel/cast_message_util.h" | 12 #include "extensions/browser/api/cast_channel/cast_message_util.h" |
| 11 #include "extensions/common/api/cast_channel/cast_channel.pb.h" | 13 #include "extensions/common/api/cast_channel/cast_channel.pb.h" |
| 14 #include "extensions/common/cast/cast_cert_validator.h" |
| 12 | 15 |
| 13 namespace extensions { | 16 namespace extensions { |
| 14 namespace core_api { | 17 namespace core_api { |
| 15 namespace cast_channel { | 18 namespace cast_channel { |
| 16 namespace { | 19 namespace { |
| 17 | 20 |
| 18 const char* const kParseErrorPrefix = "Failed to parse auth message: "; | 21 const char* const kParseErrorPrefix = "Failed to parse auth message: "; |
| 19 | 22 |
| 20 const unsigned char kAudioOnlyPolicy[] = | 23 const unsigned char kAudioOnlyPolicy[] = |
| 21 {0x06, 0x0A, 0x2B, 0x06, 0x01, 0x04, 0x01, 0xD6, 0x79, 0x02, 0x05, 0x02}; | 24 {0x06, 0x0A, 0x2B, 0x06, 0x01, 0x04, 0x01, 0xD6, 0x79, 0x02, 0x05, 0x02}; |
| 22 | 25 |
| 26 namespace cast_crypto = ::extensions::core_api::cast_crypto; |
| 27 |
| 23 // Extracts an embedded DeviceAuthMessage payload from an auth challenge reply | 28 // Extracts an embedded DeviceAuthMessage payload from an auth challenge reply |
| 24 // message. | 29 // message. |
| 25 AuthResult ParseAuthMessage(const CastMessage& challenge_reply, | 30 AuthResult ParseAuthMessage(const CastMessage& challenge_reply, |
| 26 DeviceAuthMessage* auth_message) { | 31 DeviceAuthMessage* auth_message) { |
| 27 if (challenge_reply.payload_type() != CastMessage_PayloadType_BINARY) { | 32 if (challenge_reply.payload_type() != CastMessage_PayloadType_BINARY) { |
| 28 return AuthResult::CreateWithParseError( | 33 return AuthResult::CreateWithParseError( |
| 29 "Wrong payload type in challenge reply", | 34 "Wrong payload type in challenge reply", |
| 30 AuthResult::ERROR_WRONG_PAYLOAD_TYPE); | 35 AuthResult::ERROR_WRONG_PAYLOAD_TYPE); |
| 31 } | 36 } |
| 32 if (!challenge_reply.has_payload_binary()) { | 37 if (!challenge_reply.has_payload_binary()) { |
| (...skipping 15 matching lines...) Expand all Loading... |
| 48 base::IntToString(auth_message->error().error_type()), | 53 base::IntToString(auth_message->error().error_type()), |
| 49 AuthResult::ERROR_MESSAGE_ERROR); | 54 AuthResult::ERROR_MESSAGE_ERROR); |
| 50 } | 55 } |
| 51 if (!auth_message->has_response()) { | 56 if (!auth_message->has_response()) { |
| 52 return AuthResult::CreateWithParseError( | 57 return AuthResult::CreateWithParseError( |
| 53 "Auth message has no response field", AuthResult::ERROR_NO_RESPONSE); | 58 "Auth message has no response field", AuthResult::ERROR_NO_RESPONSE); |
| 54 } | 59 } |
| 55 return AuthResult(); | 60 return AuthResult(); |
| 56 } | 61 } |
| 57 | 62 |
| 63 AuthResult TranslateVerificationResult( |
| 64 const cast_crypto::VerificationResult& result) { |
| 65 AuthResult translated; |
| 66 translated.error_message = result.error_message; |
| 67 translated.nss_error_code = result.library_error_code; |
| 68 switch (result.error_type) { |
| 69 case cast_crypto::VerificationResult::ERROR_NONE: |
| 70 translated.error_type = AuthResult::ERROR_NONE; |
| 71 break; |
| 72 case cast_crypto::VerificationResult::ERROR_CERT_INVALID: |
| 73 translated.error_type = AuthResult::ERROR_CERT_PARSING_FAILED; |
| 74 break; |
| 75 case cast_crypto::VerificationResult::ERROR_CERT_UNTRUSTED: |
| 76 translated.error_type = AuthResult::ERROR_CERT_NOT_SIGNED_BY_TRUSTED_CA; |
| 77 break; |
| 78 case cast_crypto::VerificationResult::ERROR_SIGNATURE_INVALID: |
| 79 translated.error_type = AuthResult::ERROR_SIGNED_BLOBS_MISMATCH; |
| 80 break; |
| 81 case cast_crypto::VerificationResult::ERROR_INTERNAL: |
| 82 translated.error_type = AuthResult::ERROR_UNEXPECTED_AUTH_LIBRARY_RESULT; |
| 83 break; |
| 84 default: |
| 85 translated.error_type = AuthResult::ERROR_CERT_NOT_SIGNED_BY_TRUSTED_CA; |
| 86 }; |
| 87 return translated; |
| 88 } |
| 89 |
| 58 } // namespace | 90 } // namespace |
| 59 | 91 |
| 60 AuthResult::AuthResult() | 92 AuthResult::AuthResult() |
| 61 : error_type(ERROR_NONE), nss_error_code(0), channel_policies(POLICY_NONE) { | 93 : error_type(ERROR_NONE), nss_error_code(0), channel_policies(POLICY_NONE) { |
| 62 } | 94 } |
| 63 | 95 |
| 64 AuthResult::~AuthResult() { | 96 AuthResult::~AuthResult() { |
| 65 } | 97 } |
| 66 | 98 |
| 67 // static | 99 // static |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 106 } | 138 } |
| 107 | 139 |
| 108 if (response.client_auth_certificate().find(reinterpret_cast<const char*>( | 140 if (response.client_auth_certificate().find(reinterpret_cast<const char*>( |
| 109 kAudioOnlyPolicy)) != std::string::npos) { | 141 kAudioOnlyPolicy)) != std::string::npos) { |
| 110 result.channel_policies |= AuthResult::POLICY_AUDIO_ONLY; | 142 result.channel_policies |= AuthResult::POLICY_AUDIO_ONLY; |
| 111 } | 143 } |
| 112 | 144 |
| 113 return result; | 145 return result; |
| 114 } | 146 } |
| 115 | 147 |
| 148 // This function does the following |
| 149 // * Verifies that the trusted CA |response.intermediate_certificate| is |
| 150 // whitelisted for use. |
| 151 // * Verifies that |response.client_auth_certificate| is signed |
| 152 // by the trusted CA certificate. |
| 153 // * Verifies that |response.signature| matches the signature |
| 154 // of |peer_cert| by |response.client_auth_certificate|'s public |
| 155 // key. |
| 156 AuthResult VerifyCredentials(const AuthResponse& response, |
| 157 const std::string& peer_cert) { |
| 158 // Verify the certificate |
| 159 scoped_ptr<cast_crypto::CertVerificationContext> verification_context; |
| 160 cast_crypto::VerificationResult ret = cast_crypto::VerifyDeviceCert( |
| 161 response.client_auth_certificate(), |
| 162 std::vector<std::string>(response.intermediate_certificate().begin(), |
| 163 response.intermediate_certificate().end()), |
| 164 &verification_context); |
| 165 |
| 166 if (ret.Success()) |
| 167 ret = verification_context->VerifySignatureOverData(response.signature(), |
| 168 peer_cert); |
| 169 |
| 170 return TranslateVerificationResult(ret); |
| 171 } |
| 172 |
| 116 } // namespace cast_channel | 173 } // namespace cast_channel |
| 117 } // namespace core_api | 174 } // namespace core_api |
| 118 } // namespace extensions | 175 } // namespace extensions |
| OLD | NEW |