blob: 09eb9357ff903b79f23cf3ba86f4c509b9c805b3 [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>
17
Yingdi Yu0b0a7362014-08-05 16:31:30 -070018#include <ndn-cxx/security/key-chain.hpp>
19#include <ndn-cxx/encoding/buffer-stream.hpp>
20#include <ndn-cxx/util/time.hpp>
21#include <ndn-cxx/util/io.hpp>
22#include "cryptopp.hpp"
23#include "endorse-certificate.hpp"
Yingdi Yufa4ce792014-02-06 18:09:22 -080024
Yingdi Yu0b0a7362014-08-05 16:31:30 -070025namespace chronos {
26
27using std::vector;
28using std::string;
29
30using ndn::KeyChain;
31using ndn::IdentityCertificate;
Yingdi Yufa4ce792014-02-06 18:09:22 -080032
33BOOST_AUTO_TEST_SUITE(TestEndorseCertificate)
34
Yingdi Yu348f5ea2014-03-01 14:47:25 -080035const string testIdCert("\
36Bv0DXwdRCBdFbmRvcnNlQ2VydGlmaWNhdGVUZXN0cwgDS0VZCAxFbmNvZGVEZWNv\
37ZGUIEWtzay0xMzk0MDcyMTQ3MzM1CAdJRC1DRVJUCAf9AUSVLNXoFAMYAQIV/QG8\
38MIIBuDAiGA8yMDE0MDMwNjAyMTU0N1oYDzIwMTQwMzEzMDkyNzQ3WjBuMA0GA1UE\
39KRMGTXlOYW1lMBIGA1UECxMLTXlJbnN0aXR1dGUwDgYDVQQBEwdNeUdyb3VwMBEG\
40A1UEAxMKTXlIb21lUGFnZTAQBgNVBFATCU15QWR2aXNvcjAUBgkqhkiG9w0BCQET\
41B015RW1haWwwggEgMA0GCSqGSIb3DQEBAQUAA4IBDQAwggEIAoIBAQDYsWD0ixQF\
42RfYs36BHNsRNv5ouEL69oaS6XX/hsQN1By4RNI6eSG5DpajtAwK1y+DXPwkLHd5S\
43BrvwLzReF7SsrF2ObawznU14GKaQdbn+eVIER7CWvSpJhH5yKS4fCPRN+b1MP8QS\
44DLvaaGu15T98cgVscIEqFkLfnWSQbdN6EnodjOH27JkBCz8Lxv9GZLrhfKGzOylR\
45fLzvCIyIXYl6HWroO+xTJQaP+miSZNVGyf4jYqz5WbQH56a9ZjUldTphjuDbBjUq\
46QaNVOzoKT+H4qh8mn399aQ9/BjM+6/WgrSw7/MO2UCgoZhySQY4HVqzUVVWnYwOU\
47NYPoOS3HdvGLAgERFkEbAQEcPAc6CBdFbmRvcnNlQ2VydGlmaWNhdGVUZXN0cwgD\
48S0VZCBFrc2stMTM5NDA3MjE0NzEyOAgHSUQtQ0VSVBf9AQARSwS/CelRRSUO4Tik\
495Q+L5zusaqq5652T92/83S5l38dO41BOf5fBUb3RtnFSbS/QaBCRfRJtDvkN2LhE\
50vksJjSAoAKUzx27UyM1eq7L8DDvsvC9mbwxGzTK2F1t3Jy81rk5X34MecvztlILs\
51nLqzqqiwl3dS1xyvg9GZez5g1yoOtRwzkHaah6svLVwzwM7kECXWRf4NoHTazWQo\
52Cs6s60F9I/xBRKJ4Cw2L/AzvB5sX1J4HvHCsplbR/GdvA8uW6i8pp7kjIhjCGewK\
53uNfH/4lHxzTl3pjsVy+EHKmwSlZ+T8cy5qaIEHxhbOzMNNVdit7XEwexOE66AVza\
5492On");
Yingdi Yufa4ce792014-02-06 18:09:22 -080055
Yingdi Yu348f5ea2014-03-01 14:47:25 -080056const string testKey("\
57MIIBIDANBgkqhkiG9w0BAQEFAAOCAQ0AMIIBCAKCAQEA2LFg9IsUBUX2LN+gRzbE\
58Tb+aLhC+vaGkul1/4bEDdQcuETSOnkhuQ6Wo7QMCtcvg1z8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Y1KkGjVTs6Ck/h+KofJp9/fWkPfwYzPuv1oK0sO/zDtlAo\
77KGYckkGOB1as1FVVp2MDlDWD6Dktx3bxiwIBETCB6DCBwAYHKwYBBSACAQEB/wSB\
78sQoxCghJREVOVElUWRIlL0VuZG9yc2VDZXJ0aWZpY2F0ZVRlc3RzL0VuY29kZURl\
79Y29kZQoUCgdhZHZpc29yEglNeUFkdmlzb3IKEAoFZW1haWwSB015RW1haWwKEAoF\
80Z3JvdXASB015R3JvdXAKFgoIaG9tZXBhZ2USCk15SG9tZVBhZ2UKGgoLaW5zdGl0\
81dXRpb24SC015SW5zdGl0dXRlCg4KBG5hbWUSBk15TmFtZTAjBgcrBgEFIAICAQH/\
82BBUKBwoFZW1haWwKCgoIaG9tZXBhZ2UWAxsBABcgS7pYcBk1e4dlsag8minK+UzI\
83L8ViVS87k09gaM6GeUA=");
84
85BOOST_AUTO_TEST_CASE(IdCert)
Yingdi Yufa4ce792014-02-06 18:09:22 -080086{
Yingdi Yu0b0a7362014-08-05 16:31:30 -070087 boost::iostreams::stream<boost::iostreams::array_source> is(testIdCert.c_str(),
88 testIdCert.size());
89 shared_ptr<IdentityCertificate> idCert = ndn::io::load<IdentityCertificate>(is);
90
Yingdi Yu348f5ea2014-03-01 14:47:25 -080091 BOOST_CHECK(static_cast<bool>(idCert));
Yingdi Yufa4ce792014-02-06 18:09:22 -080092
Yingdi Yu0b0a7362014-08-05 16:31:30 -070093 const ndn::Certificate::SubjectDescriptionList& subjectDescription =
94 idCert->getSubjectDescriptionList();
Yingdi Yu348f5ea2014-03-01 14:47:25 -080095 BOOST_CHECK_EQUAL(subjectDescription.size(), 6);
96
Yingdi Yu0b0a7362014-08-05 16:31:30 -070097 ndn::Certificate::SubjectDescriptionList::const_iterator it = subjectDescription.begin();
98 ndn::Certificate::SubjectDescriptionList::const_iterator end = subjectDescription.end();
Yingdi Yu348f5ea2014-03-01 14:47:25 -080099 int count = 0;
100 for(; it != end; it++)
101 {
102 if(it->getOidString() == "2.5.4.41")
103 {
104 BOOST_CHECK_EQUAL(it->getValue(), "MyName");
105 count++;
106 }
107 if(it->getOidString() == "2.5.4.11")
108 {
109 BOOST_CHECK_EQUAL(it->getValue(), "MyInstitute");
110 count++;
111 }
112 if(it->getOidString() == "2.5.4.1")
113 {
114 BOOST_CHECK_EQUAL(it->getValue(), "MyGroup");
115 count++;
116 }
117 if(it->getOidString() == "2.5.4.3")
118 {
119 BOOST_CHECK_EQUAL(it->getValue(), "MyHomePage");
120 count++;
121 }
122 if(it->getOidString() == "2.5.4.80")
123 {
124 BOOST_CHECK_EQUAL(it->getValue(), "MyAdvisor");
125 count++;
126 }
127 if(it->getOidString() == "1.2.840.113549.1.9.1")
128 {
129 BOOST_CHECK_EQUAL(it->getValue(), "MyEmail");
130 count++;
131 }
132 }
133 BOOST_CHECK_EQUAL(count, 6);
134
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700135 BOOST_CHECK_EQUAL(idCert->getName().toUri(),
136 "/EndorseCertificateTests/KEY/EncodeDecode/ksk-1394072147335/ID-CERT/%FD%01D%95%2C%D5%E8");
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800137
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700138 ndn::OBufferStream keyOs;
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800139 {
140 using namespace CryptoPP;
141 StringSource(testKey, true, new Base64Decoder(new FileSink(keyOs)));
142 }
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700143 ndn::PublicKey key(keyOs.buf()->buf(), keyOs.buf()->size());
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800144 BOOST_CHECK(key == idCert->getPublicKeyInfo());
145}
146
147BOOST_AUTO_TEST_CASE(ConstructFromIdCert)
148{
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700149 boost::iostreams::stream<boost::iostreams::array_source> is(testIdCert.c_str(),
150 testIdCert.size());
151 shared_ptr<IdentityCertificate> idCert = ndn::io::load<IdentityCertificate>(is);
152
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800153 Profile profile(*idCert);
Yingdi Yufa4ce792014-02-06 18:09:22 -0800154 vector<string> endorseList;
155 endorseList.push_back("email");
156 endorseList.push_back("homepage");
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800157 EndorseCertificate endorseCertificate(*idCert, profile, endorseList);
Yingdi Yufa4ce792014-02-06 18:09:22 -0800158
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700159 KeyChain keyChain("sqlite3", "file");
160 keyChain.signWithSha256(endorseCertificate);
Yingdi Yufa4ce792014-02-06 18:09:22 -0800161 const Block& endorseDataBlock = endorseCertificate.wireEncode();
162
163 Data decodedEndorseData;
Yingdi Yufa4ce792014-02-06 18:09:22 -0800164 decodedEndorseData.wireDecode(endorseDataBlock);
165 EndorseCertificate decodedEndorse(decodedEndorseData);
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700166 BOOST_CHECK_EQUAL(decodedEndorse.getProfile().get("IDENTITY"),
167 "/EndorseCertificateTests/EncodeDecode");
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800168 BOOST_CHECK_EQUAL(decodedEndorse.getProfile().get("name"), "MyName");
169 BOOST_CHECK_EQUAL(decodedEndorse.getProfile().get("homepage"), "MyHomePage");
Yingdi Yufa4ce792014-02-06 18:09:22 -0800170 BOOST_CHECK_EQUAL(decodedEndorse.getEndorseList().size(), 2);
171 BOOST_CHECK_EQUAL(decodedEndorse.getEndorseList().at(0), "email");
172 BOOST_CHECK_EQUAL(decodedEndorse.getEndorseList().at(1), "homepage");
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700173 BOOST_CHECK_EQUAL(decodedEndorse.getSigner(),
174 "/EndorseCertificateTests/EncodeDecode/ksk-1394072147335");
175 BOOST_CHECK_EQUAL(decodedEndorse.getPublicKeyName(),
176 "/EndorseCertificateTests/EncodeDecode/ksk-1394072147335");
Yingdi Yufa4ce792014-02-06 18:09:22 -0800177}
178
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800179BOOST_AUTO_TEST_CASE(ConstructFromEndorseCert)
180{
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700181 boost::iostreams::stream<boost::iostreams::array_source> is(testEndorseCert.c_str(),
182 testEndorseCert.size());
183 shared_ptr<Data> rawData = ndn::io::load<Data>(is);
Yingdi Yufa4ce792014-02-06 18:09:22 -0800184
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800185 EndorseCertificate rawEndorse(*rawData);
186 vector<string> endorseList;
187 endorseList.push_back("institution");
188 endorseList.push_back("group");
189 endorseList.push_back("advisor");
190 Name signer("/EndorseCertificateTests/Singer/ksk-1234567890");
191 EndorseCertificate endorseCertificate(rawEndorse, signer, endorseList);
192
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700193 KeyChain keyChain("sqlite3", "file");
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800194 keyChain.signWithSha256(endorseCertificate);
195
196 const Block& endorseDataBlock = endorseCertificate.wireEncode();
197
198 Data decodedEndorseData;
199 decodedEndorseData.wireDecode(endorseDataBlock);
200 EndorseCertificate decodedEndorse(decodedEndorseData);
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700201 BOOST_CHECK_EQUAL(decodedEndorse.getProfile().get("IDENTITY"),
202 "/EndorseCertificateTests/EncodeDecode");
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800203 BOOST_CHECK_EQUAL(decodedEndorse.getProfile().get("name"), "MyName");
204 BOOST_CHECK_EQUAL(decodedEndorse.getProfile().get("homepage"), "MyHomePage");
205 BOOST_CHECK_EQUAL(decodedEndorse.getEndorseList().size(), 3);
206 BOOST_CHECK_EQUAL(decodedEndorse.getEndorseList().at(0), "institution");
207 BOOST_CHECK_EQUAL(decodedEndorse.getEndorseList().at(1), "group");
208 BOOST_CHECK_EQUAL(decodedEndorse.getEndorseList().at(2), "advisor");
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700209 BOOST_CHECK_EQUAL(decodedEndorse.getSigner(),
210 "/EndorseCertificateTests/Singer/ksk-1234567890");
211 BOOST_CHECK_EQUAL(decodedEndorse.getPublicKeyName(),
212 "/EndorseCertificateTests/EncodeDecode/ksk-1394072147335");
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800213}
Yingdi Yufa4ce792014-02-06 18:09:22 -0800214
215BOOST_AUTO_TEST_SUITE_END()
Yingdi Yu0b0a7362014-08-05 16:31:30 -0700216
217} // namespace chronos