blob: eff1ab387c20bde68f60d4618bdf53be422a687a [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"
José Quevedo641de4c2016-01-29 00:11:24 +000028#include "../util/test-home-environment-fixture.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
32#include <boost/filesystem.hpp>
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -070033#include <boost/algorithm/string.hpp>
34#include <fstream>
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
Spyridon Mastorakis429634f2015-02-19 17:35:33 -080042BOOST_FIXTURE_TEST_SUITE(SecurityKeyChain, util::TestHomeEnvironmentFixture)
Yingdi Yuf56c68f2014-04-24 21:50:13 -070043
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -070044template<class Path>
Alexander Afanasyevcf490552016-06-27 22:51:36 -070045class TestHomeAndPibFixture : public TestHomeFixture<Path>
Yingdi Yuf56c68f2014-04-24 21:50:13 -070046{
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -070047public:
48 TestHomeAndPibFixture()
49 {
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -070050 unsetenv("NDN_CLIENT_PIB");
51 unsetenv("NDN_CLIENT_TPM");
52 }
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -070053};
54
55struct PibPathSqlite3File
56{
57 const std::string PATH = "build/keys-sqlite3-file/";
58};
59
60BOOST_FIXTURE_TEST_CASE(ConstructorNormalConfig, TestHomeAndPibFixture<PibPathSqlite3File>)
61{
62 createClientConf({"pib=pib-sqlite3:%PATH%", "tpm=tpm-file:%PATH%"});
Yingdi Yuf56c68f2014-04-24 21:50:13 -070063
64 BOOST_REQUIRE_NO_THROW(KeyChain());
65
Alexander Afanasyev07113802015-01-15 19:14:36 -080066 KeyChain keyChain;
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -070067 BOOST_CHECK_EQUAL(keyChain.getPib().getPibLocator(), "pib-sqlite3:" + m_pibDir);
68 BOOST_CHECK_EQUAL(keyChain.getPib().getTpmLocator(), "tpm-file:" + m_pibDir);
69 BOOST_CHECK_EQUAL(keyChain.getTpm().getTpmLocator(), "tpm-file:" + m_pibDir);
Yingdi Yuf56c68f2014-04-24 21:50:13 -070070}
71
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -070072struct PibPathSqlite3Empty
Yingdi Yuf56c68f2014-04-24 21:50:13 -070073{
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -070074 const std::string PATH = "build/keys-sqlite3-empty/";
75};
Yingdi Yuf56c68f2014-04-24 21:50:13 -070076
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -070077BOOST_FIXTURE_TEST_CASE(ConstructorEmptyConfig, TestHomeAndPibFixture<PibPathSqlite3Empty>)
78{
79 createClientConf({"pib=pib-sqlite3:%PATH%"});
Yingdi Yuf56c68f2014-04-24 21:50:13 -070080
Yingdi Yu41546342014-11-30 23:37:53 -080081#if defined(NDN_CXX_HAVE_OSX_SECURITY)
82 std::string oldHOME;
83 if (std::getenv("OLD_HOME"))
84 oldHOME = std::getenv("OLD_HOME");
85
86 std::string HOME;
87 if (std::getenv("HOME"))
88 HOME = std::getenv("HOME");
89
90 if (!oldHOME.empty())
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -070091 setenv("HOME", oldHOME.c_str(), true);
Yingdi Yu41546342014-11-30 23:37:53 -080092 else
93 unsetenv("HOME");
94#endif
95
96 BOOST_REQUIRE_NO_THROW(KeyChain());
Alexander Afanasyev07113802015-01-15 19:14:36 -080097 KeyChain keyChain;
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -070098 BOOST_CHECK_EQUAL(keyChain.getPib().getPibLocator(), "pib-sqlite3:" + m_pibDir);
Alexander Afanasyev07113802015-01-15 19:14:36 -080099
100#if defined(NDN_CXX_HAVE_OSX_SECURITY)
101 BOOST_CHECK_EQUAL(keyChain.getPib().getTpmLocator(), "tpm-osxkeychain:");
102 BOOST_CHECK_EQUAL(keyChain.getTpm().getTpmLocator(), "tpm-osxkeychain:");
103#else
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700104 BOOST_CHECK_EQUAL(keyChain.getPib().getTpmLocator(), "tpm-file:");
105 BOOST_CHECK_EQUAL(keyChain.getTpm().getTpmLocator(), "tpm-file:");
Alexander Afanasyev07113802015-01-15 19:14:36 -0800106#endif
Yingdi Yu41546342014-11-30 23:37:53 -0800107
108#if defined(NDN_CXX_HAVE_OSX_SECURITY)
109 if (!HOME.empty())
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700110 setenv("HOME", HOME.c_str(), true);
Yingdi Yu41546342014-11-30 23:37:53 -0800111 else
112 unsetenv("HOME");
113#endif
Yingdi Yu41546342014-11-30 23:37:53 -0800114}
115
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700116struct PibPathEmptyFile
Yingdi Yu41546342014-11-30 23:37:53 -0800117{
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700118 const std::string PATH = "build/keys-empty-file/";
119};
Yingdi Yu41546342014-11-30 23:37:53 -0800120
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700121BOOST_FIXTURE_TEST_CASE(ConstructorEmpty2Config, TestHomeAndPibFixture<PibPathEmptyFile>)
122{
123 createClientConf({"tpm=tpm-file:%PATH%"});
Yingdi Yu41546342014-11-30 23:37:53 -0800124
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700125 BOOST_REQUIRE_NO_THROW(KeyChain());
126
Alexander Afanasyev07113802015-01-15 19:14:36 -0800127 KeyChain keyChain;
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700128 BOOST_CHECK_EQUAL(keyChain.getPib().getPibLocator(), "pib-sqlite3:");
129 BOOST_CHECK_EQUAL(keyChain.getPib().getTpmLocator(), "tpm-file:" + m_pibDir);
130 BOOST_CHECK_EQUAL(keyChain.getTpm().getTpmLocator(), "tpm-file:" + m_pibDir);
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700131}
132
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700133BOOST_FIXTURE_TEST_CASE(ConstructorMalConfig, TestHomeAndPibFixture<DefaultPibDir>)
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700134{
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700135 createClientConf({"pib=lord", "tpm=ring"});
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700136
137 BOOST_REQUIRE_THROW(KeyChain(), KeyChain::Error); // Wrong configuration. Error expected.
138}
139
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700140BOOST_FIXTURE_TEST_CASE(ConstructorMal2Config, TestHomeAndPibFixture<DefaultPibDir>)
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700141{
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700142 createClientConf({"pib=pib-sqlite3:%PATH%", "tpm=just-wrong"});
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700143 BOOST_REQUIRE_THROW(KeyChain(), KeyChain::Error); // Wrong configuration. Error expected.
144}
145
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700146BOOST_FIXTURE_TEST_CASE(ExportIdentity, IdentityManagementFixture)
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700147{
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700148 Name identity("/TestKeyChain/ExportIdentity/");
149 identity.appendVersion();
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700150 addIdentity(identity);
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700151
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700152 shared_ptr<SecuredBag> exported = m_keyChain.exportIdentity(identity, "1234");
Yingdi Yu64c3fb42014-02-26 17:30:04 -0800153
154 Block block = exported->wireEncode();
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800155
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700156 Name keyName = m_keyChain.getDefaultKeyNameForIdentity(identity);
157 Name certName = m_keyChain.getDefaultCertificateNameForKey(keyName);
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800158
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700159 m_keyChain.deleteIdentity(identity);
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800160
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700161 BOOST_CHECK_EQUAL(m_keyChain.doesIdentityExist(identity), false);
162 BOOST_CHECK_EQUAL(m_keyChain.doesPublicKeyExist(keyName), false);
Yingdi Yu99b2a002015-08-12 12:47:44 -0700163 BOOST_CHECK_EQUAL(m_keyChain.doesKeyExistInTpm(keyName, KeyClass::PRIVATE), false);
164 BOOST_CHECK_EQUAL(m_keyChain.doesKeyExistInTpm(keyName, KeyClass::PUBLIC), false);
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700165 BOOST_CHECK_EQUAL(m_keyChain.doesCertificateExist(certName), false);
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800166
Yingdi Yu64c3fb42014-02-26 17:30:04 -0800167 SecuredBag imported;
168 imported.wireDecode(block);
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700169 m_keyChain.importIdentity(imported, "1234");
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800170
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700171 BOOST_CHECK(m_keyChain.doesIdentityExist(identity));
172 BOOST_CHECK(m_keyChain.doesPublicKeyExist(keyName));
Yingdi Yu99b2a002015-08-12 12:47:44 -0700173 BOOST_CHECK(m_keyChain.doesKeyExistInTpm(keyName, KeyClass::PRIVATE));
174 BOOST_CHECK(m_keyChain.doesKeyExistInTpm(keyName, KeyClass::PUBLIC));
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700175 BOOST_CHECK(m_keyChain.doesCertificateExist(certName));
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800176}
177
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700178BOOST_FIXTURE_TEST_CASE(PrepareIdentityCertificate, IdentityManagementFixture)
Yingdi Yuc55680b2014-02-26 12:31:35 -0800179{
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700180 Name identity("/TestKeyChain/PrepareIdentityCertificate/");
181 identity.appendVersion();
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700182 addIdentity(identity);
Yingdi Yuc55680b2014-02-26 12:31:35 -0800183
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700184 std::vector<v1::CertificateSubjectDescription> subjectDescription;
Yingdi Yuc55680b2014-02-26 12:31:35 -0800185 Name lowerIdentity = identity;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700186 lowerIdentity.append("Lower").appendVersion();
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700187 Name lowerKeyName = m_keyChain.generateRsaKeyPair(lowerIdentity, true);
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700188 shared_ptr<v1::IdentityCertificate> idCert =
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700189 m_keyChain.prepareUnsignedIdentityCertificate(lowerKeyName, identity,
190 time::system_clock::now(),
191 time::system_clock::now() + time::days(365),
192 subjectDescription);
Yingdi Yuc55680b2014-02-26 12:31:35 -0800193 BOOST_CHECK(static_cast<bool>(idCert));
Yingdi Yu5ec0ee32014-06-24 16:26:09 -0700194 BOOST_CHECK_EQUAL(idCert->getName().getPrefix(5),
195 Name().append(identity).append("KEY").append("Lower"));
Junxiao Shi8ca43252015-06-11 21:29:43 -0700196 BOOST_CHECK(idCert->getFreshnessPeriod() >= time::milliseconds::zero());
Yingdi Yuc55680b2014-02-26 12:31:35 -0800197
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700198 shared_ptr<v1::IdentityCertificate> idCert11 =
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700199 m_keyChain.prepareUnsignedIdentityCertificate(lowerKeyName, identity,
200 time::system_clock::now(),
201 time::system_clock::now() + time::days(365),
202 subjectDescription,
203 lowerIdentity);
Yingdi Yu0eb5d722014-06-10 15:06:25 -0700204 BOOST_CHECK(static_cast<bool>(idCert11));
Yingdi Yu5ec0ee32014-06-24 16:26:09 -0700205 BOOST_CHECK_EQUAL(idCert11->getName().getPrefix(6),
Yingdi Yu0eb5d722014-06-10 15:06:25 -0700206 Name().append(lowerIdentity).append("KEY"));
Yingdi Yuc55680b2014-02-26 12:31:35 -0800207
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700208 Name anotherIdentity("/TestKeyChain/PrepareIdentityCertificate/Another/");
209 anotherIdentity.appendVersion();
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700210 Name anotherKeyName = m_keyChain.generateRsaKeyPair(anotherIdentity, true);
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700211 shared_ptr<v1::IdentityCertificate> idCert2 =
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700212 m_keyChain.prepareUnsignedIdentityCertificate(anotherKeyName, identity,
213 time::system_clock::now(),
214 time::system_clock::now() + time::days(365),
215 subjectDescription);
Yingdi Yuc55680b2014-02-26 12:31:35 -0800216 BOOST_CHECK(static_cast<bool>(idCert2));
Yingdi Yu5ec0ee32014-06-24 16:26:09 -0700217 BOOST_CHECK_EQUAL(idCert2->getName().getPrefix(5), Name().append(anotherIdentity).append("KEY"));
Yingdi Yuc55680b2014-02-26 12:31:35 -0800218
219
220 Name wrongKeyName1;
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700221 shared_ptr<v1::IdentityCertificate> idCert3 =
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700222 m_keyChain.prepareUnsignedIdentityCertificate(wrongKeyName1, identity,
223 time::system_clock::now(),
224 time::system_clock::now() + time::days(365),
225 subjectDescription);
Yingdi Yu5ec0ee32014-06-24 16:26:09 -0700226 BOOST_CHECK_EQUAL(static_cast<bool>(idCert3), false);
Yingdi Yuc55680b2014-02-26 12:31:35 -0800227
228
229 Name wrongKeyName2("/TestKeyChain/PrepareIdentityCertificate");
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700230 shared_ptr<v1::IdentityCertificate> idCert4 =
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700231 m_keyChain.prepareUnsignedIdentityCertificate(wrongKeyName2, identity,
232 time::system_clock::now(),
233 time::system_clock::now() + time::days(365),
234 subjectDescription);
Yingdi Yu5ec0ee32014-06-24 16:26:09 -0700235 BOOST_CHECK_EQUAL(static_cast<bool>(idCert4), false);
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700236
Yingdi Yuc55680b2014-02-26 12:31:35 -0800237
238 Name wrongKeyName3("/TestKeyChain/PrepareIdentityCertificate/ksk-1234");
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700239 shared_ptr<v1::IdentityCertificate> idCert5 =
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700240 m_keyChain.prepareUnsignedIdentityCertificate(wrongKeyName3, identity,
241 time::system_clock::now(),
242 time::system_clock::now() + time::days(365),
243 subjectDescription);
Yingdi Yu5ec0ee32014-06-24 16:26:09 -0700244 BOOST_CHECK_EQUAL(static_cast<bool>(idCert5), false);
Yingdi Yuc55680b2014-02-26 12:31:35 -0800245}
246
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700247BOOST_FIXTURE_TEST_CASE(Delete, IdentityManagementFixture)
Yingdi Yu41546342014-11-30 23:37:53 -0800248{
Yingdi Yu41546342014-11-30 23:37:53 -0800249 Name identity("/TestSecPublicInfoSqlite3/Delete");
250 identity.appendVersion();
251
252 Name certName1;
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700253 BOOST_REQUIRE_NO_THROW(certName1 = m_keyChain.createIdentity(identity));
Yingdi Yu41546342014-11-30 23:37:53 -0800254
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700255 Name keyName1 = v1::IdentityCertificate::certificateNameToPublicKeyName(certName1);
Yingdi Yu41546342014-11-30 23:37:53 -0800256 Name keyName2;
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700257 BOOST_REQUIRE_NO_THROW(keyName2 = m_keyChain.generateRsaKeyPairAsDefault(identity));
Yingdi Yu41546342014-11-30 23:37:53 -0800258
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700259 shared_ptr<v1::IdentityCertificate> cert2;
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700260 BOOST_REQUIRE_NO_THROW(cert2 = m_keyChain.selfSign(keyName2));
Yingdi Yu41546342014-11-30 23:37:53 -0800261 Name certName2 = cert2->getName();
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700262 BOOST_REQUIRE_NO_THROW(m_keyChain.addCertificateAsKeyDefault(*cert2));
Yingdi Yu41546342014-11-30 23:37:53 -0800263
264 Name keyName3;
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700265 BOOST_REQUIRE_NO_THROW(keyName3 = m_keyChain.generateRsaKeyPairAsDefault(identity));
Yingdi Yu41546342014-11-30 23:37:53 -0800266
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700267 shared_ptr<v1::IdentityCertificate> cert3;
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700268 BOOST_REQUIRE_NO_THROW(cert3 = m_keyChain.selfSign(keyName3));
Yingdi Yu41546342014-11-30 23:37:53 -0800269 Name certName3 = cert3->getName();
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700270 BOOST_REQUIRE_NO_THROW(m_keyChain.addCertificateAsKeyDefault(*cert3));
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700271 shared_ptr<v1::IdentityCertificate> cert4;
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700272 BOOST_REQUIRE_NO_THROW(cert4 = m_keyChain.selfSign(keyName3));
Yingdi Yu41546342014-11-30 23:37:53 -0800273 Name certName4 = cert4->getName();
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700274 BOOST_REQUIRE_NO_THROW(m_keyChain.addCertificateAsKeyDefault(*cert4));
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700275 shared_ptr<v1::IdentityCertificate> cert5;
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700276 BOOST_REQUIRE_NO_THROW(cert5 = m_keyChain.selfSign(keyName3));
Yingdi Yu41546342014-11-30 23:37:53 -0800277 Name certName5 = cert5->getName();
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700278 BOOST_REQUIRE_NO_THROW(m_keyChain.addCertificateAsKeyDefault(*cert5));
Yingdi Yu41546342014-11-30 23:37:53 -0800279
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700280 BOOST_CHECK_EQUAL(m_keyChain.doesIdentityExist(identity), true);
281 BOOST_CHECK_EQUAL(m_keyChain.doesPublicKeyExist(keyName1), true);
282 BOOST_CHECK_EQUAL(m_keyChain.doesPublicKeyExist(keyName2), true);
283 BOOST_CHECK_EQUAL(m_keyChain.doesPublicKeyExist(keyName3), true);
284 BOOST_CHECK_EQUAL(m_keyChain.doesCertificateExist(certName1), true);
285 BOOST_CHECK_EQUAL(m_keyChain.doesCertificateExist(certName2), true);
286 BOOST_CHECK_EQUAL(m_keyChain.doesCertificateExist(certName3), true);
287 BOOST_CHECK_EQUAL(m_keyChain.doesCertificateExist(certName4), true);
288 BOOST_CHECK_EQUAL(m_keyChain.doesCertificateExist(certName5), true);
Yingdi Yu41546342014-11-30 23:37:53 -0800289
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700290 BOOST_REQUIRE_NO_THROW(m_keyChain.deleteCertificate(certName5));
291 BOOST_CHECK_EQUAL(m_keyChain.doesCertificateExist(certName5), false);
292 BOOST_CHECK_EQUAL(m_keyChain.doesCertificateExist(certName3), true);
293 BOOST_CHECK_EQUAL(m_keyChain.doesCertificateExist(certName4), true);
294 BOOST_CHECK_EQUAL(m_keyChain.doesPublicKeyExist(keyName3), true);
Yingdi Yu41546342014-11-30 23:37:53 -0800295
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700296 BOOST_REQUIRE_NO_THROW(m_keyChain.deleteKey(keyName3));
297 BOOST_CHECK_EQUAL(m_keyChain.doesCertificateExist(certName4), false);
298 BOOST_CHECK_EQUAL(m_keyChain.doesCertificateExist(certName3), false);
299 BOOST_CHECK_EQUAL(m_keyChain.doesPublicKeyExist(keyName3), false);
300 BOOST_CHECK_EQUAL(m_keyChain.doesPublicKeyExist(keyName2), true);
301 BOOST_CHECK_EQUAL(m_keyChain.doesPublicKeyExist(keyName1), true);
302 BOOST_CHECK_EQUAL(m_keyChain.doesIdentityExist(identity), true);
Yingdi Yu41546342014-11-30 23:37:53 -0800303
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700304 BOOST_REQUIRE_NO_THROW(m_keyChain.deleteIdentity(identity));
305 BOOST_CHECK_EQUAL(m_keyChain.doesCertificateExist(certName2), false);
306 BOOST_CHECK_EQUAL(m_keyChain.doesPublicKeyExist(keyName2), false);
307 BOOST_CHECK_EQUAL(m_keyChain.doesCertificateExist(certName1), false);
308 BOOST_CHECK_EQUAL(m_keyChain.doesPublicKeyExist(keyName1), false);
309 BOOST_CHECK_EQUAL(m_keyChain.doesIdentityExist(identity), false);
Yingdi Yu41546342014-11-30 23:37:53 -0800310}
311
Alexander Afanasyev07113802015-01-15 19:14:36 -0800312BOOST_AUTO_TEST_CASE(KeyChainWithCustomTpmAndPib)
313{
314 BOOST_REQUIRE_NO_THROW((KeyChain("pib-dummy", "tpm-dummy")));
Alexander Afanasyev34a37632015-01-16 17:37:36 -0800315 BOOST_REQUIRE_NO_THROW((KeyChain("pib-dummy2", "tpm-dummy2")));
Alexander Afanasyev07113802015-01-15 19:14:36 -0800316 BOOST_REQUIRE_NO_THROW((KeyChain("dummy", "dummy")));
317 BOOST_REQUIRE_NO_THROW((KeyChain("dummy:", "dummy:")));
318 BOOST_REQUIRE_NO_THROW((KeyChain("dummy:/something", "dummy:/something")));
319
320 KeyChain keyChain("dummy", "dummy");
Alexander Afanasyev34a37632015-01-16 17:37:36 -0800321 BOOST_CHECK_EQUAL(keyChain.getPib().getPibLocator(), "pib-dummy:");
322 BOOST_CHECK_EQUAL(keyChain.getPib().getTpmLocator(), "tpm-dummy:");
323 BOOST_CHECK_EQUAL(keyChain.getTpm().getTpmLocator(), "tpm-dummy:");
Alexander Afanasyev07113802015-01-15 19:14:36 -0800324 BOOST_CHECK_EQUAL(keyChain.getDefaultIdentity(), "/dummy/key");
325}
326
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700327BOOST_FIXTURE_TEST_CASE(GeneralSigningInterface, IdentityManagementFixture)
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700328{
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700329 Name id("/id");
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700330 Name certName = m_keyChain.createIdentity(id);
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700331 shared_ptr<v1::IdentityCertificate> idCert = m_keyChain.getCertificate(certName);
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700332 Name keyName = idCert->getPublicKeyName();
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700333 m_keyChain.setDefaultIdentity(id);
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700334
335 Name id2("/id2");
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700336 Name cert2Name = m_keyChain.createIdentity(id2);
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700337 shared_ptr<v1::IdentityCertificate> id2Cert = m_keyChain.getCertificate(cert2Name);
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700338
339 // SigningInfo is set to default
340 Data data1("/data1");
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700341 m_keyChain.sign(data1);
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700342 BOOST_CHECK(Validator::verifySignature(data1, idCert->getPublicKeyInfo()));
343 BOOST_CHECK_EQUAL(data1.getSignature().getKeyLocator().getName(), certName.getPrefix(-1));
344
345 Interest interest1("/interest1");
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700346 m_keyChain.sign(interest1);
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700347 BOOST_CHECK(Validator::verifySignature(interest1, idCert->getPublicKeyInfo()));
348 SignatureInfo sigInfo1(interest1.getName()[-2].blockFromValue());
349 BOOST_CHECK_EQUAL(sigInfo1.getKeyLocator().getName(), certName.getPrefix(-1));
350
351 // SigningInfo is set to Identity
352 Data data2("/data2");
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700353 m_keyChain.sign(data2, SigningInfo(SigningInfo::SIGNER_TYPE_ID, id2));
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700354 BOOST_CHECK(Validator::verifySignature(data2, id2Cert->getPublicKeyInfo()));
355 BOOST_CHECK_EQUAL(data2.getSignature().getKeyLocator().getName(), cert2Name.getPrefix(-1));
356
357 Interest interest2("/interest2");
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700358 m_keyChain.sign(interest2, SigningInfo(SigningInfo::SIGNER_TYPE_ID, id2));
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700359 BOOST_CHECK(Validator::verifySignature(interest2, id2Cert->getPublicKeyInfo()));
360 SignatureInfo sigInfo2(interest2.getName()[-2].blockFromValue());
361 BOOST_CHECK_EQUAL(sigInfo2.getKeyLocator().getName(), cert2Name.getPrefix(-1));
362
363 // SigningInfo is set to Key
364 Data data3("/data3");
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700365 m_keyChain.sign(data3, SigningInfo(SigningInfo::SIGNER_TYPE_KEY, keyName));
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700366 BOOST_CHECK(Validator::verifySignature(data3, idCert->getPublicKeyInfo()));
367 BOOST_CHECK_EQUAL(data3.getSignature().getKeyLocator().getName(), certName.getPrefix(-1));
368
369 Interest interest3("/interest3");
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700370 m_keyChain.sign(interest3);
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700371 BOOST_CHECK(Validator::verifySignature(interest3, idCert->getPublicKeyInfo()));
372 SignatureInfo sigInfo3(interest1.getName()[-2].blockFromValue());
373 BOOST_CHECK_EQUAL(sigInfo3.getKeyLocator().getName(), certName.getPrefix(-1));
374
375 // SigningInfo is set to Cert
376 Data data4("/data4");
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700377 m_keyChain.sign(data4, SigningInfo(SigningInfo::SIGNER_TYPE_CERT, certName));
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700378 BOOST_CHECK(Validator::verifySignature(data4, idCert->getPublicKeyInfo()));
379 BOOST_CHECK_EQUAL(data4.getSignature().getKeyLocator().getName(), certName.getPrefix(-1));
380
381 Interest interest4("/interest4");
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700382 m_keyChain.sign(interest4, SigningInfo(SigningInfo::SIGNER_TYPE_CERT, certName));
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700383 BOOST_CHECK(Validator::verifySignature(interest4, idCert->getPublicKeyInfo()));
384 SignatureInfo sigInfo4(interest4.getName()[-2].blockFromValue());
385 BOOST_CHECK_EQUAL(sigInfo4.getKeyLocator().getName(), certName.getPrefix(-1));
386
387
388 // SigningInfo is set to DigestSha256
389 Data data5("/data5");
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700390 m_keyChain.sign(data5, SigningInfo(SigningInfo::SIGNER_TYPE_SHA256));
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700391 BOOST_CHECK(Validator::verifySignature(data5, DigestSha256(data5.getSignature())));
392
393 Interest interest5("/interest4");
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700394 m_keyChain.sign(interest5, SigningInfo(SigningInfo::SIGNER_TYPE_SHA256));
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700395 BOOST_CHECK(Validator::verifySignature(interest5,
396 DigestSha256(Signature(interest5.getName()[-2].blockFromValue(),
397 interest5.getName()[-1].blockFromValue()))));
398}
399
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700400BOOST_FIXTURE_TEST_CASE(EcdsaSigningByIdentityNoCert, IdentityManagementFixture)
José Quevedo641de4c2016-01-29 00:11:24 +0000401{
José Quevedo641de4c2016-01-29 00:11:24 +0000402 Data data("/test/data");
403
404 Name nonExistingIdentity = Name("/non-existing/identity").appendVersion();
405
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700406 BOOST_CHECK_NO_THROW(m_keyChain.sign(data, signingByIdentity(nonExistingIdentity)));
José Quevedo641de4c2016-01-29 00:11:24 +0000407 BOOST_CHECK_EQUAL(data.getSignature().getType(),
408 KeyChain::getSignatureType(KeyChain::DEFAULT_KEY_PARAMS.getKeyType(),
Yingdi Yu99b2a002015-08-12 12:47:44 -0700409 DigestAlgorithm::SHA256));
José Quevedo641de4c2016-01-29 00:11:24 +0000410 BOOST_CHECK(nonExistingIdentity.isPrefixOf(data.getSignature().getKeyLocator().getName()));
411
412 Name ecdsaIdentity = Name("/ndn/test/ecdsa").appendVersion();
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700413 Name ecdsaKeyName = m_keyChain.generateEcdsaKeyPairAsDefault(ecdsaIdentity, false, 256);
414 BOOST_CHECK_NO_THROW(m_keyChain.sign(data, signingByIdentity(ecdsaIdentity)));
José Quevedo641de4c2016-01-29 00:11:24 +0000415 BOOST_CHECK_EQUAL(data.getSignature().getType(),
Yingdi Yu99b2a002015-08-12 12:47:44 -0700416 KeyChain::getSignatureType(EcdsaKeyParams().getKeyType(), DigestAlgorithm::SHA256));
José Quevedo641de4c2016-01-29 00:11:24 +0000417 BOOST_CHECK(ecdsaIdentity.isPrefixOf(data.getSignature().getKeyLocator().getName()));
418}
419
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800420BOOST_AUTO_TEST_SUITE_END()
421
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700422} // namespace tests
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700423} // namespace security
Yingdi Yu8dceb1d2014-02-18 12:45:10 -0800424} // namespace ndn