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.

601 lines
25 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright 2011 The WebRTC Project Authors. All rights reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #include <string.h>
  11. #include <memory>
  12. #include <string>
  13. #include <vector>
  14. #include "absl/strings/str_replace.h"
  15. #include "rtc_base/checks.h"
  16. #include "rtc_base/fake_ssl_identity.h"
  17. #include "rtc_base/helpers.h"
  18. #include "rtc_base/logging.h"
  19. #include "rtc_base/message_digest.h"
  20. #include "rtc_base/ssl_fingerprint.h"
  21. #include "rtc_base/ssl_identity.h"
  22. #include "test/gtest.h"
  23. using rtc::SSLIdentity;
  24. const char kTestCertificate[] =
  25. "-----BEGIN CERTIFICATE-----\n"
  26. "MIIB6TCCAVICAQYwDQYJKoZIhvcNAQEEBQAwWzELMAkGA1UEBhMCQVUxEzARBgNV\n"
  27. "BAgTClF1ZWVuc2xhbmQxGjAYBgNVBAoTEUNyeXB0U29mdCBQdHkgTHRkMRswGQYD\n"
  28. "VQQDExJUZXN0IENBICgxMDI0IGJpdCkwHhcNMDAxMDE2MjIzMTAzWhcNMDMwMTE0\n"
  29. "MjIzMTAzWjBjMQswCQYDVQQGEwJBVTETMBEGA1UECBMKUXVlZW5zbGFuZDEaMBgG\n"
  30. "A1UEChMRQ3J5cHRTb2Z0IFB0eSBMdGQxIzAhBgNVBAMTGlNlcnZlciB0ZXN0IGNl\n"
  31. "cnQgKDUxMiBiaXQpMFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAJ+zw4Qnlf8SMVIP\n"
  32. "Fe9GEcStgOY2Ww/dgNdhjeD8ckUJNP5VZkVDTGiXav6ooKXfX3j/7tdkuD8Ey2//\n"
  33. "Kv7+ue0CAwEAATANBgkqhkiG9w0BAQQFAAOBgQCT0grFQeZaqYb5EYfk20XixZV4\n"
  34. "GmyAbXMftG1Eo7qGiMhYzRwGNWxEYojf5PZkYZXvSqZ/ZXHXa4g59jK/rJNnaVGM\n"
  35. "k+xIX8mxQvlV0n5O9PIha5BX5teZnkHKgL8aKKLKW1BK7YTngsfSzzaeame5iKfz\n"
  36. "itAE+OjGF+PFKbwX8Q==\n"
  37. "-----END CERTIFICATE-----\n";
  38. const unsigned char kTestCertSha1[] = {0xA6, 0xC8, 0x59, 0xEA, 0xC3, 0x7E, 0x6D,
  39. 0x33, 0xCF, 0xE2, 0x69, 0x9D, 0x74, 0xE6,
  40. 0xF6, 0x8A, 0x9E, 0x47, 0xA7, 0xCA};
  41. const unsigned char kTestCertSha224[] = {
  42. 0xd4, 0xce, 0xc6, 0xcf, 0x28, 0xcb, 0xe9, 0x77, 0x38, 0x36,
  43. 0xcf, 0xb1, 0x3b, 0x4a, 0xd7, 0xbd, 0xae, 0x24, 0x21, 0x08,
  44. 0xcf, 0x6a, 0x44, 0x0d, 0x3f, 0x94, 0x2a, 0x5b};
  45. const unsigned char kTestCertSha256[] = {
  46. 0x41, 0x6b, 0xb4, 0x93, 0x47, 0x79, 0x77, 0x24, 0x77, 0x0b, 0x8b,
  47. 0x2e, 0xa6, 0x2b, 0xe0, 0xf9, 0x0a, 0xed, 0x1f, 0x31, 0xa6, 0xf7,
  48. 0x5c, 0xa1, 0x5a, 0xc4, 0xb0, 0xa2, 0xa4, 0x78, 0xb9, 0x76};
  49. const unsigned char kTestCertSha384[] = {
  50. 0x42, 0x31, 0x9a, 0x79, 0x1d, 0xd6, 0x08, 0xbf, 0x3b, 0xba, 0x36, 0xd8,
  51. 0x37, 0x4a, 0x9a, 0x75, 0xd3, 0x25, 0x6e, 0x28, 0x92, 0xbe, 0x06, 0xb7,
  52. 0xc5, 0xa0, 0x83, 0xe3, 0x86, 0xb1, 0x03, 0xfc, 0x64, 0x47, 0xd6, 0xd8,
  53. 0xaa, 0xd9, 0x36, 0x60, 0x04, 0xcc, 0xbe, 0x7d, 0x6a, 0xe8, 0x34, 0x49};
  54. const unsigned char kTestCertSha512[] = {
  55. 0x51, 0x1d, 0xec, 0x02, 0x3d, 0x51, 0x45, 0xd3, 0xd8, 0x1d, 0xa4,
  56. 0x9d, 0x43, 0xc9, 0xee, 0x32, 0x6f, 0x4f, 0x37, 0xee, 0xab, 0x3f,
  57. 0x25, 0xdf, 0x72, 0xfc, 0x61, 0x1a, 0xd5, 0x92, 0xff, 0x6b, 0x28,
  58. 0x71, 0x58, 0xb3, 0xe1, 0x8a, 0x18, 0xcf, 0x61, 0x33, 0x0e, 0x14,
  59. 0xc3, 0x04, 0xaa, 0x07, 0xf6, 0xa5, 0xda, 0xdc, 0x42, 0x42, 0x22,
  60. 0x35, 0xce, 0x26, 0x58, 0x4a, 0x33, 0x6d, 0xbc, 0xb6};
  61. // These PEM strings were created by generating an identity with
  62. // |SSLIdentity::Generate| and invoking |identity->PrivateKeyToPEMString()|,
  63. // |identity->PublicKeyToPEMString()| and
  64. // |identity->certificate().ToPEMString()|. If the crypto library is updated,
  65. // and the update changes the string form of the keys, these will have to be
  66. // updated too. The fingerprint, fingerprint algorithm and base64 certificate
  67. // were created by calling |identity->certificate().GetStats()|.
  68. static const char kRSA_PRIVATE_KEY_PEM[] =
  69. "-----BEGIN PRIVATE KEY-----\n"
  70. "MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAMQPqDStRlYeDpkX\n"
  71. "erRmv+a1naM8vSVSY0gG2plnrnofViWRW3MRqWC+020MsIj3hPZeSAnt/y/FL/nr\n"
  72. "4Ea7NXcwdRo1/1xEK7U/f/cjSg1aunyvHCHwcFcMr31HLFvHr0ZgcFwbgIuFLNEl\n"
  73. "7kK5HMO9APz1ntUjek8BmBj8yMl9AgMBAAECgYA8FWBC5GcNtSBcIinkZyigF0A7\n"
  74. "6j081sa+J/uNz4xUuI257ZXM6biygUhhvuXK06/XoIULJfhyN0fAm1yb0HtNhiUs\n"
  75. "kMOYeon6b8FqFaPjrQf7Gr9FMiIHXNK19uegTMKztXyPZoUWlX84X0iawY95x0Y3\n"
  76. "73f6P2rN2UOjlVVjAQJBAOKy3l2w3Zj2w0oAJox0eMwl+RxBNt1C42SHrob2mFUT\n"
  77. "rytpVVYOasr8CoDI0kjacjI94sLum+buJoXXX6YTGO0CQQDdZwlYIEkoS3ftfxPa\n"
  78. "Ai0YTBzAWvHJg0r8Gk/TkHo6IM+LSsZ9ZYUv/vBe4BKLw1I4hZ+bQvBiq+f8ROtk\n"
  79. "+TDRAkAPL3ghwoU1h+IRBO2QHwUwd6K2N9AbBi4BP+168O3HVSg4ujeTKigRLMzv\n"
  80. "T4R2iNt5bhfQgvdCgtVlxcWMdF8JAkBwDCg3eEdt5BuyjwBt8XH+/O4ED0KUWCTH\n"
  81. "x00k5dZlupsuhE5Fwe4QpzXg3gekwdnHjyCCQ/NCDHvgOMTkmhQxAkA9V03KRX9b\n"
  82. "bhvEzY/fu8gEp+EzsER96/D79az5z1BaMGL5OPM2xHBPJATKlswnAa7Lp3QKGZGk\n"
  83. "TxslfL18J71s\n"
  84. "-----END PRIVATE KEY-----\n";
  85. static const char kRSA_PUBLIC_KEY_PEM[] =
  86. "-----BEGIN PUBLIC KEY-----\n"
  87. "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDED6g0rUZWHg6ZF3q0Zr/mtZ2j\n"
  88. "PL0lUmNIBtqZZ656H1YlkVtzEalgvtNtDLCI94T2XkgJ7f8vxS/56+BGuzV3MHUa\n"
  89. "Nf9cRCu1P3/3I0oNWrp8rxwh8HBXDK99Ryxbx69GYHBcG4CLhSzRJe5CuRzDvQD8\n"
  90. "9Z7VI3pPAZgY/MjJfQIDAQAB\n"
  91. "-----END PUBLIC KEY-----\n";
  92. static const char kRSA_CERT_PEM[] =
  93. "-----BEGIN CERTIFICATE-----\n"
  94. "MIIBnDCCAQWgAwIBAgIJAOEHLgeWYwrpMA0GCSqGSIb3DQEBCwUAMBAxDjAMBgNV\n"
  95. "BAMMBXRlc3QxMB4XDTE2MDQyNDE4MTAyMloXDTE2MDUyNTE4MTAyMlowEDEOMAwG\n"
  96. "A1UEAwwFdGVzdDEwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBAMQPqDStRlYe\n"
  97. "DpkXerRmv+a1naM8vSVSY0gG2plnrnofViWRW3MRqWC+020MsIj3hPZeSAnt/y/F\n"
  98. "L/nr4Ea7NXcwdRo1/1xEK7U/f/cjSg1aunyvHCHwcFcMr31HLFvHr0ZgcFwbgIuF\n"
  99. "LNEl7kK5HMO9APz1ntUjek8BmBj8yMl9AgMBAAEwDQYJKoZIhvcNAQELBQADgYEA\n"
  100. "C3ehaZFl+oEYN069C2ht/gMzuC77L854RF/x7xRtNZzkcg9TVgXXdM3auUvJi8dx\n"
  101. "yTpU3ixErjQvoZew5ngXTEvTY8BSQUijJEaLWh8n6NDKRbEGTdAk8nPAmq9hdCFq\n"
  102. "e3UkexqNHm3g/VxG4NUC1Y+w29ai0/Rgh+VvgbDwK+Q=\n"
  103. "-----END CERTIFICATE-----\n";
  104. static const char kRSA_FINGERPRINT[] =
  105. "3C:E8:B2:70:09:CF:A9:09:5A:F4:EF:8F:8D:8A:32:FF:EA:04:91:BA:6E:D4:17:78:16"
  106. ":2A:EE:F9:9A:DD:E2:2B";
  107. static const char kRSA_FINGERPRINT_ALGORITHM[] = "sha-256";
  108. static const char kRSA_BASE64_CERTIFICATE[] =
  109. "MIIBnDCCAQWgAwIBAgIJAOEHLgeWYwrpMA0GCSqGSIb3DQEBCwUAMBAxDjAMBgNVBAMMBXRlc3"
  110. "QxMB4XDTE2MDQyNDE4MTAyMloXDTE2MDUyNTE4MTAyMlowEDEOMAwGA1UEAwwFdGVzdDEwgZ8w"
  111. "DQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBAMQPqDStRlYeDpkXerRmv+a1naM8vSVSY0gG2plnrn"
  112. "ofViWRW3MRqWC+020MsIj3hPZeSAnt/y/FL/nr4Ea7NXcwdRo1/1xEK7U/f/cjSg1aunyvHCHw"
  113. "cFcMr31HLFvHr0ZgcFwbgIuFLNEl7kK5HMO9APz1ntUjek8BmBj8yMl9AgMBAAEwDQYJKoZIhv"
  114. "cNAQELBQADgYEAC3ehaZFl+oEYN069C2ht/gMzuC77L854RF/x7xRtNZzkcg9TVgXXdM3auUvJ"
  115. "i8dxyTpU3ixErjQvoZew5ngXTEvTY8BSQUijJEaLWh8n6NDKRbEGTdAk8nPAmq9hdCFqe3Ukex"
  116. "qNHm3g/VxG4NUC1Y+w29ai0/Rgh+VvgbDwK+Q=";
  117. static const char kECDSA_PRIVATE_KEY_PEM[] =
  118. "-----BEGIN PRIVATE KEY-----\n"
  119. "MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQg/AkEA2hklq7dQ2rN\n"
  120. "ZxYL6hOUACL4pn7P4FYlA3ZQhIChRANCAAR7YgdO3utP/8IqVRq8G4VZKreMAxeN\n"
  121. "rUa12twthv4uFjuHAHa9D9oyAjncmn+xvZZRyVmKrA56jRzENcEEHoAg\n"
  122. "-----END PRIVATE KEY-----\n";
  123. static const char kECDSA_PUBLIC_KEY_PEM[] =
  124. "-----BEGIN PUBLIC KEY-----\n"
  125. "MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEe2IHTt7rT//CKlUavBuFWSq3jAMX\n"
  126. "ja1GtdrcLYb+LhY7hwB2vQ/aMgI53Jp/sb2WUclZiqwOeo0cxDXBBB6AIA==\n"
  127. "-----END PUBLIC KEY-----\n";
  128. static const char kECDSA_CERT_PEM[] =
  129. "-----BEGIN CERTIFICATE-----\n"
  130. "MIIBFDCBu6ADAgECAgkArpkxjw62sW4wCgYIKoZIzj0EAwIwEDEOMAwGA1UEAwwF\n"
  131. "dGVzdDMwHhcNMTYwNDI0MTgxNDM4WhcNMTYwNTI1MTgxNDM4WjAQMQ4wDAYDVQQD\n"
  132. "DAV0ZXN0MzBZMBMGByqGSM49AgEGCCqGSM49AwEHA0IABHtiB07e60//wipVGrwb\n"
  133. "hVkqt4wDF42tRrXa3C2G/i4WO4cAdr0P2jICOdyaf7G9llHJWYqsDnqNHMQ1wQQe\n"
  134. "gCAwCgYIKoZIzj0EAwIDSAAwRQIhANyreQ/K5yuPPpirsd0e/4WGLHou6bIOSQks\n"
  135. "DYzo56NmAiAKOr3u8ol3LmygbUCwEvtWrS8QcJDygxHPACo99hkekw==\n"
  136. "-----END CERTIFICATE-----\n";
  137. static const char kECDSA_FINGERPRINT[] =
  138. "9F:47:FA:88:76:3D:18:B8:00:A0:59:9D:C3:5D:34:0B:1F:B8:99:9E:68:DA:F3:A5:DA"
  139. ":50:33:A9:FF:4D:31:89";
  140. static const char kECDSA_FINGERPRINT_ALGORITHM[] = "sha-256";
  141. static const char kECDSA_BASE64_CERTIFICATE[] =
  142. "MIIBFDCBu6ADAgECAgkArpkxjw62sW4wCgYIKoZIzj0EAwIwEDEOMAwGA1UEAwwFdGVzdDMwHh"
  143. "cNMTYwNDI0MTgxNDM4WhcNMTYwNTI1MTgxNDM4WjAQMQ4wDAYDVQQDDAV0ZXN0MzBZMBMGByqG"
  144. "SM49AgEGCCqGSM49AwEHA0IABHtiB07e60//wipVGrwbhVkqt4wDF42tRrXa3C2G/i4WO4cAdr"
  145. "0P2jICOdyaf7G9llHJWYqsDnqNHMQ1wQQegCAwCgYIKoZIzj0EAwIDSAAwRQIhANyreQ/K5yuP"
  146. "Ppirsd0e/4WGLHou6bIOSQksDYzo56NmAiAKOr3u8ol3LmygbUCwEvtWrS8QcJDygxHPACo99h"
  147. "kekw==";
  148. struct IdentityAndInfo {
  149. std::unique_ptr<rtc::SSLIdentity> identity;
  150. std::vector<std::string> ders;
  151. std::vector<std::string> pems;
  152. std::vector<std::string> fingerprints;
  153. };
  154. IdentityAndInfo CreateFakeIdentityAndInfoFromDers(
  155. const std::vector<std::string>& ders) {
  156. RTC_CHECK(!ders.empty());
  157. IdentityAndInfo info;
  158. info.ders = ders;
  159. for (const std::string& der : ders) {
  160. info.pems.push_back(rtc::SSLIdentity::DerToPem(
  161. "CERTIFICATE", reinterpret_cast<const unsigned char*>(der.c_str()),
  162. der.length()));
  163. }
  164. info.identity.reset(new rtc::FakeSSLIdentity(info.pems));
  165. // Strip header/footer and newline characters of PEM strings.
  166. for (size_t i = 0; i < info.pems.size(); ++i) {
  167. absl::StrReplaceAll({{"-----BEGIN CERTIFICATE-----", ""},
  168. {"-----END CERTIFICATE-----", ""},
  169. {"\n", ""}},
  170. &info.pems[i]);
  171. }
  172. // Fingerprints for the whole certificate chain, starting with leaf
  173. // certificate.
  174. const rtc::SSLCertChain& chain = info.identity->cert_chain();
  175. std::unique_ptr<rtc::SSLFingerprint> fp;
  176. for (size_t i = 0; i < chain.GetSize(); i++) {
  177. fp = rtc::SSLFingerprint::Create("sha-1", chain.Get(i));
  178. EXPECT_TRUE(fp);
  179. info.fingerprints.push_back(fp->GetRfc4572Fingerprint());
  180. }
  181. EXPECT_EQ(info.ders.size(), info.fingerprints.size());
  182. return info;
  183. }
  184. class SSLIdentityTest : public ::testing::Test {
  185. public:
  186. void SetUp() override {
  187. identity_rsa1_.reset(SSLIdentity::Generate("test1", rtc::KT_RSA));
  188. identity_rsa2_.reset(SSLIdentity::Generate("test2", rtc::KT_RSA));
  189. identity_ecdsa1_.reset(SSLIdentity::Generate("test3", rtc::KT_ECDSA));
  190. identity_ecdsa2_.reset(SSLIdentity::Generate("test4", rtc::KT_ECDSA));
  191. ASSERT_TRUE(identity_rsa1_);
  192. ASSERT_TRUE(identity_rsa2_);
  193. ASSERT_TRUE(identity_ecdsa1_);
  194. ASSERT_TRUE(identity_ecdsa2_);
  195. test_cert_ = rtc::SSLCertificate::FromPEMString(kTestCertificate);
  196. ASSERT_TRUE(test_cert_);
  197. }
  198. void TestGetSignatureDigestAlgorithm() {
  199. std::string digest_algorithm;
  200. ASSERT_TRUE(identity_rsa1_->certificate().GetSignatureDigestAlgorithm(
  201. &digest_algorithm));
  202. ASSERT_EQ(rtc::DIGEST_SHA_256, digest_algorithm);
  203. ASSERT_TRUE(identity_rsa2_->certificate().GetSignatureDigestAlgorithm(
  204. &digest_algorithm));
  205. ASSERT_EQ(rtc::DIGEST_SHA_256, digest_algorithm);
  206. ASSERT_TRUE(identity_ecdsa1_->certificate().GetSignatureDigestAlgorithm(
  207. &digest_algorithm));
  208. ASSERT_EQ(rtc::DIGEST_SHA_256, digest_algorithm);
  209. ASSERT_TRUE(identity_ecdsa2_->certificate().GetSignatureDigestAlgorithm(
  210. &digest_algorithm));
  211. ASSERT_EQ(rtc::DIGEST_SHA_256, digest_algorithm);
  212. // The test certificate has an MD5-based signature.
  213. ASSERT_TRUE(test_cert_->GetSignatureDigestAlgorithm(&digest_algorithm));
  214. ASSERT_EQ(rtc::DIGEST_MD5, digest_algorithm);
  215. }
  216. typedef unsigned char DigestType[rtc::MessageDigest::kMaxSize];
  217. void TestDigestHelper(DigestType digest,
  218. const SSLIdentity* identity,
  219. const std::string& algorithm,
  220. size_t expected_len) {
  221. DigestType digest1;
  222. size_t digest_len;
  223. bool rv;
  224. memset(digest, 0, expected_len);
  225. rv = identity->certificate().ComputeDigest(algorithm, digest,
  226. sizeof(DigestType), &digest_len);
  227. EXPECT_TRUE(rv);
  228. EXPECT_EQ(expected_len, digest_len);
  229. // Repeat digest computation for the identity as a sanity check.
  230. memset(digest1, 0xff, expected_len);
  231. rv = identity->certificate().ComputeDigest(algorithm, digest1,
  232. sizeof(DigestType), &digest_len);
  233. EXPECT_TRUE(rv);
  234. EXPECT_EQ(expected_len, digest_len);
  235. EXPECT_EQ(0, memcmp(digest, digest1, expected_len));
  236. }
  237. void TestDigestForGeneratedCert(const std::string& algorithm,
  238. size_t expected_len) {
  239. DigestType digest[4];
  240. ASSERT_TRUE(expected_len <= sizeof(DigestType));
  241. TestDigestHelper(digest[0], identity_rsa1_.get(), algorithm, expected_len);
  242. TestDigestHelper(digest[1], identity_rsa2_.get(), algorithm, expected_len);
  243. TestDigestHelper(digest[2], identity_ecdsa1_.get(), algorithm,
  244. expected_len);
  245. TestDigestHelper(digest[3], identity_ecdsa2_.get(), algorithm,
  246. expected_len);
  247. // Sanity check that all four digests are unique. This could theoretically
  248. // fail, since cryptographic hash collisions have a non-zero probability.
  249. for (int i = 0; i < 4; i++) {
  250. for (int j = 0; j < 4; j++) {
  251. if (i != j)
  252. EXPECT_NE(0, memcmp(digest[i], digest[j], expected_len));
  253. }
  254. }
  255. }
  256. void TestDigestForFixedCert(const std::string& algorithm,
  257. size_t expected_len,
  258. const unsigned char* expected_digest) {
  259. bool rv;
  260. DigestType digest;
  261. size_t digest_len;
  262. ASSERT_TRUE(expected_len <= sizeof(DigestType));
  263. rv = test_cert_->ComputeDigest(algorithm, digest, sizeof(digest),
  264. &digest_len);
  265. EXPECT_TRUE(rv);
  266. EXPECT_EQ(expected_len, digest_len);
  267. EXPECT_EQ(0, memcmp(digest, expected_digest, expected_len));
  268. }
  269. void TestCloningIdentity(const SSLIdentity& identity) {
  270. // Convert |identity| to PEM strings and create a new identity by converting
  271. // back from the string format.
  272. std::string priv_pem = identity.PrivateKeyToPEMString();
  273. std::string publ_pem = identity.PublicKeyToPEMString();
  274. std::string cert_pem = identity.certificate().ToPEMString();
  275. std::unique_ptr<SSLIdentity> clone(
  276. SSLIdentity::FromPEMStrings(priv_pem, cert_pem));
  277. EXPECT_TRUE(clone);
  278. // Make sure the clone is identical to the original.
  279. EXPECT_TRUE(identity == *clone);
  280. ASSERT_EQ(identity.certificate().CertificateExpirationTime(),
  281. clone->certificate().CertificateExpirationTime());
  282. // At this point we are confident that the identities are identical. To be
  283. // extra sure, we compare PEM strings of the clone with the original. Note
  284. // that the PEM strings of two identities are not strictly guaranteed to be
  285. // equal (they describe structs whose members could be listed in a different
  286. // order, for example). But because the same function is used to produce
  287. // both PEMs, its a good enough bet that this comparison will work. If the
  288. // assumption stops holding in the future we can always remove this from the
  289. // unittest.
  290. std::string clone_priv_pem = clone->PrivateKeyToPEMString();
  291. std::string clone_publ_pem = clone->PublicKeyToPEMString();
  292. std::string clone_cert_pem = clone->certificate().ToPEMString();
  293. ASSERT_EQ(priv_pem, clone_priv_pem);
  294. ASSERT_EQ(publ_pem, clone_publ_pem);
  295. ASSERT_EQ(cert_pem, clone_cert_pem);
  296. }
  297. protected:
  298. std::unique_ptr<SSLIdentity> identity_rsa1_;
  299. std::unique_ptr<SSLIdentity> identity_rsa2_;
  300. std::unique_ptr<SSLIdentity> identity_ecdsa1_;
  301. std::unique_ptr<SSLIdentity> identity_ecdsa2_;
  302. std::unique_ptr<rtc::SSLCertificate> test_cert_;
  303. };
  304. TEST_F(SSLIdentityTest, FixedDigestSHA1) {
  305. TestDigestForFixedCert(rtc::DIGEST_SHA_1, 20, kTestCertSha1);
  306. }
  307. // HASH_AlgSHA224 is not supported in the chromium linux build.
  308. TEST_F(SSLIdentityTest, FixedDigestSHA224) {
  309. TestDigestForFixedCert(rtc::DIGEST_SHA_224, 28, kTestCertSha224);
  310. }
  311. TEST_F(SSLIdentityTest, FixedDigestSHA256) {
  312. TestDigestForFixedCert(rtc::DIGEST_SHA_256, 32, kTestCertSha256);
  313. }
  314. TEST_F(SSLIdentityTest, FixedDigestSHA384) {
  315. TestDigestForFixedCert(rtc::DIGEST_SHA_384, 48, kTestCertSha384);
  316. }
  317. TEST_F(SSLIdentityTest, FixedDigestSHA512) {
  318. TestDigestForFixedCert(rtc::DIGEST_SHA_512, 64, kTestCertSha512);
  319. }
  320. // HASH_AlgSHA224 is not supported in the chromium linux build.
  321. TEST_F(SSLIdentityTest, DigestSHA224) {
  322. TestDigestForGeneratedCert(rtc::DIGEST_SHA_224, 28);
  323. }
  324. TEST_F(SSLIdentityTest, DigestSHA256) {
  325. TestDigestForGeneratedCert(rtc::DIGEST_SHA_256, 32);
  326. }
  327. TEST_F(SSLIdentityTest, DigestSHA384) {
  328. TestDigestForGeneratedCert(rtc::DIGEST_SHA_384, 48);
  329. }
  330. TEST_F(SSLIdentityTest, DigestSHA512) {
  331. TestDigestForGeneratedCert(rtc::DIGEST_SHA_512, 64);
  332. }
  333. TEST_F(SSLIdentityTest, IdentityComparison) {
  334. EXPECT_TRUE(*identity_rsa1_ == *identity_rsa1_);
  335. EXPECT_FALSE(*identity_rsa1_ == *identity_rsa2_);
  336. EXPECT_FALSE(*identity_rsa1_ == *identity_ecdsa1_);
  337. EXPECT_FALSE(*identity_rsa1_ == *identity_ecdsa2_);
  338. EXPECT_TRUE(*identity_rsa2_ == *identity_rsa2_);
  339. EXPECT_FALSE(*identity_rsa2_ == *identity_ecdsa1_);
  340. EXPECT_FALSE(*identity_rsa2_ == *identity_ecdsa2_);
  341. EXPECT_TRUE(*identity_ecdsa1_ == *identity_ecdsa1_);
  342. EXPECT_FALSE(*identity_ecdsa1_ == *identity_ecdsa2_);
  343. }
  344. TEST_F(SSLIdentityTest, FromPEMStringsRSA) {
  345. std::unique_ptr<SSLIdentity> identity(
  346. SSLIdentity::FromPEMStrings(kRSA_PRIVATE_KEY_PEM, kRSA_CERT_PEM));
  347. EXPECT_TRUE(identity);
  348. EXPECT_EQ(kRSA_PRIVATE_KEY_PEM, identity->PrivateKeyToPEMString());
  349. EXPECT_EQ(kRSA_PUBLIC_KEY_PEM, identity->PublicKeyToPEMString());
  350. EXPECT_EQ(kRSA_CERT_PEM, identity->certificate().ToPEMString());
  351. }
  352. TEST_F(SSLIdentityTest, FromPEMStringsEC) {
  353. std::unique_ptr<SSLIdentity> identity(
  354. SSLIdentity::FromPEMStrings(kECDSA_PRIVATE_KEY_PEM, kECDSA_CERT_PEM));
  355. EXPECT_TRUE(identity);
  356. EXPECT_EQ(kECDSA_PRIVATE_KEY_PEM, identity->PrivateKeyToPEMString());
  357. EXPECT_EQ(kECDSA_PUBLIC_KEY_PEM, identity->PublicKeyToPEMString());
  358. EXPECT_EQ(kECDSA_CERT_PEM, identity->certificate().ToPEMString());
  359. }
  360. TEST_F(SSLIdentityTest, CloneIdentityRSA) {
  361. TestCloningIdentity(*identity_rsa1_);
  362. TestCloningIdentity(*identity_rsa2_);
  363. }
  364. TEST_F(SSLIdentityTest, CloneIdentityECDSA) {
  365. TestCloningIdentity(*identity_ecdsa1_);
  366. TestCloningIdentity(*identity_ecdsa2_);
  367. }
  368. TEST_F(SSLIdentityTest, PemDerConversion) {
  369. std::string der;
  370. EXPECT_TRUE(SSLIdentity::PemToDer("CERTIFICATE", kTestCertificate, &der));
  371. EXPECT_EQ(
  372. kTestCertificate,
  373. SSLIdentity::DerToPem("CERTIFICATE",
  374. reinterpret_cast<const unsigned char*>(der.data()),
  375. der.length()));
  376. }
  377. TEST_F(SSLIdentityTest, GetSignatureDigestAlgorithm) {
  378. TestGetSignatureDigestAlgorithm();
  379. }
  380. TEST_F(SSLIdentityTest, SSLCertificateGetStatsRSA) {
  381. std::unique_ptr<SSLIdentity> identity(
  382. SSLIdentity::FromPEMStrings(kRSA_PRIVATE_KEY_PEM, kRSA_CERT_PEM));
  383. std::unique_ptr<rtc::SSLCertificateStats> stats =
  384. identity->certificate().GetStats();
  385. EXPECT_EQ(stats->fingerprint, kRSA_FINGERPRINT);
  386. EXPECT_EQ(stats->fingerprint_algorithm, kRSA_FINGERPRINT_ALGORITHM);
  387. EXPECT_EQ(stats->base64_certificate, kRSA_BASE64_CERTIFICATE);
  388. EXPECT_FALSE(stats->issuer);
  389. }
  390. TEST_F(SSLIdentityTest, SSLCertificateGetStatsECDSA) {
  391. std::unique_ptr<SSLIdentity> identity(
  392. SSLIdentity::FromPEMStrings(kECDSA_PRIVATE_KEY_PEM, kECDSA_CERT_PEM));
  393. std::unique_ptr<rtc::SSLCertificateStats> stats =
  394. identity->certificate().GetStats();
  395. EXPECT_EQ(stats->fingerprint, kECDSA_FINGERPRINT);
  396. EXPECT_EQ(stats->fingerprint_algorithm, kECDSA_FINGERPRINT_ALGORITHM);
  397. EXPECT_EQ(stats->base64_certificate, kECDSA_BASE64_CERTIFICATE);
  398. EXPECT_FALSE(stats->issuer);
  399. }
  400. TEST_F(SSLIdentityTest, SSLCertificateGetStatsWithChain) {
  401. std::vector<std::string> ders;
  402. ders.push_back("every der results in");
  403. ders.push_back("an identity + certificate");
  404. ders.push_back("in a certificate chain");
  405. IdentityAndInfo info = CreateFakeIdentityAndInfoFromDers(ders);
  406. EXPECT_TRUE(info.identity);
  407. EXPECT_EQ(info.ders, ders);
  408. EXPECT_EQ(info.pems.size(), info.ders.size());
  409. EXPECT_EQ(info.fingerprints.size(), info.ders.size());
  410. std::unique_ptr<rtc::SSLCertificateStats> first_stats =
  411. info.identity->cert_chain().GetStats();
  412. rtc::SSLCertificateStats* cert_stats = first_stats.get();
  413. for (size_t i = 0; i < info.ders.size(); ++i) {
  414. EXPECT_EQ(cert_stats->fingerprint, info.fingerprints[i]);
  415. EXPECT_EQ(cert_stats->fingerprint_algorithm, "sha-1");
  416. EXPECT_EQ(cert_stats->base64_certificate, info.pems[i]);
  417. cert_stats = cert_stats->issuer.get();
  418. EXPECT_EQ(static_cast<bool>(cert_stats), i + 1 < info.ders.size());
  419. }
  420. }
  421. class SSLIdentityExpirationTest : public ::testing::Test {
  422. public:
  423. SSLIdentityExpirationTest() {
  424. // Set use of the test RNG to get deterministic expiration timestamp.
  425. rtc::SetRandomTestMode(true);
  426. }
  427. ~SSLIdentityExpirationTest() override {
  428. // Put it back for the next test.
  429. rtc::SetRandomTestMode(false);
  430. }
  431. void TestASN1TimeToSec() {
  432. struct asn_example {
  433. const char* string;
  434. bool long_format;
  435. int64_t want;
  436. } static const data[] = {
  437. // clang-format off
  438. // clang formatting breaks this nice alignment
  439. // Valid examples.
  440. {"19700101000000Z", true, 0},
  441. {"700101000000Z", false, 0},
  442. {"19700101000001Z", true, 1},
  443. {"700101000001Z", false, 1},
  444. {"19700101000100Z", true, 60},
  445. {"19700101000101Z", true, 61},
  446. {"19700101010000Z", true, 3600},
  447. {"19700101010001Z", true, 3601},
  448. {"19700101010100Z", true, 3660},
  449. {"19700101010101Z", true, 3661},
  450. {"710911012345Z", false, 53400225},
  451. {"20000101000000Z", true, 946684800},
  452. {"20000101000000Z", true, 946684800},
  453. {"20151130140156Z", true, 1448892116},
  454. {"151130140156Z", false, 1448892116},
  455. {"20491231235959Z", true, 2524607999},
  456. {"491231235959Z", false, 2524607999},
  457. {"20500101000000Z", true, 2524607999+1},
  458. {"20700101000000Z", true, 3155760000},
  459. {"21000101000000Z", true, 4102444800},
  460. {"24000101000000Z", true, 13569465600},
  461. // Invalid examples.
  462. {"19700101000000", true, -1}, // missing Z long format
  463. {"19700101000000X", true, -1}, // X instead of Z long format
  464. {"197001010000000", true, -1}, // 0 instead of Z long format
  465. {"1970010100000000Z", true, -1}, // excess digits long format
  466. {"700101000000", false, -1}, // missing Z short format
  467. {"700101000000X", false, -1}, // X instead of Z short format
  468. {"7001010000000", false, -1}, // 0 instead of Z short format
  469. {"70010100000000Z", false, -1}, // excess digits short format
  470. {":9700101000000Z", true, -1}, // invalid character
  471. {"1:700101000001Z", true, -1}, // invalid character
  472. {"19:00101000100Z", true, -1}, // invalid character
  473. {"197:0101000101Z", true, -1}, // invalid character
  474. {"1970:101010000Z", true, -1}, // invalid character
  475. {"19700:01010001Z", true, -1}, // invalid character
  476. {"197001:1010100Z", true, -1}, // invalid character
  477. {"1970010:010101Z", true, -1}, // invalid character
  478. {"70010100:000Z", false, -1}, // invalid character
  479. {"700101000:01Z", false, -1}, // invalid character
  480. {"2000010100:000Z", true, -1}, // invalid character
  481. {"21000101000:00Z", true, -1}, // invalid character
  482. {"240001010000:0Z", true, -1}, // invalid character
  483. {"500101000000Z", false, -1}, // but too old for epoch
  484. {"691231235959Z", false, -1}, // too old for epoch
  485. {"19611118043000Z", false, -1}, // way too old for epoch
  486. // clang-format off
  487. };
  488. unsigned char buf[20];
  489. // Run all examples and check for the expected result.
  490. for (const auto& entry : data) {
  491. size_t length = strlen(entry.string);
  492. memcpy(buf, entry.string, length); // Copy the ASN1 string...
  493. buf[length] = rtc::CreateRandomId(); // ...and terminate it with junk.
  494. int64_t res = rtc::ASN1TimeToSec(buf, length, entry.long_format);
  495. RTC_LOG(LS_VERBOSE) << entry.string;
  496. ASSERT_EQ(entry.want, res);
  497. }
  498. // Run all examples again, but with an invalid length.
  499. for (const auto& entry : data) {
  500. size_t length = strlen(entry.string);
  501. memcpy(buf, entry.string, length); // Copy the ASN1 string...
  502. buf[length] = rtc::CreateRandomId(); // ...and terminate it with junk.
  503. int64_t res = rtc::ASN1TimeToSec(buf, length - 1, entry.long_format);
  504. RTC_LOG(LS_VERBOSE) << entry.string;
  505. ASSERT_EQ(-1, res);
  506. }
  507. }
  508. void TestExpireTime(int times) {
  509. // We test just ECDSA here since what we're out to exercise is the
  510. // interfaces for expiration setting and reading.
  511. for (int i = 0; i < times; i++) {
  512. // We limit the time to < 2^31 here, i.e., we stay before 2038, since else
  513. // we hit time offset limitations in OpenSSL on some 32-bit systems.
  514. time_t time_before_generation = time(nullptr);
  515. time_t lifetime =
  516. rtc::CreateRandomId() % (0x80000000 - time_before_generation);
  517. rtc::KeyParams key_params = rtc::KeyParams::ECDSA(rtc::EC_NIST_P256);
  518. SSLIdentity* identity =
  519. rtc::SSLIdentity::GenerateWithExpiration("", key_params, lifetime);
  520. time_t time_after_generation = time(nullptr);
  521. EXPECT_LE(time_before_generation + lifetime,
  522. identity->certificate().CertificateExpirationTime());
  523. EXPECT_GE(time_after_generation + lifetime,
  524. identity->certificate().CertificateExpirationTime());
  525. delete identity;
  526. }
  527. }
  528. };
  529. TEST_F(SSLIdentityExpirationTest, TestASN1TimeToSec) {
  530. TestASN1TimeToSec();
  531. }
  532. TEST_F(SSLIdentityExpirationTest, TestExpireTime) {
  533. TestExpireTime(500);
  534. }