blob: 8009daa0df689bf0693e989fc2ba4acc36c24e58 [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/**
José Quevedo641de4c2016-01-29 00:11:24 +00003 * Copyright (c) 2013-2016 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"
Yingdi Yu1b0311c2015-06-10 14:58:47 -070023#include "security/validator.hpp"
José Quevedo641de4c2016-01-29 00:11:24 +000024#include "security/signing-helpers.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"
Davide Pesaventoeee3e822016-11-26 19:19:34 +010028#include "../test-home-env-saver.hpp"
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -070029#include "key-chain-fixture.hpp"
30#include "identity-management-fixture.hpp"
José Quevedo641de4c2016-01-29 00:11:24 +000031
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -070032#include <boost/algorithm/string.hpp>
Davide Pesaventoeee3e822016-11-26 19:19:34 +010033#include <boost/filesystem.hpp>
34#include <cstdlib>
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070035
Yingdi Yu8dceb1d2014-02-18 12:45:10 -080036namespace ndn {
Yingdi Yu1b0311c2015-06-10 14:58:47 -070037namespace security {
Yingdi Yuf56c68f2014-04-24 21:50:13 -070038namespace tests {
Yingdi Yu8dceb1d2014-02-18 12:45:10 -080039
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -070040using namespace ndn::tests;
41
Davide Pesaventoeee3e822016-11-26 19:19:34 +010042BOOST_AUTO_TEST_SUITE(Security)
43BOOST_FIXTURE_TEST_SUITE(TestKeyChain, TestHomeEnvSaver)
Yingdi Yuf56c68f2014-04-24 21:50:13 -070044
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -070045template<class Path>
Alexander Afanasyevcf490552016-06-27 22:51:36 -070046class TestHomeAndPibFixture : public TestHomeFixture<Path>
Yingdi Yuf56c68f2014-04-24 21:50:13 -070047{
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -070048public:
49 TestHomeAndPibFixture()
50 {
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -070051 unsetenv("NDN_CLIENT_PIB");
52 unsetenv("NDN_CLIENT_TPM");
53 }
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -070054};
55
56struct PibPathSqlite3File
57{
58 const std::string PATH = "build/keys-sqlite3-file/";
59};
60
61BOOST_FIXTURE_TEST_CASE(ConstructorNormalConfig, TestHomeAndPibFixture<PibPathSqlite3File>)
62{
63 createClientConf({"pib=pib-sqlite3:%PATH%", "tpm=tpm-file:%PATH%"});
Yingdi Yuf56c68f2014-04-24 21:50:13 -070064
65 BOOST_REQUIRE_NO_THROW(KeyChain());
66
Alexander Afanasyev07113802015-01-15 19:14:36 -080067 KeyChain keyChain;
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -070068 BOOST_CHECK_EQUAL(keyChain.getPib().getPibLocator(), "pib-sqlite3:" + m_pibDir);
69 BOOST_CHECK_EQUAL(keyChain.getPib().getTpmLocator(), "tpm-file:" + m_pibDir);
70 BOOST_CHECK_EQUAL(keyChain.getTpm().getTpmLocator(), "tpm-file:" + m_pibDir);
Yingdi Yuf56c68f2014-04-24 21:50:13 -070071}
72
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -070073struct PibPathSqlite3Empty
Yingdi Yuf56c68f2014-04-24 21:50:13 -070074{
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -070075 const std::string PATH = "build/keys-sqlite3-empty/";
76};
Yingdi Yuf56c68f2014-04-24 21:50:13 -070077
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -070078BOOST_FIXTURE_TEST_CASE(ConstructorEmptyConfig, TestHomeAndPibFixture<PibPathSqlite3Empty>)
79{
80 createClientConf({"pib=pib-sqlite3:%PATH%"});
Yingdi Yuf56c68f2014-04-24 21:50:13 -070081
Yingdi Yu41546342014-11-30 23:37:53 -080082#if defined(NDN_CXX_HAVE_OSX_SECURITY)
83 std::string oldHOME;
84 if (std::getenv("OLD_HOME"))
85 oldHOME = std::getenv("OLD_HOME");
86
87 std::string HOME;
88 if (std::getenv("HOME"))
89 HOME = std::getenv("HOME");
90
91 if (!oldHOME.empty())
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -070092 setenv("HOME", oldHOME.c_str(), true);
Yingdi Yu41546342014-11-30 23:37:53 -080093 else
94 unsetenv("HOME");
95#endif
96
97 BOOST_REQUIRE_NO_THROW(KeyChain());
Alexander Afanasyev07113802015-01-15 19:14:36 -080098 KeyChain keyChain;
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -070099 BOOST_CHECK_EQUAL(keyChain.getPib().getPibLocator(), "pib-sqlite3:" + m_pibDir);
Alexander Afanasyev07113802015-01-15 19:14:36 -0800100
101#if defined(NDN_CXX_HAVE_OSX_SECURITY)
102 BOOST_CHECK_EQUAL(keyChain.getPib().getTpmLocator(), "tpm-osxkeychain:");
103 BOOST_CHECK_EQUAL(keyChain.getTpm().getTpmLocator(), "tpm-osxkeychain:");
104#else
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700105 BOOST_CHECK_EQUAL(keyChain.getPib().getTpmLocator(), "tpm-file:");
106 BOOST_CHECK_EQUAL(keyChain.getTpm().getTpmLocator(), "tpm-file:");
Alexander Afanasyev07113802015-01-15 19:14:36 -0800107#endif
Yingdi Yu41546342014-11-30 23:37:53 -0800108
109#if defined(NDN_CXX_HAVE_OSX_SECURITY)
110 if (!HOME.empty())
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700111 setenv("HOME", HOME.c_str(), true);
Yingdi Yu41546342014-11-30 23:37:53 -0800112 else
113 unsetenv("HOME");
114#endif
Yingdi Yu41546342014-11-30 23:37:53 -0800115}
116
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700117struct PibPathEmptyFile
Yingdi Yu41546342014-11-30 23:37:53 -0800118{
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700119 const std::string PATH = "build/keys-empty-file/";
120};
Yingdi Yu41546342014-11-30 23:37:53 -0800121
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700122BOOST_FIXTURE_TEST_CASE(ConstructorEmpty2Config, TestHomeAndPibFixture<PibPathEmptyFile>)
123{
124 createClientConf({"tpm=tpm-file:%PATH%"});
Yingdi Yu41546342014-11-30 23:37:53 -0800125
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700126 BOOST_REQUIRE_NO_THROW(KeyChain());
127
Alexander Afanasyev07113802015-01-15 19:14:36 -0800128 KeyChain keyChain;
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700129 BOOST_CHECK_EQUAL(keyChain.getPib().getPibLocator(), "pib-sqlite3:");
130 BOOST_CHECK_EQUAL(keyChain.getPib().getTpmLocator(), "tpm-file:" + m_pibDir);
131 BOOST_CHECK_EQUAL(keyChain.getTpm().getTpmLocator(), "tpm-file:" + m_pibDir);
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700132}
133
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700134BOOST_FIXTURE_TEST_CASE(ConstructorMalConfig, TestHomeAndPibFixture<DefaultPibDir>)
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700135{
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700136 createClientConf({"pib=lord", "tpm=ring"});
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700137
138 BOOST_REQUIRE_THROW(KeyChain(), KeyChain::Error); // Wrong configuration. Error expected.
139}
140
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700141BOOST_FIXTURE_TEST_CASE(ConstructorMal2Config, TestHomeAndPibFixture<DefaultPibDir>)
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700142{
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700143 createClientConf({"pib=pib-sqlite3:%PATH%", "tpm=just-wrong"});
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700144 BOOST_REQUIRE_THROW(KeyChain(), KeyChain::Error); // Wrong configuration. Error expected.
145}
146
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700147BOOST_FIXTURE_TEST_CASE(ExportIdentity, IdentityManagementFixture)
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700148{
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700149 Name identity("/TestKeyChain/ExportIdentity/");
150 identity.appendVersion();
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700151 addIdentity(identity);
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700152
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700153 shared_ptr<SecuredBag> exported = m_keyChain.exportIdentity(identity, "1234");
Yingdi Yu64c3fb42014-02-26 17:30:04 -0800154
155 Block block = exported->wireEncode();
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800156
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700157 Name keyName = m_keyChain.getDefaultKeyNameForIdentity(identity);
158 Name certName = m_keyChain.getDefaultCertificateNameForKey(keyName);
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800159
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700160 m_keyChain.deleteIdentity(identity);
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800161
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700162 BOOST_CHECK_EQUAL(m_keyChain.doesIdentityExist(identity), false);
163 BOOST_CHECK_EQUAL(m_keyChain.doesPublicKeyExist(keyName), false);
Yingdi Yu99b2a002015-08-12 12:47:44 -0700164 BOOST_CHECK_EQUAL(m_keyChain.doesKeyExistInTpm(keyName, KeyClass::PRIVATE), false);
165 BOOST_CHECK_EQUAL(m_keyChain.doesKeyExistInTpm(keyName, KeyClass::PUBLIC), false);
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700166 BOOST_CHECK_EQUAL(m_keyChain.doesCertificateExist(certName), false);
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800167
Yingdi Yu64c3fb42014-02-26 17:30:04 -0800168 SecuredBag imported;
169 imported.wireDecode(block);
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700170 m_keyChain.importIdentity(imported, "1234");
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800171
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700172 BOOST_CHECK(m_keyChain.doesIdentityExist(identity));
173 BOOST_CHECK(m_keyChain.doesPublicKeyExist(keyName));
Yingdi Yu99b2a002015-08-12 12:47:44 -0700174 BOOST_CHECK(m_keyChain.doesKeyExistInTpm(keyName, KeyClass::PRIVATE));
175 BOOST_CHECK(m_keyChain.doesKeyExistInTpm(keyName, KeyClass::PUBLIC));
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700176 BOOST_CHECK(m_keyChain.doesCertificateExist(certName));
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800177}
178
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700179BOOST_FIXTURE_TEST_CASE(PrepareIdentityCertificate, IdentityManagementFixture)
Yingdi Yuc55680b2014-02-26 12:31:35 -0800180{
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700181 Name identity("/TestKeyChain/PrepareIdentityCertificate/");
182 identity.appendVersion();
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700183 addIdentity(identity);
Yingdi Yuc55680b2014-02-26 12:31:35 -0800184
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700185 std::vector<v1::CertificateSubjectDescription> subjectDescription;
Yingdi Yuc55680b2014-02-26 12:31:35 -0800186 Name lowerIdentity = identity;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700187 lowerIdentity.append("Lower").appendVersion();
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700188 Name lowerKeyName = m_keyChain.generateRsaKeyPair(lowerIdentity, true);
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700189 shared_ptr<v1::IdentityCertificate> idCert =
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700190 m_keyChain.prepareUnsignedIdentityCertificate(lowerKeyName, identity,
191 time::system_clock::now(),
192 time::system_clock::now() + time::days(365),
193 subjectDescription);
Yingdi Yuc55680b2014-02-26 12:31:35 -0800194 BOOST_CHECK(static_cast<bool>(idCert));
Yingdi Yu5ec0ee32014-06-24 16:26:09 -0700195 BOOST_CHECK_EQUAL(idCert->getName().getPrefix(5),
196 Name().append(identity).append("KEY").append("Lower"));
Junxiao Shi8ca43252015-06-11 21:29:43 -0700197 BOOST_CHECK(idCert->getFreshnessPeriod() >= time::milliseconds::zero());
Yingdi Yuc55680b2014-02-26 12:31:35 -0800198
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700199 shared_ptr<v1::IdentityCertificate> idCert11 =
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700200 m_keyChain.prepareUnsignedIdentityCertificate(lowerKeyName, identity,
201 time::system_clock::now(),
202 time::system_clock::now() + time::days(365),
203 subjectDescription,
204 lowerIdentity);
Yingdi Yu0eb5d722014-06-10 15:06:25 -0700205 BOOST_CHECK(static_cast<bool>(idCert11));
Yingdi Yu5ec0ee32014-06-24 16:26:09 -0700206 BOOST_CHECK_EQUAL(idCert11->getName().getPrefix(6),
Yingdi Yu0eb5d722014-06-10 15:06:25 -0700207 Name().append(lowerIdentity).append("KEY"));
Yingdi Yuc55680b2014-02-26 12:31:35 -0800208
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700209 Name anotherIdentity("/TestKeyChain/PrepareIdentityCertificate/Another/");
210 anotherIdentity.appendVersion();
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700211 Name anotherKeyName = m_keyChain.generateRsaKeyPair(anotherIdentity, true);
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700212 shared_ptr<v1::IdentityCertificate> idCert2 =
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700213 m_keyChain.prepareUnsignedIdentityCertificate(anotherKeyName, identity,
214 time::system_clock::now(),
215 time::system_clock::now() + time::days(365),
216 subjectDescription);
Yingdi Yuc55680b2014-02-26 12:31:35 -0800217 BOOST_CHECK(static_cast<bool>(idCert2));
Yingdi Yu5ec0ee32014-06-24 16:26:09 -0700218 BOOST_CHECK_EQUAL(idCert2->getName().getPrefix(5), Name().append(anotherIdentity).append("KEY"));
Yingdi Yuc55680b2014-02-26 12:31:35 -0800219
220
221 Name wrongKeyName1;
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700222 shared_ptr<v1::IdentityCertificate> idCert3 =
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700223 m_keyChain.prepareUnsignedIdentityCertificate(wrongKeyName1, identity,
224 time::system_clock::now(),
225 time::system_clock::now() + time::days(365),
226 subjectDescription);
Yingdi Yu5ec0ee32014-06-24 16:26:09 -0700227 BOOST_CHECK_EQUAL(static_cast<bool>(idCert3), false);
Yingdi Yuc55680b2014-02-26 12:31:35 -0800228
229
230 Name wrongKeyName2("/TestKeyChain/PrepareIdentityCertificate");
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700231 shared_ptr<v1::IdentityCertificate> idCert4 =
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700232 m_keyChain.prepareUnsignedIdentityCertificate(wrongKeyName2, identity,
233 time::system_clock::now(),
234 time::system_clock::now() + time::days(365),
235 subjectDescription);
Yingdi Yu5ec0ee32014-06-24 16:26:09 -0700236 BOOST_CHECK_EQUAL(static_cast<bool>(idCert4), false);
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700237
Yingdi Yuc55680b2014-02-26 12:31:35 -0800238
239 Name wrongKeyName3("/TestKeyChain/PrepareIdentityCertificate/ksk-1234");
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700240 shared_ptr<v1::IdentityCertificate> idCert5 =
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700241 m_keyChain.prepareUnsignedIdentityCertificate(wrongKeyName3, identity,
242 time::system_clock::now(),
243 time::system_clock::now() + time::days(365),
244 subjectDescription);
Yingdi Yu5ec0ee32014-06-24 16:26:09 -0700245 BOOST_CHECK_EQUAL(static_cast<bool>(idCert5), false);
Yingdi Yuc55680b2014-02-26 12:31:35 -0800246}
247
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700248BOOST_FIXTURE_TEST_CASE(Delete, IdentityManagementFixture)
Yingdi Yu41546342014-11-30 23:37:53 -0800249{
Yingdi Yu41546342014-11-30 23:37:53 -0800250 Name identity("/TestSecPublicInfoSqlite3/Delete");
251 identity.appendVersion();
252
253 Name certName1;
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700254 BOOST_REQUIRE_NO_THROW(certName1 = m_keyChain.createIdentity(identity));
Yingdi Yu41546342014-11-30 23:37:53 -0800255
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700256 Name keyName1 = v1::IdentityCertificate::certificateNameToPublicKeyName(certName1);
Yingdi Yu41546342014-11-30 23:37:53 -0800257 Name keyName2;
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700258 BOOST_REQUIRE_NO_THROW(keyName2 = m_keyChain.generateRsaKeyPairAsDefault(identity));
Yingdi Yu41546342014-11-30 23:37:53 -0800259
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700260 shared_ptr<v1::IdentityCertificate> cert2;
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700261 BOOST_REQUIRE_NO_THROW(cert2 = m_keyChain.selfSign(keyName2));
Yingdi Yu41546342014-11-30 23:37:53 -0800262 Name certName2 = cert2->getName();
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700263 BOOST_REQUIRE_NO_THROW(m_keyChain.addCertificateAsKeyDefault(*cert2));
Yingdi Yu41546342014-11-30 23:37:53 -0800264
265 Name keyName3;
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700266 BOOST_REQUIRE_NO_THROW(keyName3 = m_keyChain.generateRsaKeyPairAsDefault(identity));
Yingdi Yu41546342014-11-30 23:37:53 -0800267
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700268 shared_ptr<v1::IdentityCertificate> cert3;
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700269 BOOST_REQUIRE_NO_THROW(cert3 = m_keyChain.selfSign(keyName3));
Yingdi Yu41546342014-11-30 23:37:53 -0800270 Name certName3 = cert3->getName();
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700271 BOOST_REQUIRE_NO_THROW(m_keyChain.addCertificateAsKeyDefault(*cert3));
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700272 shared_ptr<v1::IdentityCertificate> cert4;
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700273 BOOST_REQUIRE_NO_THROW(cert4 = m_keyChain.selfSign(keyName3));
Yingdi Yu41546342014-11-30 23:37:53 -0800274 Name certName4 = cert4->getName();
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700275 BOOST_REQUIRE_NO_THROW(m_keyChain.addCertificateAsKeyDefault(*cert4));
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700276 shared_ptr<v1::IdentityCertificate> cert5;
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700277 BOOST_REQUIRE_NO_THROW(cert5 = m_keyChain.selfSign(keyName3));
Yingdi Yu41546342014-11-30 23:37:53 -0800278 Name certName5 = cert5->getName();
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700279 BOOST_REQUIRE_NO_THROW(m_keyChain.addCertificateAsKeyDefault(*cert5));
Yingdi Yu41546342014-11-30 23:37:53 -0800280
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700281 BOOST_CHECK_EQUAL(m_keyChain.doesIdentityExist(identity), true);
282 BOOST_CHECK_EQUAL(m_keyChain.doesPublicKeyExist(keyName1), true);
283 BOOST_CHECK_EQUAL(m_keyChain.doesPublicKeyExist(keyName2), true);
284 BOOST_CHECK_EQUAL(m_keyChain.doesPublicKeyExist(keyName3), true);
285 BOOST_CHECK_EQUAL(m_keyChain.doesCertificateExist(certName1), true);
286 BOOST_CHECK_EQUAL(m_keyChain.doesCertificateExist(certName2), true);
287 BOOST_CHECK_EQUAL(m_keyChain.doesCertificateExist(certName3), true);
288 BOOST_CHECK_EQUAL(m_keyChain.doesCertificateExist(certName4), true);
289 BOOST_CHECK_EQUAL(m_keyChain.doesCertificateExist(certName5), true);
Yingdi Yu41546342014-11-30 23:37:53 -0800290
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700291 BOOST_REQUIRE_NO_THROW(m_keyChain.deleteCertificate(certName5));
292 BOOST_CHECK_EQUAL(m_keyChain.doesCertificateExist(certName5), false);
293 BOOST_CHECK_EQUAL(m_keyChain.doesCertificateExist(certName3), true);
294 BOOST_CHECK_EQUAL(m_keyChain.doesCertificateExist(certName4), true);
295 BOOST_CHECK_EQUAL(m_keyChain.doesPublicKeyExist(keyName3), true);
Yingdi Yu41546342014-11-30 23:37:53 -0800296
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700297 BOOST_REQUIRE_NO_THROW(m_keyChain.deleteKey(keyName3));
298 BOOST_CHECK_EQUAL(m_keyChain.doesCertificateExist(certName4), false);
299 BOOST_CHECK_EQUAL(m_keyChain.doesCertificateExist(certName3), false);
300 BOOST_CHECK_EQUAL(m_keyChain.doesPublicKeyExist(keyName3), false);
301 BOOST_CHECK_EQUAL(m_keyChain.doesPublicKeyExist(keyName2), true);
302 BOOST_CHECK_EQUAL(m_keyChain.doesPublicKeyExist(keyName1), true);
303 BOOST_CHECK_EQUAL(m_keyChain.doesIdentityExist(identity), true);
Yingdi Yu41546342014-11-30 23:37:53 -0800304
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700305 BOOST_REQUIRE_NO_THROW(m_keyChain.deleteIdentity(identity));
306 BOOST_CHECK_EQUAL(m_keyChain.doesCertificateExist(certName2), false);
307 BOOST_CHECK_EQUAL(m_keyChain.doesPublicKeyExist(keyName2), false);
308 BOOST_CHECK_EQUAL(m_keyChain.doesCertificateExist(certName1), false);
309 BOOST_CHECK_EQUAL(m_keyChain.doesPublicKeyExist(keyName1), false);
310 BOOST_CHECK_EQUAL(m_keyChain.doesIdentityExist(identity), false);
Yingdi Yu41546342014-11-30 23:37:53 -0800311}
312
Alexander Afanasyev07113802015-01-15 19:14:36 -0800313BOOST_AUTO_TEST_CASE(KeyChainWithCustomTpmAndPib)
314{
315 BOOST_REQUIRE_NO_THROW((KeyChain("pib-dummy", "tpm-dummy")));
Alexander Afanasyev34a37632015-01-16 17:37:36 -0800316 BOOST_REQUIRE_NO_THROW((KeyChain("pib-dummy2", "tpm-dummy2")));
Alexander Afanasyev07113802015-01-15 19:14:36 -0800317 BOOST_REQUIRE_NO_THROW((KeyChain("dummy", "dummy")));
318 BOOST_REQUIRE_NO_THROW((KeyChain("dummy:", "dummy:")));
319 BOOST_REQUIRE_NO_THROW((KeyChain("dummy:/something", "dummy:/something")));
320
321 KeyChain keyChain("dummy", "dummy");
Alexander Afanasyev34a37632015-01-16 17:37:36 -0800322 BOOST_CHECK_EQUAL(keyChain.getPib().getPibLocator(), "pib-dummy:");
323 BOOST_CHECK_EQUAL(keyChain.getPib().getTpmLocator(), "tpm-dummy:");
324 BOOST_CHECK_EQUAL(keyChain.getTpm().getTpmLocator(), "tpm-dummy:");
Alexander Afanasyev07113802015-01-15 19:14:36 -0800325 BOOST_CHECK_EQUAL(keyChain.getDefaultIdentity(), "/dummy/key");
326}
327
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700328BOOST_FIXTURE_TEST_CASE(GeneralSigningInterface, IdentityManagementFixture)
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700329{
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700330 Name id("/id");
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700331 Name certName = m_keyChain.createIdentity(id);
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700332 shared_ptr<v1::IdentityCertificate> idCert = m_keyChain.getCertificate(certName);
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700333 Name keyName = idCert->getPublicKeyName();
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700334 m_keyChain.setDefaultIdentity(id);
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700335
336 Name id2("/id2");
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700337 Name cert2Name = m_keyChain.createIdentity(id2);
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700338 shared_ptr<v1::IdentityCertificate> id2Cert = m_keyChain.getCertificate(cert2Name);
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700339
340 // SigningInfo is set to default
341 Data data1("/data1");
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700342 m_keyChain.sign(data1);
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700343 BOOST_CHECK(Validator::verifySignature(data1, idCert->getPublicKeyInfo()));
344 BOOST_CHECK_EQUAL(data1.getSignature().getKeyLocator().getName(), certName.getPrefix(-1));
345
346 Interest interest1("/interest1");
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700347 m_keyChain.sign(interest1);
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700348 BOOST_CHECK(Validator::verifySignature(interest1, idCert->getPublicKeyInfo()));
349 SignatureInfo sigInfo1(interest1.getName()[-2].blockFromValue());
350 BOOST_CHECK_EQUAL(sigInfo1.getKeyLocator().getName(), certName.getPrefix(-1));
351
352 // SigningInfo is set to Identity
353 Data data2("/data2");
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700354 m_keyChain.sign(data2, SigningInfo(SigningInfo::SIGNER_TYPE_ID, id2));
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700355 BOOST_CHECK(Validator::verifySignature(data2, id2Cert->getPublicKeyInfo()));
356 BOOST_CHECK_EQUAL(data2.getSignature().getKeyLocator().getName(), cert2Name.getPrefix(-1));
357
358 Interest interest2("/interest2");
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700359 m_keyChain.sign(interest2, SigningInfo(SigningInfo::SIGNER_TYPE_ID, id2));
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700360 BOOST_CHECK(Validator::verifySignature(interest2, id2Cert->getPublicKeyInfo()));
361 SignatureInfo sigInfo2(interest2.getName()[-2].blockFromValue());
362 BOOST_CHECK_EQUAL(sigInfo2.getKeyLocator().getName(), cert2Name.getPrefix(-1));
363
364 // SigningInfo is set to Key
365 Data data3("/data3");
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700366 m_keyChain.sign(data3, SigningInfo(SigningInfo::SIGNER_TYPE_KEY, keyName));
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700367 BOOST_CHECK(Validator::verifySignature(data3, idCert->getPublicKeyInfo()));
368 BOOST_CHECK_EQUAL(data3.getSignature().getKeyLocator().getName(), certName.getPrefix(-1));
369
370 Interest interest3("/interest3");
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700371 m_keyChain.sign(interest3);
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700372 BOOST_CHECK(Validator::verifySignature(interest3, idCert->getPublicKeyInfo()));
373 SignatureInfo sigInfo3(interest1.getName()[-2].blockFromValue());
374 BOOST_CHECK_EQUAL(sigInfo3.getKeyLocator().getName(), certName.getPrefix(-1));
375
376 // SigningInfo is set to Cert
377 Data data4("/data4");
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700378 m_keyChain.sign(data4, SigningInfo(SigningInfo::SIGNER_TYPE_CERT, certName));
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700379 BOOST_CHECK(Validator::verifySignature(data4, idCert->getPublicKeyInfo()));
380 BOOST_CHECK_EQUAL(data4.getSignature().getKeyLocator().getName(), certName.getPrefix(-1));
381
382 Interest interest4("/interest4");
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700383 m_keyChain.sign(interest4, SigningInfo(SigningInfo::SIGNER_TYPE_CERT, certName));
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700384 BOOST_CHECK(Validator::verifySignature(interest4, idCert->getPublicKeyInfo()));
385 SignatureInfo sigInfo4(interest4.getName()[-2].blockFromValue());
386 BOOST_CHECK_EQUAL(sigInfo4.getKeyLocator().getName(), certName.getPrefix(-1));
387
388
389 // SigningInfo is set to DigestSha256
390 Data data5("/data5");
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700391 m_keyChain.sign(data5, SigningInfo(SigningInfo::SIGNER_TYPE_SHA256));
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700392 BOOST_CHECK(Validator::verifySignature(data5, DigestSha256(data5.getSignature())));
393
394 Interest interest5("/interest4");
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700395 m_keyChain.sign(interest5, SigningInfo(SigningInfo::SIGNER_TYPE_SHA256));
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700396 BOOST_CHECK(Validator::verifySignature(interest5,
397 DigestSha256(Signature(interest5.getName()[-2].blockFromValue(),
398 interest5.getName()[-1].blockFromValue()))));
399}
400
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700401BOOST_FIXTURE_TEST_CASE(EcdsaSigningByIdentityNoCert, IdentityManagementFixture)
José Quevedo641de4c2016-01-29 00:11:24 +0000402{
José Quevedo641de4c2016-01-29 00:11:24 +0000403 Data data("/test/data");
404
405 Name nonExistingIdentity = Name("/non-existing/identity").appendVersion();
406
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700407 BOOST_CHECK_NO_THROW(m_keyChain.sign(data, signingByIdentity(nonExistingIdentity)));
José Quevedo641de4c2016-01-29 00:11:24 +0000408 BOOST_CHECK_EQUAL(data.getSignature().getType(),
409 KeyChain::getSignatureType(KeyChain::DEFAULT_KEY_PARAMS.getKeyType(),
Yingdi Yu99b2a002015-08-12 12:47:44 -0700410 DigestAlgorithm::SHA256));
José Quevedo641de4c2016-01-29 00:11:24 +0000411 BOOST_CHECK(nonExistingIdentity.isPrefixOf(data.getSignature().getKeyLocator().getName()));
412
413 Name ecdsaIdentity = Name("/ndn/test/ecdsa").appendVersion();
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700414 Name ecdsaKeyName = m_keyChain.generateEcdsaKeyPairAsDefault(ecdsaIdentity, false, 256);
415 BOOST_CHECK_NO_THROW(m_keyChain.sign(data, signingByIdentity(ecdsaIdentity)));
José Quevedo641de4c2016-01-29 00:11:24 +0000416 BOOST_CHECK_EQUAL(data.getSignature().getType(),
Yingdi Yu99b2a002015-08-12 12:47:44 -0700417 KeyChain::getSignatureType(EcdsaKeyParams().getKeyType(), DigestAlgorithm::SHA256));
José Quevedo641de4c2016-01-29 00:11:24 +0000418 BOOST_CHECK(ecdsaIdentity.isPrefixOf(data.getSignature().getKeyLocator().getName()));
419}
420
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100421BOOST_AUTO_TEST_SUITE_END() // TestKeyChain
422BOOST_AUTO_TEST_SUITE_END() // Security
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800423
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700424} // namespace tests
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700425} // namespace security
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800426} // namespace ndn