ct_test_util.cc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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
  3. // found in the LICENSE file.
  4. #include "net/test/ct_test_util.h"
  5. #include <stdint.h>
  6. #include <string.h>
  7. #include <vector>
  8. #include "base/base64.h"
  9. #include "base/strings/string_number_conversions.h"
  10. #include "base/strings/string_piece.h"
  11. #include "base/strings/string_util.h"
  12. #include "base/strings/stringprintf.h"
  13. #include "net/base/hex_utils.h"
  14. #include "net/cert/ct_serialization.h"
  15. #include "net/cert/merkle_tree_leaf.h"
  16. #include "net/cert/signed_tree_head.h"
  17. #include "net/cert/x509_certificate.h"
  18. namespace net::ct {
  19. namespace {
  20. // The following test vectors are from
  21. // http://code.google.com/p/certificate-transparency
  22. const char kDefaultDerCert[] =
  23. "308202ca30820233a003020102020106300d06092a864886f70d01010505003055310b3009"
  24. "06035504061302474231243022060355040a131b4365727469666963617465205472616e73"
  25. "706172656e6379204341310e300c0603550408130557616c65733110300e06035504071307"
  26. "4572772057656e301e170d3132303630313030303030305a170d3232303630313030303030"
  27. "305a3052310b30090603550406130247423121301f060355040a1318436572746966696361"
  28. "7465205472616e73706172656e6379310e300c0603550408130557616c65733110300e0603"
  29. "55040713074572772057656e30819f300d06092a864886f70d010101050003818d00308189"
  30. "02818100b1fa37936111f8792da2081c3fe41925008531dc7f2c657bd9e1de4704160b4c9f"
  31. "19d54ada4470404c1c51341b8f1f7538dddd28d9aca48369fc5646ddcc7617f8168aae5b41"
  32. "d43331fca2dadfc804d57208949061f9eef902ca47ce88c644e000f06eeeccabdc9dd2f68a"
  33. "22ccb09dc76e0dbc73527765b1a37a8c676253dcc10203010001a381ac3081a9301d060355"
  34. "1d0e041604146a0d982a3b62c44b6d2ef4e9bb7a01aa9cb798e2307d0603551d2304763074"
  35. "80145f9d880dc873e654d4f80dd8e6b0c124b447c355a159a4573055310b30090603550406"
  36. "1302474231243022060355040a131b4365727469666963617465205472616e73706172656e"
  37. "6379204341310e300c0603550408130557616c65733110300e060355040713074572772057"
  38. "656e82010030090603551d1304023000300d06092a864886f70d010105050003818100171c"
  39. "d84aac414a9a030f22aac8f688b081b2709b848b4e5511406cd707fed028597a9faefc2eee"
  40. "2978d633aaac14ed3235197da87e0f71b8875f1ac9e78b281749ddedd007e3ecf50645f8cb"
  41. "f667256cd6a1647b5e13203bb8582de7d6696f656d1c60b95f456b7fcf338571908f1c6972"
  42. "7d24c4fccd249295795814d1dac0e6";
  43. const char kDefaultIssuerKeyHash[] =
  44. "02adddca08b8bf9861f035940c940156d8350fdff899a6239c6bd77255b8f8fc";
  45. const char kDefaultDerTbsCert[] =
  46. "30820233a003020102020107300d06092a864886f70d01010505003055310b300906035504"
  47. "061302474231243022060355040a131b4365727469666963617465205472616e7370617265"
  48. "6e6379204341310e300c0603550408130557616c65733110300e0603550407130745727720"
  49. "57656e301e170d3132303630313030303030305a170d3232303630313030303030305a3052"
  50. "310b30090603550406130247423121301f060355040a131843657274696669636174652054"
  51. "72616e73706172656e6379310e300c0603550408130557616c65733110300e060355040713"
  52. "074572772057656e30819f300d06092a864886f70d010101050003818d0030818902818100"
  53. "beef98e7c26877ae385f75325a0c1d329bedf18faaf4d796bf047eb7e1ce15c95ba2f80ee4"
  54. "58bd7db86f8a4b252191a79bd700c38e9c0389b45cd4dc9a120ab21e0cb41cd0e72805a410"
  55. "cd9c5bdb5d4927726daf1710f60187377ea25b1a1e39eed0b88119dc154dc68f7da8e30caf"
  56. "158a33e6c9509f4a05b01409ff5dd87eb50203010001a381ac3081a9301d0603551d0e0416"
  57. "04142031541af25c05ffd8658b6843794f5e9036f7b4307d0603551d230476307480145f9d"
  58. "880dc873e654d4f80dd8e6b0c124b447c355a159a4573055310b3009060355040613024742"
  59. "31243022060355040a131b4365727469666963617465205472616e73706172656e63792043"
  60. "41310e300c0603550408130557616c65733110300e060355040713074572772057656e8201"
  61. "0030090603551d1304023000";
  62. const char kDefaultExtensions[] = "666f6f626172"; // "foobar"
  63. const char kTestDigitallySigned[] =
  64. "0403004730450220606e10ae5c2d5a1b0aed49dc4937f48de71a4e9784e9c208dfbfe9ef53"
  65. "6cf7f2022100beb29c72d7d06d61d06bdb38a069469aa86fe12e18bb7cc45689a2c0187ef5"
  66. "a5";
  67. const char kTestSignedCertificateTimestamp[] =
  68. "00df1c2ec11500945247a96168325ddc5c7959e8f7c6d388fc002e0bbd3f74d7640000013d"
  69. "db27ded900000403004730450220606e10ae5c2d5a1b0aed49dc4937f48de71a4e9784e9c2"
  70. "08dfbfe9ef536cf7f2022100beb29c72d7d06d61d06bdb38a069469aa86fe12e18bb7cc456"
  71. "89a2c0187ef5a5";
  72. const char kEcP256PublicKey[] =
  73. "3059301306072a8648ce3d020106082a8648ce3d0301070342000499783cb14533c0161a5a"
  74. "b45bf95d08a29cd0ea8dd4c84274e2be59ad15c676960cf0afa1074a57ac644b23479e5b3f"
  75. "b7b245eb4b420ef370210371a944beaceb";
  76. const char kTestKeyId[] =
  77. "df1c2ec11500945247a96168325ddc5c7959e8f7c6d388fc002e0bbd3f74d764";
  78. const int64_t kTestTimestamp = INT64_C(1396877277237);
  79. const char kTestSCTSignatureData[] =
  80. "30450220606e10ae5c2d5a1b0aed49dc4937f48de71a4e9784e9c208dfbfe9ef536cf7f202"
  81. "2100beb29c72d7d06d61d06bdb38a069469aa86fe12e18bb7cc45689a2c0187ef5a5";
  82. const char kTestSCTPrecertSignatureData[] =
  83. "30450220482f6751af35dba65436be1fd6640f3dbf9a41429495924530288fa3e5e23e0602"
  84. "2100e4edc0db3ac572b1e2f5e8ab6a680653987dcf41027dfeffa105519d89edbf08";
  85. // A well-formed OCSP response with fake SCT contents. Does not come from
  86. // http://code.google.com/p/certificate-transparency, does not pertain to any
  87. // of the test certs here, and is only used to test extracting the extension
  88. // contents from the response.
  89. const char kFakeOCSPResponse[] =
  90. "3082016e0a0100a08201673082016306092b060105050730010104820154308201503081ba"
  91. "a21604144edfdf5ff9c90ffacfca66e7fbc436bc39ee3fc7180f3230313030313031303630"
  92. "3030305a30818e30818b3049300906052b0e03021a050004141833a1e6a4f09577cca0e64c"
  93. "e7d145ca4b93700904144edfdf5ff9c90ffacfca66e7fbc436bc39ee3fc7021001aef99bde"
  94. "e0bb58c6f2b816bc3ae02f8000180f32303130303130313036303030305aa011180f323033"
  95. "30303130313036303030305aa11830163014060a2b06010401d67902040504060404746573"
  96. "74300d06092a864886f70d0101050500038181003586ffcf0794e64eb643d52a3d570a1c93"
  97. "836395986a2f792dd4e9c70b05161186c55c1658e0607dc9ec0d0924ac37fb99506c870579"
  98. "634be1de62ba2fced5f61f3b428f959fcee9bddf6f268c8e14c14fdf3b447786e638a5c8cc"
  99. "b610893df17a60e4cff30f4780aeffe0086ef19910f0d9cd7414bc93d1945686f88ad0a3c3"
  100. ;
  101. const char kFakeOCSPResponseCert[] =
  102. "3082022930820192a003020102021001aef99bdee0bb58c6f2b816bc3ae02f300d06092a86"
  103. "4886f70d01010505003015311330110603550403130a54657374696e67204341301e170d31"
  104. "30303130313036303030305a170d3332313230313036303030305a30373112301006035504"
  105. "0313093132372e302e302e31310b300906035504061302585831143012060355040a130b54"
  106. "657374696e67204f726730819d300d06092a864886f70d010101050003818b003081870281"
  107. "8100a71998f2930bfe73d031a87f133d2f378eeeeed52a77e44d0fc9ff6f07ff32cbf3da99"
  108. "9de4ed65832afcb0807f98787506539d258a0ce3c2c77967653099a9034a9b115a876c39a8"
  109. "c4e4ed4acd0c64095946fb39eeeb47a0704dbb018acf48c3a1c4b895fc409fb4a340a986b1"
  110. "afc45519ab9eca47c30185c771c64aa5ecf07d020103a35a3058303a06082b060105050701"
  111. "01010100042b3029302706082b06010505073001861b687474703a2f2f3132372e302e302e"
  112. "313a35353038312f6f637370301a0603551d200101000410300e300c060a2b06010401d679"
  113. "020401300d06092a864886f70d01010505000381810065e04fadd3484197f3412479d917e1"
  114. "9d8f7db57b526f2d0e4c046f86cebe643bf568ea0cd6570b228842aa057c6a7c79f209dfcd"
  115. "3419a4d93b1ecfb1c0224f33083c7d4da023499fbd00d81d6711ad58ffcf65f1545247fe9d"
  116. "83203425fd706b4fc5e797002af3d88151be5901eef56ec30aacdfc404be1bd35865ff1943"
  117. "2516";
  118. const char kFakeOCSPResponseIssuerCert[] =
  119. "308201d13082013aa003020102020101300d06092a864886f70d0101050500301531133011"
  120. "0603550403130a54657374696e67204341301e170d3130303130313036303030305a170d33"
  121. "32313230313036303030305a3015311330110603550403130a54657374696e672043413081"
  122. "9d300d06092a864886f70d010101050003818b0030818702818100a71998f2930bfe73d031"
  123. "a87f133d2f378eeeeed52a77e44d0fc9ff6f07ff32cbf3da999de4ed65832afcb0807f9878"
  124. "7506539d258a0ce3c2c77967653099a9034a9b115a876c39a8c4e4ed4acd0c64095946fb39"
  125. "eeeb47a0704dbb018acf48c3a1c4b895fc409fb4a340a986b1afc45519ab9eca47c30185c7"
  126. "71c64aa5ecf07d020103a333303130120603551d130101ff040830060101ff020100301b06"
  127. "03551d200101000411300f300d060b2b06010401d6790201ce0f300d06092a864886f70d01"
  128. "01050500038181003f4936f8d00e83fbdde331f2c64335dcf7dec8b1a2597683edeed61af0"
  129. "fa862412fad848938fe7ab77f1f9a43671ff6fdb729386e26f49e7aca0c0ea216e5970d933"
  130. "3ea1e11df2ccb357a5fed5220f9c6239e8946b9b7517707631d51ab996833d58a022cff5a6"
  131. "2169ac9258ec110efee78da9ab4a641e3b3c9ee5e8bd291460";
  132. const char kFakeOCSPExtensionValue[] = "74657374"; // "test"
  133. // For the sample STH
  134. const char kSampleSTHSHA256RootHash[] =
  135. "726467216167397babca293dca398e4ce6b621b18b9bc42f30c900d1f92ac1e4";
  136. const char kSampleSTHTreeHeadSignature[] =
  137. "0403004730450220365a91a2a88f2b9332f41d8959fa7086da7e6d634b7b089bc9da066426"
  138. "6c7a20022100e38464f3c0fd066257b982074f7ac87655e0c8f714768a050b4be9a7b441cb"
  139. "d3";
  140. size_t kSampleSTHTreeSize = 21u;
  141. } // namespace
  142. void GetX509CertSignedEntry(SignedEntryData* entry) {
  143. entry->type = ct::SignedEntryData::LOG_ENTRY_TYPE_X509;
  144. entry->leaf_certificate = HexDecode(kDefaultDerCert);
  145. }
  146. void GetX509CertTreeLeaf(MerkleTreeLeaf* tree_leaf) {
  147. tree_leaf->timestamp = base::Time::FromJsTime(kTestTimestamp);
  148. GetX509CertSignedEntry(&tree_leaf->signed_entry);
  149. tree_leaf->extensions = HexDecode(kDefaultExtensions);
  150. }
  151. std::string GetDerEncodedX509Cert() {
  152. return HexDecode(kDefaultDerCert);
  153. }
  154. void GetPrecertSignedEntry(SignedEntryData* entry) {
  155. entry->type = ct::SignedEntryData::LOG_ENTRY_TYPE_PRECERT;
  156. std::string issuer_hash(HexDecode(kDefaultIssuerKeyHash));
  157. memcpy(entry->issuer_key_hash.data, issuer_hash.data(), issuer_hash.size());
  158. entry->tbs_certificate = HexDecode(kDefaultDerTbsCert);
  159. }
  160. void GetPrecertTreeLeaf(MerkleTreeLeaf* tree_leaf) {
  161. tree_leaf->timestamp = base::Time::FromJsTime(kTestTimestamp);
  162. GetPrecertSignedEntry(&tree_leaf->signed_entry);
  163. tree_leaf->extensions = HexDecode(kDefaultExtensions);
  164. }
  165. std::string GetTestDigitallySigned() {
  166. return HexDecode(kTestDigitallySigned);
  167. }
  168. std::string GetTestSignedCertificateTimestamp() {
  169. return HexDecode(kTestSignedCertificateTimestamp);
  170. }
  171. std::string GetTestPublicKey() {
  172. return HexDecode(kEcP256PublicKey);
  173. }
  174. std::string GetTestPublicKeyId() {
  175. return HexDecode(kTestKeyId);
  176. }
  177. void GetX509CertSCT(scoped_refptr<SignedCertificateTimestamp>* sct_ref) {
  178. CHECK(sct_ref != nullptr);
  179. *sct_ref = base::MakeRefCounted<SignedCertificateTimestamp>();
  180. SignedCertificateTimestamp *const sct(sct_ref->get());
  181. sct->version = ct::SignedCertificateTimestamp::V1;
  182. sct->log_id = HexDecode(kTestKeyId);
  183. // Time the log issued a SCT for this certificate, which is
  184. // Fri Apr 5 10:04:16.089 2013
  185. sct->timestamp =
  186. base::Time::UnixEpoch() + base::Milliseconds(INT64_C(1365181456089));
  187. sct->extensions.clear();
  188. sct->signature.hash_algorithm = ct::DigitallySigned::HASH_ALGO_SHA256;
  189. sct->signature.signature_algorithm = ct::DigitallySigned::SIG_ALGO_ECDSA;
  190. sct->signature.signature_data = HexDecode(kTestSCTSignatureData);
  191. }
  192. void GetPrecertSCT(scoped_refptr<SignedCertificateTimestamp>* sct_ref) {
  193. CHECK(sct_ref != nullptr);
  194. *sct_ref = base::MakeRefCounted<SignedCertificateTimestamp>();
  195. SignedCertificateTimestamp *const sct(sct_ref->get());
  196. sct->version = ct::SignedCertificateTimestamp::V1;
  197. sct->log_id = HexDecode(kTestKeyId);
  198. // Time the log issued a SCT for this Precertificate, which is
  199. // Fri Apr 5 10:04:16.275 2013
  200. sct->timestamp =
  201. base::Time::UnixEpoch() + base::Milliseconds(INT64_C(1365181456275));
  202. sct->extensions.clear();
  203. sct->signature.hash_algorithm = ct::DigitallySigned::HASH_ALGO_SHA256;
  204. sct->signature.signature_algorithm = ct::DigitallySigned::SIG_ALGO_ECDSA;
  205. sct->signature.signature_data = HexDecode(kTestSCTPrecertSignatureData);
  206. }
  207. std::string GetDefaultIssuerKeyHash() {
  208. return HexDecode(kDefaultIssuerKeyHash);
  209. }
  210. std::string GetDerEncodedFakeOCSPResponse() {
  211. return HexDecode(kFakeOCSPResponse);
  212. }
  213. std::string GetFakeOCSPExtensionValue() {
  214. return HexDecode(kFakeOCSPExtensionValue);
  215. }
  216. std::string GetDerEncodedFakeOCSPResponseCert() {
  217. return HexDecode(kFakeOCSPResponseCert);
  218. }
  219. std::string GetDerEncodedFakeOCSPResponseIssuerCert() {
  220. return HexDecode(kFakeOCSPResponseIssuerCert);
  221. }
  222. // A sample, valid STH
  223. bool GetSampleSignedTreeHead(SignedTreeHead* sth) {
  224. sth->version = SignedTreeHead::V1;
  225. sth->timestamp = base::Time::UnixEpoch() + base::Milliseconds(kTestTimestamp);
  226. sth->tree_size = kSampleSTHTreeSize;
  227. std::string sha256_root_hash = GetSampleSTHSHA256RootHash();
  228. memcpy(sth->sha256_root_hash, sha256_root_hash.c_str(), kSthRootHashLength);
  229. sth->log_id = GetTestPublicKeyId();
  230. return GetSampleSTHTreeHeadDecodedSignature(&(sth->signature));
  231. }
  232. bool GetSampleEmptySignedTreeHead(SignedTreeHead* sth) {
  233. sth->version = SignedTreeHead::V1;
  234. sth->timestamp =
  235. base::Time::UnixEpoch() + base::Milliseconds(INT64_C(1450443594920));
  236. sth->tree_size = 0;
  237. std::string empty_root_hash = HexDecode(
  238. "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");
  239. memcpy(sth->sha256_root_hash, empty_root_hash.c_str(), kSthRootHashLength);
  240. sth->log_id = GetTestPublicKeyId();
  241. std::string tree_head_signature = HexDecode(
  242. "040300463044022046c26401de9416403da54762dc1f1687c38eafd791b15e484ab4c5f7"
  243. "f52721fe02201bf537a3bbea47109fc76c2273fe0f3349f493a07de9335c266330105fb0"
  244. "2a4a");
  245. base::StringPiece sp(tree_head_signature);
  246. return DecodeDigitallySigned(&sp, &(sth->signature)) && sp.empty();
  247. }
  248. bool GetBadEmptySignedTreeHead(SignedTreeHead* sth) {
  249. sth->version = SignedTreeHead::V1;
  250. sth->timestamp =
  251. base::Time::UnixEpoch() + base::Milliseconds(INT64_C(1450870952897));
  252. sth->tree_size = 0;
  253. memset(sth->sha256_root_hash, 'f', kSthRootHashLength);
  254. sth->log_id = GetTestPublicKeyId();
  255. std::string tree_head_signature = HexDecode(
  256. "04030046304402207cab04c62dee5d1cbc95fec30cd8417313f71587b75f133ad2e6f324"
  257. "74f164d702205e2f3a9bce46f87d7e20e951a4e955da3cb502f8717a22fabd7c5d7e1bef"
  258. "46ea");
  259. base::StringPiece sp(tree_head_signature);
  260. return DecodeDigitallySigned(&sp, &(sth->signature)) && sp.empty();
  261. }
  262. std::string GetSampleSTHSHA256RootHash() {
  263. return HexDecode(kSampleSTHSHA256RootHash);
  264. }
  265. std::string GetSampleSTHTreeHeadSignature() {
  266. return HexDecode(kSampleSTHTreeHeadSignature);
  267. }
  268. bool GetSampleSTHTreeHeadDecodedSignature(DigitallySigned* signature) {
  269. std::string tree_head_signature = HexDecode(kSampleSTHTreeHeadSignature);
  270. base::StringPiece sp(tree_head_signature);
  271. return DecodeDigitallySigned(&sp, signature) && sp.empty();
  272. }
  273. std::string GetSampleSTHAsJson() {
  274. return CreateSignedTreeHeadJsonString(kSampleSTHTreeSize, kTestTimestamp,
  275. GetSampleSTHSHA256RootHash(),
  276. GetSampleSTHTreeHeadSignature());
  277. }
  278. std::string CreateSignedTreeHeadJsonString(size_t tree_size,
  279. int64_t timestamp,
  280. std::string sha256_root_hash,
  281. std::string tree_head_signature) {
  282. std::string sth_json =
  283. std::string("{\"tree_size\":") + base::NumberToString(tree_size) +
  284. std::string(",\"timestamp\":") + base::NumberToString(timestamp);
  285. if (!sha256_root_hash.empty()) {
  286. std::string root_hash_b64;
  287. base::Base64Encode(sha256_root_hash, &root_hash_b64);
  288. sth_json += base::StringPrintf(",\"sha256_root_hash\":\"%s\"",
  289. root_hash_b64.c_str());
  290. }
  291. if (!tree_head_signature.empty()) {
  292. std::string tree_head_signature_b64;
  293. base::Base64Encode(tree_head_signature, &tree_head_signature_b64);
  294. sth_json += base::StringPrintf(",\"tree_head_signature\":\"%s\"",
  295. tree_head_signature_b64.c_str());
  296. }
  297. sth_json += "}";
  298. return sth_json;
  299. }
  300. std::string CreateConsistencyProofJsonString(
  301. const std::vector<std::string>& raw_nodes) {
  302. std::string consistency_proof_json = std::string("{\"consistency\":[");
  303. for (auto it = raw_nodes.begin(); it != raw_nodes.end(); ++it) {
  304. std::string proof_node_b64;
  305. base::Base64Encode(*it, &proof_node_b64);
  306. consistency_proof_json +=
  307. base::StringPrintf("\"%s\"", proof_node_b64.c_str());
  308. if (it + 1 != raw_nodes.end())
  309. consistency_proof_json += std::string(",");
  310. }
  311. consistency_proof_json += std::string("]}");
  312. return consistency_proof_json;
  313. }
  314. std::string GetSCTListForTesting() {
  315. const std::string sct = ct::GetTestSignedCertificateTimestamp();
  316. std::string sct_list;
  317. ct::EncodeSCTListForTesting(sct, &sct_list);
  318. return sct_list;
  319. }
  320. std::string GetSCTListWithInvalidSCT() {
  321. std::string sct(ct::GetTestSignedCertificateTimestamp());
  322. // Change a byte inside the Log ID part of the SCT so it does not match the
  323. // log used in the tests.
  324. sct[15] = 't';
  325. std::string sct_list;
  326. ct::EncodeSCTListForTesting(sct, &sct_list);
  327. return sct_list;
  328. }
  329. bool CheckForSingleVerifiedSCTInResult(
  330. const SignedCertificateTimestampAndStatusList& scts,
  331. const std::string& log_description) {
  332. return (scts.size() == 1 && scts[0].status == ct::SCT_STATUS_OK &&
  333. scts[0].sct->log_description == log_description);
  334. }
  335. bool CheckForSCTOrigin(const SignedCertificateTimestampAndStatusList& scts,
  336. ct::SignedCertificateTimestamp::Origin origin) {
  337. for (const auto& sct_and_status : scts)
  338. if (sct_and_status.status == SCT_STATUS_OK &&
  339. sct_and_status.sct->origin == origin)
  340. return true;
  341. return false;
  342. }
  343. } // namespace net::ct