Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /** |
Varun Patil | a24bd3e | 2020-11-24 10:08:33 +0530 | [diff] [blame^] | 3 | * Copyright (C) 2020 Regents of the University of California. |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 4 | * @author: Yingdi Yu <yingdi@cs.ucla.edu> |
| 5 | * See COPYING for copyright and distribution information. |
| 6 | */ |
| 7 | |
Varun Patil | a24bd3e | 2020-11-24 10:08:33 +0530 | [diff] [blame^] | 8 | #include "endorse-certificate.hpp" |
| 9 | #include "cryptopp.hpp" |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 10 | |
| 11 | #include <boost/test/unit_test.hpp> |
Qiuhan Ding | 52f1383 | 2015-03-06 14:05:59 -0800 | [diff] [blame] | 12 | #include <boost/filesystem.hpp> |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 13 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 14 | #include <ndn-cxx/security/key-chain.hpp> |
| 15 | #include <ndn-cxx/encoding/buffer-stream.hpp> |
| 16 | #include <ndn-cxx/util/time.hpp> |
| 17 | #include <ndn-cxx/util/io.hpp> |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 18 | |
Qiuhan Ding | 52f1383 | 2015-03-06 14:05:59 -0800 | [diff] [blame] | 19 | namespace chronochat { |
| 20 | namespace tests { |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 21 | |
| 22 | using std::vector; |
| 23 | using std::string; |
| 24 | |
| 25 | using ndn::KeyChain; |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 26 | using ndn::security::Certificate; |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 27 | |
| 28 | BOOST_AUTO_TEST_SUITE(TestEndorseCertificate) |
| 29 | |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 30 | std::string |
| 31 | getTestFile(std::string path) { |
| 32 | std::ifstream t(path); |
| 33 | std::stringstream buffer; |
| 34 | buffer << t.rdbuf(); |
| 35 | return buffer.str(); |
| 36 | } |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 37 | |
| 38 | BOOST_AUTO_TEST_CASE(IdCert) |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 39 | { |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 40 | const std::string testIdCert = getTestFile("test/cert/testid.cert"); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 41 | boost::iostreams::stream<boost::iostreams::array_source> is(testIdCert.c_str(), |
| 42 | testIdCert.size()); |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 43 | std::shared_ptr<Certificate> idCert = ndn::io::load<Certificate>(is); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 44 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 45 | BOOST_CHECK(static_cast<bool>(idCert)); |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 46 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 47 | BOOST_CHECK_EQUAL(idCert->getName().toUri(), |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 48 | "/EndorseCertificateTests/KEY/6%C7%E3%06%EC%8CB%3D/self/%FD%00%00%01u%D55a%B3"); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | BOOST_AUTO_TEST_CASE(ConstructFromIdCert) |
| 52 | { |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 53 | const std::string testIdCert = getTestFile("test/cert/testid.cert"); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 54 | boost::iostreams::stream<boost::iostreams::array_source> is(testIdCert.c_str(), |
| 55 | testIdCert.size()); |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 56 | std::shared_ptr<Certificate> idCert = ndn::io::load<Certificate>(is); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 57 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 58 | Profile profile(*idCert); |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 59 | vector<string> endorseList; |
| 60 | endorseList.push_back("email"); |
| 61 | endorseList.push_back("homepage"); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 62 | EndorseCertificate endorseCertificate(*idCert, profile, endorseList); |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 63 | |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 64 | KeyChain keyChain("pib-memory:", "tpm-memory:"); |
Qiuhan Ding | 52f1383 | 2015-03-06 14:05:59 -0800 | [diff] [blame] | 65 | |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 66 | auto signOpts = ndn::security::SigningInfo(ndn::security::SigningInfo::SignerType::SIGNER_TYPE_SHA256); |
| 67 | keyChain.sign(endorseCertificate, signOpts.setSignatureInfo(endorseCertificate.getSignatureInfo())); |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 68 | const Block& endorseDataBlock = endorseCertificate.wireEncode(); |
| 69 | |
| 70 | Data decodedEndorseData; |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 71 | decodedEndorseData.wireDecode(endorseDataBlock); |
| 72 | EndorseCertificate decodedEndorse(decodedEndorseData); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 73 | BOOST_CHECK_EQUAL(decodedEndorse.getProfile().get("IDENTITY"), |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 74 | "/EndorseCertificateTests"); |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 75 | BOOST_CHECK_EQUAL(decodedEndorse.getEndorseList().size(), 2); |
| 76 | BOOST_CHECK_EQUAL(decodedEndorse.getEndorseList().at(0), "email"); |
| 77 | BOOST_CHECK_EQUAL(decodedEndorse.getEndorseList().at(1), "homepage"); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 78 | BOOST_CHECK_EQUAL(decodedEndorse.getSigner(), |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 79 | "/EndorseCertificateTests/KEY/6%C7%E3%06%EC%8CB%3D"); |
| 80 | BOOST_CHECK_EQUAL(decodedEndorse.getKeyName(), |
| 81 | "/EndorseCertificateTests/PROFILE-CERT/KEY/6%C7%E3%06%EC%8CB%3D"); |
| 82 | |
| 83 | const std::string testIdKey = getTestFile("test/cert/testid.key"); |
| 84 | ndn::OBufferStream keyOs; |
| 85 | { |
| 86 | using namespace CryptoPP; |
| 87 | StringSource(testIdKey, true, new Base64Decoder(new FileSink(keyOs))); |
| 88 | } |
| 89 | BOOST_CHECK(idCert->getPublicKey() == *keyOs.buf()); |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 90 | } |
| 91 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 92 | BOOST_AUTO_TEST_CASE(ConstructFromEndorseCert) |
| 93 | { |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 94 | const std::string testEndorseCert = getTestFile("test/cert/endorse.cert"); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 95 | boost::iostreams::stream<boost::iostreams::array_source> is(testEndorseCert.c_str(), |
| 96 | testEndorseCert.size()); |
| 97 | shared_ptr<Data> rawData = ndn::io::load<Data>(is); |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 98 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 99 | EndorseCertificate rawEndorse(*rawData); |
| 100 | vector<string> endorseList; |
| 101 | endorseList.push_back("institution"); |
| 102 | endorseList.push_back("group"); |
| 103 | endorseList.push_back("advisor"); |
| 104 | Name signer("/EndorseCertificateTests/Singer/ksk-1234567890"); |
| 105 | EndorseCertificate endorseCertificate(rawEndorse, signer, endorseList); |
| 106 | |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 107 | KeyChain keyChain("pib-memory:", "tpm-memory:"); |
Qiuhan Ding | 52f1383 | 2015-03-06 14:05:59 -0800 | [diff] [blame] | 108 | |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 109 | auto signOpts = ndn::security::SigningInfo(ndn::security::SigningInfo::SignerType::SIGNER_TYPE_SHA256); |
| 110 | keyChain.sign(endorseCertificate, signOpts.setSignatureInfo(endorseCertificate.getSignatureInfo())); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 111 | |
| 112 | const Block& endorseDataBlock = endorseCertificate.wireEncode(); |
| 113 | |
| 114 | Data decodedEndorseData; |
| 115 | decodedEndorseData.wireDecode(endorseDataBlock); |
| 116 | EndorseCertificate decodedEndorse(decodedEndorseData); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 117 | BOOST_CHECK_EQUAL(decodedEndorse.getProfile().get("IDENTITY"), |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 118 | "/EndorseCertificateTests"); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 119 | BOOST_CHECK_EQUAL(decodedEndorse.getEndorseList().size(), 3); |
| 120 | BOOST_CHECK_EQUAL(decodedEndorse.getEndorseList().at(0), "institution"); |
| 121 | BOOST_CHECK_EQUAL(decodedEndorse.getEndorseList().at(1), "group"); |
| 122 | BOOST_CHECK_EQUAL(decodedEndorse.getEndorseList().at(2), "advisor"); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 123 | BOOST_CHECK_EQUAL(decodedEndorse.getSigner(), |
| 124 | "/EndorseCertificateTests/Singer/ksk-1234567890"); |
Varun Patil | 3d85090 | 2020-11-23 12:19:14 +0530 | [diff] [blame] | 125 | BOOST_CHECK_EQUAL(decodedEndorse.getKeyName(), |
| 126 | "/EndorseCertificateTests/KEY/6%C7%E3%06%EC%8CB%3D"); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 127 | } |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 128 | |
| 129 | BOOST_AUTO_TEST_SUITE_END() |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 130 | |
Qiuhan Ding | 52f1383 | 2015-03-06 14:05:59 -0800 | [diff] [blame] | 131 | } // namespace tests |
| 132 | } // namespace chronochat |