| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/cert/ct_serialization.h" | 5 #include "net/cert/ct_serialization.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
| 10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 | 26 |
| 27 protected: | 27 protected: |
| 28 std::string test_digitally_signed_; | 28 std::string test_digitally_signed_; |
| 29 }; | 29 }; |
| 30 | 30 |
| 31 TEST_F(CtSerializationTest, DecodesDigitallySigned) { | 31 TEST_F(CtSerializationTest, DecodesDigitallySigned) { |
| 32 base::StringPiece digitally_signed(test_digitally_signed_); | 32 base::StringPiece digitally_signed(test_digitally_signed_); |
| 33 ct::DigitallySigned parsed; | 33 ct::DigitallySigned parsed; |
| 34 | 34 |
| 35 ASSERT_TRUE(ct::DecodeDigitallySigned(&digitally_signed, &parsed)); | 35 ASSERT_TRUE(ct::DecodeDigitallySigned(&digitally_signed, &parsed)); |
| 36 EXPECT_EQ( | 36 EXPECT_EQ(ct::DigitallySigned::HASH_ALGO_SHA256, parsed.hash_algorithm); |
| 37 ct::DigitallySigned::HASH_ALGO_SHA256, | |
| 38 parsed.hash_algorithm); | |
| 39 | 37 |
| 40 EXPECT_EQ( | 38 EXPECT_EQ(ct::DigitallySigned::SIG_ALGO_ECDSA, parsed.signature_algorithm); |
| 41 ct::DigitallySigned::SIG_ALGO_ECDSA, | |
| 42 parsed.signature_algorithm); | |
| 43 | 39 |
| 44 // The encoded data contains the signature itself from the 4th byte. | 40 // The encoded data contains the signature itself from the 4th byte. |
| 45 // The first bytes are: | 41 // The first bytes are: |
| 46 // 1 byte of hash algorithm | 42 // 1 byte of hash algorithm |
| 47 // 1 byte of signature algorithm | 43 // 1 byte of signature algorithm |
| 48 // 2 bytes - prefix containing length of the signature data. | 44 // 2 bytes - prefix containing length of the signature data. |
| 49 EXPECT_EQ( | 45 EXPECT_EQ(test_digitally_signed_.substr(4), parsed.signature_data); |
| 50 test_digitally_signed_.substr(4), | |
| 51 parsed.signature_data); | |
| 52 } | 46 } |
| 53 | 47 |
| 54 | |
| 55 TEST_F(CtSerializationTest, FailsToDecodePartialDigitallySigned) { | 48 TEST_F(CtSerializationTest, FailsToDecodePartialDigitallySigned) { |
| 56 base::StringPiece digitally_signed(test_digitally_signed_); | 49 base::StringPiece digitally_signed(test_digitally_signed_); |
| 57 base::StringPiece partial_digitally_signed( | 50 base::StringPiece partial_digitally_signed( |
| 58 digitally_signed.substr(0, test_digitally_signed_.size() - 5)); | 51 digitally_signed.substr(0, test_digitally_signed_.size() - 5)); |
| 59 ct::DigitallySigned parsed; | 52 ct::DigitallySigned parsed; |
| 60 | 53 |
| 61 ASSERT_FALSE(ct::DecodeDigitallySigned(&partial_digitally_signed, &parsed)); | 54 ASSERT_FALSE(ct::DecodeDigitallySigned(&partial_digitally_signed, &parsed)); |
| 62 } | 55 } |
| 63 | 56 |
| 64 | |
| 65 TEST_F(CtSerializationTest, EncodesDigitallySigned) { | 57 TEST_F(CtSerializationTest, EncodesDigitallySigned) { |
| 66 ct::DigitallySigned digitally_signed; | 58 ct::DigitallySigned digitally_signed; |
| 67 digitally_signed.hash_algorithm = ct::DigitallySigned::HASH_ALGO_SHA256; | 59 digitally_signed.hash_algorithm = ct::DigitallySigned::HASH_ALGO_SHA256; |
| 68 digitally_signed.signature_algorithm = ct::DigitallySigned::SIG_ALGO_ECDSA; | 60 digitally_signed.signature_algorithm = ct::DigitallySigned::SIG_ALGO_ECDSA; |
| 69 digitally_signed.signature_data = test_digitally_signed_.substr(4); | 61 digitally_signed.signature_data = test_digitally_signed_.substr(4); |
| 70 | 62 |
| 71 std::string encoded; | 63 std::string encoded; |
| 72 | 64 |
| 73 ASSERT_TRUE(ct::EncodeDigitallySigned(digitally_signed, &encoded)); | 65 ASSERT_TRUE(ct::EncodeDigitallySigned(digitally_signed, &encoded)); |
| 74 EXPECT_EQ(test_digitally_signed_, encoded); | 66 EXPECT_EQ(test_digitally_signed_, encoded); |
| 75 } | 67 } |
| 76 | 68 |
| 77 | |
| 78 TEST_F(CtSerializationTest, EncodesLogEntryForX509Cert) { | 69 TEST_F(CtSerializationTest, EncodesLogEntryForX509Cert) { |
| 79 ct::LogEntry entry; | 70 ct::LogEntry entry; |
| 80 GetX509CertLogEntry(&entry); | 71 GetX509CertLogEntry(&entry); |
| 81 | 72 |
| 82 std::string encoded; | 73 std::string encoded; |
| 83 ASSERT_TRUE(ct::EncodeLogEntry(entry, &encoded)); | 74 ASSERT_TRUE(ct::EncodeLogEntry(entry, &encoded)); |
| 84 EXPECT_EQ((718U + 5U), encoded.size()); | 75 EXPECT_EQ((718U + 5U), encoded.size()); |
| 85 // First two bytes are log entry type. Next, length: | 76 // First two bytes are log entry type. Next, length: |
| 86 // Length is 718 which is 512 + 206, which is 0x2ce | 77 // Length is 718 which is 512 + 206, which is 0x2ce |
| 87 std::string expected_prefix("\0\0\0\x2\xCE", 5); | 78 std::string expected_prefix("\0\0\0\x2\xCE", 5); |
| 88 // Note we use std::string comparison rather than ASSERT_STREQ due | 79 // Note we use std::string comparison rather than ASSERT_STREQ due |
| 89 // to null characters in the buffer. | 80 // to null characters in the buffer. |
| 90 EXPECT_EQ(expected_prefix, encoded.substr(0, 5)); | 81 EXPECT_EQ(expected_prefix, encoded.substr(0, 5)); |
| 91 } | 82 } |
| 92 | 83 |
| 93 TEST_F(CtSerializationTest, EncodesV1SCTSignedData) { | 84 TEST_F(CtSerializationTest, EncodesV1SCTSignedData) { |
| 94 base::Time timestamp = base::Time::UnixEpoch() + | 85 base::Time timestamp = base::Time::UnixEpoch() + |
| 95 base::TimeDelta::FromMilliseconds(1348589665525); | 86 base::TimeDelta::FromMilliseconds(1348589665525); |
| 96 std::string dummy_entry("abc"); | 87 std::string dummy_entry("abc"); |
| 97 std::string empty_extensions; | 88 std::string empty_extensions; |
| 98 // For now, no known failure cases. | 89 // For now, no known failure cases. |
| 99 std::string encoded; | 90 std::string encoded; |
| 100 ASSERT_TRUE(ct::EncodeV1SCTSignedData( | 91 ASSERT_TRUE(ct::EncodeV1SCTSignedData( |
| 101 timestamp, | 92 timestamp, dummy_entry, empty_extensions, &encoded)); |
| 102 dummy_entry, | 93 EXPECT_EQ((size_t)15, encoded.size()); |
| 103 empty_extensions, | |
| 104 &encoded)); | |
| 105 EXPECT_EQ((size_t) 15, encoded.size()); | |
| 106 // Byte 0 is version, byte 1 is signature type | 94 // Byte 0 is version, byte 1 is signature type |
| 107 // Bytes 2-10 are timestamp | 95 // Bytes 2-10 are timestamp |
| 108 // Bytes 11-14 are the log signature | 96 // Bytes 11-14 are the log signature |
| 109 // Byte 15 is the empty extension | 97 // Byte 15 is the empty extension |
| 110 //EXPECT_EQ(0, timestamp.ToTimeT()); | 98 // EXPECT_EQ(0, timestamp.ToTimeT()); |
| 111 std::string expected_buffer( | 99 std::string expected_buffer( |
| 112 "\x0\x0\x0\x0\x1\x39\xFE\x35\x3C\xF5\x61\x62\x63\x0\x0", 15); | 100 "\x0\x0\x0\x0\x1\x39\xFE\x35\x3C\xF5\x61\x62\x63\x0\x0", 15); |
| 113 EXPECT_EQ(expected_buffer, encoded); | 101 EXPECT_EQ(expected_buffer, encoded); |
| 114 } | 102 } |
| 115 | 103 |
| 116 TEST_F(CtSerializationTest, DecodesSCTList) { | 104 TEST_F(CtSerializationTest, DecodesSCTList) { |
| 117 // Two items in the list: "abc", "def" | 105 // Two items in the list: "abc", "def" |
| 118 base::StringPiece encoded("\x0\xa\x0\x3\x61\x62\x63\x0\x3\x64\x65\x66", 12); | 106 base::StringPiece encoded("\x0\xa\x0\x3\x61\x62\x63\x0\x3\x64\x65\x66", 12); |
| 119 std::vector<base::StringPiece> decoded; | 107 std::vector<base::StringPiece> decoded; |
| 120 | 108 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 133 | 121 |
| 134 TEST_F(CtSerializationTest, DecodesSignedCertificateTimestamp) { | 122 TEST_F(CtSerializationTest, DecodesSignedCertificateTimestamp) { |
| 135 std::string encoded_test_sct(ct::GetTestSignedCertificateTimestamp()); | 123 std::string encoded_test_sct(ct::GetTestSignedCertificateTimestamp()); |
| 136 base::StringPiece encoded_sct(encoded_test_sct); | 124 base::StringPiece encoded_sct(encoded_test_sct); |
| 137 | 125 |
| 138 scoped_refptr<ct::SignedCertificateTimestamp> sct; | 126 scoped_refptr<ct::SignedCertificateTimestamp> sct; |
| 139 ASSERT_TRUE(ct::DecodeSignedCertificateTimestamp(&encoded_sct, &sct)); | 127 ASSERT_TRUE(ct::DecodeSignedCertificateTimestamp(&encoded_sct, &sct)); |
| 140 EXPECT_EQ(0, sct->version); | 128 EXPECT_EQ(0, sct->version); |
| 141 EXPECT_EQ(ct::GetTestPublicKeyId(), sct->log_id); | 129 EXPECT_EQ(ct::GetTestPublicKeyId(), sct->log_id); |
| 142 base::Time expected_time = base::Time::UnixEpoch() + | 130 base::Time expected_time = base::Time::UnixEpoch() + |
| 143 base::TimeDelta::FromMilliseconds(1365181456089); | 131 base::TimeDelta::FromMilliseconds(1365181456089); |
| 144 EXPECT_EQ(expected_time, sct->timestamp); | 132 EXPECT_EQ(expected_time, sct->timestamp); |
| 145 // Subtracting 4 bytes for signature data (hash & sig algs), | 133 // Subtracting 4 bytes for signature data (hash & sig algs), |
| 146 // actual signature data should be 71 bytes. | 134 // actual signature data should be 71 bytes. |
| 147 EXPECT_EQ((size_t) 71, sct->signature.signature_data.size()); | 135 EXPECT_EQ((size_t)71, sct->signature.signature_data.size()); |
| 148 EXPECT_TRUE(sct->extensions.empty()); | 136 EXPECT_TRUE(sct->extensions.empty()); |
| 149 } | 137 } |
| 150 | 138 |
| 151 TEST_F(CtSerializationTest, FailsDecodingInvalidSignedCertificateTimestamp) { | 139 TEST_F(CtSerializationTest, FailsDecodingInvalidSignedCertificateTimestamp) { |
| 152 // Invalid version | 140 // Invalid version |
| 153 base::StringPiece invalid_version_sct("\x2\x0", 2); | 141 base::StringPiece invalid_version_sct("\x2\x0", 2); |
| 154 scoped_refptr<ct::SignedCertificateTimestamp> sct; | 142 scoped_refptr<ct::SignedCertificateTimestamp> sct; |
| 155 | 143 |
| 156 ASSERT_FALSE( | 144 ASSERT_FALSE( |
| 157 ct::DecodeSignedCertificateTimestamp(&invalid_version_sct, &sct)); | 145 ct::DecodeSignedCertificateTimestamp(&invalid_version_sct, &sct)); |
| 158 | 146 |
| 159 // Valid version, invalid length (missing data) | 147 // Valid version, invalid length (missing data) |
| 160 base::StringPiece invalid_length_sct("\x0\xa\xb\xc", 4); | 148 base::StringPiece invalid_length_sct("\x0\xa\xb\xc", 4); |
| 161 ASSERT_FALSE( | 149 ASSERT_FALSE(ct::DecodeSignedCertificateTimestamp(&invalid_length_sct, &sct)); |
| 162 ct::DecodeSignedCertificateTimestamp(&invalid_length_sct, &sct)); | |
| 163 } | 150 } |
| 164 | 151 |
| 165 } // namespace net | 152 } // namespace net |
| 166 | |
| OLD | NEW |