blob: 6fef721a277eaf5ca306b5cdbc9a85831ff497ff [file] [log] [blame]
Yingdi Yufa4ce792014-02-06 18:09:22 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/**
Varun Patila24bd3e2020-11-24 10:08:33 +05303 * Copyright (C) 2020 Regents of the University of California.
Yingdi Yufa4ce792014-02-06 18:09:22 -08004 * @author: Yingdi Yu <yingdi@cs.ucla.edu>
5 * See COPYING for copyright and distribution information.
6 */
7
Varun Patila24bd3e2020-11-24 10:08:33 +05308#include "endorse-certificate.hpp"
9#include "cryptopp.hpp"
Yingdi Yufa4ce792014-02-06 18:09:22 -080010
11#include <boost/test/unit_test.hpp>
Qiuhan Ding52f13832015-03-06 14:05:59 -080012#include <boost/filesystem.hpp>
Yingdi Yufa4ce792014-02-06 18:09:22 -080013
Yingdi Yu0b0a7362014-08-05 16:31:30 -070014#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 Yufa4ce792014-02-06 18:09:22 -080018
Qiuhan Ding52f13832015-03-06 14:05:59 -080019namespace chronochat {
20namespace tests {
Yingdi Yu0b0a7362014-08-05 16:31:30 -070021
22using std::vector;
23using std::string;
24
25using ndn::KeyChain;
Varun Patil3d850902020-11-23 12:19:14 +053026using ndn::security::Certificate;
Yingdi Yufa4ce792014-02-06 18:09:22 -080027
28BOOST_AUTO_TEST_SUITE(TestEndorseCertificate)
29
Varun Patil3d850902020-11-23 12:19:14 +053030std::string
31getTestFile(std::string path) {
32 std::ifstream t(path);
33 std::stringstream buffer;
34 buffer << t.rdbuf();
35 return buffer.str();
36}
Yingdi Yu348f5ea2014-03-01 14:47:25 -080037
38BOOST_AUTO_TEST_CASE(IdCert)
Yingdi Yufa4ce792014-02-06 18:09:22 -080039{
Varun Patil3d850902020-11-23 12:19:14 +053040 const std::string testIdCert = getTestFile("test/cert/testid.cert");
Yingdi Yu0b0a7362014-08-05 16:31:30 -070041 boost::iostreams::stream<boost::iostreams::array_source> is(testIdCert.c_str(),
42 testIdCert.size());
Varun Patil3d850902020-11-23 12:19:14 +053043 std::shared_ptr<Certificate> idCert = ndn::io::load<Certificate>(is);
Yingdi Yu0b0a7362014-08-05 16:31:30 -070044
Yingdi Yu348f5ea2014-03-01 14:47:25 -080045 BOOST_CHECK(static_cast<bool>(idCert));
Yingdi Yufa4ce792014-02-06 18:09:22 -080046
Yingdi Yu0b0a7362014-08-05 16:31:30 -070047 BOOST_CHECK_EQUAL(idCert->getName().toUri(),
Varun Patil3d850902020-11-23 12:19:14 +053048 "/EndorseCertificateTests/KEY/6%C7%E3%06%EC%8CB%3D/self/%FD%00%00%01u%D55a%B3");
Yingdi Yu348f5ea2014-03-01 14:47:25 -080049}
50
51BOOST_AUTO_TEST_CASE(ConstructFromIdCert)
52{
Varun Patil3d850902020-11-23 12:19:14 +053053 const std::string testIdCert = getTestFile("test/cert/testid.cert");
Yingdi Yu0b0a7362014-08-05 16:31:30 -070054 boost::iostreams::stream<boost::iostreams::array_source> is(testIdCert.c_str(),
55 testIdCert.size());
Varun Patil3d850902020-11-23 12:19:14 +053056 std::shared_ptr<Certificate> idCert = ndn::io::load<Certificate>(is);
Yingdi Yu0b0a7362014-08-05 16:31:30 -070057
Yingdi Yu348f5ea2014-03-01 14:47:25 -080058 Profile profile(*idCert);
Yingdi Yufa4ce792014-02-06 18:09:22 -080059 vector<string> endorseList;
60 endorseList.push_back("email");
61 endorseList.push_back("homepage");
Yingdi Yu348f5ea2014-03-01 14:47:25 -080062 EndorseCertificate endorseCertificate(*idCert, profile, endorseList);
Yingdi Yufa4ce792014-02-06 18:09:22 -080063
Varun Patil3d850902020-11-23 12:19:14 +053064 KeyChain keyChain("pib-memory:", "tpm-memory:");
Qiuhan Ding52f13832015-03-06 14:05:59 -080065
Varun Patil3d850902020-11-23 12:19:14 +053066 auto signOpts = ndn::security::SigningInfo(ndn::security::SigningInfo::SignerType::SIGNER_TYPE_SHA256);
67 keyChain.sign(endorseCertificate, signOpts.setSignatureInfo(endorseCertificate.getSignatureInfo()));
Yingdi Yufa4ce792014-02-06 18:09:22 -080068 const Block& endorseDataBlock = endorseCertificate.wireEncode();
69
70 Data decodedEndorseData;
Yingdi Yufa4ce792014-02-06 18:09:22 -080071 decodedEndorseData.wireDecode(endorseDataBlock);
72 EndorseCertificate decodedEndorse(decodedEndorseData);
Yingdi Yu0b0a7362014-08-05 16:31:30 -070073 BOOST_CHECK_EQUAL(decodedEndorse.getProfile().get("IDENTITY"),
Varun Patil3d850902020-11-23 12:19:14 +053074 "/EndorseCertificateTests");
Yingdi Yufa4ce792014-02-06 18:09:22 -080075 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 Yu0b0a7362014-08-05 16:31:30 -070078 BOOST_CHECK_EQUAL(decodedEndorse.getSigner(),
Varun Patil3d850902020-11-23 12:19:14 +053079 "/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 Yufa4ce792014-02-06 18:09:22 -080090}
91
Yingdi Yu348f5ea2014-03-01 14:47:25 -080092BOOST_AUTO_TEST_CASE(ConstructFromEndorseCert)
93{
Varun Patil3d850902020-11-23 12:19:14 +053094 const std::string testEndorseCert = getTestFile("test/cert/endorse.cert");
Yingdi Yu0b0a7362014-08-05 16:31:30 -070095 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 Yufa4ce792014-02-06 18:09:22 -080098
Yingdi Yu348f5ea2014-03-01 14:47:25 -080099 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 Patil3d850902020-11-23 12:19:14 +0530107 KeyChain keyChain("pib-memory:", "tpm-memory:");
Qiuhan Ding52f13832015-03-06 14:05:59 -0800108
Varun Patil3d850902020-11-23 12:19:14 +0530109 auto signOpts = ndn::security::SigningInfo(ndn::security::SigningInfo::SignerType::SIGNER_TYPE_SHA256);
110 keyChain.sign(endorseCertificate, signOpts.setSignatureInfo(endorseCertificate.getSignatureInfo()));
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800111
112 const Block& endorseDataBlock = endorseCertificate.wireEncode();
113
114 Data decodedEndorseData;
115 decodedEndorseData.wireDecode(endorseDataBlock);
116 EndorseCertificate decodedEndorse(decodedEndorseData);
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700117 BOOST_CHECK_EQUAL(decodedEndorse.getProfile().get("IDENTITY"),
Varun Patil3d850902020-11-23 12:19:14 +0530118 "/EndorseCertificateTests");
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800119 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 Yu0b0a7362014-08-05 16:31:30 -0700123 BOOST_CHECK_EQUAL(decodedEndorse.getSigner(),
124 "/EndorseCertificateTests/Singer/ksk-1234567890");
Varun Patil3d850902020-11-23 12:19:14 +0530125 BOOST_CHECK_EQUAL(decodedEndorse.getKeyName(),
126 "/EndorseCertificateTests/KEY/6%C7%E3%06%EC%8CB%3D");
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800127}
Yingdi Yufa4ce792014-02-06 18:09:22 -0800128
129BOOST_AUTO_TEST_SUITE_END()
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700130
Qiuhan Ding52f13832015-03-06 14:05:59 -0800131} // namespace tests
132} // namespace chronochat