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("\ |
Qiuhan Ding | 0cfc151 | 2015-02-17 17:44:11 -0800 | [diff] [blame] | 68 | Bv0CYweICBdFbmRvcnNlQ2VydGlmaWNhdGVUZXN0cwgMRW5jb2RlRGVjb2RlCBFr\ |
| 69 | c2stMTM5NDA3MjE0NzMzNQgMUFJPRklMRS1DRVJUCDMHMQgXRW5kb3JzZUNlcnRp\ |
| 70 | ZmljYXRlVGVzdHMIBlNpbmdlcggOa3NrLTEyMzQ1Njc4OTAICf0AAAFMoXR8NRQD\ |
| 71 | GAECFf0BqTCCAaUwIhgPMjAxMzEyMjYyMzIyNTRaGA8yMDEzMTIyNjIzMjI1NFow\ |
| 72 | QDA+BgNVBCkTNy9FbmRvcnNlQ2VydGlmaWNhdGVUZXN0cy9FbmNvZGVEZWNvZGUv\ |
| 73 | a3NrLTEzOTQwNzIxNDczMzUwgZ0wDQYJKoZIhvcNAQEBBQADgYsAMIGHAoGBAJ4G\ |
| 74 | PkeFsjQ3qoVHrAMkg7WcqAU6JB7riQG76ZuywyKsaOPwbALOaKbE0KcGkJyqGwgd\ |
| 75 | i0OaM2dEbSGjG4ial15ZxBUL2Sy9UQdhgq3BuNe/m899JMJj85cX6/5iJbpbTYrC\ |
| 76 | er1Dio+48vHFajDTUIzImt/v7TXnemLqdny7CCbHAgERMIGcMGsGBysGAQUgAgEB\ |
| 77 | Af8EXYhbiTGKCElERU5USVRZiyUvRW5kb3JzZUNlcnRpZmljYXRlVGVzdHMvRW5j\ |
| 78 | b2RlRGVjb2RliRaKCGhvbWVwYWdliwpNeUhvbWVQYWdliQ6KBG5hbWWLBk15TmFt\ |
| 79 | ZTAtBgcrBgEFIAICAQH/BB+MHYsLaW5zdGl0dXRpb26LBWdyb3VwiwdhZHZpc29y\ |
| 80 | FgMbAQAXIHalD2NUzM7abX6QY+2qWNLVMC+ch2xnVyrlf89ZH/IV"); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 81 | |
| 82 | BOOST_AUTO_TEST_CASE(IdCert) |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 83 | { |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 84 | boost::iostreams::stream<boost::iostreams::array_source> is(testIdCert.c_str(), |
| 85 | testIdCert.size()); |
| 86 | shared_ptr<IdentityCertificate> idCert = ndn::io::load<IdentityCertificate>(is); |
| 87 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 88 | BOOST_CHECK(static_cast<bool>(idCert)); |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 89 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 90 | const ndn::Certificate::SubjectDescriptionList& subjectDescription = |
| 91 | idCert->getSubjectDescriptionList(); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 92 | BOOST_CHECK_EQUAL(subjectDescription.size(), 6); |
| 93 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 94 | ndn::Certificate::SubjectDescriptionList::const_iterator it = subjectDescription.begin(); |
| 95 | ndn::Certificate::SubjectDescriptionList::const_iterator end = subjectDescription.end(); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 96 | int count = 0; |
| 97 | for(; it != end; it++) |
| 98 | { |
| 99 | if(it->getOidString() == "2.5.4.41") |
| 100 | { |
| 101 | BOOST_CHECK_EQUAL(it->getValue(), "MyName"); |
| 102 | count++; |
| 103 | } |
| 104 | if(it->getOidString() == "2.5.4.11") |
| 105 | { |
| 106 | BOOST_CHECK_EQUAL(it->getValue(), "MyInstitute"); |
| 107 | count++; |
| 108 | } |
| 109 | if(it->getOidString() == "2.5.4.1") |
| 110 | { |
| 111 | BOOST_CHECK_EQUAL(it->getValue(), "MyGroup"); |
| 112 | count++; |
| 113 | } |
| 114 | if(it->getOidString() == "2.5.4.3") |
| 115 | { |
| 116 | BOOST_CHECK_EQUAL(it->getValue(), "MyHomePage"); |
| 117 | count++; |
| 118 | } |
| 119 | if(it->getOidString() == "2.5.4.80") |
| 120 | { |
| 121 | BOOST_CHECK_EQUAL(it->getValue(), "MyAdvisor"); |
| 122 | count++; |
| 123 | } |
| 124 | if(it->getOidString() == "1.2.840.113549.1.9.1") |
| 125 | { |
| 126 | BOOST_CHECK_EQUAL(it->getValue(), "MyEmail"); |
| 127 | count++; |
| 128 | } |
| 129 | } |
| 130 | BOOST_CHECK_EQUAL(count, 6); |
| 131 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 132 | BOOST_CHECK_EQUAL(idCert->getName().toUri(), |
| 133 | "/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] | 134 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 135 | ndn::OBufferStream keyOs; |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 136 | { |
| 137 | using namespace CryptoPP; |
| 138 | StringSource(testKey, true, new Base64Decoder(new FileSink(keyOs))); |
| 139 | } |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 140 | ndn::PublicKey key(keyOs.buf()->buf(), keyOs.buf()->size()); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 141 | BOOST_CHECK(key == idCert->getPublicKeyInfo()); |
| 142 | } |
| 143 | |
| 144 | BOOST_AUTO_TEST_CASE(ConstructFromIdCert) |
| 145 | { |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 146 | boost::iostreams::stream<boost::iostreams::array_source> is(testIdCert.c_str(), |
| 147 | testIdCert.size()); |
| 148 | shared_ptr<IdentityCertificate> idCert = ndn::io::load<IdentityCertificate>(is); |
| 149 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 150 | Profile profile(*idCert); |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 151 | vector<string> endorseList; |
| 152 | endorseList.push_back("email"); |
| 153 | endorseList.push_back("homepage"); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 154 | EndorseCertificate endorseCertificate(*idCert, profile, endorseList); |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 155 | |
Qiuhan Ding | 52f1383 | 2015-03-06 14:05:59 -0800 | [diff] [blame] | 156 | boost::filesystem::path keyChainTmpPath = |
| 157 | boost::filesystem::path(TEST_CERT_PATH) / "TestEndorseCertificate"; |
| 158 | KeyChain keyChain(std::string("sqlite3:").append(keyChainTmpPath.string()), |
| 159 | std::string("tpm-file:").append(keyChainTmpPath.string())); |
| 160 | |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 161 | keyChain.signWithSha256(endorseCertificate); |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 162 | const Block& endorseDataBlock = endorseCertificate.wireEncode(); |
| 163 | |
| 164 | Data decodedEndorseData; |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 165 | decodedEndorseData.wireDecode(endorseDataBlock); |
| 166 | EndorseCertificate decodedEndorse(decodedEndorseData); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 167 | BOOST_CHECK_EQUAL(decodedEndorse.getProfile().get("IDENTITY"), |
| 168 | "/EndorseCertificateTests/EncodeDecode"); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 169 | BOOST_CHECK_EQUAL(decodedEndorse.getProfile().get("name"), "MyName"); |
| 170 | BOOST_CHECK_EQUAL(decodedEndorse.getProfile().get("homepage"), "MyHomePage"); |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 171 | BOOST_CHECK_EQUAL(decodedEndorse.getEndorseList().size(), 2); |
| 172 | BOOST_CHECK_EQUAL(decodedEndorse.getEndorseList().at(0), "email"); |
| 173 | BOOST_CHECK_EQUAL(decodedEndorse.getEndorseList().at(1), "homepage"); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 174 | BOOST_CHECK_EQUAL(decodedEndorse.getSigner(), |
| 175 | "/EndorseCertificateTests/EncodeDecode/ksk-1394072147335"); |
| 176 | BOOST_CHECK_EQUAL(decodedEndorse.getPublicKeyName(), |
| 177 | "/EndorseCertificateTests/EncodeDecode/ksk-1394072147335"); |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 178 | } |
| 179 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 180 | BOOST_AUTO_TEST_CASE(ConstructFromEndorseCert) |
| 181 | { |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 182 | boost::iostreams::stream<boost::iostreams::array_source> is(testEndorseCert.c_str(), |
| 183 | testEndorseCert.size()); |
| 184 | shared_ptr<Data> rawData = ndn::io::load<Data>(is); |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 185 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 186 | EndorseCertificate rawEndorse(*rawData); |
| 187 | vector<string> endorseList; |
| 188 | endorseList.push_back("institution"); |
| 189 | endorseList.push_back("group"); |
| 190 | endorseList.push_back("advisor"); |
| 191 | Name signer("/EndorseCertificateTests/Singer/ksk-1234567890"); |
| 192 | EndorseCertificate endorseCertificate(rawEndorse, signer, endorseList); |
| 193 | |
Qiuhan Ding | 52f1383 | 2015-03-06 14:05:59 -0800 | [diff] [blame] | 194 | boost::filesystem::path keyChainTmpPath = |
| 195 | boost::filesystem::path(TEST_CERT_PATH) / "TestEndorseCertificate"; |
| 196 | KeyChain keyChain(std::string("sqlite3:").append(keyChainTmpPath.string()), |
| 197 | std::string("tpm-file:").append(keyChainTmpPath.string())); |
| 198 | |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 199 | keyChain.signWithSha256(endorseCertificate); |
| 200 | |
| 201 | const Block& endorseDataBlock = endorseCertificate.wireEncode(); |
| 202 | |
| 203 | Data decodedEndorseData; |
| 204 | decodedEndorseData.wireDecode(endorseDataBlock); |
| 205 | EndorseCertificate decodedEndorse(decodedEndorseData); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 206 | BOOST_CHECK_EQUAL(decodedEndorse.getProfile().get("IDENTITY"), |
| 207 | "/EndorseCertificateTests/EncodeDecode"); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 208 | BOOST_CHECK_EQUAL(decodedEndorse.getProfile().get("name"), "MyName"); |
| 209 | BOOST_CHECK_EQUAL(decodedEndorse.getProfile().get("homepage"), "MyHomePage"); |
| 210 | BOOST_CHECK_EQUAL(decodedEndorse.getEndorseList().size(), 3); |
| 211 | BOOST_CHECK_EQUAL(decodedEndorse.getEndorseList().at(0), "institution"); |
| 212 | BOOST_CHECK_EQUAL(decodedEndorse.getEndorseList().at(1), "group"); |
| 213 | BOOST_CHECK_EQUAL(decodedEndorse.getEndorseList().at(2), "advisor"); |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 214 | BOOST_CHECK_EQUAL(decodedEndorse.getSigner(), |
| 215 | "/EndorseCertificateTests/Singer/ksk-1234567890"); |
| 216 | BOOST_CHECK_EQUAL(decodedEndorse.getPublicKeyName(), |
| 217 | "/EndorseCertificateTests/EncodeDecode/ksk-1394072147335"); |
Yingdi Yu | 348f5ea | 2014-03-01 14:47:25 -0800 | [diff] [blame] | 218 | } |
Yingdi Yu | fa4ce79 | 2014-02-06 18:09:22 -0800 | [diff] [blame] | 219 | |
| 220 | BOOST_AUTO_TEST_SUITE_END() |
Yingdi Yu | 0b0a736 | 2014-08-05 16:31:30 -0700 | [diff] [blame] | 221 | |
Qiuhan Ding | 52f1383 | 2015-03-06 14:05:59 -0800 | [diff] [blame] | 222 | } // namespace tests |
| 223 | } // namespace chronochat |