1 // Copyright (c) 2012 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 <string> 6 7 #include "remoting/base/rsa_key_pair.h" 8 #include "remoting/base/test_rsa_key_pair.h" 9 #include "testing/gmock/include/gmock/gmock.h" 10 #include "testing/gtest/include/gtest/gtest.h" 11 12 namespace remoting { 13 14 namespace { 15 const char kTestMessage[] = "Test Message"; 16 17 // |kTestMessage| signed with the key from |kTestRsaKeyPair|. 18 const char kExpectedSignature[] = 19 "LfUyXU2AiKM4rpWivUR3bLiQiRt1W3iIenNfJEB8RWyoEfnvSBoD52x8q9yFvtLFDEMPWyIrwM+N2" 20 "LuaWBKG1c0R7h+twBgvpExzZneJl+lbGMRx9ba8m/KAFrUWA/NRzOen2NHCuPybOEasgrPgGWBrmf" 21 "gDcvyW8QiGuKLopGj/4c5CQT4yE8JjsyU3Qqo2ZPK4neJYQhOmAlg+Q5dAPLpzWMj5HQyOVHJaSXZ" 22 "Y8vl/LiKvbdofYLeYNVKAE4q5mfpQMrsysPYpbxBV60AhFyrvtC040MFGcflKQRZNiZwMXVb7DclC" 23 "BPgvK7rI5Y0ERtVm+yNmH7vCivfyAnDUYA=="; 24 25 // Another RSA key pair, different from |kTestRsaKeyPair| 26 const char kTestRsaKeyPair2[] = 27 "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDLNd9bNNBFxXSasqWHv8ydItmoi" 28 "NhiWV/1Z6JI6TpKTnTm9yQSxIoCLHT8fB8QA0wzzgYEuZpDHIZDv1WxSdC3kgx84JrWe43+SPDZ2b" 29 "7ekuhXinPK+f3Nw5GjGaAUevyeWNoDD3GFka5q4zw8W1OE8E/z50FfPqOUejg+qyrgovEWQIZWvI0" 30 "CHdy8HTtxT7G0YbPJYZ8ycRkUrsXtY2RQX4IaCMEcdAmCm2Q2hYldulbZX6Bvv5GX8FOWYOerbYVK" 31 "ZsmH5II+KEP4We75ONVR2jdCCJ5L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xqCVeMGlbPn2Ga3IdxhjXwdG0uV0a2V7JPEcRiiPjzUsDw" 45 "yunroXwIVzuU3saacVnPURkDynGDh6XC6u9UOLuUHb3ZURZ7rxcS6by/HdZ3FRAoGBAOhjtjyfFEn" 46 "bZtjcQd+bNtoTPV/L71+K8AYPwV0td5Qy5VbBrTIlv7pNFxE4bYNuEe6cI2cxTua4i5IoKYXyUm5u" 47 "SvUVkkz7CpoiFwMnnLsNrZmazVS2zq0Y2a2ai8C3mPgLOdroS2fBBAcuFApeq1PvISmT6ZnJJ8Yah" 48 "HQCfClh"; 49 50 } // namespace 51 52 class RsaKeyPairTest : public testing::Test { 53 }; 54 55 TEST_F(RsaKeyPairTest, ImportExportImport) { 56 // Load a key from a string, export to a string, load again, and verify that 57 // we generate the same signature with both keys. 58 scoped_refptr<RsaKeyPair> exported_key = RsaKeyPair::FromString( 59 kTestRsaKeyPair); 60 scoped_refptr<RsaKeyPair> imported_key = RsaKeyPair::FromString( 61 exported_key->ToString()); 62 63 ASSERT_EQ(exported_key->SignMessage(kTestMessage), 64 imported_key->SignMessage(kTestMessage)); 65 } 66 67 TEST_F(RsaKeyPairTest, Signatures) { 68 // Sign a message and check that we get the expected signature. 69 scoped_refptr<RsaKeyPair> key_pair = RsaKeyPair::FromString(kTestRsaKeyPair); 70 71 std::string signature_base64 = key_pair->SignMessage(kTestMessage); 72 ASSERT_EQ(signature_base64, std::string(kExpectedSignature)); 73 } 74 75 TEST_F(RsaKeyPairTest, GenerateKey) { 76 // Test that we can generate a valid key. 77 scoped_refptr<RsaKeyPair> key_pair = RsaKeyPair::Generate(); 78 ASSERT_TRUE(key_pair.get()); 79 ASSERT_NE(key_pair->ToString(), ""); 80 ASSERT_NE(key_pair->GetPublicKey(), ""); 81 ASSERT_NE(key_pair->SignMessage(kTestMessage), ""); 82 } 83 84 TEST_F(RsaKeyPairTest, SignaturesDiffer) { 85 // Sign using different keys/messages and check that signatures are different. 86 scoped_refptr<RsaKeyPair> key_pair1 = RsaKeyPair::FromString(kTestRsaKeyPair); 87 scoped_refptr<RsaKeyPair> key_pair2 = RsaKeyPair::FromString( 88 kTestRsaKeyPair2); 89 90 std::string signature_kp1_msg1_base64 = key_pair1->SignMessage(kTestMessage); 91 std::string signature_kp2_msg1_base64 = key_pair2->SignMessage(kTestMessage); 92 std::string signature_kp1_msg2_base64 = key_pair1->SignMessage("Different"); 93 ASSERT_NE(signature_kp1_msg1_base64, signature_kp2_msg1_base64); 94 ASSERT_NE(signature_kp1_msg1_base64, signature_kp1_msg2_base64); 95 ASSERT_NE(key_pair1->GetPublicKey(), key_pair2->GetPublicKey()); 96 } 97 98 } // namespace remoting 99