Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(558)

Side by Side Diff: extensions/browser/api/cast_channel/cast_auth_util.cc

Issue 792353002: Refactoring of Cast-related crypto code (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fixed ChromeOS unit tests. Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 TranslateVerificationError(cast_crypto::VerificationResult error) {
mark a. foltz 2014/12/18 00:43:41 Better named TranslateVerificationResult since it'
sheretov 2015/01/03 03:18:02 Done.
61 AuthResult result;
62 result.error_message = error.error_message;
63
64 static AuthResult::ErrorType error_type_map[] = {
mark a. foltz 2014/12/18 00:43:41 We generally use switch statements for this type o
sheretov 2015/01/03 03:18:02 Done.
65 AuthResult::ERROR_NONE,
66 AuthResult::ERROR_CERT_PARSING_FAILED,
67 AuthResult::ERROR_CERT_NOT_SIGNED_BY_TRUSTED_CA,
68 AuthResult::ERROR_SIGNED_BLOBS_MISMATCH,
69 AuthResult::ERROR_UNEXPECTED_AUTH_LIBRARY_RESULT};
70 int idx = static_cast<int>(error.error_type);
71 result.error_type =
72 (idx < 0 || idx > cast_crypto::VerificationResult::ERROR_TYPE_MAX)
73 ? AuthResult::ERROR_UNEXPECTED_AUTH_LIBRARY_RESULT
mark a. foltz 2014/12/18 00:43:41 How will AuthResult::ErrorType and error.error_typ
sheretov 2015/01/03 03:18:02 Done in cast_cert_validator.h, but I think the com
74 : error_type_map[idx];
75
76 result.nss_error_code = error.library_error_code;
77
78 return result;
79 }
80
55 } // namespace 81 } // namespace
56 82
57 AuthResult::AuthResult() : error_type(ERROR_NONE), nss_error_code(0) { 83 AuthResult::AuthResult() : error_type(ERROR_NONE), nss_error_code(0) {
58 } 84 }
59 85
60 AuthResult::~AuthResult() { 86 AuthResult::~AuthResult() {
61 } 87 }
62 88
63 // static 89 // static
64 AuthResult AuthResult::CreateWithParseError(const std::string& error_message, 90 AuthResult AuthResult::CreateWithParseError(const std::string& error_message,
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
97 123
98 const AuthResponse& response = auth_message.response(); 124 const AuthResponse& response = auth_message.response();
99 result = VerifyCredentials(response, peer_cert); 125 result = VerifyCredentials(response, peer_cert);
100 if (!result.success()) { 126 if (!result.success()) {
101 return result; 127 return result;
102 } 128 }
103 129
104 return AuthResult(); 130 return AuthResult();
105 } 131 }
106 132
133 // This function does the following
134 // * Verifies that the trusted CA |response.intermediate_certificate| is
135 // whitelisted for use.
136 // * Verifies that |response.client_auth_certificate| is signed
137 // by the trusted CA certificate.
138 // * Verifies that |response.signature| matches the signature
139 // of |peer_cert| by |response.client_auth_certificate|'s public
140 // key.
141 AuthResult VerifyCredentials(const AuthResponse& response,
142 const std::string& peer_cert) {
143 // Verify the certificate
144 scoped_ptr<cast_crypto::CertVerificationContext> verification_context;
145 cast_crypto::VerificationResult ret = cast_crypto::VerifyDeviceCert(
146 response.client_auth_certificate(),
147 std::vector<std::string>(response.intermediate_certificate().begin(),
148 response.intermediate_certificate().end()),
149 &verification_context);
150
151 if (ret.Success())
152 ret = verification_context->VerifySignatureOverData(response.signature(),
153 peer_cert);
154
155 return TranslateVerificationError(ret);
156 }
157
107 } // namespace cast_channel 158 } // namespace cast_channel
108 } // namespace core_api 159 } // namespace core_api
109 } // namespace extensions 160 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698