blob: 95bc012723ce99ff2f2ee2fa61b6963ddf591dbf [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 Yufa4ce792014-02-06 18:09:22 -080018#include <ndn-cpp-dev/security/key-chain.hpp>
19#include <ndn-cpp-dev/util/time.hpp>
Yingdi Yu348f5ea2014-03-01 14:47:25 -080020#include <ndn-cpp-dev/util/io.hpp>
21#include <cryptopp/base64.h>
22#include <cryptopp/files.h>
Yingdi Yufa4ce792014-02-06 18:09:22 -080023#include "endorse-certificate.h"
24
25using namespace ndn;
26using namespace std;
27using namespace chronos;
28
29BOOST_AUTO_TEST_SUITE(TestEndorseCertificate)
30
Yingdi Yu348f5ea2014-03-01 14:47:25 -080031const string testIdCert("\
32Bv0DXwdRCBdFbmRvcnNlQ2VydGlmaWNhdGVUZXN0cwgDS0VZCAxFbmNvZGVEZWNv\
33ZGUIEWtzay0xMzk0MDcyMTQ3MzM1CAdJRC1DRVJUCAf9AUSVLNXoFAMYAQIV/QG8\
34MIIBuDAiGA8yMDE0MDMwNjAyMTU0N1oYDzIwMTQwMzEzMDkyNzQ3WjBuMA0GA1UE\
35KRMGTXlOYW1lMBIGA1UECxMLTXlJbnN0aXR1dGUwDgYDVQQBEwdNeUdyb3VwMBEG\
36A1UEAxMKTXlIb21lUGFnZTAQBgNVBFATCU15QWR2aXNvcjAUBgkqhkiG9w0BCQET\
37B015RW1haWwwggEgMA0GCSqGSIb3DQEBAQUAA4IBDQAwggEIAoIBAQDYsWD0ixQF\
38RfYs36BHNsRNv5ouEL69oaS6XX/hsQN1By4RNI6eSG5DpajtAwK1y+DXPwkLHd5S\
39BrvwLzReF7SsrF2ObawznU14GKaQdbn+eVIER7CWvSpJhH5yKS4fCPRN+b1MP8QS\
40DLvaaGu15T98cgVscIEqFkLfnWSQbdN6EnodjOH27JkBCz8Lxv9GZLrhfKGzOylR\
41fLzvCIyIXYl6HWroO+xTJQaP+miSZNVGyf4jYqz5WbQH56a9ZjUldTphjuDbBjUq\
42QaNVOzoKT+H4qh8mn399aQ9/BjM+6/WgrSw7/MO2UCgoZhySQY4HVqzUVVWnYwOU\
43NYPoOS3HdvGLAgERFkEbAQEcPAc6CBdFbmRvcnNlQ2VydGlmaWNhdGVUZXN0cwgD\
44S0VZCBFrc2stMTM5NDA3MjE0NzEyOAgHSUQtQ0VSVBf9AQARSwS/CelRRSUO4Tik\
455Q+L5zusaqq5652T92/83S5l38dO41BOf5fBUb3RtnFSbS/QaBCRfRJtDvkN2LhE\
46vksJjSAoAKUzx27UyM1eq7L8DDvsvC9mbwxGzTK2F1t3Jy81rk5X34MecvztlILs\
47nLqzqqiwl3dS1xyvg9GZez5g1yoOtRwzkHaah6svLVwzwM7kECXWRf4NoHTazWQo\
48Cs6s60F9I/xBRKJ4Cw2L/AzvB5sX1J4HvHCsplbR/GdvA8uW6i8pp7kjIhjCGewK\
49uNfH/4lHxzTl3pjsVy+EHKmwSlZ+T8cy5qaIEHxhbOzMNNVdit7XEwexOE66AVza\
5092On");
Yingdi Yufa4ce792014-02-06 18:09:22 -080051
Yingdi Yu348f5ea2014-03-01 14:47:25 -080052const string testKey("\
53MIIBIDANBgkqhkiG9w0BAQEFAAOCAQ0AMIIBCAKCAQEA2LFg9IsUBUX2LN+gRzbE\
54Tb+aLhC+vaGkul1/4bEDdQcuETSOnkhuQ6Wo7QMCtcvg1z8JCx3eUga78C80Xhe0\
55rKxdjm2sM51NeBimkHW5/nlSBEewlr0qSYR+cikuHwj0Tfm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Vp2MDlDWD6Dktx3bxiwIBETCB6DCBwAYHKwYBBSACAQEB/wSB\
74sQoxCghJREVOVElUWRIlL0VuZG9yc2VDZXJ0aWZpY2F0ZVRlc3RzL0VuY29kZURl\
75Y29kZQoUCgdhZHZpc29yEglNeUFkdmlzb3IKEAoFZW1haWwSB015RW1haWwKEAoF\
76Z3JvdXASB015R3JvdXAKFgoIaG9tZXBhZ2USCk15SG9tZVBhZ2UKGgoLaW5zdGl0\
77dXRpb24SC015SW5zdGl0dXRlCg4KBG5hbWUSBk15TmFtZTAjBgcrBgEFIAICAQH/\
78BBUKBwoFZW1haWwKCgoIaG9tZXBhZ2UWAxsBABcgS7pYcBk1e4dlsag8minK+UzI\
79L8ViVS87k09gaM6GeUA=");
80
81BOOST_AUTO_TEST_CASE(IdCert)
Yingdi Yufa4ce792014-02-06 18:09:22 -080082{
Yingdi Yu348f5ea2014-03-01 14:47:25 -080083 boost::iostreams::stream<boost::iostreams::array_source> is (testIdCert.c_str(), testIdCert.size());
84 shared_ptr<IdentityCertificate> idCert = io::load<IdentityCertificate>(is);
85
86 BOOST_CHECK(static_cast<bool>(idCert));
Yingdi Yufa4ce792014-02-06 18:09:22 -080087
Yingdi Yu348f5ea2014-03-01 14:47:25 -080088 const Certificate::SubjectDescriptionList& subjectDescription = idCert->getSubjectDescriptionList();
89 BOOST_CHECK_EQUAL(subjectDescription.size(), 6);
90
91 Certificate::SubjectDescriptionList::const_iterator it = subjectDescription.begin();
92 Certificate::SubjectDescriptionList::const_iterator end = subjectDescription.end();
93 int count = 0;
94 for(; it != end; it++)
95 {
96 if(it->getOidString() == "2.5.4.41")
97 {
98 BOOST_CHECK_EQUAL(it->getValue(), "MyName");
99 count++;
100 }
101 if(it->getOidString() == "2.5.4.11")
102 {
103 BOOST_CHECK_EQUAL(it->getValue(), "MyInstitute");
104 count++;
105 }
106 if(it->getOidString() == "2.5.4.1")
107 {
108 BOOST_CHECK_EQUAL(it->getValue(), "MyGroup");
109 count++;
110 }
111 if(it->getOidString() == "2.5.4.3")
112 {
113 BOOST_CHECK_EQUAL(it->getValue(), "MyHomePage");
114 count++;
115 }
116 if(it->getOidString() == "2.5.4.80")
117 {
118 BOOST_CHECK_EQUAL(it->getValue(), "MyAdvisor");
119 count++;
120 }
121 if(it->getOidString() == "1.2.840.113549.1.9.1")
122 {
123 BOOST_CHECK_EQUAL(it->getValue(), "MyEmail");
124 count++;
125 }
126 }
127 BOOST_CHECK_EQUAL(count, 6);
128
129 BOOST_CHECK_EQUAL(idCert->getName().toUri(), "/EndorseCertificateTests/KEY/EncodeDecode/ksk-1394072147335/ID-CERT/%FD%01D%95%2C%D5%E8");
130
131 OBufferStream keyOs;
132 {
133 using namespace CryptoPP;
134 StringSource(testKey, true, new Base64Decoder(new FileSink(keyOs)));
135 }
136 PublicKey key(keyOs.buf()->buf(), keyOs.buf()->size());
137 BOOST_CHECK(key == idCert->getPublicKeyInfo());
138}
139
140BOOST_AUTO_TEST_CASE(ConstructFromIdCert)
141{
142 boost::iostreams::stream<boost::iostreams::array_source> is (testIdCert.c_str(), testIdCert.size());
143 shared_ptr<IdentityCertificate> idCert = io::load<IdentityCertificate>(is);
144
145 Profile profile(*idCert);
Yingdi Yufa4ce792014-02-06 18:09:22 -0800146 vector<string> endorseList;
147 endorseList.push_back("email");
148 endorseList.push_back("homepage");
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800149 EndorseCertificate endorseCertificate(*idCert, profile, endorseList);
Yingdi Yufa4ce792014-02-06 18:09:22 -0800150
151 KeyChainImpl<SecPublicInfoSqlite3, SecTpmFile> keyChain;
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800152 keyChain.signWithSha256(endorseCertificate);
Yingdi Yufa4ce792014-02-06 18:09:22 -0800153 const Block& endorseDataBlock = endorseCertificate.wireEncode();
154
155 Data decodedEndorseData;
Yingdi Yufa4ce792014-02-06 18:09:22 -0800156 decodedEndorseData.wireDecode(endorseDataBlock);
157 EndorseCertificate decodedEndorse(decodedEndorseData);
158 BOOST_CHECK_EQUAL(decodedEndorse.getProfile().get("IDENTITY"), "/EndorseCertificateTests/EncodeDecode");
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800159 BOOST_CHECK_EQUAL(decodedEndorse.getProfile().get("name"), "MyName");
160 BOOST_CHECK_EQUAL(decodedEndorse.getProfile().get("homepage"), "MyHomePage");
Yingdi Yufa4ce792014-02-06 18:09:22 -0800161 BOOST_CHECK_EQUAL(decodedEndorse.getEndorseList().size(), 2);
162 BOOST_CHECK_EQUAL(decodedEndorse.getEndorseList().at(0), "email");
163 BOOST_CHECK_EQUAL(decodedEndorse.getEndorseList().at(1), "homepage");
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800164 BOOST_CHECK_EQUAL(decodedEndorse.getSigner(), "/EndorseCertificateTests/EncodeDecode/ksk-1394072147335");
165 BOOST_CHECK_EQUAL(decodedEndorse.getPublicKeyName(), "/EndorseCertificateTests/EncodeDecode/ksk-1394072147335");
Yingdi Yufa4ce792014-02-06 18:09:22 -0800166}
167
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800168BOOST_AUTO_TEST_CASE(ConstructFromEndorseCert)
169{
170 boost::iostreams::stream<boost::iostreams::array_source> is (testEndorseCert.c_str(), testEndorseCert.size());
171 shared_ptr<Data> rawData = io::load<Data>(is);
Yingdi Yufa4ce792014-02-06 18:09:22 -0800172
Yingdi Yu348f5ea2014-03-01 14:47:25 -0800173 EndorseCertificate rawEndorse(*rawData);
174 vector<string> endorseList;
175 endorseList.push_back("institution");
176 endorseList.push_back("group");
177 endorseList.push_back("advisor");
178 Name signer("/EndorseCertificateTests/Singer/ksk-1234567890");
179 EndorseCertificate endorseCertificate(rawEndorse, signer, endorseList);
180
181 KeyChainImpl<SecPublicInfoSqlite3, SecTpmFile> keyChain;
182 keyChain.signWithSha256(endorseCertificate);
183
184 const Block& endorseDataBlock = endorseCertificate.wireEncode();
185
186 Data decodedEndorseData;
187 decodedEndorseData.wireDecode(endorseDataBlock);
188 EndorseCertificate decodedEndorse(decodedEndorseData);
189 BOOST_CHECK_EQUAL(decodedEndorse.getProfile().get("IDENTITY"), "/EndorseCertificateTests/EncodeDecode");
190 BOOST_CHECK_EQUAL(decodedEndorse.getProfile().get("name"), "MyName");
191 BOOST_CHECK_EQUAL(decodedEndorse.getProfile().get("homepage"), "MyHomePage");
192 BOOST_CHECK_EQUAL(decodedEndorse.getEndorseList().size(), 3);
193 BOOST_CHECK_EQUAL(decodedEndorse.getEndorseList().at(0), "institution");
194 BOOST_CHECK_EQUAL(decodedEndorse.getEndorseList().at(1), "group");
195 BOOST_CHECK_EQUAL(decodedEndorse.getEndorseList().at(2), "advisor");
196 BOOST_CHECK_EQUAL(decodedEndorse.getSigner(), "/EndorseCertificateTests/Singer/ksk-1234567890");
197 BOOST_CHECK_EQUAL(decodedEndorse.getPublicKeyName(), "/EndorseCertificateTests/EncodeDecode/ksk-1394072147335");
198}
Yingdi Yufa4ce792014-02-06 18:09:22 -0800199
200BOOST_AUTO_TEST_SUITE_END()