| 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/files/file_path.h" | 9 #include "base/files/file_path.h" |
| 10 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 72 digitally_signed.hash_algorithm = ct::DigitallySigned::HASH_ALGO_SHA256; | 72 digitally_signed.hash_algorithm = ct::DigitallySigned::HASH_ALGO_SHA256; |
| 73 digitally_signed.signature_algorithm = ct::DigitallySigned::SIG_ALGO_ECDSA; | 73 digitally_signed.signature_algorithm = ct::DigitallySigned::SIG_ALGO_ECDSA; |
| 74 digitally_signed.signature_data = test_digitally_signed_.substr(4); | 74 digitally_signed.signature_data = test_digitally_signed_.substr(4); |
| 75 | 75 |
| 76 std::string encoded; | 76 std::string encoded; |
| 77 | 77 |
| 78 ASSERT_TRUE(ct::EncodeDigitallySigned(digitally_signed, &encoded)); | 78 ASSERT_TRUE(ct::EncodeDigitallySigned(digitally_signed, &encoded)); |
| 79 EXPECT_EQ(test_digitally_signed_, encoded); | 79 EXPECT_EQ(test_digitally_signed_, encoded); |
| 80 } | 80 } |
| 81 | 81 |
| 82 | 82 TEST_F(CtSerializationTest, EncodesSignedEntryForX509Cert) { |
| 83 TEST_F(CtSerializationTest, EncodesLogEntryForX509Cert) { | 83 ct::SignedEntryData entry; |
| 84 ct::LogEntry entry; | 84 ct::GetX509CertSignedEntry(&entry); |
| 85 ct::GetX509CertLogEntry(&entry); | |
| 86 | 85 |
| 87 std::string encoded; | 86 std::string encoded; |
| 88 ASSERT_TRUE(ct::EncodeLogEntry(entry, &encoded)); | 87 ASSERT_TRUE(ct::EncodeSignedEntry(entry, &encoded)); |
| 89 EXPECT_EQ((718U + 5U), encoded.size()); | 88 EXPECT_EQ((718U + 5U), encoded.size()); |
| 90 // First two bytes are log entry type. Next, length: | 89 // First two bytes are log entry type. Next, length: |
| 91 // Length is 718 which is 512 + 206, which is 0x2ce | 90 // Length is 718 which is 512 + 206, which is 0x2ce |
| 92 std::string expected_prefix("\0\0\0\x2\xCE", 5); | 91 std::string expected_prefix("\0\0\0\x2\xCE", 5); |
| 93 // Note we use std::string comparison rather than ASSERT_STREQ due | 92 // Note we use std::string comparison rather than ASSERT_STREQ due |
| 94 // to null characters in the buffer. | 93 // to null characters in the buffer. |
| 95 EXPECT_EQ(expected_prefix, encoded.substr(0, 5)); | 94 EXPECT_EQ(expected_prefix, encoded.substr(0, 5)); |
| 96 } | 95 } |
| 97 | 96 |
| 98 TEST_F(CtSerializationTest, EncodesLogEntryForPrecert) { | 97 TEST_F(CtSerializationTest, EncodesSignedEntryForPrecert) { |
| 99 ct::LogEntry entry; | 98 ct::SignedEntryData entry; |
| 100 ct::GetPrecertLogEntry(&entry); | 99 ct::GetPrecertSignedEntry(&entry); |
| 101 | 100 |
| 102 std::string encoded; | 101 std::string encoded; |
| 103 ASSERT_TRUE(ct::EncodeLogEntry(entry, &encoded)); | 102 ASSERT_TRUE(ct::EncodeSignedEntry(entry, &encoded)); |
| 104 EXPECT_EQ(604u, encoded.size()); | 103 EXPECT_EQ(604u, encoded.size()); |
| 105 // First two bytes are the log entry type. | 104 // First two bytes are the log entry type. |
| 106 EXPECT_EQ(std::string("\x00\x01", 2), encoded.substr(0, 2)); | 105 EXPECT_EQ(std::string("\x00\x01", 2), encoded.substr(0, 2)); |
| 107 // Next comes the 32-byte issuer key hash | 106 // Next comes the 32-byte issuer key hash |
| 108 EXPECT_THAT(encoded.substr(2, 32), | 107 EXPECT_THAT(encoded.substr(2, 32), |
| 109 ElementsAreArray(entry.issuer_key_hash.data)); | 108 ElementsAreArray(entry.issuer_key_hash.data)); |
| 110 // Then the length of the TBS cert (604 bytes = 0x237) | 109 // Then the length of the TBS cert (604 bytes = 0x237) |
| 111 EXPECT_EQ(std::string("\x00\x02\x37", 3), encoded.substr(34, 3)); | 110 EXPECT_EQ(std::string("\x00\x02\x37", 3), encoded.substr(34, 3)); |
| 112 // Then the TBS cert itself | 111 // Then the TBS cert itself |
| 113 EXPECT_EQ(entry.tbs_certificate, encoded.substr(37)); | 112 EXPECT_EQ(entry.tbs_certificate, encoded.substr(37)); |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 196 "Version encoded incorrectly"; | 195 "Version encoded incorrectly"; |
| 197 EXPECT_EQ(std::string("\x00", 1), encoded.substr(1, 1)) << | 196 EXPECT_EQ(std::string("\x00", 1), encoded.substr(1, 1)) << |
| 198 "Merkle tree leaf type encoded incorrectly"; | 197 "Merkle tree leaf type encoded incorrectly"; |
| 199 EXPECT_EQ(std::string("\x00\x00\x01\x45\x3c\x5f\xb8\x35", 8), | 198 EXPECT_EQ(std::string("\x00\x00\x01\x45\x3c\x5f\xb8\x35", 8), |
| 200 encoded.substr(2, 8)) << | 199 encoded.substr(2, 8)) << |
| 201 "Timestamp encoded incorrectly"; | 200 "Timestamp encoded incorrectly"; |
| 202 EXPECT_EQ(std::string("\x00\x00", 2), encoded.substr(10, 2)) << | 201 EXPECT_EQ(std::string("\x00\x00", 2), encoded.substr(10, 2)) << |
| 203 "Log entry type encoded incorrectly"; | 202 "Log entry type encoded incorrectly"; |
| 204 EXPECT_EQ(std::string("\x00\x02\xce", 3), encoded.substr(12, 3)) << | 203 EXPECT_EQ(std::string("\x00\x02\xce", 3), encoded.substr(12, 3)) << |
| 205 "Certificate length encoded incorrectly"; | 204 "Certificate length encoded incorrectly"; |
| 206 EXPECT_EQ(tree_leaf.log_entry.leaf_certificate, encoded.substr(15, 718)) << | 205 EXPECT_EQ(tree_leaf.signed_entry.leaf_certificate, encoded.substr(15, 718)) |
| 207 "Certificate encoded incorrectly"; | 206 << "Certificate encoded incorrectly"; |
| 208 EXPECT_EQ(std::string("\x00\x06", 2), encoded.substr(733, 2)) << | 207 EXPECT_EQ(std::string("\x00\x06", 2), encoded.substr(733, 2)) << |
| 209 "CT extensions length encoded incorrectly"; | 208 "CT extensions length encoded incorrectly"; |
| 210 EXPECT_EQ(tree_leaf.extensions, encoded.substr(735, 6)) << | 209 EXPECT_EQ(tree_leaf.extensions, encoded.substr(735, 6)) << |
| 211 "CT extensions encoded incorrectly"; | 210 "CT extensions encoded incorrectly"; |
| 212 } | 211 } |
| 213 | 212 |
| 214 TEST_F(CtSerializationTest, EncodesMerkleTreeLeafForPrecert) { | 213 TEST_F(CtSerializationTest, EncodesMerkleTreeLeafForPrecert) { |
| 215 ct::MerkleTreeLeaf tree_leaf; | 214 ct::MerkleTreeLeaf tree_leaf; |
| 216 ct::GetPrecertTreeLeaf(&tree_leaf); | 215 ct::GetPrecertTreeLeaf(&tree_leaf); |
| 217 | 216 |
| 218 std::string encoded; | 217 std::string encoded; |
| 219 ASSERT_TRUE(ct::EncodeTreeLeaf(tree_leaf, &encoded)); | 218 ASSERT_TRUE(ct::EncodeTreeLeaf(tree_leaf, &encoded)); |
| 220 EXPECT_EQ(622u, encoded.size()) << "Merkle tree leaf encoded incorrectly"; | 219 EXPECT_EQ(622u, encoded.size()) << "Merkle tree leaf encoded incorrectly"; |
| 221 EXPECT_EQ(std::string("\x00", 1), encoded.substr(0, 1)) << | 220 EXPECT_EQ(std::string("\x00", 1), encoded.substr(0, 1)) << |
| 222 "Version encoded incorrectly"; | 221 "Version encoded incorrectly"; |
| 223 EXPECT_EQ(std::string("\x00", 1), encoded.substr(1, 1)) << | 222 EXPECT_EQ(std::string("\x00", 1), encoded.substr(1, 1)) << |
| 224 "Merkle tree leaf type encoded incorrectly"; | 223 "Merkle tree leaf type encoded incorrectly"; |
| 225 EXPECT_EQ(std::string("\x00\x00\x01\x45\x3c\x5f\xb8\x35", 8), | 224 EXPECT_EQ(std::string("\x00\x00\x01\x45\x3c\x5f\xb8\x35", 8), |
| 226 encoded.substr(2, 8)) << | 225 encoded.substr(2, 8)) << |
| 227 "Timestamp encoded incorrectly"; | 226 "Timestamp encoded incorrectly"; |
| 228 EXPECT_EQ(std::string("\x00\x01", 2), encoded.substr(10, 2)) << | 227 EXPECT_EQ(std::string("\x00\x01", 2), encoded.substr(10, 2)) << |
| 229 "Log entry type encoded incorrectly"; | 228 "Log entry type encoded incorrectly"; |
| 230 EXPECT_THAT(encoded.substr(12, 32), | 229 EXPECT_THAT(encoded.substr(12, 32), |
| 231 ElementsAreArray(tree_leaf.log_entry.issuer_key_hash.data)) << | 230 ElementsAreArray(tree_leaf.signed_entry.issuer_key_hash.data)) |
| 232 "Issuer key hash encoded incorrectly"; | 231 << "Issuer key hash encoded incorrectly"; |
| 233 EXPECT_EQ(std::string("\x00\x02\x37", 3), encoded.substr(44, 3)) << | 232 EXPECT_EQ(std::string("\x00\x02\x37", 3), encoded.substr(44, 3)) << |
| 234 "TBS certificate length encoded incorrectly"; | 233 "TBS certificate length encoded incorrectly"; |
| 235 EXPECT_EQ(tree_leaf.log_entry.tbs_certificate, encoded.substr(47, 567)) << | 234 EXPECT_EQ(tree_leaf.signed_entry.tbs_certificate, encoded.substr(47, 567)) |
| 236 "TBS certificate encoded incorrectly"; | 235 << "TBS certificate encoded incorrectly"; |
| 237 EXPECT_EQ(std::string("\x00\x06", 2), encoded.substr(614, 2)) << | 236 EXPECT_EQ(std::string("\x00\x06", 2), encoded.substr(614, 2)) << |
| 238 "CT extensions length encoded incorrectly"; | 237 "CT extensions length encoded incorrectly"; |
| 239 EXPECT_EQ(tree_leaf.extensions, encoded.substr(616, 6)) << | 238 EXPECT_EQ(tree_leaf.extensions, encoded.substr(616, 6)) << |
| 240 "CT extensions encoded incorrectly"; | 239 "CT extensions encoded incorrectly"; |
| 241 } | 240 } |
| 242 | 241 |
| 243 TEST_F(CtSerializationTest, EncodesValidSignedTreeHead) { | 242 TEST_F(CtSerializationTest, EncodesValidSignedTreeHead) { |
| 244 ct::SignedTreeHead signed_tree_head; | 243 ct::SignedTreeHead signed_tree_head; |
| 245 ASSERT_TRUE(GetSampleSignedTreeHead(&signed_tree_head)); | 244 ASSERT_TRUE(GetSampleSignedTreeHead(&signed_tree_head)); |
| 246 | 245 |
| 247 std::string encoded; | 246 std::string encoded; |
| 248 ct::EncodeTreeHeadSignature(signed_tree_head, &encoded); | 247 ct::EncodeTreeHeadSignature(signed_tree_head, &encoded); |
| 249 // Expected size is 50 bytes: | 248 // Expected size is 50 bytes: |
| 250 // Byte 0 is version, byte 1 is signature type | 249 // Byte 0 is version, byte 1 is signature type |
| 251 // Bytes 2-9 are timestamp | 250 // Bytes 2-9 are timestamp |
| 252 // Bytes 10-17 are tree size | 251 // Bytes 10-17 are tree size |
| 253 // Bytes 18-49 are sha256 root hash | 252 // Bytes 18-49 are sha256 root hash |
| 254 ASSERT_EQ(50u, encoded.length()); | 253 ASSERT_EQ(50u, encoded.length()); |
| 255 std::string expected_buffer( | 254 std::string expected_buffer( |
| 256 "\x0\x1\x0\x0\x1\x45\x3c\x5f\xb8\x35\x0\x0\x0\x0\x0\x0\x0\x15", 18); | 255 "\x0\x1\x0\x0\x1\x45\x3c\x5f\xb8\x35\x0\x0\x0\x0\x0\x0\x0\x15", 18); |
| 257 expected_buffer.append(ct::GetSampleSTHSHA256RootHash()); | 256 expected_buffer.append(ct::GetSampleSTHSHA256RootHash()); |
| 258 ASSERT_EQ(expected_buffer, encoded); | 257 ASSERT_EQ(expected_buffer, encoded); |
| 259 } | 258 } |
| 260 | 259 |
| 261 } // namespace net | 260 } // namespace net |
| OLD | NEW |