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 |
23 namespace cast_crypto = ::extensions::core_api::cast_crypto; | |
24 | |
20 // Extracts an embedded DeviceAuthMessage payload from an auth challenge reply | 25 // Extracts an embedded DeviceAuthMessage payload from an auth challenge reply |
21 // message. | 26 // message. |
22 AuthResult ParseAuthMessage(const CastMessage& challenge_reply, | 27 AuthResult ParseAuthMessage(const CastMessage& challenge_reply, |
23 DeviceAuthMessage* auth_message) { | 28 DeviceAuthMessage* auth_message) { |
24 if (challenge_reply.payload_type() != CastMessage_PayloadType_BINARY) { | 29 if (challenge_reply.payload_type() != CastMessage_PayloadType_BINARY) { |
25 return AuthResult::CreateWithParseError( | 30 return AuthResult::CreateWithParseError( |
26 "Wrong payload type in challenge reply", | 31 "Wrong payload type in challenge reply", |
27 AuthResult::ERROR_WRONG_PAYLOAD_TYPE); | 32 AuthResult::ERROR_WRONG_PAYLOAD_TYPE); |
28 } | 33 } |
29 if (!challenge_reply.has_payload_binary()) { | 34 if (!challenge_reply.has_payload_binary()) { |
(...skipping 15 matching lines...) Expand all Loading... | |
45 base::IntToString(auth_message->error().error_type()), | 50 base::IntToString(auth_message->error().error_type()), |
46 AuthResult::ERROR_MESSAGE_ERROR); | 51 AuthResult::ERROR_MESSAGE_ERROR); |
47 } | 52 } |
48 if (!auth_message->has_response()) { | 53 if (!auth_message->has_response()) { |
49 return AuthResult::CreateWithParseError( | 54 return AuthResult::CreateWithParseError( |
50 "Auth message has no response field", AuthResult::ERROR_NO_RESPONSE); | 55 "Auth message has no response field", AuthResult::ERROR_NO_RESPONSE); |
51 } | 56 } |
52 return AuthResult(); | 57 return AuthResult(); |
53 } | 58 } |
54 | 59 |
60 AuthResult TranslateVerificationResult( | |
61 const cast_crypto::VerificationResult& result) { | |
62 AuthResult translated; | |
63 translated.error_message = result.error_message; | |
64 translated.nss_error_code = result.library_error_code; | |
65 switch (result.error_type) { | |
66 case cast_crypto::VerificationResult::ERROR_NONE: | |
67 translated.error_type = AuthResult::ERROR_NONE; | |
68 break; | |
69 case cast_crypto::VerificationResult::ERROR_CERT_INVALID: | |
70 translated.error_type = AuthResult::ERROR_CERT_PARSING_FAILED; | |
71 break; | |
72 case cast_crypto::VerificationResult::ERROR_CERT_UNTRUSTED: | |
73 translated.error_type = AuthResult::ERROR_CERT_NOT_SIGNED_BY_TRUSTED_CA; | |
74 break; | |
75 case cast_crypto::VerificationResult::ERROR_SIGNATURE_INVALID: | |
76 translated.error_type = AuthResult::ERROR_SIGNED_BLOBS_MISMATCH; | |
77 break; | |
78 case cast_crypto::VerificationResult::ERROR_INTERNAL: | |
79 translated.error_type = AuthResult::ERROR_UNEXPECTED_AUTH_LIBRARY_RESULT; | |
80 break; | |
81 default: | |
82 translated.error_type = AuthResult::ERROR_CERT_NOT_SIGNED_BY_TRUSTED_CA; | |
83 }; | |
84 return translated; | |
85 } | |
86 | |
55 } // namespace | 87 } // namespace |
56 | 88 |
57 AuthResult::AuthResult() : error_type(ERROR_NONE), nss_error_code(0) { | 89 AuthResult::AuthResult() : error_type(ERROR_NONE), nss_error_code(0) { |
58 } | 90 } |
59 | 91 |
60 AuthResult::~AuthResult() { | 92 AuthResult::~AuthResult() { |
61 } | 93 } |
62 | 94 |
63 // static | 95 // static |
64 AuthResult AuthResult::CreateWithParseError(const std::string& error_message, | 96 AuthResult AuthResult::CreateWithParseError(const std::string& error_message, |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
97 | 129 |
98 const AuthResponse& response = auth_message.response(); | 130 const AuthResponse& response = auth_message.response(); |
99 result = VerifyCredentials(response, peer_cert); | 131 result = VerifyCredentials(response, peer_cert); |
100 if (!result.success()) { | 132 if (!result.success()) { |
101 return result; | 133 return result; |
102 } | 134 } |
103 | 135 |
104 return AuthResult(); | 136 return AuthResult(); |
105 } | 137 } |
106 | 138 |
139 // This function does the following | |
140 // * Verifies that the trusted CA |response.intermediate_certificate| is | |
141 // whitelisted for use. | |
142 // * Verifies that |response.client_auth_certificate| is signed | |
143 // by the trusted CA certificate. | |
144 // * Verifies that |response.signature| matches the signature | |
145 // of |peer_cert| by |response.client_auth_certificate|'s public | |
146 // key. | |
147 AuthResult VerifyCredentials(const AuthResponse& response, | |
148 const std::string& peer_cert) { | |
149 // Verify the certificate | |
150 scoped_ptr<cast_crypto::CertVerificationContext> verification_context; | |
151 cast_crypto::VerificationResult ret = cast_crypto::VerifyDeviceCert( | |
152 response.client_auth_certificate(), | |
153 std::vector<std::string>(response.intermediate_certificate().begin(), | |
mark a. foltz
2015/01/13 21:27:46
Is it necessary to duplicate all intermediate_cert
| |
154 response.intermediate_certificate().end()), | |
155 &verification_context); | |
156 | |
157 if (ret.Success()) | |
158 ret = verification_context->VerifySignatureOverData(response.signature(), | |
159 peer_cert); | |
160 | |
161 return TranslateVerificationResult(ret); | |
162 } | |
163 | |
107 } // namespace cast_channel | 164 } // namespace cast_channel |
108 } // namespace core_api | 165 } // namespace core_api |
109 } // namespace extensions | 166 } // namespace extensions |
OLD | NEW |