/* * Copyright 2011 The WebRTC Project Authors. All rights reserved. * * Use of this source code is governed by a BSD-style license * that can be found in the LICENSE file in the root of the source * tree. An additional intellectual property rights grant can be found * in the file PATENTS. All contributing project authors may * be found in the AUTHORS file in the root of the source tree.
*/
// These PEM strings were created by generating an identity with // `SSLIdentity::Create` and invoking `identity->PrivateKeyToPEMString()`, // `identity->PublicKeyToPEMString()` and // `identity->certificate().ToPEMString()`. If the crypto library is updated, // and the update changes the string form of the keys, these will have to be // updated too. The fingerprint, fingerprint algorithm and base64 certificate // were created by calling `identity->certificate().GetStats()`. staticconstchar kRSA_PRIVATE_KEY_PEM[] = "-----BEGIN PRIVATE KEY-----\n" "MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAMQPqDStRlYeDpkX\n" "erRmv+a1naM8vSVSY0gG2plnrnofViWRW3MRqWC+020MsIj3hPZeSAnt/y/FL/nr\n" "4Ea7NXcwdRo1/1xEK7U/f/cjSg1aunyvHCHwcFcMr31HLFvHr0ZgcFwbgIuFLNEl\n" "7kK5HMO9APz1ntUjek8BmBj8yMl9AgMBAAECgYA8FWBC5GcNtSBcIinkZyigF0A7\n" "6j081sa+J/uNz4xUuI257ZXM6biygUhhvuXK06/XoIULJfhyN0fAm1yb0HtNhiUs\n" "kMOYeon6b8FqFaPjrQf7Gr9FMiIHXNK19uegTMKztXyPZoUWlX84X0iawY95x0Y3\n" "73f6P2rN2UOjlVVjAQJBAOKy3l2w3Zj2w0oAJox0eMwl+RxBNt1C42SHrob2mFUT\n" "rytpVVYOasr8CoDI0kjacjI94sLum+buJoXXX6YTGO0CQQDdZwlYIEkoS3ftfxPa\n" "Ai0YTBzAWvHJg0r8Gk/TkHo6IM+LSsZ9ZYUv/vBe4BKLw1I4hZ+bQvBiq+f8ROtk\n" "+TDRAkAPL3ghwoU1h+IRBO2QHwUwd6K2N9AbBi4BP+168O3HVSg4ujeTKigRLMzv\n" "T4R2iNt5bhfQgvdCgtVlxcWMdF8JAkBwDCg3eEdt5BuyjwBt8XH+/O4ED0KUWCTH\n" "x00k5dZlupsuhE5Fwe4QpzXg3gekwdnHjyCCQ/NCDHvgOMTkmhQxAkA9V03KRX9b\n" "bhvEzY/fu8gEp+EzsER96/D79az5z1BaMGL5OPM2xHBPJATKlswnAa7Lp3QKGZGk\n" "TxslfL18J71s\n" "-----END PRIVATE KEY-----\n"; staticconstchar kRSA_PUBLIC_KEY_PEM[] = "-----BEGIN PUBLIC KEY-----\n" "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDED6g0rUZWHg6ZF3q0Zr/mtZ2j\n" "PL0lUmNIBtqZZ656H1YlkVtzEalgvtNtDLCI94T2XkgJ7f8vxS/56+BGuzV3MHUa\n" "Nf9cRCu1P3/3I0oNWrp8rxwh8HBXDK99Ryxbx69GYHBcG4CLhSzRJe5CuRzDvQD8\n" "9Z7VI3pPAZgY/MjJfQIDAQAB\n" "-----END PUBLIC KEY-----\n"; staticconstchar kRSA_CERT_PEM[] = "-----BEGIN CERTIFICATE-----\n" "MIIBnDCCAQWgAwIBAgIJAOEHLgeWYwrpMA0GCSqGSIb3DQEBCwUAMBAxDjAMBgNV\n" "BAMMBXRlc3QxMB4XDTE2MDQyNDE4MTAyMloXDTE2MDUyNTE4MTAyMlowEDEOMAwG\n" "A1UEAwwFdGVzdDEwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBAMQPqDStRlYe\n" "DpkXerRmv+a1naM8vSVSY0gG2plnrnofViWRW3MRqWC+020MsIj3hPZeSAnt/y/F\n" "L/nr4Ea7NXcwdRo1/1xEK7U/f/cjSg1aunyvHCHwcFcMr31HLFvHr0ZgcFwbgIuF\n" "LNEl7kK5HMO9APz1ntUjek8BmBj8yMl9AgMBAAEwDQYJKoZIhvcNAQELBQADgYEA\n" "C3ehaZFl+oEYN069C2ht/gMzuC77L854RF/x7xRtNZzkcg9TVgXXdM3auUvJi8dx\n" "yTpU3ixErjQvoZew5ngXTEvTY8BSQUijJEaLWh8n6NDKRbEGTdAk8nPAmq9hdCFq\n" "e3UkexqNHm3g/VxG4NUC1Y+w29ai0/Rgh+VvgbDwK+Q=\n" "-----END CERTIFICATE-----\n"; staticconstchar kRSA_FINGERPRINT[] = "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" ":2A:EE:F9:9A:DD:E2:2B"; staticconstchar kRSA_FINGERPRINT_ALGORITHM[] = "sha-256"; staticconstchar kRSA_BASE64_CERTIFICATE[] = "MIIBnDCCAQWgAwIBAgIJAOEHLgeWYwrpMA0GCSqGSIb3DQEBCwUAMBAxDjAMBgNVBAMMBXRlc3" "QxMB4XDTE2MDQyNDE4MTAyMloXDTE2MDUyNTE4MTAyMlowEDEOMAwGA1UEAwwFdGVzdDEwgZ8w" "DQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBAMQPqDStRlYeDpkXerRmv+a1naM8vSVSY0gG2plnrn" "ofViWRW3MRqWC+020MsIj3hPZeSAnt/y/FL/nr4Ea7NXcwdRo1/1xEK7U/f/cjSg1aunyvHCHw" "cFcMr31HLFvHr0ZgcFwbgIuFLNEl7kK5HMO9APz1ntUjek8BmBj8yMl9AgMBAAEwDQYJKoZIhv" "cNAQELBQADgYEAC3ehaZFl+oEYN069C2ht/gMzuC77L854RF/x7xRtNZzkcg9TVgXXdM3auUvJ" "i8dxyTpU3ixErjQvoZew5ngXTEvTY8BSQUijJEaLWh8n6NDKRbEGTdAk8nPAmq9hdCFqe3Ukex" "qNHm3g/VxG4NUC1Y+w29ai0/Rgh+VvgbDwK+Q=";
// The test certificate has an MD5-based signature.
ASSERT_TRUE(test_cert_->GetSignatureDigestAlgorithm(&digest_algorithm));
ASSERT_EQ(rtc::DIGEST_MD5, digest_algorithm);
}
// Sanity check that all four digests are unique. This could theoretically // fail, since cryptographic hash collisions have a non-zero probability. for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { if (i != j)
EXPECT_NE(0, memcmp(digest[i], digest[j], expected_len));
}
}
}
void TestCloningIdentity(const SSLIdentity& identity) { // Convert `identity` to PEM strings and create a new identity by converting // back from the string format.
std::string priv_pem = identity.PrivateKeyToPEMString();
std::string publ_pem = identity.PublicKeyToPEMString();
std::string cert_pem = identity.certificate().ToPEMString();
std::unique_ptr<SSLIdentity> clone =
SSLIdentity::CreateFromPEMStrings(priv_pem, cert_pem);
EXPECT_TRUE(clone);
// Make sure the clone is identical to the original.
EXPECT_TRUE(identity == *clone);
ASSERT_EQ(identity.certificate().CertificateExpirationTime(),
clone->certificate().CertificateExpirationTime());
// At this point we are confident that the identities are identical. To be // extra sure, we compare PEM strings of the clone with the original. Note // that the PEM strings of two identities are not strictly guaranteed to be // equal (they describe structs whose members could be listed in a different // order, for example). But because the same function is used to produce // both PEMs, its a good enough bet that this comparison will work. If the // assumption stops holding in the future we can always remove this from the // unittest.
std::string clone_priv_pem = clone->PrivateKeyToPEMString();
std::string clone_publ_pem = clone->PublicKeyToPEMString();
std::string clone_cert_pem = clone->certificate().ToPEMString();
ASSERT_EQ(priv_pem, clone_priv_pem);
ASSERT_EQ(publ_pem, clone_publ_pem);
ASSERT_EQ(cert_pem, clone_cert_pem);
}
// HASH_AlgSHA224 is not supported in the chromium linux build.
TEST_F(SSLIdentityTest, FixedDigestSHA224) {
TestDigestForFixedCert(rtc::DIGEST_SHA_224, SHA224_DIGEST_LENGTH,
kTestCertSha224);
}
// HASH_AlgSHA224 is not supported in the chromium linux build.
TEST_F(SSLIdentityTest, DigestSHA224) {
TestDigestForGeneratedCert(rtc::DIGEST_SHA_224, SHA224_DIGEST_LENGTH);
}
TEST_F(SSLIdentityTest, FromPEMChainStrings) { // This doesn't form a valid certificate chain, but that doesn't matter for // the purposes of the test
std::string chain(kRSA_CERT_PEM);
chain.append(kTestCertificate);
std::unique_ptr<SSLIdentity> identity(
SSLIdentity::CreateFromPEMChainStrings(kRSA_PRIVATE_KEY_PEM, chain));
EXPECT_TRUE(identity);
EXPECT_EQ(kRSA_PRIVATE_KEY_PEM, identity->PrivateKeyToPEMString());
EXPECT_EQ(kRSA_PUBLIC_KEY_PEM, identity->PublicKeyToPEMString());
ASSERT_EQ(2u, identity->cert_chain().GetSize());
EXPECT_EQ(kRSA_CERT_PEM, identity->cert_chain().Get(0).ToPEMString());
EXPECT_EQ(kTestCertificate, identity->cert_chain().Get(1).ToPEMString());
}
TEST_F(SSLIdentityTest, SSLCertificateGetStatsWithChain) {
std::vector<std::string> ders;
ders.push_back("every der results in");
ders.push_back("an identity + certificate");
ders.push_back("in a certificate chain");
IdentityAndInfo info = CreateFakeIdentityAndInfoFromDers(ders);
EXPECT_TRUE(info.identity);
EXPECT_EQ(info.ders, ders);
EXPECT_EQ(info.pems.size(), info.ders.size());
EXPECT_EQ(info.fingerprints.size(), info.ders.size());
std::unique_ptr<rtc::SSLCertificateStats> first_stats =
info.identity->cert_chain().GetStats();
rtc::SSLCertificateStats* cert_stats = first_stats.get(); for (size_t i = 0; i < info.ders.size(); ++i) {
EXPECT_EQ(cert_stats->fingerprint, info.fingerprints[i]);
EXPECT_EQ(cert_stats->fingerprint_algorithm, "sha-1");
EXPECT_EQ(cert_stats->base64_certificate, info.pems[i]);
cert_stats = cert_stats->issuer.get();
EXPECT_EQ(static_cast<bool>(cert_stats), i + 1 < info.ders.size());
}
}
class SSLIdentityExpirationTest : public ::testing::Test { public:
SSLIdentityExpirationTest() { // Set use of the test RNG to get deterministic expiration timestamp.
rtc::SetRandomTestMode(true);
}
~SSLIdentityExpirationTest() override { // Put it back for the next test.
rtc::SetRandomTestMode(false);
}
// Invalid examples.
{"19700101000000", true, -1}, // missing Z long format
{"19700101000000X", true, -1}, // X instead of Z long format
{"197001010000000", true, -1}, // 0 instead of Z long format
{"1970010100000000Z", true, -1}, // excess digits long format
{"700101000000", false, -1}, // missing Z short format
{"700101000000X", false, -1}, // X instead of Z short format
{"7001010000000", false, -1}, // 0 instead of Z short format
{"70010100000000Z", false, -1}, // excess digits short format
{":9700101000000Z", true, -1}, // invalid character
{"1:700101000001Z", true, -1}, // invalid character
{"19:00101000100Z", true, -1}, // invalid character
{"197:0101000101Z", true, -1}, // invalid character
{"1970:101010000Z", true, -1}, // invalid character
{"19700:01010001Z", true, -1}, // invalid character
{"197001:1010100Z", true, -1}, // invalid character
{"1970010:010101Z", true, -1}, // invalid character
{"70010100:000Z", false, -1}, // invalid character
{"700101000:01Z", false, -1}, // invalid character
{"2000010100:000Z", true, -1}, // invalid character
{"21000101000:00Z", true, -1}, // invalid character
{"240001010000:0Z", true, -1}, // invalid character
{"500101000000Z", false, -1}, // but too old for epoch
{"691231235959Z", false, -1}, // too old for epoch
{"19611118043000Z", false, -1}, // way too old for epoch
// clang-format off
};
unsignedchar buf[EVP_MAX_MD_SIZE];
// Run all examples and check for the expected result. for (constauto& entry : data) {
size_t length = strlen(entry.string);
memcpy(buf, entry.string, length); // Copy the ASN1 string...
buf[length] = rtc::CreateRandomId(); // ...and terminate it with junk.
int64_t res = rtc::ASN1TimeToSec(buf, length, entry.long_format);
RTC_LOG(LS_VERBOSE) << entry.string;
ASSERT_EQ(entry.want, res);
} // Run all examples again, but with an invalid length. for (constauto& entry : data) {
size_t length = strlen(entry.string);
memcpy(buf, entry.string, length); // Copy the ASN1 string...
buf[length] = rtc::CreateRandomId(); // ...and terminate it with junk.
int64_t res = rtc::ASN1TimeToSec(buf, length - 1, entry.long_format);
RTC_LOG(LS_VERBOSE) << entry.string;
ASSERT_EQ(-1, res);
}
}
void TestExpireTime(int times) { // We test just ECDSA here since what we're out to exercise is the // interfaces for expiration setting and reading. for (int i = 0; i < times; i++) { // We limit the time to < 2^31 here, i.e., we stay before 2038, since else // we hit time offset limitations in OpenSSL on some 32-bit systems.
time_t time_before_generation = time(nullptr);
time_t lifetime =
rtc::CreateRandomId() % (0x80000000 - time_before_generation);
rtc::KeyParams key_params = rtc::KeyParams::ECDSA(rtc::EC_NIST_P256); auto identity =
rtc::SSLIdentity::Create("", key_params, lifetime);
time_t time_after_generation = time(nullptr);
EXPECT_LE(time_before_generation + lifetime,
identity->certificate().CertificateExpirationTime());
EXPECT_GE(time_after_generation + lifetime,
identity->certificate().CertificateExpirationTime());
}
}
};
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.