You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

153 lines
5.7 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2011, Google Inc.
  4. * Portions Copyright 2011, RTFM, Inc.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice,
  10. * this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. * 3. The name of the author may not be used to endorse or promote products
  15. * derived from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  18. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  19. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  20. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  21. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  22. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  23. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  24. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  25. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  26. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. #include <string>
  29. #include "talk/base/gunit.h"
  30. #include "talk/base/ssladapter.h"
  31. #include "talk/base/sslidentity.h"
  32. const char kTestCertificate[] = "-----BEGIN CERTIFICATE-----\n"
  33. "MIIB6TCCAVICAQYwDQYJKoZIhvcNAQEEBQAwWzELMAkGA1UEBhMCQVUxEzARBgNV\n"
  34. "BAgTClF1ZWVuc2xhbmQxGjAYBgNVBAoTEUNyeXB0U29mdCBQdHkgTHRkMRswGQYD\n"
  35. "VQQDExJUZXN0IENBICgxMDI0IGJpdCkwHhcNMDAxMDE2MjIzMTAzWhcNMDMwMTE0\n"
  36. "MjIzMTAzWjBjMQswCQYDVQQGEwJBVTETMBEGA1UECBMKUXVlZW5zbGFuZDEaMBgG\n"
  37. "A1UEChMRQ3J5cHRTb2Z0IFB0eSBMdGQxIzAhBgNVBAMTGlNlcnZlciB0ZXN0IGNl\n"
  38. "cnQgKDUxMiBiaXQpMFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAJ+zw4Qnlf8SMVIP\n"
  39. "Fe9GEcStgOY2Ww/dgNdhjeD8ckUJNP5VZkVDTGiXav6ooKXfX3j/7tdkuD8Ey2//\n"
  40. "Kv7+ue0CAwEAATANBgkqhkiG9w0BAQQFAAOBgQCT0grFQeZaqYb5EYfk20XixZV4\n"
  41. "GmyAbXMftG1Eo7qGiMhYzRwGNWxEYojf5PZkYZXvSqZ/ZXHXa4g59jK/rJNnaVGM\n"
  42. "k+xIX8mxQvlV0n5O9PIha5BX5teZnkHKgL8aKKLKW1BK7YTngsfSzzaeame5iKfz\n"
  43. "itAE+OjGF+PFKbwX8Q==\n"
  44. "-----END CERTIFICATE-----\n";
  45. const unsigned char kTestCertSha1[] = {0xA6, 0xC8, 0x59, 0xEA,
  46. 0xC3, 0x7E, 0x6D, 0x33,
  47. 0xCF, 0xE2, 0x69, 0x9D,
  48. 0x74, 0xE6, 0xF6, 0x8A,
  49. 0x9E, 0x47, 0xA7, 0xCA};
  50. class SSLIdentityTest : public testing::Test {
  51. public:
  52. SSLIdentityTest() :
  53. identity1_(NULL), identity2_(NULL) {
  54. }
  55. ~SSLIdentityTest() {
  56. }
  57. static void SetUpTestCase() {
  58. talk_base::InitializeSSL();
  59. }
  60. virtual void SetUp() {
  61. identity1_.reset(talk_base::SSLIdentity::Generate("test1"));
  62. identity2_.reset(talk_base::SSLIdentity::Generate("test2"));
  63. ASSERT_TRUE(identity1_);
  64. ASSERT_TRUE(identity2_);
  65. test_cert_.reset(
  66. talk_base::SSLCertificate::FromPEMString(kTestCertificate, 0));
  67. ASSERT_TRUE(test_cert_);
  68. }
  69. void TestDigest(const std::string &algorithm, size_t expected_len,
  70. const unsigned char *expected_digest = NULL) {
  71. unsigned char digest1[64];
  72. unsigned char digest1b[64];
  73. unsigned char digest2[64];
  74. size_t digest1_len;
  75. size_t digest1b_len;
  76. size_t digest2_len;
  77. bool rv;
  78. rv = identity1_->certificate().ComputeDigest(algorithm,
  79. digest1, sizeof(digest1),
  80. &digest1_len);
  81. EXPECT_TRUE(rv);
  82. EXPECT_EQ(expected_len, digest1_len);
  83. rv = identity1_->certificate().ComputeDigest(algorithm,
  84. digest1b, sizeof(digest1b),
  85. &digest1b_len);
  86. EXPECT_TRUE(rv);
  87. EXPECT_EQ(expected_len, digest1b_len);
  88. EXPECT_EQ(0, memcmp(digest1, digest1b, expected_len));
  89. rv = identity2_->certificate().ComputeDigest(algorithm,
  90. digest2, sizeof(digest2),
  91. &digest2_len);
  92. EXPECT_TRUE(rv);
  93. EXPECT_EQ(expected_len, digest2_len);
  94. EXPECT_NE(0, memcmp(digest1, digest2, expected_len));
  95. // If we have an expected hash for the test cert, check it.
  96. if (expected_digest) {
  97. unsigned char digest3[64];
  98. size_t digest3_len;
  99. rv = test_cert_->ComputeDigest(algorithm, digest3, sizeof(digest3),
  100. &digest3_len);
  101. EXPECT_TRUE(rv);
  102. EXPECT_EQ(expected_len, digest3_len);
  103. EXPECT_EQ(0, memcmp(digest3, expected_digest, expected_len));
  104. }
  105. }
  106. private:
  107. talk_base::scoped_ptr<talk_base::SSLIdentity> identity1_;
  108. talk_base::scoped_ptr<talk_base::SSLIdentity> identity2_;
  109. talk_base::scoped_ptr<talk_base::SSLCertificate> test_cert_;
  110. };
  111. TEST_F(SSLIdentityTest, DigestSHA1) {
  112. TestDigest(talk_base::DIGEST_SHA_1, 20, kTestCertSha1);
  113. }
  114. // HASH_AlgSHA224 is not supported in the chromium linux build.
  115. #if SSL_USE_NSS
  116. TEST_F(SSLIdentityTest, DISABLED_DigestSHA224) {
  117. #else
  118. TEST_F(SSLIdentityTest, DigestSHA224) {
  119. #endif
  120. TestDigest(talk_base::DIGEST_SHA_224, 28);
  121. }
  122. TEST_F(SSLIdentityTest, DigestSHA256) {
  123. TestDigest(talk_base::DIGEST_SHA_256, 32);
  124. }
  125. TEST_F(SSLIdentityTest, DigestSHA384) {
  126. TestDigest(talk_base::DIGEST_SHA_384, 48);
  127. }
  128. TEST_F(SSLIdentityTest, DigestSHA512) {
  129. TestDigest(talk_base::DIGEST_SHA_512, 64);
  130. }