blob: 5f8e06b8dadf12b555b7f719fc60a812dabae70d [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Yingdi Yu8dceb1d2014-02-18 12:45:10 -08002/**
Alexander Afanasyev34a37632015-01-16 17:37:36 -08003 * Copyright (c) 2013-2015 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
Yingdi Yu8dceb1d2014-02-18 12:45:10 -080020 */
21
Yingdi Yu8dceb1d2014-02-18 12:45:10 -080022#include "security/key-chain.hpp"
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -070023#include "../util/test-home-environment-fixture.hpp"
Yingdi Yuf56c68f2014-04-24 21:50:13 -070024#include <boost/filesystem.hpp>
Yingdi Yu8dceb1d2014-02-18 12:45:10 -080025
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070026#include "boost-test.hpp"
Alexander Afanasyev07113802015-01-15 19:14:36 -080027#include "dummy-keychain.hpp"
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070028
Yingdi Yu8dceb1d2014-02-18 12:45:10 -080029namespace ndn {
Yingdi Yuf56c68f2014-04-24 21:50:13 -070030namespace tests {
Yingdi Yu8dceb1d2014-02-18 12:45:10 -080031
Yingdi Yu5ec0ee32014-06-24 16:26:09 -070032using std::vector;
33
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -070034BOOST_FIXTURE_TEST_SUITE(SecurityTestKeyChain, util::TestHomeEnvironmentFixture)
Yingdi Yuf56c68f2014-04-24 21:50:13 -070035
36BOOST_AUTO_TEST_CASE(ConstructorNormalConfig)
37{
38 using namespace boost::filesystem;
39
Alexander Afanasyev8b1674a2014-05-15 00:58:43 -070040 setenv("TEST_HOME", "tests/unit-tests/security/config-file-home", 1);
Yingdi Yuf56c68f2014-04-24 21:50:13 -070041
42 BOOST_REQUIRE_NO_THROW(KeyChain());
43
Alexander Afanasyev07113802015-01-15 19:14:36 -080044 KeyChain keyChain;
45 BOOST_CHECK_EQUAL(keyChain.getPib().getPibLocator(),
46 "pib-sqlite3:/tmp/test/ndn-cxx/keychain/sqlite3-file/");
47 BOOST_CHECK_EQUAL(keyChain.getPib().getTpmLocator(),
48 "tpm-file:/tmp/test/ndn-cxx/keychain/sqlite3-file/");
49 BOOST_CHECK_EQUAL(keyChain.getTpm().getTpmLocator(),
50 "tpm-file:/tmp/test/ndn-cxx/keychain/sqlite3-file/");
51
Yingdi Yuf56c68f2014-04-24 21:50:13 -070052 path pibPath(absolute(std::getenv("TEST_HOME")));
53 pibPath /= ".ndn/ndnsec-public-info.db";
54
55 boost::filesystem::remove(pibPath);
56}
57
58BOOST_AUTO_TEST_CASE(ConstructorEmptyConfig)
59{
60 using namespace boost::filesystem;
61
Alexander Afanasyev8b1674a2014-05-15 00:58:43 -070062 setenv("TEST_HOME", "tests/unit-tests/security/config-file-empty-home", 1);
Yingdi Yuf56c68f2014-04-24 21:50:13 -070063
Yingdi Yu41546342014-11-30 23:37:53 -080064#if defined(NDN_CXX_HAVE_OSX_SECURITY)
65 std::string oldHOME;
66 if (std::getenv("OLD_HOME"))
67 oldHOME = std::getenv("OLD_HOME");
68
69 std::string HOME;
70 if (std::getenv("HOME"))
71 HOME = std::getenv("HOME");
72
73 if (!oldHOME.empty())
74 setenv("HOME", oldHOME.c_str(), 1);
75 else
76 unsetenv("HOME");
77#endif
78
79 BOOST_REQUIRE_NO_THROW(KeyChain());
Alexander Afanasyev07113802015-01-15 19:14:36 -080080 KeyChain keyChain;
81 BOOST_CHECK_EQUAL(keyChain.getPib().getPibLocator(),
82 "pib-sqlite3:/tmp/test/ndn-cxx/keychain/sqlite3-empty/");
83
84#if defined(NDN_CXX_HAVE_OSX_SECURITY)
85 BOOST_CHECK_EQUAL(keyChain.getPib().getTpmLocator(), "tpm-osxkeychain:");
86 BOOST_CHECK_EQUAL(keyChain.getTpm().getTpmLocator(), "tpm-osxkeychain:");
87#else
88 BOOST_CHECK_EQUAL(keyChain.getPib().getTpmLocator(),
89 "tpm-file:");
90 BOOST_CHECK_EQUAL(keyChain.getTpm().getTpmLocator(),
91 "tpm-file:");
92#endif
Yingdi Yu41546342014-11-30 23:37:53 -080093
94#if defined(NDN_CXX_HAVE_OSX_SECURITY)
95 if (!HOME.empty())
96 setenv("HOME", HOME.c_str(), 1);
97 else
98 unsetenv("HOME");
99#endif
100
101 path pibPath(absolute(std::getenv("TEST_HOME")));
102 pibPath /= ".ndn/ndnsec-public-info.db";
103
104 boost::filesystem::remove(pibPath);
105}
106
107BOOST_AUTO_TEST_CASE(ConstructorEmpty2Config)
108{
109 using namespace boost::filesystem;
110
111 setenv("TEST_HOME", "tests/unit-tests/security/config-file-empty2-home", 1);
112
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700113 BOOST_REQUIRE_NO_THROW(KeyChain());
114
Alexander Afanasyev07113802015-01-15 19:14:36 -0800115 KeyChain keyChain;
116 BOOST_CHECK_EQUAL(keyChain.getPib().getPibLocator(),
117 "pib-sqlite3:");
118 BOOST_CHECK_EQUAL(keyChain.getPib().getTpmLocator(),
119 "tpm-file:/tmp/test/ndn-cxx/keychain/empty-file/");
120 BOOST_CHECK_EQUAL(keyChain.getTpm().getTpmLocator(),
121 "tpm-file:/tmp/test/ndn-cxx/keychain/empty-file/");
122
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700123 path pibPath(absolute(std::getenv("TEST_HOME")));
124 pibPath /= ".ndn/ndnsec-public-info.db";
125
126 boost::filesystem::remove(pibPath);
127}
128
129BOOST_AUTO_TEST_CASE(ConstructorMalConfig)
130{
131 using namespace boost::filesystem;
132
Alexander Afanasyev8b1674a2014-05-15 00:58:43 -0700133 setenv("TEST_HOME", "tests/unit-tests/security/config-file-malformed-home", 1);
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700134
135 BOOST_REQUIRE_THROW(KeyChain(), KeyChain::Error); // Wrong configuration. Error expected.
136}
137
138BOOST_AUTO_TEST_CASE(ConstructorMal2Config)
139{
140 using namespace boost::filesystem;
141
Alexander Afanasyev8b1674a2014-05-15 00:58:43 -0700142 setenv("TEST_HOME", "tests/unit-tests/security/config-file-malformed2-home", 1);
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700143
144 BOOST_REQUIRE_THROW(KeyChain(), KeyChain::Error); // Wrong configuration. Error expected.
145}
146
147BOOST_AUTO_TEST_CASE(ExportIdentity)
148{
Yingdi Yu41546342014-11-30 23:37:53 -0800149 KeyChain keyChain;
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800150
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700151 Name identity("/TestKeyChain/ExportIdentity/");
152 identity.appendVersion();
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800153 keyChain.createIdentity(identity);
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700154
Yingdi Yu64c3fb42014-02-26 17:30:04 -0800155 shared_ptr<SecuredBag> exported = keyChain.exportIdentity(identity, "1234");
156
157 Block block = exported->wireEncode();
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800158
159 Name keyName = keyChain.getDefaultKeyNameForIdentity(identity);
160 Name certName = keyChain.getDefaultCertificateNameForKey(keyName);
161
162 keyChain.deleteIdentity(identity);
163
Yingdi Yu5ec0ee32014-06-24 16:26:09 -0700164 BOOST_CHECK_EQUAL(keyChain.doesIdentityExist(identity), false);
165 BOOST_CHECK_EQUAL(keyChain.doesPublicKeyExist(keyName), false);
166 BOOST_CHECK_EQUAL(keyChain.doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE), false);
167 BOOST_CHECK_EQUAL(keyChain.doesKeyExistInTpm(keyName, KEY_CLASS_PUBLIC), false);
168 BOOST_CHECK_EQUAL(keyChain.doesCertificateExist(certName), false);
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800169
Yingdi Yu64c3fb42014-02-26 17:30:04 -0800170 SecuredBag imported;
171 imported.wireDecode(block);
172 keyChain.importIdentity(imported, "1234");
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800173
Yingdi Yu5ec0ee32014-06-24 16:26:09 -0700174 BOOST_CHECK(keyChain.doesIdentityExist(identity));
175 BOOST_CHECK(keyChain.doesPublicKeyExist(keyName));
176 BOOST_CHECK(keyChain.doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE));
177 BOOST_CHECK(keyChain.doesKeyExistInTpm(keyName, KEY_CLASS_PUBLIC));
178 BOOST_CHECK(keyChain.doesCertificateExist(certName));
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800179
180 keyChain.deleteIdentity(identity);
181
Yingdi Yu5ec0ee32014-06-24 16:26:09 -0700182 BOOST_CHECK_EQUAL(keyChain.doesIdentityExist(identity), false);
183 BOOST_CHECK_EQUAL(keyChain.doesPublicKeyExist(keyName), false);
184 BOOST_CHECK_EQUAL(keyChain.doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE), false);
185 BOOST_CHECK_EQUAL(keyChain.doesKeyExistInTpm(keyName, KEY_CLASS_PUBLIC), false);
186 BOOST_CHECK_EQUAL(keyChain.doesCertificateExist(certName), false);
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800187}
188
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700189BOOST_AUTO_TEST_CASE(PrepareIdentityCertificate)
Yingdi Yuc55680b2014-02-26 12:31:35 -0800190{
Yingdi Yu41546342014-11-30 23:37:53 -0800191 KeyChain keyChain;
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700192
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700193 Name identity("/TestKeyChain/PrepareIdentityCertificate/");
194 identity.appendVersion();
Yingdi Yuc55680b2014-02-26 12:31:35 -0800195 keyChain.createIdentity(identity);
196
197 vector<CertificateSubjectDescription> subjectDescription;
198 Name lowerIdentity = identity;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700199 lowerIdentity.append("Lower").appendVersion();
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700200 Name lowerKeyName = keyChain.generateRsaKeyPair(lowerIdentity, true);
Yingdi Yu5ec0ee32014-06-24 16:26:09 -0700201 shared_ptr<IdentityCertificate> idCert =
202 keyChain.prepareUnsignedIdentityCertificate(lowerKeyName, identity,
203 time::system_clock::now(),
204 time::system_clock::now() + time::days(365),
205 subjectDescription);
Yingdi Yuc55680b2014-02-26 12:31:35 -0800206 BOOST_CHECK(static_cast<bool>(idCert));
Yingdi Yu5ec0ee32014-06-24 16:26:09 -0700207 BOOST_CHECK_EQUAL(idCert->getName().getPrefix(5),
208 Name().append(identity).append("KEY").append("Lower"));
Yingdi Yuc55680b2014-02-26 12:31:35 -0800209
Yingdi Yu0eb5d722014-06-10 15:06:25 -0700210 shared_ptr<IdentityCertificate> idCert11 =
211 keyChain.prepareUnsignedIdentityCertificate(lowerKeyName, identity,
212 time::system_clock::now(),
213 time::system_clock::now() + time::days(365),
214 subjectDescription,
215 lowerIdentity);
216 BOOST_CHECK(static_cast<bool>(idCert11));
Yingdi Yu5ec0ee32014-06-24 16:26:09 -0700217 BOOST_CHECK_EQUAL(idCert11->getName().getPrefix(6),
Yingdi Yu0eb5d722014-06-10 15:06:25 -0700218 Name().append(lowerIdentity).append("KEY"));
Yingdi Yuc55680b2014-02-26 12:31:35 -0800219
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700220 Name anotherIdentity("/TestKeyChain/PrepareIdentityCertificate/Another/");
221 anotherIdentity.appendVersion();
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700222 Name anotherKeyName = keyChain.generateRsaKeyPair(anotherIdentity, true);
Yingdi Yu5ec0ee32014-06-24 16:26:09 -0700223 shared_ptr<IdentityCertificate> idCert2 =
224 keyChain.prepareUnsignedIdentityCertificate(anotherKeyName, identity,
225 time::system_clock::now(),
226 time::system_clock::now() + time::days(365),
227 subjectDescription);
Yingdi Yuc55680b2014-02-26 12:31:35 -0800228 BOOST_CHECK(static_cast<bool>(idCert2));
Yingdi Yu5ec0ee32014-06-24 16:26:09 -0700229 BOOST_CHECK_EQUAL(idCert2->getName().getPrefix(5), Name().append(anotherIdentity).append("KEY"));
Yingdi Yuc55680b2014-02-26 12:31:35 -0800230
231
232 Name wrongKeyName1;
Yingdi Yu5ec0ee32014-06-24 16:26:09 -0700233 shared_ptr<IdentityCertificate> idCert3 =
234 keyChain.prepareUnsignedIdentityCertificate(wrongKeyName1, identity,
235 time::system_clock::now(),
236 time::system_clock::now() + time::days(365),
237 subjectDescription);
238 BOOST_CHECK_EQUAL(static_cast<bool>(idCert3), false);
Yingdi Yuc55680b2014-02-26 12:31:35 -0800239
240
241 Name wrongKeyName2("/TestKeyChain/PrepareIdentityCertificate");
Yingdi Yu5ec0ee32014-06-24 16:26:09 -0700242 shared_ptr<IdentityCertificate> idCert4 =
243 keyChain.prepareUnsignedIdentityCertificate(wrongKeyName2, identity,
244 time::system_clock::now(),
245 time::system_clock::now() + time::days(365),
246 subjectDescription);
247 BOOST_CHECK_EQUAL(static_cast<bool>(idCert4), false);
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700248
Yingdi Yuc55680b2014-02-26 12:31:35 -0800249
250 Name wrongKeyName3("/TestKeyChain/PrepareIdentityCertificate/ksk-1234");
Yingdi Yu5ec0ee32014-06-24 16:26:09 -0700251 shared_ptr<IdentityCertificate> idCert5 =
252 keyChain.prepareUnsignedIdentityCertificate(wrongKeyName3, identity,
253 time::system_clock::now(),
254 time::system_clock::now() + time::days(365),
255 subjectDescription);
256 BOOST_CHECK_EQUAL(static_cast<bool>(idCert5), false);
Yingdi Yuc55680b2014-02-26 12:31:35 -0800257
258 keyChain.deleteIdentity(identity);
259 keyChain.deleteIdentity(lowerIdentity);
260 keyChain.deleteIdentity(anotherIdentity);
261}
262
Yingdi Yu41546342014-11-30 23:37:53 -0800263BOOST_AUTO_TEST_CASE(Delete)
264{
265 KeyChain keyChain;
266
267 Name identity("/TestSecPublicInfoSqlite3/Delete");
268 identity.appendVersion();
269
270 Name certName1;
271 BOOST_REQUIRE_NO_THROW(certName1 = keyChain.createIdentity(identity));
272
273 Name keyName1 = IdentityCertificate::certificateNameToPublicKeyName(certName1);
274 Name keyName2;
275 BOOST_REQUIRE_NO_THROW(keyName2 = keyChain.generateRsaKeyPairAsDefault(identity));
276
277 shared_ptr<IdentityCertificate> cert2;
278 BOOST_REQUIRE_NO_THROW(cert2 = keyChain.selfSign(keyName2));
279 Name certName2 = cert2->getName();
280 BOOST_REQUIRE_NO_THROW(keyChain.addCertificateAsKeyDefault(*cert2));
281
282 Name keyName3;
283 BOOST_REQUIRE_NO_THROW(keyName3 = keyChain.generateRsaKeyPairAsDefault(identity));
284
285 shared_ptr<IdentityCertificate> cert3;
286 BOOST_REQUIRE_NO_THROW(cert3 = keyChain.selfSign(keyName3));
287 Name certName3 = cert3->getName();
288 BOOST_REQUIRE_NO_THROW(keyChain.addCertificateAsKeyDefault(*cert3));
289 shared_ptr<IdentityCertificate> cert4;
290 BOOST_REQUIRE_NO_THROW(cert4 = keyChain.selfSign(keyName3));
291 Name certName4 = cert4->getName();
292 BOOST_REQUIRE_NO_THROW(keyChain.addCertificateAsKeyDefault(*cert4));
293 shared_ptr<IdentityCertificate> cert5;
294 BOOST_REQUIRE_NO_THROW(cert5 = keyChain.selfSign(keyName3));
295 Name certName5 = cert5->getName();
296 BOOST_REQUIRE_NO_THROW(keyChain.addCertificateAsKeyDefault(*cert5));
297
298 BOOST_CHECK_EQUAL(keyChain.doesIdentityExist(identity), true);
299 BOOST_CHECK_EQUAL(keyChain.doesPublicKeyExist(keyName1), true);
300 BOOST_CHECK_EQUAL(keyChain.doesPublicKeyExist(keyName2), true);
301 BOOST_CHECK_EQUAL(keyChain.doesPublicKeyExist(keyName3), true);
302 BOOST_CHECK_EQUAL(keyChain.doesCertificateExist(certName1), true);
303 BOOST_CHECK_EQUAL(keyChain.doesCertificateExist(certName2), true);
304 BOOST_CHECK_EQUAL(keyChain.doesCertificateExist(certName3), true);
305 BOOST_CHECK_EQUAL(keyChain.doesCertificateExist(certName4), true);
306 BOOST_CHECK_EQUAL(keyChain.doesCertificateExist(certName5), true);
307
308 BOOST_REQUIRE_NO_THROW(keyChain.deleteCertificate(certName5));
309 BOOST_CHECK_EQUAL(keyChain.doesCertificateExist(certName5), false);
310 BOOST_CHECK_EQUAL(keyChain.doesCertificateExist(certName3), true);
311 BOOST_CHECK_EQUAL(keyChain.doesCertificateExist(certName4), true);
312 BOOST_CHECK_EQUAL(keyChain.doesPublicKeyExist(keyName3), true);
313
314 BOOST_REQUIRE_NO_THROW(keyChain.deleteKey(keyName3));
315 BOOST_CHECK_EQUAL(keyChain.doesCertificateExist(certName4), false);
316 BOOST_CHECK_EQUAL(keyChain.doesCertificateExist(certName3), false);
317 BOOST_CHECK_EQUAL(keyChain.doesPublicKeyExist(keyName3), false);
318 BOOST_CHECK_EQUAL(keyChain.doesPublicKeyExist(keyName2), true);
319 BOOST_CHECK_EQUAL(keyChain.doesPublicKeyExist(keyName1), true);
320 BOOST_CHECK_EQUAL(keyChain.doesIdentityExist(identity), true);
321
322 BOOST_REQUIRE_NO_THROW(keyChain.deleteIdentity(identity));
323 BOOST_CHECK_EQUAL(keyChain.doesCertificateExist(certName2), false);
324 BOOST_CHECK_EQUAL(keyChain.doesPublicKeyExist(keyName2), false);
325 BOOST_CHECK_EQUAL(keyChain.doesCertificateExist(certName1), false);
326 BOOST_CHECK_EQUAL(keyChain.doesPublicKeyExist(keyName1), false);
327 BOOST_CHECK_EQUAL(keyChain.doesIdentityExist(identity), false);
328}
329
Alexander Afanasyev07113802015-01-15 19:14:36 -0800330BOOST_AUTO_TEST_CASE(KeyChainWithCustomTpmAndPib)
331{
332 BOOST_REQUIRE_NO_THROW((KeyChain("pib-dummy", "tpm-dummy")));
Alexander Afanasyev34a37632015-01-16 17:37:36 -0800333 BOOST_REQUIRE_NO_THROW((KeyChain("pib-dummy2", "tpm-dummy2")));
Alexander Afanasyev07113802015-01-15 19:14:36 -0800334 BOOST_REQUIRE_NO_THROW((KeyChain("dummy", "dummy")));
335 BOOST_REQUIRE_NO_THROW((KeyChain("dummy:", "dummy:")));
336 BOOST_REQUIRE_NO_THROW((KeyChain("dummy:/something", "dummy:/something")));
337
338 KeyChain keyChain("dummy", "dummy");
Alexander Afanasyev34a37632015-01-16 17:37:36 -0800339 BOOST_CHECK_EQUAL(keyChain.getPib().getPibLocator(), "pib-dummy:");
340 BOOST_CHECK_EQUAL(keyChain.getPib().getTpmLocator(), "tpm-dummy:");
341 BOOST_CHECK_EQUAL(keyChain.getTpm().getTpmLocator(), "tpm-dummy:");
Alexander Afanasyev07113802015-01-15 19:14:36 -0800342 BOOST_CHECK_EQUAL(keyChain.getDefaultIdentity(), "/dummy/key");
343}
344
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800345BOOST_AUTO_TEST_SUITE_END()
346
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700347} // namespace tests
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800348} // namespace ndn