blob: 5fa5c79be37a12d490acf62b67f37782a3210a1d [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("\
59MIIBIDANBgkqhkiG9w0BAQEFAAOCAQ0AMIIBCAKCAQEA2LFg9IsUBUX2LN+gRzbE\
60Tb+aLhC+vaGkul1/4bEDdQcuETSOnkhuQ6Wo7QMCtcvg1z8JCx3eUga78C80Xhe0\
61rKxdjm2sM51NeBimkHW5/nlSBEewlr0qSYR+cikuHwj0Tfm9TD/EEgy72mhrteU/\
62fHIFbHCBKhZC351kkG3TehJ6HYzh9uyZAQs/C8b/RmS64XyhszspUXy87wiMiF2J\
63eh1q6DvsUyUGj/pokmTVRsn+I2Ks+Vm0B+emvWY1JXU6YY7g2wY1KkGjVTs6Ck/h\
64+KofJp9/fWkPfwYzPuv1oK0sO/zDtlAoKGYckkGOB1as1FVVp2MDlDWD6Dktx3bx\
65iwIBEQ==");
Yingdi Yufa4ce792014-02-06 18:09:22 -080066
Yingdi Yu348f5ea2014-03-01 14:47:25 -080067const string testEndorseCert("\
Qiuhan Ding0cfc1512015-02-17 17:44:11 -080068Bv0CYweICBdFbmRvcnNlQ2VydGlmaWNhdGVUZXN0cwgMRW5jb2RlRGVjb2RlCBFr\
69c2stMTM5NDA3MjE0NzMzNQgMUFJPRklMRS1DRVJUCDMHMQgXRW5kb3JzZUNlcnRp\
70ZmljYXRlVGVzdHMIBlNpbmdlcggOa3NrLTEyMzQ1Njc4OTAICf0AAAFMoXR8NRQD\
71GAECFf0BqTCCAaUwIhgPMjAxMzEyMjYyMzIyNTRaGA8yMDEzMTIyNjIzMjI1NFow\
72QDA+BgNVBCkTNy9FbmRvcnNlQ2VydGlmaWNhdGVUZXN0cy9FbmNvZGVEZWNvZGUv\
73a3NrLTEzOTQwNzIxNDczMzUwgZ0wDQYJKoZIhvcNAQEBBQADgYsAMIGHAoGBAJ4G\
74PkeFsjQ3qoVHrAMkg7WcqAU6JB7riQG76ZuywyKsaOPwbALOaKbE0KcGkJyqGwgd\
75i0OaM2dEbSGjG4ial15ZxBUL2Sy9UQdhgq3BuNe/m899JMJj85cX6/5iJbpbTYrC\
76er1Dio+48vHFajDTUIzImt/v7TXnemLqdny7CCbHAgERMIGcMGsGBysGAQUgAgEB\
77Af8EXYhbiTGKCElERU5USVRZiyUvRW5kb3JzZUNlcnRpZmljYXRlVGVzdHMvRW5j\
78b2RlRGVjb2RliRaKCGhvbWVwYWdliwpNeUhvbWVQYWdliQ6KBG5hbWWLBk15TmFt\
79ZTAtBgcrBgEFIAICAQH/BB+MHYsLaW5zdGl0dXRpb26LBWdyb3VwiwdhZHZpc29y\
80FgMbAQAXIHalD2NUzM7abX6QY+2qWNLVMC+ch2xnVyrlf89ZH/IV");
Yingdi Yu348f5ea2014-03-01 14:47:25 -080081
82BOOST_AUTO_TEST_CASE(IdCert)
Yingdi Yufa4ce792014-02-06 18:09:22 -080083{
Yingdi Yu0b0a7362014-08-05 16:31:30 -070084 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 Yu348f5ea2014-03-01 14:47:25 -080088 BOOST_CHECK(static_cast<bool>(idCert));
Yingdi Yufa4ce792014-02-06 18:09:22 -080089
Yingdi Yu0b0a7362014-08-05 16:31:30 -070090 const ndn::Certificate::SubjectDescriptionList& subjectDescription =
91 idCert->getSubjectDescriptionList();
Yingdi Yu348f5ea2014-03-01 14:47:25 -080092 BOOST_CHECK_EQUAL(subjectDescription.size(), 6);
93
Yingdi Yu0b0a7362014-08-05 16:31:30 -070094 ndn::Certificate::SubjectDescriptionList::const_iterator it = subjectDescription.begin();
95 ndn::Certificate::SubjectDescriptionList::const_iterator end = subjectDescription.end();
Yingdi Yu348f5ea2014-03-01 14:47:25 -080096 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 Yu0b0a7362014-08-05 16:31:30 -0700132 BOOST_CHECK_EQUAL(idCert->getName().toUri(),
133 "/EndorseCertificateTests/KEY/EncodeDecode/ksk-1394072147335/ID-CERT/%FD%01D%95%2C%D5%E8");
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800134
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700135 ndn::OBufferStream keyOs;
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800136 {
137 using namespace CryptoPP;
138 StringSource(testKey, true, new Base64Decoder(new FileSink(keyOs)));
139 }
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700140 ndn::PublicKey key(keyOs.buf()->buf(), keyOs.buf()->size());
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800141 BOOST_CHECK(key == idCert->getPublicKeyInfo());
142}
143
144BOOST_AUTO_TEST_CASE(ConstructFromIdCert)
145{
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700146 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 Yu348f5ea2014-03-01 14:47:25 -0800150 Profile profile(*idCert);
Yingdi Yufa4ce792014-02-06 18:09:22 -0800151 vector<string> endorseList;
152 endorseList.push_back("email");
153 endorseList.push_back("homepage");
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800154 EndorseCertificate endorseCertificate(*idCert, profile, endorseList);
Yingdi Yufa4ce792014-02-06 18:09:22 -0800155
Qiuhan Ding52f13832015-03-06 14:05:59 -0800156 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 Yu0b0a7362014-08-05 16:31:30 -0700161 keyChain.signWithSha256(endorseCertificate);
Yingdi Yufa4ce792014-02-06 18:09:22 -0800162 const Block& endorseDataBlock = endorseCertificate.wireEncode();
163
164 Data decodedEndorseData;
Yingdi Yufa4ce792014-02-06 18:09:22 -0800165 decodedEndorseData.wireDecode(endorseDataBlock);
166 EndorseCertificate decodedEndorse(decodedEndorseData);
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700167 BOOST_CHECK_EQUAL(decodedEndorse.getProfile().get("IDENTITY"),
168 "/EndorseCertificateTests/EncodeDecode");
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800169 BOOST_CHECK_EQUAL(decodedEndorse.getProfile().get("name"), "MyName");
170 BOOST_CHECK_EQUAL(decodedEndorse.getProfile().get("homepage"), "MyHomePage");
Yingdi Yufa4ce792014-02-06 18:09:22 -0800171 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 Yu0b0a7362014-08-05 16:31:30 -0700174 BOOST_CHECK_EQUAL(decodedEndorse.getSigner(),
175 "/EndorseCertificateTests/EncodeDecode/ksk-1394072147335");
176 BOOST_CHECK_EQUAL(decodedEndorse.getPublicKeyName(),
177 "/EndorseCertificateTests/EncodeDecode/ksk-1394072147335");
Yingdi Yufa4ce792014-02-06 18:09:22 -0800178}
179
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800180BOOST_AUTO_TEST_CASE(ConstructFromEndorseCert)
181{
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700182 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 Yufa4ce792014-02-06 18:09:22 -0800185
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800186 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 Ding52f13832015-03-06 14:05:59 -0800194 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 Yu348f5ea2014-03-01 14:47:25 -0800199 keyChain.signWithSha256(endorseCertificate);
200
201 const Block& endorseDataBlock = endorseCertificate.wireEncode();
202
203 Data decodedEndorseData;
204 decodedEndorseData.wireDecode(endorseDataBlock);
205 EndorseCertificate decodedEndorse(decodedEndorseData);
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700206 BOOST_CHECK_EQUAL(decodedEndorse.getProfile().get("IDENTITY"),
207 "/EndorseCertificateTests/EncodeDecode");
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800208 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 Yu0b0a7362014-08-05 16:31:30 -0700214 BOOST_CHECK_EQUAL(decodedEndorse.getSigner(),
215 "/EndorseCertificateTests/Singer/ksk-1234567890");
216 BOOST_CHECK_EQUAL(decodedEndorse.getPublicKeyName(),
217 "/EndorseCertificateTests/EncodeDecode/ksk-1394072147335");
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800218}
Yingdi Yufa4ce792014-02-06 18:09:22 -0800219
220BOOST_AUTO_TEST_SUITE_END()
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700221
Qiuhan Ding52f13832015-03-06 14:05:59 -0800222} // namespace tests
223} // namespace chronochat