blob: 8d9717355a00f281917373f59e20f38e06b40bf2 [file] [log] [blame]
Yingdi Yufa4ce792014-02-06 18:09:22 -08001/* -*- 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 Ding52f13832015-03-06 14:05:59 -080017#include <boost/filesystem.hpp>
Yingdi Yufa4ce792014-02-06 18:09:22 -080018
Yingdi Yu0b0a7362014-08-05 16:31:30 -070019#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 Yufa4ce792014-02-06 18:09:22 -080025
Qiuhan Ding52f13832015-03-06 14:05:59 -080026namespace chronochat {
27namespace tests {
Yingdi Yu0b0a7362014-08-05 16:31:30 -070028
29using std::vector;
30using std::string;
31
32using ndn::KeyChain;
33using ndn::IdentityCertificate;
Yingdi Yufa4ce792014-02-06 18:09:22 -080034
35BOOST_AUTO_TEST_SUITE(TestEndorseCertificate)
36
Yingdi Yu348f5ea2014-03-01 14:47:25 -080037const string testIdCert("\
38Bv0DXwdRCBdFbmRvcnNlQ2VydGlmaWNhdGVUZXN0cwgDS0VZCAxFbmNvZGVEZWNv\
39ZGUIEWtzay0xMzk0MDcyMTQ3MzM1CAdJRC1DRVJUCAf9AUSVLNXoFAMYAQIV/QG8\
40MIIBuDAiGA8yMDE0MDMwNjAyMTU0N1oYDzIwMTQwMzEzMDkyNzQ3WjBuMA0GA1UE\
41KRMGTXlOYW1lMBIGA1UECxMLTXlJbnN0aXR1dGUwDgYDVQQBEwdNeUdyb3VwMBEG\
42A1UEAxMKTXlIb21lUGFnZTAQBgNVBFATCU15QWR2aXNvcjAUBgkqhkiG9w0BCQET\
43B015RW1haWwwggEgMA0GCSqGSIb3DQEBAQUAA4IBDQAwggEIAoIBAQDYsWD0ixQF\
44RfYs36BHNsRNv5ouEL69oaS6XX/hsQN1By4RNI6eSG5DpajtAwK1y+DXPwkLHd5S\
45BrvwLzReF7SsrF2ObawznU14GKaQdbn+eVIER7CWvSpJhH5yKS4fCPRN+b1MP8QS\
46DLvaaGu15T98cgVscIEqFkLfnWSQbdN6EnodjOH27JkBCz8Lxv9GZLrhfKGzOylR\
47fLzvCIyIXYl6HWroO+xTJQaP+miSZNVGyf4jYqz5WbQH56a9ZjUldTphjuDbBjUq\
48QaNVOzoKT+H4qh8mn399aQ9/BjM+6/WgrSw7/MO2UCgoZhySQY4HVqzUVVWnYwOU\
49NYPoOS3HdvGLAgERFkEbAQEcPAc6CBdFbmRvcnNlQ2VydGlmaWNhdGVUZXN0cwgD\
50S0VZCBFrc2stMTM5NDA3MjE0NzEyOAgHSUQtQ0VSVBf9AQARSwS/CelRRSUO4Tik\
515Q+L5zusaqq5652T92/83S5l38dO41BOf5fBUb3RtnFSbS/QaBCRfRJtDvkN2LhE\
52vksJjSAoAKUzx27UyM1eq7L8DDvsvC9mbwxGzTK2F1t3Jy81rk5X34MecvztlILs\
53nLqzqqiwl3dS1xyvg9GZez5g1yoOtRwzkHaah6svLVwzwM7kECXWRf4NoHTazWQo\
54Cs6s60F9I/xBRKJ4Cw2L/AzvB5sX1J4HvHCsplbR/GdvA8uW6i8pp7kjIhjCGewK\
55uNfH/4lHxzTl3pjsVy+EHKmwSlZ+T8cy5qaIEHxhbOzMNNVdit7XEwexOE66AVza\
5692On");
Yingdi Yufa4ce792014-02-06 18:09:22 -080057
Yingdi Yu348f5ea2014-03-01 14:47:25 -080058const string testKey("\
59MIIBIDANBgkqh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kkG3TehJ6HYzh9uyZ\
77AQs/C8b/RmS64XyhszspUXy87wiMiF2Jeh1q6DvsUyUGj/pokmTVRsn+I2Ks+Vm0\
78B+emvWY1JXU6YY7g2wY1KkGjVTs6Ck/h+KofJp9/fWkPfwYzPuv1oK0sO/zDtlAo\
79KGYckkGOB1as1FVVp2MDlDWD6Dktx3bxiwIBETCB6DCBwAYHKwYBBSACAQEB/wSB\
80sQoxCghJREVOVElUWRIlL0VuZG9yc2VDZXJ0aWZpY2F0ZVRlc3RzL0VuY29kZURl\
81Y29kZQoUCgdhZHZpc29yEglNeUFkdmlzb3IKEAoFZW1haWwSB015RW1haWwKEAoF\
82Z3JvdXASB015R3JvdXAKFgoIaG9tZXBhZ2USCk15SG9tZVBhZ2UKGgoLaW5zdGl0\
83dXRpb24SC015SW5zdGl0dXRlCg4KBG5hbWUSBk15TmFtZTAjBgcrBgEFIAICAQH/\
84BBUKBwoFZW1haWwKCgoIaG9tZXBhZ2UWAxsBABcgS7pYcBk1e4dlsag8minK+UzI\
85L8ViVS87k09gaM6GeUA=");
86
87BOOST_AUTO_TEST_CASE(IdCert)
Yingdi Yufa4ce792014-02-06 18:09:22 -080088{
Yingdi Yu0b0a7362014-08-05 16:31:30 -070089 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 Yu348f5ea2014-03-01 14:47:25 -080093 BOOST_CHECK(static_cast<bool>(idCert));
Yingdi Yufa4ce792014-02-06 18:09:22 -080094
Yingdi Yu0b0a7362014-08-05 16:31:30 -070095 const ndn::Certificate::SubjectDescriptionList& subjectDescription =
96 idCert->getSubjectDescriptionList();
Yingdi Yu348f5ea2014-03-01 14:47:25 -080097 BOOST_CHECK_EQUAL(subjectDescription.size(), 6);
98
Yingdi Yu0b0a7362014-08-05 16:31:30 -070099 ndn::Certificate::SubjectDescriptionList::const_iterator it = subjectDescription.begin();
100 ndn::Certificate::SubjectDescriptionList::const_iterator end = subjectDescription.end();
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800101 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 Yu0b0a7362014-08-05 16:31:30 -0700137 BOOST_CHECK_EQUAL(idCert->getName().toUri(),
138 "/EndorseCertificateTests/KEY/EncodeDecode/ksk-1394072147335/ID-CERT/%FD%01D%95%2C%D5%E8");
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800139
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700140 ndn::OBufferStream keyOs;
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800141 {
142 using namespace CryptoPP;
143 StringSource(testKey, true, new Base64Decoder(new FileSink(keyOs)));
144 }
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700145 ndn::PublicKey key(keyOs.buf()->buf(), keyOs.buf()->size());
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800146 BOOST_CHECK(key == idCert->getPublicKeyInfo());
147}
148
149BOOST_AUTO_TEST_CASE(ConstructFromIdCert)
150{
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700151 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 Yu348f5ea2014-03-01 14:47:25 -0800155 Profile profile(*idCert);
Yingdi Yufa4ce792014-02-06 18:09:22 -0800156 vector<string> endorseList;
157 endorseList.push_back("email");
158 endorseList.push_back("homepage");
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800159 EndorseCertificate endorseCertificate(*idCert, profile, endorseList);
Yingdi Yufa4ce792014-02-06 18:09:22 -0800160
Qiuhan Ding52f13832015-03-06 14:05:59 -0800161 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 Yu0b0a7362014-08-05 16:31:30 -0700166 keyChain.signWithSha256(endorseCertificate);
Yingdi Yufa4ce792014-02-06 18:09:22 -0800167 const Block& endorseDataBlock = endorseCertificate.wireEncode();
168
169 Data decodedEndorseData;
Yingdi Yufa4ce792014-02-06 18:09:22 -0800170 decodedEndorseData.wireDecode(endorseDataBlock);
171 EndorseCertificate decodedEndorse(decodedEndorseData);
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700172 BOOST_CHECK_EQUAL(decodedEndorse.getProfile().get("IDENTITY"),
173 "/EndorseCertificateTests/EncodeDecode");
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800174 BOOST_CHECK_EQUAL(decodedEndorse.getProfile().get("name"), "MyName");
175 BOOST_CHECK_EQUAL(decodedEndorse.getProfile().get("homepage"), "MyHomePage");
Yingdi Yufa4ce792014-02-06 18:09:22 -0800176 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 Yu0b0a7362014-08-05 16:31:30 -0700179 BOOST_CHECK_EQUAL(decodedEndorse.getSigner(),
180 "/EndorseCertificateTests/EncodeDecode/ksk-1394072147335");
181 BOOST_CHECK_EQUAL(decodedEndorse.getPublicKeyName(),
182 "/EndorseCertificateTests/EncodeDecode/ksk-1394072147335");
Yingdi Yufa4ce792014-02-06 18:09:22 -0800183}
184
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800185BOOST_AUTO_TEST_CASE(ConstructFromEndorseCert)
186{
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700187 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 Yufa4ce792014-02-06 18:09:22 -0800190
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800191 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 Ding52f13832015-03-06 14:05:59 -0800199 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 Yu348f5ea2014-03-01 14:47:25 -0800204 keyChain.signWithSha256(endorseCertificate);
205
206 const Block& endorseDataBlock = endorseCertificate.wireEncode();
207
208 Data decodedEndorseData;
209 decodedEndorseData.wireDecode(endorseDataBlock);
210 EndorseCertificate decodedEndorse(decodedEndorseData);
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700211 BOOST_CHECK_EQUAL(decodedEndorse.getProfile().get("IDENTITY"),
212 "/EndorseCertificateTests/EncodeDecode");
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800213 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 Yu0b0a7362014-08-05 16:31:30 -0700219 BOOST_CHECK_EQUAL(decodedEndorse.getSigner(),
220 "/EndorseCertificateTests/Singer/ksk-1234567890");
221 BOOST_CHECK_EQUAL(decodedEndorse.getPublicKeyName(),
222 "/EndorseCertificateTests/EncodeDecode/ksk-1394072147335");
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800223}
Yingdi Yufa4ce792014-02-06 18:09:22 -0800224
225BOOST_AUTO_TEST_SUITE_END()
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700226
Qiuhan Ding52f13832015-03-06 14:05:59 -0800227} // namespace tests
228} // namespace chronochat