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> |
Qiuhan Ding | 52f1383 | 2015-03-06 14:05:59 -0800 | [diff] [blame^] | 17 | #include <boost/filesystem.hpp> |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 18 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 19 | #include <ndn-cxx/security/key-chain.hpp> |
| 20 | #include <ndn-cxx/encoding/buffer-stream.hpp> |
| 21 | #include <ndn-cxx/util/time.hpp> |
| 22 | #include <ndn-cxx/util/io.hpp> |
| 23 | #include "cryptopp.hpp" |
| 24 | #include "endorse-certificate.hpp" |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 25 | |
Qiuhan Ding | 52f1383 | 2015-03-06 14:05:59 -0800 | [diff] [blame^] | 26 | namespace chronochat { |
| 27 | namespace tests { |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 28 | |
| 29 | using std::vector; |
| 30 | using std::string; |
| 31 | |
| 32 | using ndn::KeyChain; |
| 33 | using ndn::IdentityCertificate; |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 34 | |
| 35 | BOOST_AUTO_TEST_SUITE(TestEndorseCertificate) |
| 36 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 37 | const string testIdCert("\ |
| 38 | Bv0DXwdRCBdFbmRvcnNlQ2VydGlmaWNhdGVUZXN0cwgDS0VZCAxFbmNvZGVEZWNv\ |
| 39 | ZGUIEWtzay0xMzk0MDcyMTQ3MzM1CAdJRC1DRVJUCAf9AUSVLNXoFAMYAQIV/QG8\ |
| 40 | MIIBuDAiGA8yMDE0MDMwNjAyMTU0N1oYDzIwMTQwMzEzMDkyNzQ3WjBuMA0GA1UE\ |
| 41 | KRMGTXlOYW1lMBIGA1UECxMLTXlJbnN0aXR1dGUwDgYDVQQBEwdNeUdyb3VwMBEG\ |
| 42 | A1UEAxMKTXlIb21lUGFnZTAQBgNVBFATCU15QWR2aXNvcjAUBgkqhkiG9w0BCQET\ |
| 43 | B015RW1haWwwggEgMA0GCSqGSIb3DQEBAQUAA4IBDQAwggEIAoIBAQDYsWD0ixQF\ |
| 44 | RfYs36BHNsRNv5ouEL69oaS6XX/hsQN1By4RNI6eSG5DpajtAwK1y+DXPwkLHd5S\ |
| 45 | BrvwLzReF7SsrF2ObawznU14GKaQdbn+eVIER7CWvSpJhH5yKS4fCPRN+b1MP8QS\ |
| 46 | DLvaaGu15T98cgVscIEqFkLfnWSQbdN6EnodjOH27JkBCz8Lxv9GZLrhfKGzOylR\ |
| 47 | fLzvCIyIXYl6HWroO+xTJQaP+miSZNVGyf4jYqz5WbQH56a9ZjUldTphjuDbBjUq\ |
| 48 | QaNVOzoKT+H4qh8mn399aQ9/BjM+6/WgrSw7/MO2UCgoZhySQY4HVqzUVVWnYwOU\ |
| 49 | NYPoOS3HdvGLAgERFkEbAQEcPAc6CBdFbmRvcnNlQ2VydGlmaWNhdGVUZXN0cwgD\ |
| 50 | S0VZCBFrc2stMTM5NDA3MjE0NzEyOAgHSUQtQ0VSVBf9AQARSwS/CelRRSUO4Tik\ |
| 51 | 5Q+L5zusaqq5652T92/83S5l38dO41BOf5fBUb3RtnFSbS/QaBCRfRJtDvkN2LhE\ |
| 52 | vksJjSAoAKUzx27UyM1eq7L8DDvsvC9mbwxGzTK2F1t3Jy81rk5X34MecvztlILs\ |
| 53 | nLqzqqiwl3dS1xyvg9GZez5g1yoOtRwzkHaah6svLVwzwM7kECXWRf4NoHTazWQo\ |
| 54 | Cs6s60F9I/xBRKJ4Cw2L/AzvB5sX1J4HvHCsplbR/GdvA8uW6i8pp7kjIhjCGewK\ |
| 55 | uNfH/4lHxzTl3pjsVy+EHKmwSlZ+T8cy5qaIEHxhbOzMNNVdit7XEwexOE66AVza\ |
| 56 | 92On"); |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 57 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 58 | const string testKey("\ |
| 59 | MIIBIDANBgkqhkiG9w0BAQEFAAOCAQ0AMIIBCAKCAQEA2LFg9IsUBUX2LN+gRzbE\ |
| 60 | Tb+aLhC+vaGkul1/4bEDdQcuETSOnkhuQ6Wo7QMCtcvg1z8JCx3eUga78C80Xhe0\ |
| 61 | rKxdjm2sM51NeBimkHW5/nlSBEewlr0qSYR+cikuHwj0Tfm9TD/EEgy72mhrteU/\ |
| 62 | fHIFbHCBKhZC351kkG3TehJ6HYzh9uyZAQs/C8b/RmS64XyhszspUXy87wiMiF2J\ |
| 63 | eh1q6DvsUyUGj/pokmTVRsn+I2Ks+Vm0B+emvWY1JXU6YY7g2wY1KkGjVTs6Ck/h\ |
| 64 | +KofJp9/fWkPfwYzPuv1oK0sO/zDtlAoKGYckkGOB1as1FVVp2MDlDWD6Dktx3bx\ |
| 65 | iwIBEQ=="); |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 66 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 67 | const string testEndorseCert("\ |
| 68 | Bv0DOgePCBdFbmRvcnNlQ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aW5zdGl0\ |
| 83 | dXRpb24SC015SW5zdGl0dXRlCg4KBG5hbWUSBk15TmFtZTAjBgcrBgEFIAICAQH/\ |
| 84 | BBUKBwoFZW1haWwKCgoIaG9tZXBhZ2UWAxsBABcgS7pYcBk1e4dlsag8minK+UzI\ |
| 85 | L8ViVS87k09gaM6GeUA="); |
| 86 | |
| 87 | BOOST_AUTO_TEST_CASE(IdCert) |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 88 | { |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 89 | boost::iostreams::stream<boost::iostreams::array_source> is(testIdCert.c_str(), |
| 90 | testIdCert.size()); |
| 91 | shared_ptr<IdentityCertificate> idCert = ndn::io::load<IdentityCertificate>(is); |
| 92 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 93 | BOOST_CHECK(static_cast<bool>(idCert)); |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 94 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 95 | const ndn::Certificate::SubjectDescriptionList& subjectDescription = |
| 96 | idCert->getSubjectDescriptionList(); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 97 | BOOST_CHECK_EQUAL(subjectDescription.size(), 6); |
| 98 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 99 | ndn::Certificate::SubjectDescriptionList::const_iterator it = subjectDescription.begin(); |
| 100 | ndn::Certificate::SubjectDescriptionList::const_iterator end = subjectDescription.end(); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 101 | int count = 0; |
| 102 | for(; it != end; it++) |
| 103 | { |
| 104 | if(it->getOidString() == "2.5.4.41") |
| 105 | { |
| 106 | BOOST_CHECK_EQUAL(it->getValue(), "MyName"); |
| 107 | count++; |
| 108 | } |
| 109 | if(it->getOidString() == "2.5.4.11") |
| 110 | { |
| 111 | BOOST_CHECK_EQUAL(it->getValue(), "MyInstitute"); |
| 112 | count++; |
| 113 | } |
| 114 | if(it->getOidString() == "2.5.4.1") |
| 115 | { |
| 116 | BOOST_CHECK_EQUAL(it->getValue(), "MyGroup"); |
| 117 | count++; |
| 118 | } |
| 119 | if(it->getOidString() == "2.5.4.3") |
| 120 | { |
| 121 | BOOST_CHECK_EQUAL(it->getValue(), "MyHomePage"); |
| 122 | count++; |
| 123 | } |
| 124 | if(it->getOidString() == "2.5.4.80") |
| 125 | { |
| 126 | BOOST_CHECK_EQUAL(it->getValue(), "MyAdvisor"); |
| 127 | count++; |
| 128 | } |
| 129 | if(it->getOidString() == "1.2.840.113549.1.9.1") |
| 130 | { |
| 131 | BOOST_CHECK_EQUAL(it->getValue(), "MyEmail"); |
| 132 | count++; |
| 133 | } |
| 134 | } |
| 135 | BOOST_CHECK_EQUAL(count, 6); |
| 136 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 137 | BOOST_CHECK_EQUAL(idCert->getName().toUri(), |
| 138 | "/EndorseCertificateTests/KEY/EncodeDecode/ksk-1394072147335/ID-CERT/%FD%01D%95%2C%D5%E8"); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 139 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 140 | ndn::OBufferStream keyOs; |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 141 | { |
| 142 | using namespace CryptoPP; |
| 143 | StringSource(testKey, true, new Base64Decoder(new FileSink(keyOs))); |
| 144 | } |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 145 | ndn::PublicKey key(keyOs.buf()->buf(), keyOs.buf()->size()); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 146 | BOOST_CHECK(key == idCert->getPublicKeyInfo()); |
| 147 | } |
| 148 | |
| 149 | BOOST_AUTO_TEST_CASE(ConstructFromIdCert) |
| 150 | { |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 151 | boost::iostreams::stream<boost::iostreams::array_source> is(testIdCert.c_str(), |
| 152 | testIdCert.size()); |
| 153 | shared_ptr<IdentityCertificate> idCert = ndn::io::load<IdentityCertificate>(is); |
| 154 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 155 | Profile profile(*idCert); |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 156 | vector<string> endorseList; |
| 157 | endorseList.push_back("email"); |
| 158 | endorseList.push_back("homepage"); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 159 | EndorseCertificate endorseCertificate(*idCert, profile, endorseList); |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 160 | |
Qiuhan Ding | 52f1383 | 2015-03-06 14:05:59 -0800 | [diff] [blame^] | 161 | boost::filesystem::path keyChainTmpPath = |
| 162 | boost::filesystem::path(TEST_CERT_PATH) / "TestEndorseCertificate"; |
| 163 | KeyChain keyChain(std::string("sqlite3:").append(keyChainTmpPath.string()), |
| 164 | std::string("tpm-file:").append(keyChainTmpPath.string())); |
| 165 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 166 | keyChain.signWithSha256(endorseCertificate); |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 167 | const Block& endorseDataBlock = endorseCertificate.wireEncode(); |
| 168 | |
| 169 | Data decodedEndorseData; |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 170 | decodedEndorseData.wireDecode(endorseDataBlock); |
| 171 | EndorseCertificate decodedEndorse(decodedEndorseData); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 172 | BOOST_CHECK_EQUAL(decodedEndorse.getProfile().get("IDENTITY"), |
| 173 | "/EndorseCertificateTests/EncodeDecode"); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 174 | BOOST_CHECK_EQUAL(decodedEndorse.getProfile().get("name"), "MyName"); |
| 175 | BOOST_CHECK_EQUAL(decodedEndorse.getProfile().get("homepage"), "MyHomePage"); |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 176 | BOOST_CHECK_EQUAL(decodedEndorse.getEndorseList().size(), 2); |
| 177 | BOOST_CHECK_EQUAL(decodedEndorse.getEndorseList().at(0), "email"); |
| 178 | BOOST_CHECK_EQUAL(decodedEndorse.getEndorseList().at(1), "homepage"); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 179 | BOOST_CHECK_EQUAL(decodedEndorse.getSigner(), |
| 180 | "/EndorseCertificateTests/EncodeDecode/ksk-1394072147335"); |
| 181 | BOOST_CHECK_EQUAL(decodedEndorse.getPublicKeyName(), |
| 182 | "/EndorseCertificateTests/EncodeDecode/ksk-1394072147335"); |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 183 | } |
| 184 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 185 | BOOST_AUTO_TEST_CASE(ConstructFromEndorseCert) |
| 186 | { |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 187 | boost::iostreams::stream<boost::iostreams::array_source> is(testEndorseCert.c_str(), |
| 188 | testEndorseCert.size()); |
| 189 | shared_ptr<Data> rawData = ndn::io::load<Data>(is); |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 190 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 191 | EndorseCertificate rawEndorse(*rawData); |
| 192 | vector<string> endorseList; |
| 193 | endorseList.push_back("institution"); |
| 194 | endorseList.push_back("group"); |
| 195 | endorseList.push_back("advisor"); |
| 196 | Name signer("/EndorseCertificateTests/Singer/ksk-1234567890"); |
| 197 | EndorseCertificate endorseCertificate(rawEndorse, signer, endorseList); |
| 198 | |
Qiuhan Ding | 52f1383 | 2015-03-06 14:05:59 -0800 | [diff] [blame^] | 199 | boost::filesystem::path keyChainTmpPath = |
| 200 | boost::filesystem::path(TEST_CERT_PATH) / "TestEndorseCertificate"; |
| 201 | KeyChain keyChain(std::string("sqlite3:").append(keyChainTmpPath.string()), |
| 202 | std::string("tpm-file:").append(keyChainTmpPath.string())); |
| 203 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 204 | keyChain.signWithSha256(endorseCertificate); |
| 205 | |
| 206 | const Block& endorseDataBlock = endorseCertificate.wireEncode(); |
| 207 | |
| 208 | Data decodedEndorseData; |
| 209 | decodedEndorseData.wireDecode(endorseDataBlock); |
| 210 | EndorseCertificate decodedEndorse(decodedEndorseData); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 211 | BOOST_CHECK_EQUAL(decodedEndorse.getProfile().get("IDENTITY"), |
| 212 | "/EndorseCertificateTests/EncodeDecode"); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 213 | BOOST_CHECK_EQUAL(decodedEndorse.getProfile().get("name"), "MyName"); |
| 214 | BOOST_CHECK_EQUAL(decodedEndorse.getProfile().get("homepage"), "MyHomePage"); |
| 215 | BOOST_CHECK_EQUAL(decodedEndorse.getEndorseList().size(), 3); |
| 216 | BOOST_CHECK_EQUAL(decodedEndorse.getEndorseList().at(0), "institution"); |
| 217 | BOOST_CHECK_EQUAL(decodedEndorse.getEndorseList().at(1), "group"); |
| 218 | BOOST_CHECK_EQUAL(decodedEndorse.getEndorseList().at(2), "advisor"); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 219 | BOOST_CHECK_EQUAL(decodedEndorse.getSigner(), |
| 220 | "/EndorseCertificateTests/Singer/ksk-1234567890"); |
| 221 | BOOST_CHECK_EQUAL(decodedEndorse.getPublicKeyName(), |
| 222 | "/EndorseCertificateTests/EncodeDecode/ksk-1394072147335"); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 223 | } |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 224 | |
| 225 | BOOST_AUTO_TEST_SUITE_END() |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 226 | |
Qiuhan Ding | 52f1383 | 2015-03-06 14:05:59 -0800 | [diff] [blame^] | 227 | } // namespace tests |
| 228 | } // namespace chronochat |