Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /** |
| 3 | * Copyright (C) 2013 Regents of the University of California. |
| 4 | * @author: Yingdi Yu <yingdi@cs.ucla.edu> |
| 5 | * See COPYING for copyright and distribution information. |
| 6 | */ |
| 7 | |
| 8 | #if __clang__ |
| 9 | #pragma clang diagnostic push |
| 10 | #pragma clang diagnostic ignored "-Wtautological-compare" |
| 11 | #pragma clang diagnostic ignored "-Wunused-function" |
| 12 | #elif __GNUC__ |
| 13 | #pragma GCC diagnostic ignored "-Wunused-function" |
| 14 | #endif |
| 15 | |
| 16 | #include <boost/test/unit_test.hpp> |
| 17 | |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 18 | #include <ndn-cpp-dev/security/key-chain.hpp> |
| 19 | #include <ndn-cpp-dev/util/time.hpp> |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 20 | #include <ndn-cpp-dev/util/io.hpp> |
| 21 | #include <cryptopp/base64.h> |
| 22 | #include <cryptopp/files.h> |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 23 | #include "endorse-certificate.h" |
| 24 | |
| 25 | using namespace ndn; |
| 26 | using namespace std; |
| 27 | using namespace chronos; |
| 28 | |
| 29 | BOOST_AUTO_TEST_SUITE(TestEndorseCertificate) |
| 30 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 31 | const string testIdCert("\ |
| 32 | Bv0DXwdRCBdFbmRvcnNlQ2VydGlmaWNhdGVUZXN0cwgDS0VZCAxFbmNvZGVEZWNv\ |
| 33 | ZGUIEWtzay0xMzk0MDcyMTQ3MzM1CAdJRC1DRVJUCAf9AUSVLNXoFAMYAQIV/QG8\ |
| 34 | MIIBuDAiGA8yMDE0MDMwNjAyMTU0N1oYDz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l3pjsVy+EHKmwSlZ+T8cy5qaIEHxhbOzMNNVdit7XEwexOE66AVza\ |
| 50 | 92On"); |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 51 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 52 | const string testKey("\ |
| 53 | MIIBIDANBgkqhkiG9w0BAQEFAAOCAQ0AMIIBCAKCAQEA2LFg9IsUBUX2LN+gRzbE\ |
| 54 | Tb+aLhC+vaGkul1/4bEDdQcuETSOnkhuQ6Wo7QMCtcvg1z8JCx3eUga78C80Xhe0\ |
| 55 | rKxdjm2sM51NeBimkHW5/nlSBEewlr0qSYR+cikuHwj0Tfm9TD/EEgy72mhrteU/\ |
| 56 | fHIFbHCBKhZC351kkG3TehJ6HYzh9uyZAQs/C8b/RmS64XyhszspUXy87wiMiF2J\ |
| 57 | eh1q6DvsUyUGj/pokmTVRsn+I2Ks+Vm0B+emvWY1JXU6YY7g2wY1KkGjVTs6Ck/h\ |
| 58 | +KofJp9/fWkPfwYzPuv1oK0sO/zDtlAoKGYckkGOB1as1FVVp2MDlDWD6Dktx3bx\ |
| 59 | iwIBEQ=="); |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 60 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 61 | const string testEndorseCert("\ |
| 62 | Bv0DOgePCBdFbmRvcnNlQ2VydGlmaWNhdGVUZXN0cwgMRW5jb2RlRGVjb2RlCBFr\ |
| 63 | c2stMTM5NDA3MjE0NzMzNQgMUFJPRklMRS1DRVJUCDwHOggXRW5kb3JzZUNlcnRp\ |
| 64 | ZmljYXRlVGVzdHMIDEVuY29kZURlY29kZQgRa3NrLTEzOTQwNzIxNDczMzUIB/0B\ |
| 65 | RJVWq1kUAxgBAhX9AnkwggJ1MCIYDzIwMTQwMzA2MDIxNTQ3WhgPMjAxNDAzMTMw\ |
| 66 | OTI3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Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 83 | boost::iostreams::stream<boost::iostreams::array_source> is (testIdCert.c_str(), testIdCert.size()); |
| 84 | shared_ptr<IdentityCertificate> idCert = io::load<IdentityCertificate>(is); |
| 85 | |
| 86 | BOOST_CHECK(static_cast<bool>(idCert)); |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 87 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 88 | const Certificate::SubjectDescriptionList& subjectDescription = idCert->getSubjectDescriptionList(); |
| 89 | BOOST_CHECK_EQUAL(subjectDescription.size(), 6); |
| 90 | |
| 91 | Certificate::SubjectDescriptionList::const_iterator it = subjectDescription.begin(); |
| 92 | Certificate::SubjectDescriptionList::const_iterator end = subjectDescription.end(); |
| 93 | int count = 0; |
| 94 | for(; it != end; it++) |
| 95 | { |
| 96 | if(it->getOidString() == "2.5.4.41") |
| 97 | { |
| 98 | BOOST_CHECK_EQUAL(it->getValue(), "MyName"); |
| 99 | count++; |
| 100 | } |
| 101 | if(it->getOidString() == "2.5.4.11") |
| 102 | { |
| 103 | BOOST_CHECK_EQUAL(it->getValue(), "MyInstitute"); |
| 104 | count++; |
| 105 | } |
| 106 | if(it->getOidString() == "2.5.4.1") |
| 107 | { |
| 108 | BOOST_CHECK_EQUAL(it->getValue(), "MyGroup"); |
| 109 | count++; |
| 110 | } |
| 111 | if(it->getOidString() == "2.5.4.3") |
| 112 | { |
| 113 | BOOST_CHECK_EQUAL(it->getValue(), "MyHomePage"); |
| 114 | count++; |
| 115 | } |
| 116 | if(it->getOidString() == "2.5.4.80") |
| 117 | { |
| 118 | BOOST_CHECK_EQUAL(it->getValue(), "MyAdvisor"); |
| 119 | count++; |
| 120 | } |
| 121 | if(it->getOidString() == "1.2.840.113549.1.9.1") |
| 122 | { |
| 123 | BOOST_CHECK_EQUAL(it->getValue(), "MyEmail"); |
| 124 | count++; |
| 125 | } |
| 126 | } |
| 127 | BOOST_CHECK_EQUAL(count, 6); |
| 128 | |
| 129 | BOOST_CHECK_EQUAL(idCert->getName().toUri(), "/EndorseCertificateTests/KEY/EncodeDecode/ksk-1394072147335/ID-CERT/%FD%01D%95%2C%D5%E8"); |
| 130 | |
| 131 | OBufferStream keyOs; |
| 132 | { |
| 133 | using namespace CryptoPP; |
| 134 | StringSource(testKey, true, new Base64Decoder(new FileSink(keyOs))); |
| 135 | } |
| 136 | PublicKey key(keyOs.buf()->buf(), keyOs.buf()->size()); |
| 137 | BOOST_CHECK(key == idCert->getPublicKeyInfo()); |
| 138 | } |
| 139 | |
| 140 | BOOST_AUTO_TEST_CASE(ConstructFromIdCert) |
| 141 | { |
| 142 | boost::iostreams::stream<boost::iostreams::array_source> is (testIdCert.c_str(), testIdCert.size()); |
| 143 | shared_ptr<IdentityCertificate> idCert = io::load<IdentityCertificate>(is); |
| 144 | |
| 145 | Profile profile(*idCert); |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 146 | vector<string> endorseList; |
| 147 | endorseList.push_back("email"); |
| 148 | endorseList.push_back("homepage"); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 149 | EndorseCertificate endorseCertificate(*idCert, profile, endorseList); |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 150 | |
| 151 | KeyChainImpl<SecPublicInfoSqlite3, SecTpmFile> keyChain; |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 152 | keyChain.signWithSha256(endorseCertificate); |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 153 | const Block& endorseDataBlock = endorseCertificate.wireEncode(); |
| 154 | |
| 155 | Data decodedEndorseData; |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 156 | decodedEndorseData.wireDecode(endorseDataBlock); |
| 157 | EndorseCertificate decodedEndorse(decodedEndorseData); |
| 158 | BOOST_CHECK_EQUAL(decodedEndorse.getProfile().get("IDENTITY"), "/EndorseCertificateTests/EncodeDecode"); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 159 | BOOST_CHECK_EQUAL(decodedEndorse.getProfile().get("name"), "MyName"); |
| 160 | BOOST_CHECK_EQUAL(decodedEndorse.getProfile().get("homepage"), "MyHomePage"); |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 161 | BOOST_CHECK_EQUAL(decodedEndorse.getEndorseList().size(), 2); |
| 162 | BOOST_CHECK_EQUAL(decodedEndorse.getEndorseList().at(0), "email"); |
| 163 | BOOST_CHECK_EQUAL(decodedEndorse.getEndorseList().at(1), "homepage"); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 164 | BOOST_CHECK_EQUAL(decodedEndorse.getSigner(), "/EndorseCertificateTests/EncodeDecode/ksk-1394072147335"); |
| 165 | BOOST_CHECK_EQUAL(decodedEndorse.getPublicKeyName(), "/EndorseCertificateTests/EncodeDecode/ksk-1394072147335"); |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 166 | } |
| 167 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 168 | BOOST_AUTO_TEST_CASE(ConstructFromEndorseCert) |
| 169 | { |
| 170 | boost::iostreams::stream<boost::iostreams::array_source> is (testEndorseCert.c_str(), testEndorseCert.size()); |
| 171 | shared_ptr<Data> rawData = io::load<Data>(is); |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 172 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 173 | EndorseCertificate rawEndorse(*rawData); |
| 174 | vector<string> endorseList; |
| 175 | endorseList.push_back("institution"); |
| 176 | endorseList.push_back("group"); |
| 177 | endorseList.push_back("advisor"); |
| 178 | Name signer("/EndorseCertificateTests/Singer/ksk-1234567890"); |
| 179 | EndorseCertificate endorseCertificate(rawEndorse, signer, endorseList); |
| 180 | |
| 181 | KeyChainImpl<SecPublicInfoSqlite3, SecTpmFile> keyChain; |
| 182 | keyChain.signWithSha256(endorseCertificate); |
| 183 | |
| 184 | const Block& endorseDataBlock = endorseCertificate.wireEncode(); |
| 185 | |
| 186 | Data decodedEndorseData; |
| 187 | decodedEndorseData.wireDecode(endorseDataBlock); |
| 188 | EndorseCertificate decodedEndorse(decodedEndorseData); |
| 189 | BOOST_CHECK_EQUAL(decodedEndorse.getProfile().get("IDENTITY"), "/EndorseCertificateTests/EncodeDecode"); |
| 190 | BOOST_CHECK_EQUAL(decodedEndorse.getProfile().get("name"), "MyName"); |
| 191 | BOOST_CHECK_EQUAL(decodedEndorse.getProfile().get("homepage"), "MyHomePage"); |
| 192 | BOOST_CHECK_EQUAL(decodedEndorse.getEndorseList().size(), 3); |
| 193 | BOOST_CHECK_EQUAL(decodedEndorse.getEndorseList().at(0), "institution"); |
| 194 | BOOST_CHECK_EQUAL(decodedEndorse.getEndorseList().at(1), "group"); |
| 195 | BOOST_CHECK_EQUAL(decodedEndorse.getEndorseList().at(2), "advisor"); |
| 196 | BOOST_CHECK_EQUAL(decodedEndorse.getSigner(), "/EndorseCertificateTests/Singer/ksk-1234567890"); |
| 197 | BOOST_CHECK_EQUAL(decodedEndorse.getPublicKeyName(), "/EndorseCertificateTests/EncodeDecode/ksk-1394072147335"); |
| 198 | } |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 199 | |
| 200 | BOOST_AUTO_TEST_SUITE_END() |