OLD | NEW |
(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 "crypto/cup.h" |
| 10 #include "crypto/random.h" |
| 11 #include "crypto/secure_util.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 |
| 14 namespace { |
| 15 |
| 16 std::string GetPublicKeyForTesting() { |
| 17 // How to generate this key: |
| 18 // openssl genpkey -out cr.pem -outform PEM -algorithm RSA |
| 19 // -pkeyopt rsa_keygen_pubexp:3 |
| 20 // openssl rsa -in cr.pem -pubout -out cr_pub.pem |
| 21 |
| 22 static const char kCupTestKey1024_Base64[] = |
| 23 "MIGdMA0GCSqGSIb3DQEBAQUAA4GLADCBhwKBgQC7ct1JhLSol2DkBcJdNjR3KkEA" |
| 24 "ZfXpF22lDD2WZu5JAZ4NiZqnHsKGJNPUbCH4AhFsXmuW5wEHhUVNhsMP6F9mQ06D" |
| 25 "i+ygwZ8aXlklmW4S0Et+SNg3i73fnYn0KDQzrzJnMu46s/CFPhjr4f0TH9b7oHkU" |
| 26 "XbqNZtG6gwaN1bmzFwIBAw=="; |
| 27 |
| 28 std::string result; |
| 29 if (!base::Base64Decode(std::string(kCupTestKey1024_Base64), &result)) |
| 30 return ""; |
| 31 |
| 32 return result; |
| 33 } |
| 34 |
| 35 } // end namespace |
| 36 |
| 37 #if defined(USE_OPENSSL) |
| 38 |
| 39 // Once CUP is implemented for OpenSSL, remove this #if block. |
| 40 TEST(CupUnitTest, OpenSSLStub) { |
| 41 crypto::ClientUpdateProtocol cup; |
| 42 |
| 43 std::string der_key; |
| 44 EXPECT_FALSE(cup.Init(8, GetPublicKeyForTesting())); |
| 45 } |
| 46 |
| 47 #else |
| 48 |
| 49 namespace crypto { |
| 50 |
| 51 class CupUnitTest : public testing::Test { |
| 52 protected: |
| 53 virtual void SetUp() { |
| 54 cup_.reset(ClientUpdateProtocol::Create(8, GetPublicKeyForTesting())); |
| 55 ASSERT_TRUE(cup_.get()); |
| 56 } |
| 57 |
| 58 void OverrideRAndRebuildKeys() { |
| 59 // This must be the same length as the modulus of the public key being |
| 60 // used in the unit test. |
| 61 static const size_t kPublicKeyLength = 1024 / 8; |
| 62 static const char kFixedR[] = |
| 63 "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do " |
| 64 "eiusmod tempor incididunt ut labore et dolore magna aliqua. "; |
| 65 |
| 66 ASSERT_EQ(kPublicKeyLength, strlen(kFixedR)); |
| 67 ASSERT_TRUE(cup_->BuildSharedKey(kPublicKeyLength, |
| 68 reinterpret_cast<const uint8*>(kFixedR))); |
| 69 } |
| 70 |
| 71 ClientUpdateProtocol& CUP() { |
| 72 return *cup_.get(); |
| 73 } |
| 74 |
| 75 private: |
| 76 scoped_ptr<ClientUpdateProtocol> cup_; |
| 77 }; |
| 78 |
| 79 TEST_F(CupUnitTest, GetVersionedSecret) { |
| 80 // Given a fixed public key set in our test fixture, if we override (r) with |
| 81 // a known data, we should be able to test (w) against an expected output. |
| 82 // |
| 83 // This expected output can be generated using this command line, where |
| 84 // plaintext.bin is the contents of kFixedR[] in OverrideRAndRebuildKeys(): |
| 85 // |
| 86 // openssl rsautl -inkey cr2_pub.pem -pubin -encrypt -raw |
| 87 // -in plaintext.bin | base64 |
| 88 // |
| 89 // Remember to prepend the key version number, and fix up the Base64 |
| 90 // afterwards to be URL-safe. |
| 91 |
| 92 static const char kExpectedVW[] = |
| 93 "8:lMmNR3mVbOitbq8ceYGStFBwrJcpvY-sauFSbMVe6VONS9x42xTOLY_KdqsWCy" |
| 94 "KuiJBiQziQLOybPUyA9vk0N5kMnC90LIh2nP2FgFG0M0Z22qjB3drsdJPi7TQZbb" |
| 95 "Xhqm587M8vjc6VlM_eoC0qYwCPaXBqXjsyiHnXetcn5X0"; |
| 96 |
| 97 EXPECT_NE(kExpectedVW, CUP().GetVersionedSecret()); |
| 98 OverrideRAndRebuildKeys(); |
| 99 EXPECT_EQ(kExpectedVW, CUP().GetVersionedSecret()); |
| 100 } |
| 101 |
| 102 TEST_F(CupUnitTest, SignRequest) { |
| 103 static const char kUrl[] = "//testserver.chromium.org/update"; |
| 104 static const char kUrlQuery[] = "?junk=present"; |
| 105 static const char kRequest[] = "testbody"; |
| 106 |
| 107 static const char kExpectedCP[] = "tfjmVMDAbU0-Kye4PjrCuyQIDCU"; |
| 108 |
| 109 OverrideRAndRebuildKeys(); |
| 110 |
| 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 EXPECT_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 EXPECT_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(CupUnitTest, 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 // We should return false if no calls to SignRequest() have been made. |
| 152 EXPECT_FALSE(CUP().ValidateResponse(kGoodResponse, kGoodC, kGoodSP)); |
| 153 |
| 154 EXPECT_TRUE(CUP().SignRequest(url, kRequest, NULL)); |
| 155 |
| 156 // Return true on a valid response and server proof. |
| 157 EXPECT_TRUE(CUP().ValidateResponse(kGoodResponse, kGoodC, kGoodSP)); |
| 158 |
| 159 // Return false on anything invalid. |
| 160 EXPECT_FALSE(CUP().ValidateResponse(kBadResponse, kGoodC, kGoodSP)); |
| 161 EXPECT_FALSE(CUP().ValidateResponse(kGoodResponse, kBadC, kGoodSP)); |
| 162 EXPECT_FALSE(CUP().ValidateResponse(kGoodResponse, kGoodC, kBadSP)); |
| 163 EXPECT_FALSE(CUP().ValidateResponse(kGoodResponse, kBadC, kBadSP)); |
| 164 EXPECT_FALSE(CUP().ValidateResponse(kBadResponse, kGoodC, kBadSP)); |
| 165 EXPECT_FALSE(CUP().ValidateResponse(kBadResponse, kBadC, kGoodSP)); |
| 166 EXPECT_FALSE(CUP().ValidateResponse(kBadResponse, kBadC, kBadSP)); |
| 167 } |
| 168 |
| 169 } // namespace crypto |
| 170 |
| 171 #endif // !defined(USE_OPENSSL) |
| 172 |
OLD | NEW |