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

Side by Side Diff: google_apis/cup/client_update_protocol_unittest.cc

Issue 15793005: Per discussion, implement the Omaha Client Update Protocol (CUP) in src/crypto. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 7 years, 6 months 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 | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include <limits>
6 #include <vector>
7
8 #include "base/base64.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "crypto/random.h"
11 #include "crypto/secure_util.h"
12 #include "google_apis/cup/client_update_protocol.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14
15 namespace {
16
17 std::string GetPublicKeyForTesting() {
18 // How to generate this key:
19 // openssl genpkey -out cr.pem -outform PEM -algorithm RSA
20 // -pkeyopt rsa_keygen_pubexp:3
21 // openssl rsa -in cr.pem -pubout -out cr_pub.pem
22
23 static const char kCupTestKey1024_Base64[] =
24 "MIGdMA0GCSqGSIb3DQEBAQUAA4GLADCBhwKBgQC7ct1JhLSol2DkBcJdNjR3KkEA"
25 "ZfXpF22lDD2WZu5JAZ4NiZqnHsKGJNPUbCH4AhFsXmuW5wEHhUVNhsMP6F9mQ06D"
26 "i+ygwZ8aXlklmW4S0Et+SNg3i73fnYn0KDQzrzJnMu46s/CFPhjr4f0TH9b7oHkU"
27 "XbqNZtG6gwaN1bmzFwIBAw==";
28
29 std::string result;
30 if (!base::Base64Decode(std::string(kCupTestKey1024_Base64), &result))
31 return "";
32
33 return result;
34 }
35
36 } // end namespace
37
38 #if defined(USE_OPENSSL)
39
40 // Once CUP is implemented for OpenSSL, remove this #if block.
41 TEST(CupTest, OpenSSLStub) {
42 scoped_ptr<ClientUpdateProtocol> cup =
43 ClientUpdateProtocol::Create(8, GetPublicKeyForTesting());
44 ASSERT_FALSE(cup.get());
45 }
46
47 #else
48
49 class CupTest : public testing::Test {
50 protected:
51 virtual void SetUp() {
52 cup_ = ClientUpdateProtocol::Create(8, GetPublicKeyForTesting());
53 ASSERT_TRUE(cup_.get());
54 }
55
56 void OverrideRAndRebuildKeys() {
57 // This must be the same length as the modulus of the public key being
58 // used in the unit test.
59 static const size_t kPublicKeyLength = 1024 / 8;
60 static const char kFixedR[] =
61 "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do "
62 "eiusmod tempor incididunt ut labore et dolore magna aliqua. ";
63
64 ASSERT_EQ(kPublicKeyLength, strlen(kFixedR));
65
66 std::vector<uint8> fixed_r(
67 reinterpret_cast<const uint8*>(kFixedR),
68 reinterpret_cast<const uint8*>(kFixedR) + kPublicKeyLength);
69 ASSERT_TRUE(cup_->BuildSharedKeyForTesting(kPublicKeyLength, fixed_r));
70 }
71
72 ClientUpdateProtocol& CUP() {
73 return *cup_.get();
74 }
75
76 private:
77 scoped_ptr<ClientUpdateProtocol> cup_;
78 };
79
80 TEST_F(CupTest, GetVersionedSecret) {
81 // Given a fixed public key set in our test fixture, if we override (r) with
82 // a known data, we should be able to test (w) against an expected output.
83 //
84 // This expected output can be generated using this command line, where
85 // plaintext.bin is the contents of kFixedR[] in OverrideRAndRebuildKeys():
86 //
87 // openssl rsautl -inkey cr2_pub.pem -pubin -encrypt -raw
88 // -in plaintext.bin | base64
89 //
90 // Remember to prepend the key version number, and fix up the Base64
91 // afterwards to be URL-safe.
92
93 static const char kExpectedVW[] =
94 "8:lMmNR3mVbOitbq8ceYGStFBwrJcpvY-sauFSbMVe6VONS9x42xTOLY_KdqsWCy"
95 "KuiJBiQziQLOybPUyA9vk0N5kMnC90LIh2nP2FgFG0M0Z22qjB3drsdJPi7TQZbb"
96 "Xhqm587M8vjc6VlM_eoC0qYwCPaXBqXjsyiHnXetcn5X0";
97
98 EXPECT_NE(kExpectedVW, CUP().GetVersionedSecret());
99 OverrideRAndRebuildKeys();
100 EXPECT_EQ(kExpectedVW, CUP().GetVersionedSecret());
101 }
102
103 TEST_F(CupTest, SignRequest) {
104 static const char kUrl[] = "//testserver.chromium.org/update";
105 static const char kUrlQuery[] = "?junk=present";
106 static const char kRequest[] = "testbody";
107
108 static const char kExpectedCP[] = "tfjmVMDAbU0-Kye4PjrCuyQIDCU";
109
110 OverrideRAndRebuildKeys();
111
112 // Check the case with no query string other than v|w.
113 std::string url(kUrl);
114 url.append("?w=");
115 url.append(CUP().GetVersionedSecret());
116
117 std::string cp;
118 ASSERT_TRUE(CUP().SignRequest(url, kRequest, &cp));
119
120 // Check the case with a pre-existing query string.
121 std::string url2(kUrl);
122 url2.append(kUrlQuery);
123 url2.append("&w=");
124 url2.append(CUP().GetVersionedSecret());
125
126 std::string cp2;
127 ASSERT_TRUE(CUP().SignRequest(url2, kRequest, &cp2));
128
129 // Changes in the URL should result in changes in the client proof.
130 EXPECT_EQ(kExpectedCP, cp2);
131 EXPECT_NE(cp, cp2);
132 }
133
134 TEST_F(CupTest, ValidateResponse) {
135 static const char kUrl[] = "//testserver.chromium.orgupdate?junk=present&w=";
136 static const char kRequest[] = "testbody";
137
138 static const char kGoodResponse[] = "intact_response";
139 static const char kGoodC[] = "c=EncryptedDataFromTheUpdateServer";
140 static const char kGoodSP[] = "5rMFMPL9Hgqb-2J8kL3scsHeNgg";
141
142 static const char kBadResponse[] = "tampered_response";
143 static const char kBadC[] = "c=TotalJunkThatAnAttackerCouldSend";
144 static const char kBadSP[] = "Base64TamperedShaOneHash";
145
146 OverrideRAndRebuildKeys();
147
148 std::string url(kUrl);
149 url.append(CUP().GetVersionedSecret());
150
151 std::string server_proof;
152 ASSERT_TRUE(CUP().SignRequest(url, kRequest, &server_proof));
153
154 // Return true on a valid response and server proof.
155 EXPECT_TRUE(CUP().ValidateResponse(kGoodResponse, kGoodC, kGoodSP));
156
157 // Return false on anything invalid.
158 EXPECT_FALSE(CUP().ValidateResponse(kBadResponse, kGoodC, kGoodSP));
159 EXPECT_FALSE(CUP().ValidateResponse(kGoodResponse, kBadC, kGoodSP));
160 EXPECT_FALSE(CUP().ValidateResponse(kGoodResponse, kGoodC, kBadSP));
161 EXPECT_FALSE(CUP().ValidateResponse(kGoodResponse, kBadC, kBadSP));
162 EXPECT_FALSE(CUP().ValidateResponse(kBadResponse, kGoodC, kBadSP));
163 EXPECT_FALSE(CUP().ValidateResponse(kBadResponse, kBadC, kGoodSP));
164 EXPECT_FALSE(CUP().ValidateResponse(kBadResponse, kBadC, kBadSP));
165 }
166
167 #endif // !defined(USE_OPENSSL)
168
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698