blob: ac45ee747b38cd797add2dab1452292415f20932 [file] [log] [blame]
Yingdi Yufe4733a2015-10-22 14:24:12 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento82d6a4c2017-12-23 19:47:20 -05002/*
Davide Pesaventof6b45892023-03-13 15:00:51 -04003 * Copyright (c) 2013-2023 Regents of the University of California.
Yingdi Yufe4733a2015-10-22 14:24:12 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6 *
7 * 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.
20 */
21
Alexander Afanasyev09236c22020-06-03 13:42:38 -040022#include "ndn-cxx/security/key-chain.hpp"
laqinfan56a812d2019-06-03 15:33:58 -050023#include "ndn-cxx/security/transform/private-key.hpp"
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050024#include "ndn-cxx/security/verification-helpers.hpp"
Yingdi Yufe4733a2015-10-22 14:24:12 -070025
Davide Pesavento7e780642018-11-24 15:51:34 -050026#include "tests/boost-test.hpp"
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050027#include "tests/key-chain-fixture.hpp"
Junxiao Shi9ee770b2022-04-25 23:33:33 +000028#include "tests/unit/clock-fixture.hpp"
Davide Pesavento7e780642018-11-24 15:51:34 -050029#include "tests/unit/test-home-env-saver.hpp"
Yingdi Yufe4733a2015-10-22 14:24:12 -070030
Davide Pesavento49e1e872023-11-11 00:45:23 -050031#include <boost/mp11/list.hpp>
Davide Pesavento73710362020-12-04 16:10:03 -050032
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040033namespace ndn::tests {
Yingdi Yufe4733a2015-10-22 14:24:12 -070034
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040035using namespace ndn::security;
Yingdi Yufe4733a2015-10-22 14:24:12 -070036
37BOOST_AUTO_TEST_SUITE(Security)
Yingdi Yufe4733a2015-10-22 14:24:12 -070038BOOST_FIXTURE_TEST_SUITE(TestKeyChain, TestHomeEnvSaver)
39
40template<class Path>
41class TestHomeAndPibFixture : public TestHomeFixture<Path>
42{
43public:
44 TestHomeAndPibFixture()
45 {
46 unsetenv("NDN_CLIENT_PIB");
47 unsetenv("NDN_CLIENT_TPM");
48 }
Alexander Afanasyev80782e02017-01-04 13:16:54 -080049
50 ~TestHomeAndPibFixture()
51 {
Davide Pesavento0e768ef2022-05-09 20:03:44 -040052 KeyChain::resetDefaultLocators();
Alexander Afanasyev80782e02017-01-04 13:16:54 -080053 }
Yingdi Yufe4733a2015-10-22 14:24:12 -070054};
55
56struct PibPathConfigFileHome
57{
Davide Pesavento3fdb02f2023-04-12 02:32:38 -040058 static constexpr std::string_view PATH = "build/config-file-home/";
Yingdi Yufe4733a2015-10-22 14:24:12 -070059};
60
61BOOST_FIXTURE_TEST_CASE(ConstructorNormalConfig, TestHomeAndPibFixture<PibPathConfigFileHome>)
62{
63 createClientConf({"pib=pib-memory:", "tpm=tpm-memory:"});
64
Yingdi Yufe4733a2015-10-22 14:24:12 -070065 KeyChain keyChain;
66 BOOST_CHECK_EQUAL(keyChain.getPib().getPibLocator(), "pib-memory:");
67 BOOST_CHECK_EQUAL(keyChain.getPib().getTpmLocator(), "tpm-memory:");
68 BOOST_CHECK_EQUAL(keyChain.getTpm().getTpmLocator(), "tpm-memory:");
69}
70
71struct PibPathConfigFileEmptyHome
72{
Davide Pesavento3fdb02f2023-04-12 02:32:38 -040073 static constexpr std::string_view PATH = "build/config-file-empty-home/";
Yingdi Yufe4733a2015-10-22 14:24:12 -070074};
75
76BOOST_FIXTURE_TEST_CASE(ConstructorEmptyConfig, TestHomeAndPibFixture<PibPathConfigFileEmptyHome>)
77{
78 createClientConf({"pib=pib-memory:"});
79
Alexander Afanasyev0cf887d2017-03-26 16:58:59 -050080#if defined(NDN_CXX_HAVE_OSX_FRAMEWORKS)
Yingdi Yufe4733a2015-10-22 14:24:12 -070081 std::string oldHOME;
82 if (std::getenv("OLD_HOME"))
83 oldHOME = std::getenv("OLD_HOME");
84
85 std::string HOME;
86 if (std::getenv("HOME"))
87 HOME = std::getenv("HOME");
88
89 if (!oldHOME.empty())
90 setenv("HOME", oldHOME.c_str(), 1);
91 else
92 unsetenv("HOME");
93#endif
94
Yingdi Yufe4733a2015-10-22 14:24:12 -070095 KeyChain keyChain;
96 BOOST_CHECK_EQUAL(keyChain.getPib().getPibLocator(), "pib-memory:");
97
Davide Pesaventodc3575f2022-07-30 21:10:34 -040098#if defined(NDN_CXX_HAVE_OSX_FRAMEWORKS) && defined(NDN_CXX_WITH_OSX_KEYCHAIN)
Yingdi Yufe4733a2015-10-22 14:24:12 -070099 BOOST_CHECK_EQUAL(keyChain.getPib().getTpmLocator(), "tpm-osxkeychain:");
100 BOOST_CHECK_EQUAL(keyChain.getTpm().getTpmLocator(), "tpm-osxkeychain:");
101#else
102 BOOST_CHECK_EQUAL(keyChain.getPib().getTpmLocator(), "tpm-file:");
103 BOOST_CHECK_EQUAL(keyChain.getTpm().getTpmLocator(), "tpm-file:");
104#endif
105
Alexander Afanasyev0cf887d2017-03-26 16:58:59 -0500106#if defined(NDN_CXX_HAVE_OSX_FRAMEWORKS)
Yingdi Yufe4733a2015-10-22 14:24:12 -0700107 if (!HOME.empty())
108 setenv("HOME", HOME.c_str(), 1);
109 else
110 unsetenv("HOME");
111
112 if (!oldHOME.empty())
113 setenv("OLD_HOME", oldHOME.c_str(), 1);
114 else
115 unsetenv("OLD_HOME");
116#endif
117}
118
119struct PibPathConfigFileEmpty2Home
120{
Davide Pesavento3fdb02f2023-04-12 02:32:38 -0400121 static constexpr std::string_view PATH = "build/config-file-empty2-home/";
Yingdi Yufe4733a2015-10-22 14:24:12 -0700122};
123
Davide Pesavento73710362020-12-04 16:10:03 -0500124BOOST_FIXTURE_TEST_CASE(ConstructorEmptyConfig2, TestHomeAndPibFixture<PibPathConfigFileEmpty2Home>)
Yingdi Yufe4733a2015-10-22 14:24:12 -0700125{
126 createClientConf({"tpm=tpm-memory:"});
127
Yingdi Yufe4733a2015-10-22 14:24:12 -0700128 KeyChain keyChain;
129 BOOST_CHECK_EQUAL(keyChain.getPib().getPibLocator(), "pib-sqlite3:");
130 BOOST_CHECK_EQUAL(keyChain.getPib().getTpmLocator(), "tpm-memory:");
131 BOOST_CHECK_EQUAL(keyChain.getTpm().getTpmLocator(), "tpm-memory:");
132}
133
134struct PibPathConfigFileMalformedHome
135{
Davide Pesavento3fdb02f2023-04-12 02:32:38 -0400136 static constexpr std::string_view PATH = "build/config-file-malformed-home/";
Yingdi Yufe4733a2015-10-22 14:24:12 -0700137};
138
Davide Pesavento73710362020-12-04 16:10:03 -0500139BOOST_FIXTURE_TEST_CASE(ConstructorBadConfig, TestHomeAndPibFixture<PibPathConfigFileMalformedHome>)
Yingdi Yufe4733a2015-10-22 14:24:12 -0700140{
141 createClientConf({"pib=lord", "tpm=ring"});
Davide Pesavento73710362020-12-04 16:10:03 -0500142 BOOST_CHECK_THROW(KeyChain(), KeyChain::Error); // Wrong configuration. Error expected.
Yingdi Yufe4733a2015-10-22 14:24:12 -0700143}
144
145struct PibPathConfigFileMalformed2Home
146{
Davide Pesavento3fdb02f2023-04-12 02:32:38 -0400147 static constexpr std::string_view PATH = "build/config-file-malformed2-home/";
Yingdi Yufe4733a2015-10-22 14:24:12 -0700148};
149
Davide Pesavento73710362020-12-04 16:10:03 -0500150BOOST_FIXTURE_TEST_CASE(ConstructorBadConfig2, TestHomeAndPibFixture<PibPathConfigFileMalformed2Home>)
Yingdi Yufe4733a2015-10-22 14:24:12 -0700151{
152 createClientConf({"pib=pib-sqlite3:%PATH%", "tpm=just-wrong"});
Davide Pesavento73710362020-12-04 16:10:03 -0500153 BOOST_CHECK_THROW(KeyChain(), KeyChain::Error); // Wrong configuration. Error expected.
Yingdi Yufe4733a2015-10-22 14:24:12 -0700154}
155
Alexander Afanasyev57d02b62018-06-15 18:19:50 -0400156struct PibPathConfigFileNonCanonicalTpm
157{
Davide Pesavento3fdb02f2023-04-12 02:32:38 -0400158 static constexpr std::string_view PATH = "build/config-file-non-canonical-tpm/";
Alexander Afanasyev57d02b62018-06-15 18:19:50 -0400159};
160
161BOOST_FIXTURE_TEST_CASE(ConstructorNonCanonicalTpm, TestHomeAndPibFixture<PibPathConfigFileNonCanonicalTpm>) // Bug 4297
162{
163 createClientConf({"pib=pib-sqlite3:", "tpm=tpm-file"});
164
165 {
166 KeyChain keyChain;
167 keyChain.createIdentity("/test");
168 BOOST_CHECK_EQUAL(keyChain.getPib().getPibLocator(), "pib-sqlite3:");
169 BOOST_CHECK_EQUAL(keyChain.getTpm().getTpmLocator(), "tpm-file:");
170 }
171
172 {
173 KeyChain keyChain;
174 BOOST_CHECK_EQUAL(keyChain.getPib().getPibLocator(), "pib-sqlite3:");
175 BOOST_CHECK_EQUAL(keyChain.getTpm().getTpmLocator(), "tpm-file:");
176 BOOST_CHECK(keyChain.getPib().getIdentities().find("/test") != keyChain.getPib().getIdentities().end());
177 }
178}
179
Yingdi Yufe4733a2015-10-22 14:24:12 -0700180BOOST_AUTO_TEST_CASE(KeyChainWithCustomTpmAndPib)
181{
Yingdi Yufe4733a2015-10-22 14:24:12 -0700182 KeyChain keyChain("pib-memory", "tpm-memory");
183 BOOST_CHECK_EQUAL(keyChain.getPib().getPibLocator(), "pib-memory:");
184 BOOST_CHECK_EQUAL(keyChain.getPib().getTpmLocator(), "tpm-memory:");
185 BOOST_CHECK_EQUAL(keyChain.getTpm().getTpmLocator(), "tpm-memory:");
Davide Pesavento0e768ef2022-05-09 20:03:44 -0400186
187 BOOST_CHECK_NO_THROW(KeyChain("pib-memory:", "tpm-memory:"));
188 BOOST_CHECK_NO_THROW(KeyChain("pib-memory:/something", "tpm-memory:/something"));
Yingdi Yufe4733a2015-10-22 14:24:12 -0700189}
190
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500191BOOST_FIXTURE_TEST_CASE(SigningWithCorruptedPibTpm, KeyChainFixture)
Alexander Afanasyevf601e192020-06-02 16:41:07 -0400192{
193 Identity id = m_keyChain.createIdentity("/test");
194
195 Data data("/foobar");
196 BOOST_CHECK_NO_THROW(m_keyChain.sign(data, signingByIdentity(id)));
197
198 // now, "corrupting TPM"
199 const_cast<Tpm&>(m_keyChain.getTpm()).deleteKey(id.getDefaultKey().getName());
200
201 BOOST_CHECK_NO_THROW(id.getDefaultKey());
202 BOOST_CHECK_THROW(m_keyChain.sign(data, signingByIdentity(id)), KeyChain::InvalidSigningInfoError);
203}
204
Davide Pesavento73710362020-12-04 16:10:03 -0500205BOOST_FIXTURE_TEST_CASE(SigningWithNonExistingIdentity, KeyChainFixture)
206{
207 Data data("/test/data");
208 BOOST_CHECK_THROW(m_keyChain.sign(data, signingByIdentity("/non-existing/identity")),
209 KeyChain::InvalidSigningInfoError);
210}
211
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500212BOOST_FIXTURE_TEST_CASE(Management, KeyChainFixture)
Yingdi Yufe4733a2015-10-22 14:24:12 -0700213{
Yingdi Yufe4733a2015-10-22 14:24:12 -0700214 BOOST_CHECK_EQUAL(m_keyChain.getPib().getIdentities().size(), 0);
Davide Pesaventoe30389c2022-05-31 17:23:33 -0400215 BOOST_CHECK_THROW(m_keyChain.getPib().getDefaultIdentity(), Pib::Error);
Yingdi Yufe4733a2015-10-22 14:24:12 -0700216
Davide Pesaventoe30389c2022-05-31 17:23:33 -0400217 // Create an identity
218 Name idName("/test/id");
219 Identity id = m_keyChain.createIdentity(idName);
220 BOOST_REQUIRE(id);
221 BOOST_CHECK_EQUAL(m_keyChain.getPib().getIdentities().size(), 1);
222 BOOST_CHECK_EQUAL(m_keyChain.getPib().getIdentity(idName), id);
223
Yingdi Yufe4733a2015-10-22 14:24:12 -0700224 // The first added identity becomes the default identity
Davide Pesaventoe30389c2022-05-31 17:23:33 -0400225 BOOST_CHECK_EQUAL(m_keyChain.getPib().getDefaultIdentity(), id);
Yingdi Yufe4733a2015-10-22 14:24:12 -0700226 // The default key of the added identity must exist
laqinfan56a812d2019-06-03 15:33:58 -0500227 Key key = id.getDefaultKey();
Davide Pesaventoe30389c2022-05-31 17:23:33 -0400228 BOOST_REQUIRE(key);
229 BOOST_CHECK_EQUAL(id.getKeys().size(), 1);
Yingdi Yufe4733a2015-10-22 14:24:12 -0700230 // The default certificate of the default key must exist
laqinfan56a812d2019-06-03 15:33:58 -0500231 BOOST_CHECK_NO_THROW(key.getDefaultCertificate());
Davide Pesaventoe30389c2022-05-31 17:23:33 -0400232 BOOST_CHECK_EQUAL(key.getCertificates().size(), 1);
Yingdi Yufe4733a2015-10-22 14:24:12 -0700233
234 // Delete key
235 Name key1Name = key.getName();
Yingdi Yufe4733a2015-10-22 14:24:12 -0700236 m_keyChain.deleteKey(id, key);
237 // The key instance should not be valid any more
238 BOOST_CHECK(!key);
Davide Pesaventoe30389c2022-05-31 17:23:33 -0400239 BOOST_CHECK_THROW(id.getDefaultKey(), Pib::Error);
Yingdi Yufe4733a2015-10-22 14:24:12 -0700240 BOOST_CHECK_THROW(id.getKey(key1Name), Pib::Error);
241 BOOST_CHECK_EQUAL(id.getKeys().size(), 0);
242
243 // Create another key
244 m_keyChain.createKey(id);
Davide Pesaventoe30389c2022-05-31 17:23:33 -0400245 // The added key becomes the default key
Yingdi Yufe4733a2015-10-22 14:24:12 -0700246 Key key2 = id.getDefaultKey();
247 BOOST_REQUIRE(key2);
248 BOOST_CHECK_NE(key2.getName(), key1Name);
249 BOOST_CHECK_EQUAL(id.getKeys().size(), 1);
laqinfan56a812d2019-06-03 15:33:58 -0500250 BOOST_CHECK_NO_THROW(key2.getDefaultCertificate());
Davide Pesaventoe30389c2022-05-31 17:23:33 -0400251 BOOST_CHECK_EQUAL(key2.getCertificates().size(), 1);
Yingdi Yufe4733a2015-10-22 14:24:12 -0700252
Davide Pesaventoe30389c2022-05-31 17:23:33 -0400253 // Create a third key
Yingdi Yufe4733a2015-10-22 14:24:12 -0700254 Key key3 = m_keyChain.createKey(id);
Junxiao Shi72c0c642018-04-20 15:41:09 +0000255 BOOST_CHECK_NE(key3.getName(), key2.getName());
Yingdi Yufe4733a2015-10-22 14:24:12 -0700256 BOOST_CHECK_EQUAL(id.getKeys().size(), 2);
Davide Pesaventoe30389c2022-05-31 17:23:33 -0400257 // The added key will not be the default key, because the default was already set
258 BOOST_CHECK_EQUAL(id.getDefaultKey().getName(), key2.getName());
Yingdi Yufe4733a2015-10-22 14:24:12 -0700259
260 // Delete cert
Davide Pesaventoe30389c2022-05-31 17:23:33 -0400261 BOOST_REQUIRE_EQUAL(key3.getCertificates().size(), 1);
Yingdi Yufe4733a2015-10-22 14:24:12 -0700262 Certificate key3Cert1 = *key3.getCertificates().begin();
263 Name key3CertName = key3Cert1.getName();
264 m_keyChain.deleteCertificate(key3, key3CertName);
265 BOOST_CHECK_EQUAL(key3.getCertificates().size(), 0);
Davide Pesaventoe30389c2022-05-31 17:23:33 -0400266 BOOST_CHECK_THROW(key3.getDefaultCertificate(), Pib::Error);
Yingdi Yufe4733a2015-10-22 14:24:12 -0700267
268 // Add cert
269 m_keyChain.addCertificate(key3, key3Cert1);
270 BOOST_CHECK_EQUAL(key3.getCertificates().size(), 1);
laqinfan56a812d2019-06-03 15:33:58 -0500271 BOOST_CHECK_NO_THROW(key3.getDefaultCertificate());
Alexander Afanasyeva10b2ff2017-01-30 12:44:15 -0800272 m_keyChain.addCertificate(key3, key3Cert1); // overwriting the cert should work
Yingdi Yufe4733a2015-10-22 14:24:12 -0700273 BOOST_CHECK_EQUAL(key3.getCertificates().size(), 1);
Davide Pesaventoe30389c2022-05-31 17:23:33 -0400274
Yingdi Yufe4733a2015-10-22 14:24:12 -0700275 // Add another cert
276 Certificate key3Cert2 = key3Cert1;
277 Name key3Cert2Name = key3.getName();
Davide Pesaventoe30389c2022-05-31 17:23:33 -0400278 key3Cert2Name.append("Self").appendVersion();
Yingdi Yufe4733a2015-10-22 14:24:12 -0700279 key3Cert2.setName(key3Cert2Name);
280 m_keyChain.addCertificate(key3, key3Cert2);
281 BOOST_CHECK_EQUAL(key3.getCertificates().size(), 2);
Davide Pesaventoe30389c2022-05-31 17:23:33 -0400282
Davide Pesavento81bd6962020-06-17 16:03:23 -0400283 // Add empty cert
284 Certificate key3Cert3 = key3Cert1;
285 key3Cert3.unsetContent();
286 BOOST_CHECK_THROW(m_keyChain.addCertificate(key3, key3Cert3), std::invalid_argument);
Yingdi Yufe4733a2015-10-22 14:24:12 -0700287
Davide Pesaventoe30389c2022-05-31 17:23:33 -0400288 // Create another identity
289 Name id2Name("/test/id2");
290 Identity id2 = m_keyChain.createIdentity(id2Name);
291 BOOST_REQUIRE(id2);
292 BOOST_CHECK_EQUAL(m_keyChain.getPib().getIdentities().size(), 2);
293 BOOST_CHECK_EQUAL(m_keyChain.getPib().getIdentity(id2Name), id2);
294
295 // Default identity setting
296 BOOST_CHECK_EQUAL(m_keyChain.getPib().getDefaultIdentity().getName(), id.getName());
297 m_keyChain.setDefaultIdentity(id2);
298 BOOST_CHECK_EQUAL(m_keyChain.getPib().getDefaultIdentity().getName(), id2.getName());
Yingdi Yufe4733a2015-10-22 14:24:12 -0700299
300 // Default key setting
301 BOOST_CHECK_EQUAL(id.getDefaultKey().getName(), key2.getName());
302 m_keyChain.setDefaultKey(id, key3);
303 BOOST_CHECK_EQUAL(id.getDefaultKey().getName(), key3.getName());
Davide Pesaventoe30389c2022-05-31 17:23:33 -0400304 // Set key of a different identity as default
305 BOOST_CHECK_THROW(m_keyChain.setDefaultKey(id, id2.getDefaultKey()), std::invalid_argument);
Yingdi Yufe4733a2015-10-22 14:24:12 -0700306
Davide Pesaventoe30389c2022-05-31 17:23:33 -0400307 // Default certificate setting
308 BOOST_CHECK_EQUAL(key3.getDefaultCertificate().getName(), key3CertName);
309 m_keyChain.setDefaultCertificate(key3, key3Cert2);
310 BOOST_CHECK_EQUAL(key3.getDefaultCertificate().getName(), key3Cert2Name);
311 // Set certificate of a different key as default
312 BOOST_CHECK_THROW(m_keyChain.setDefaultCertificate(key3, key2.getDefaultCertificate()),
313 std::invalid_argument);
314
315 // Delete certificate name mismatch
316 BOOST_CHECK_THROW(m_keyChain.deleteCertificate(key2, key3CertName), std::invalid_argument);
317 // Delete key name mismatch
318 BOOST_CHECK_THROW(m_keyChain.deleteKey(id, id2.getDefaultKey()), std::invalid_argument);
Yingdi Yufe4733a2015-10-22 14:24:12 -0700319
320 // Delete identity
321 m_keyChain.deleteIdentity(id);
322 // The identity instance should not be valid any more
323 BOOST_CHECK(!id);
Davide Pesaventoe30389c2022-05-31 17:23:33 -0400324 BOOST_CHECK_THROW(m_keyChain.getPib().getIdentity(idName), Pib::Error);
325 BOOST_CHECK_EQUAL(m_keyChain.getPib().getIdentities().size(), 1);
Yingdi Yufe4733a2015-10-22 14:24:12 -0700326}
327
Davide Pesavento73710362020-12-04 16:10:03 -0500328struct DataPkt
Yingdi Yufe4733a2015-10-22 14:24:12 -0700329{
Davide Pesavento73710362020-12-04 16:10:03 -0500330 Data packet{"/data"};
331 SignedInterestFormat sigFormat = SignedInterestFormat::V02; // irrelevant for Data
Yingdi Yufe4733a2015-10-22 14:24:12 -0700332
Davide Pesavento73710362020-12-04 16:10:03 -0500333 SignatureInfo
334 getSignatureInfo() const
335 {
336 return packet.getSignatureInfo();
337 }
338};
laqinfan56a812d2019-06-03 15:33:58 -0500339
Davide Pesavento73710362020-12-04 16:10:03 -0500340struct InterestV02Pkt
341{
Davide Pesaventoaee2ada2022-02-18 14:43:02 -0500342 Interest packet{"/interest02"};
Davide Pesavento73710362020-12-04 16:10:03 -0500343 SignedInterestFormat sigFormat = SignedInterestFormat::V02;
Eric Newberryb74bbda2020-06-18 19:33:58 -0700344
Davide Pesavento73710362020-12-04 16:10:03 -0500345 SignatureInfo
346 getSignatureInfo() const
347 {
348 return SignatureInfo(packet.getName()[signed_interest::POS_SIG_INFO].blockFromValue());
349 }
350};
Eric Newberryb74bbda2020-06-18 19:33:58 -0700351
Davide Pesavento73710362020-12-04 16:10:03 -0500352struct InterestV03Pkt
353{
Davide Pesaventoaee2ada2022-02-18 14:43:02 -0500354 Interest packet{"/interest03"};
Davide Pesavento73710362020-12-04 16:10:03 -0500355 SignedInterestFormat sigFormat = SignedInterestFormat::V03;
Eric Newberryb74bbda2020-06-18 19:33:58 -0700356
Davide Pesavento73710362020-12-04 16:10:03 -0500357 SignatureInfo
358 getSignatureInfo() const
359 {
360 return packet.getSignatureInfo().value(); // use .value() for checked access
361 }
362};
Eric Newberryb74bbda2020-06-18 19:33:58 -0700363
Davide Pesavento73710362020-12-04 16:10:03 -0500364template<typename KeyParams>
365struct DefaultIdentity
366{
367 Identity
368 operator()(KeyChain& keyChain) const
369 {
370 auto id = keyChain.createIdentity("/id", KeyParams());
371 BOOST_ASSERT(keyChain.getPib().getDefaultIdentity() == id);
372 return id;
373 }
374};
Eric Newberryb74bbda2020-06-18 19:33:58 -0700375
Davide Pesavento73710362020-12-04 16:10:03 -0500376template<typename KeyParams>
377struct NonDefaultIdentity
378{
379 Identity
380 operator()(KeyChain& keyChain) const
381 {
382 auto id = keyChain.createIdentity("/id");
383 auto id2 = keyChain.createIdentity("/id2", KeyParams());
384 BOOST_ASSERT(keyChain.getPib().getDefaultIdentity() == id);
385 return id2;
386 }
387};
Eric Newberryb74bbda2020-06-18 19:33:58 -0700388
Davide Pesavento73710362020-12-04 16:10:03 -0500389template<typename KeyParams>
390struct DefaultKey
391{
392 Key
393 operator()(KeyChain&, const Identity& id) const
394 {
395 auto key = id.getDefaultKey();
396 BOOST_ASSERT(key.getKeyType() == KeyParams().getKeyType());
397 return key;
398 }
399};
Eric Newberryb74bbda2020-06-18 19:33:58 -0700400
Davide Pesavento73710362020-12-04 16:10:03 -0500401template<typename KeyParams>
402struct NonDefaultKey
403{
404 Key
405 operator()(KeyChain& keyChain, const Identity& id) const
406 {
407 auto key2 = keyChain.createKey(id, KeyParams());
408 BOOST_ASSERT(id.getDefaultKey() != key2);
409 return key2;
410 }
411};
Eric Newberryb74bbda2020-06-18 19:33:58 -0700412
Davide Pesavento73710362020-12-04 16:10:03 -0500413template<typename PacketType,
414 template<typename> class IdentityMaker = DefaultIdentity,
415 template<typename> class KeyMaker = DefaultKey,
416 typename AsymmetricKeyParams = EcKeyParams,
417 uint32_t SignatureTypeTlvValue = tlv::SignatureSha256WithEcdsa>
418struct AsymmetricSigningBase : protected KeyChainFixture, protected PacketType
419{
420 const Identity id = IdentityMaker<AsymmetricKeyParams>()(m_keyChain);
421 const Key key = KeyMaker<AsymmetricKeyParams>()(m_keyChain, id);
422 const Certificate cert = key.getDefaultCertificate();
423
424 const uint32_t expectedSigType = SignatureTypeTlvValue;
425 const bool shouldHaveKeyLocator = true;
Davide Pesaventof6b45892023-03-13 15:00:51 -0400426 const std::optional<KeyLocator> expectedKeyLocator = cert.getName();
Davide Pesavento73710362020-12-04 16:10:03 -0500427
428 bool
429 verify(const SigningInfo&) const
430 {
431 return verifySignature(this->packet, key);
432 }
433};
434
435template<typename PacketType,
436 typename AsymmetricKeyParams,
437 uint32_t SignatureTypeTlvValue>
438struct AsymmetricSigning : protected AsymmetricSigningBase<PacketType, DefaultIdentity, DefaultKey,
439 AsymmetricKeyParams, SignatureTypeTlvValue>
440{
441 const std::vector<SigningInfo> signingInfos = {
Yingdi Yufe4733a2015-10-22 14:24:12 -0700442 SigningInfo(),
Davide Pesavento73710362020-12-04 16:10:03 -0500443 SigningInfo(""),
Yingdi Yufe4733a2015-10-22 14:24:12 -0700444
Davide Pesavento73710362020-12-04 16:10:03 -0500445 SigningInfo(this->id),
446 SigningInfo(SigningInfo::SIGNER_TYPE_ID, this->id.getName()),
447 SigningInfo("id:" + this->id.getName().toUri()),
448 signingByIdentity(this->id),
449 signingByIdentity(this->id.getName()),
Yingdi Yufe4733a2015-10-22 14:24:12 -0700450
Davide Pesavento73710362020-12-04 16:10:03 -0500451 SigningInfo(this->key),
452 SigningInfo(SigningInfo::SIGNER_TYPE_KEY, this->key.getName()),
453 SigningInfo("key:" + this->key.getName().toUri()),
454 signingByKey(this->key),
455 signingByKey(this->key.getName()),
Alexander Afanasyevd6d78aa2017-01-02 18:14:23 -0800456
Davide Pesavento73710362020-12-04 16:10:03 -0500457 SigningInfo(SigningInfo::SIGNER_TYPE_CERT, this->cert.getName()),
458 SigningInfo("cert:" + this->cert.getName().toUri()),
459 signingByCertificate(this->cert),
460 signingByCertificate(this->cert.getName()),
461 };
462};
Yingdi Yufe4733a2015-10-22 14:24:12 -0700463
Davide Pesavento73710362020-12-04 16:10:03 -0500464template<typename PacketType>
465using RsaSigning = AsymmetricSigning<PacketType, RsaKeyParams, tlv::SignatureSha256WithRsa>;
Alexander Afanasyevd6d78aa2017-01-02 18:14:23 -0800466
Davide Pesavento73710362020-12-04 16:10:03 -0500467template<typename PacketType>
468using EcdsaSigning = AsymmetricSigning<PacketType, EcKeyParams, tlv::SignatureSha256WithEcdsa>;
Yingdi Yufe4733a2015-10-22 14:24:12 -0700469
Davide Pesavento73710362020-12-04 16:10:03 -0500470template<typename PacketType>
471struct SigningWithNonDefaultIdentity : protected AsymmetricSigningBase<PacketType, NonDefaultIdentity>
472{
473 const std::vector<SigningInfo> signingInfos = {
474 signingByIdentity(this->id),
475 signingByIdentity(this->id.getName()),
476 signingByKey(this->key),
477 signingByCertificate(this->cert),
478 };
479};
480
481template<typename PacketType>
482struct SigningWithNonDefaultKey : protected AsymmetricSigningBase<PacketType, NonDefaultIdentity, NonDefaultKey>
483{
484 const std::vector<SigningInfo> signingInfos = {
485 signingByKey(this->key),
486 signingByKey(this->key.getName()),
487 signingByCertificate(this->cert),
488 };
489};
490
491template<typename PacketType,
492 DigestAlgorithm DigestAlgo = DigestAlgorithm::SHA256,
493 uint32_t SignatureTypeTlvValue = tlv::SignatureHmacWithSha256>
494struct HmacSigning : protected KeyChainFixture, protected PacketType
495{
496 const std::vector<SigningInfo> signingInfos = {
497 SigningInfo(SigningInfo::SIGNER_TYPE_HMAC, m_keyChain.createHmacKey()),
laqinfan56a812d2019-06-03 15:33:58 -0500498 SigningInfo("hmac-sha256:QjM3NEEyNkE3MTQ5MDQzN0FBMDI0RTRGQURENUI0OTdGREZGMUE4RUE2RkYxMkY2RkI2NUFGMjcyMEI1OUNDRg=="),
Davide Pesavento73710362020-12-04 16:10:03 -0500499 };
laqinfan56a812d2019-06-03 15:33:58 -0500500
Davide Pesavento73710362020-12-04 16:10:03 -0500501 const uint32_t expectedSigType = SignatureTypeTlvValue;
502 const bool shouldHaveKeyLocator = true;
Davide Pesaventof6b45892023-03-13 15:00:51 -0400503 const std::optional<KeyLocator> expectedKeyLocator = std::nullopt; // don't check KeyLocator value
Davide Pesavento73710362020-12-04 16:10:03 -0500504
505 bool
506 verify(const SigningInfo& si) const
507 {
508 return verifySignature(this->packet, m_keyChain.getTpm(), si.getSignerName(), DigestAlgo);
509 }
510};
511
512template<typename PacketType>
513struct Sha256Signing : protected KeyChainFixture, protected PacketType
514{
515 const std::vector<SigningInfo> signingInfos = {
Yingdi Yufe4733a2015-10-22 14:24:12 -0700516 SigningInfo(SigningInfo::SIGNER_TYPE_SHA256),
Davide Pesavento73710362020-12-04 16:10:03 -0500517 SigningInfo("id:" + SigningInfo::getDigestSha256Identity().toUri()),
Yingdi Yufe4733a2015-10-22 14:24:12 -0700518 signingWithSha256()
519 };
520
Davide Pesavento73710362020-12-04 16:10:03 -0500521 const uint32_t expectedSigType = tlv::DigestSha256;
522 const bool shouldHaveKeyLocator = false;
Davide Pesaventof6b45892023-03-13 15:00:51 -0400523 const std::optional<KeyLocator> expectedKeyLocator = std::nullopt;
Yingdi Yufe4733a2015-10-22 14:24:12 -0700524
Davide Pesavento73710362020-12-04 16:10:03 -0500525 bool
526 verify(const SigningInfo&) const
527 {
Davide Pesaventof6b45892023-03-13 15:00:51 -0400528 return verifySignature(this->packet, std::nullopt);
Davide Pesavento73710362020-12-04 16:10:03 -0500529 }
530};
Yingdi Yufe4733a2015-10-22 14:24:12 -0700531
Davide Pesavento49e1e872023-11-11 00:45:23 -0500532using SigningTests = boost::mp11::mp_list<
Davide Pesavento73710362020-12-04 16:10:03 -0500533 RsaSigning<DataPkt>,
534 RsaSigning<InterestV02Pkt>,
535 RsaSigning<InterestV03Pkt>,
536 EcdsaSigning<DataPkt>,
537 EcdsaSigning<InterestV02Pkt>,
538 EcdsaSigning<InterestV03Pkt>,
539 HmacSigning<DataPkt>,
540 HmacSigning<InterestV02Pkt>,
541 HmacSigning<InterestV03Pkt>,
542 Sha256Signing<DataPkt>,
543 Sha256Signing<InterestV02Pkt>,
544 Sha256Signing<InterestV03Pkt>,
545 SigningWithNonDefaultIdentity<DataPkt>,
Davide Pesavento809f7542021-03-24 18:53:05 -0400546 SigningWithNonDefaultIdentity<InterestV03Pkt>,
547 SigningWithNonDefaultKey<DataPkt>,
548 SigningWithNonDefaultKey<InterestV03Pkt>
Davide Pesavento73710362020-12-04 16:10:03 -0500549>;
Eric Newberryb74bbda2020-06-18 19:33:58 -0700550
Davide Pesavento73710362020-12-04 16:10:03 -0500551BOOST_FIXTURE_TEST_CASE_TEMPLATE(SigningInterface, T, SigningTests, T)
552{
553 BOOST_TEST_CONTEXT("Packet = " << this->packet.getName()) {
554 for (auto signingInfo : this->signingInfos) {
555 signingInfo.setSignedInterestFormat(this->sigFormat);
556
557 BOOST_TEST_CONTEXT("SigningInfo = " << signingInfo) {
558 this->m_keyChain.sign(this->packet, signingInfo);
559
560 auto sigInfo = this->getSignatureInfo();
561 BOOST_CHECK_EQUAL(sigInfo.getSignatureType(), this->expectedSigType);
562 BOOST_CHECK_EQUAL(sigInfo.hasKeyLocator(), this->shouldHaveKeyLocator);
563 if (this->expectedKeyLocator) {
564 BOOST_CHECK_EQUAL(sigInfo.getKeyLocator(), *this->expectedKeyLocator);
565 }
566 BOOST_CHECK(this->verify(signingInfo));
Eric Newberryb74bbda2020-06-18 19:33:58 -0700567 }
Yingdi Yufe4733a2015-10-22 14:24:12 -0700568 }
569 }
570}
571
Junxiao Shi9ee770b2022-04-25 23:33:33 +0000572class MakeCertificateFixture : public ClockFixture
573{
574public:
575 MakeCertificateFixture()
576 : requesterKeyChain("pib-memory:", "tpm-memory:")
577 , signerKeyChain("pib-memory:", "tpm-memory:")
578 {
579 m_systemClock->setNow(time::fromIsoString("20091117T203458,651387237").time_since_epoch());
580
581 requester = requesterKeyChain.createIdentity("/requester").getDefaultKey();
582 Name signerIdentityName("/signer");
583 signerKey = signerKeyChain.createIdentity(signerIdentityName).getDefaultKey();
584 signerParams = signingByIdentity(signerIdentityName);
585 }
586
587 void
Davide Pesaventof6b45892023-03-13 15:00:51 -0400588 checkKeyLocatorName(const Certificate& cert,
589 const std::optional<Name>& klName = std::nullopt) const
Junxiao Shi9ee770b2022-04-25 23:33:33 +0000590 {
591 auto kl = cert.getKeyLocator();
592 if (!kl.has_value()) {
593 BOOST_ERROR("KeyLocator is missing");
594 return;
595 }
Davide Pesavento07db0732022-05-06 15:20:26 -0400596 BOOST_CHECK_EQUAL(kl->getName(), klName.value_or(signerKey.getDefaultCertificate().getName()));
Junxiao Shi9ee770b2022-04-25 23:33:33 +0000597 }
598
599 void
600 checkCertFromDefaults(const Certificate& cert) const
601 {
602 BOOST_CHECK(Certificate::isValidName(cert.getName()));
603 BOOST_CHECK_EQUAL(cert.getKeyName(), requester.getName());
604 BOOST_CHECK_EQUAL(cert.getName()[-2], name::Component("NA"));
605 BOOST_CHECK(cert.getName()[-1].isVersion());
606
607 BOOST_CHECK_EQUAL(cert.getContentType(), tlv::ContentType_Key);
608 BOOST_CHECK_EQUAL(cert.getFreshnessPeriod(), 1_h);
609
Davide Pesavento8e2a61d2022-05-13 18:44:03 -0400610 BOOST_TEST(cert.getPublicKey() == requester.getPublicKey(), boost::test_tools::per_element());
Junxiao Shi9ee770b2022-04-25 23:33:33 +0000611
612 checkKeyLocatorName(cert);
613
614 BOOST_CHECK(cert.isValid());
615 auto vp = cert.getValidityPeriod().getPeriod();
616 BOOST_CHECK_EQUAL(vp.first, time::fromIsoString("20091117T203458"));
617 BOOST_CHECK_EQUAL(vp.second, time::fromIsoString("20101117T203458"));
618
619 auto adBlock = cert.getSignatureInfo().getCustomTlv(tlv::AdditionalDescription);
620 BOOST_CHECK(!adBlock.has_value());
621 }
622
623public:
624 KeyChain requesterKeyChain;
625 pib::Key requester;
626
627 KeyChain signerKeyChain;
628 pib::Key signerKey;
629 Name signerCertificateName;
630 SigningInfo signerParams;
631};
632
633BOOST_FIXTURE_TEST_SUITE(MakeCertificate, MakeCertificateFixture)
634
635BOOST_AUTO_TEST_CASE(DefaultsFromKey)
636{
637 auto cert = signerKeyChain.makeCertificate(requester, signerParams);
638 checkCertFromDefaults(cert);
639}
640
641BOOST_AUTO_TEST_CASE(DefaultsFromCert)
642{
643 auto cert = signerKeyChain.makeCertificate(requester.getDefaultCertificate(), signerParams);
644 checkCertFromDefaults(cert);
645}
646
647BOOST_AUTO_TEST_CASE(Options)
648{
649 MakeCertificateOptions opts;
650 opts.issuerId = name::Component::fromEscapedString("ISSUER");
651 opts.version = 41218268;
652 opts.freshnessPeriod = 321_s;
653 opts.validity.emplace(time::fromIsoString("20060702T150405"),
654 time::fromIsoString("20160702T150405"));
655
656 SignatureInfo sigInfo;
657 sigInfo.setKeyLocator(signerKey.getName());
658 sigInfo.setValidityPeriod(ValidityPeriod(time::fromIsoString("20060102T150405"),
659 time::fromIsoString("20160102T150405")));
660 sigInfo.addCustomTlv(Block(0xF0));
661 signerParams.setSignatureInfo(sigInfo);
662
663 auto cert = signerKeyChain.makeCertificate(requester, signerParams, opts);
664
665 BOOST_CHECK_EQUAL(cert.getName(),
666 Name(requester.getName()).append(PartialName("ISSUER/v=41218268")));
667 BOOST_CHECK_EQUAL(cert.getFreshnessPeriod(), 321_s);
668 checkKeyLocatorName(cert, signerKey.getName());
669
670 auto vp = cert.getValidityPeriod().getPeriod();
671 BOOST_CHECK_EQUAL(vp.first, time::fromIsoString("20060702T150405"));
672 BOOST_CHECK_EQUAL(vp.second, time::fromIsoString("20160702T150405"));
673
674 BOOST_CHECK(cert.getSignatureInfo().getCustomTlv(0xF0).has_value());
675}
676
677BOOST_AUTO_TEST_CASE(ErrSigner)
678{
679 signerParams = signingByIdentity("/nonexistent");
680 BOOST_CHECK_THROW(signerKeyChain.makeCertificate(requester, signerParams), KeyChain::Error);
681}
682
683BOOST_AUTO_TEST_CASE(ErrZeroFreshness)
684{
685 MakeCertificateOptions opts;
686 opts.freshnessPeriod = 0_ms;
687 BOOST_CHECK_THROW(signerKeyChain.makeCertificate(requester, signerParams, opts),
688 std::invalid_argument);
689}
690
691BOOST_AUTO_TEST_CASE(ErrNegativeFreshness)
692{
693 MakeCertificateOptions opts;
694 opts.freshnessPeriod = -1_ms;
695 BOOST_CHECK_THROW(signerKeyChain.makeCertificate(requester, signerParams, opts),
696 std::invalid_argument);
697}
698
699BOOST_AUTO_TEST_CASE(ErrContent)
700{
701 Certificate request(requester.getDefaultCertificate());
Davide Pesavento07db0732022-05-06 15:20:26 -0400702
703 // malformed public key
Junxiao Shi9ee770b2022-04-25 23:33:33 +0000704 const auto& oldContent = request.getContent();
705 std::vector<uint8_t> content(oldContent.value_begin(), oldContent.value_end());
706 content[0] ^= 0x80;
707 request.setContent(content);
708 BOOST_CHECK_THROW(signerKeyChain.makeCertificate(request, signerParams), std::invalid_argument);
Davide Pesavento07db0732022-05-06 15:20:26 -0400709
710 // empty content
Davide Pesavento296c3a12023-05-04 21:40:40 -0400711 request.setContent("");
Davide Pesavento07db0732022-05-06 15:20:26 -0400712 BOOST_CHECK_THROW(signerKeyChain.makeCertificate(request, signerParams), std::invalid_argument);
Junxiao Shi9ee770b2022-04-25 23:33:33 +0000713}
714
715BOOST_AUTO_TEST_SUITE_END() // MakeCertificate
716
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500717BOOST_FIXTURE_TEST_CASE(ImportPrivateKey, KeyChainFixture)
laqinfan56a812d2019-06-03 15:33:58 -0500718{
Davide Pesavento765abc92021-12-27 00:44:04 -0500719 const Name keyName("/test/device2");
720 const uint8_t rawKey[] = "nPSNOHyZKsg2WLqHAs7MXGb0sjQb4zCT";
laqinfan56a812d2019-06-03 15:33:58 -0500721 auto key = make_shared<transform::PrivateKey>();
Davide Pesavento765abc92021-12-27 00:44:04 -0500722 key->loadRaw(KeyType::HMAC, rawKey);
laqinfan56a812d2019-06-03 15:33:58 -0500723
724 m_keyChain.importPrivateKey(keyName, key);
725 BOOST_CHECK_EQUAL(m_keyChain.getTpm().hasKey(keyName), true);
726 BOOST_CHECK_THROW(m_keyChain.importPrivateKey(keyName, key), KeyChain::Error);
727}
728
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500729BOOST_FIXTURE_TEST_CASE(ExportImport, KeyChainFixture)
Yingdi Yufe4733a2015-10-22 14:24:12 -0700730{
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500731 Identity id = m_keyChain.createIdentity("/TestKeyChain/ExportIdentity");
Yingdi Yufe4733a2015-10-22 14:24:12 -0700732 Certificate cert = id.getDefaultKey().getDefaultCertificate();
733
734 shared_ptr<SafeBag> exported = m_keyChain.exportSafeBag(cert, "1234", 4);
Davide Pesaventoe30389c2022-05-31 17:23:33 -0400735 const auto& block = exported->wireEncode();
Yingdi Yufe4733a2015-10-22 14:24:12 -0700736
737 m_keyChain.deleteIdentity(id);
Davide Pesaventoe30389c2022-05-31 17:23:33 -0400738 BOOST_CHECK_EXCEPTION(m_keyChain.exportSafeBag(cert, "1234", 4), KeyChain::Error, [] (const auto& e) {
739 return std::string(e.what()).find("Failed to export private key") == 0;
740 });
Yingdi Yufe4733a2015-10-22 14:24:12 -0700741
742 BOOST_CHECK_EQUAL(m_keyChain.getTpm().hasKey(cert.getKeyName()), false);
743 BOOST_CHECK_EQUAL(m_keyChain.getPib().getIdentities().size(), 0);
744
Davide Pesaventoe30389c2022-05-31 17:23:33 -0400745 SafeBag imported(block);
Yingdi Yufe4733a2015-10-22 14:24:12 -0700746 m_keyChain.importSafeBag(imported, "1234", 4);
Davide Pesaventoe30389c2022-05-31 17:23:33 -0400747 BOOST_CHECK_EXCEPTION(m_keyChain.importSafeBag(imported, "1234", 4), KeyChain::Error, [] (const auto& e) {
748 return std::string(e.what()).find("already exists") != std::string::npos;
749 });
Yingdi Yufe4733a2015-10-22 14:24:12 -0700750
751 BOOST_CHECK_EQUAL(m_keyChain.getTpm().hasKey(cert.getKeyName()), true);
752 BOOST_CHECK_EQUAL(m_keyChain.getPib().getIdentities().size(), 1);
Yingdi Yufe4733a2015-10-22 14:24:12 -0700753 Identity newId = m_keyChain.getPib().getIdentity(cert.getIdentity());
754 BOOST_CHECK_EQUAL(newId.getKeys().size(), 1);
Yingdi Yufe4733a2015-10-22 14:24:12 -0700755 Key newKey = newId.getKey(cert.getKeyName());
756 BOOST_CHECK_EQUAL(newKey.getCertificates().size(), 1);
Davide Pesaventoe30389c2022-05-31 17:23:33 -0400757 Certificate newCert = newKey.getCertificate(cert.getName());
758 BOOST_CHECK_EQUAL(newCert, cert);
759}
Yingdi Yufe4733a2015-10-22 14:24:12 -0700760
Davide Pesaventoe30389c2022-05-31 17:23:33 -0400761BOOST_FIXTURE_TEST_CASE(ImportInvalid, KeyChainFixture)
762{
763 Identity id = m_keyChain.createIdentity("/TestKeyChain/ExportIdentity");
764 Certificate cert = id.getDefaultKey().getDefaultCertificate();
765
766 auto exported = m_keyChain.exportSafeBag(cert, "1234", 4);
767 m_keyChain.deleteIdentity(id);
768
769 Identity id2 = m_keyChain.createIdentity("/TestKeyChain/AnotherIdentity");
770 Certificate cert2 = id2.getDefaultKey().getDefaultCertificate();
771 m_keyChain.deleteIdentity(id2);
772
773 SafeBag mismatch(cert2, exported->getEncryptedKey());
774 BOOST_CHECK_EXCEPTION(m_keyChain.importSafeBag(mismatch, "1234", 4), KeyChain::Error, [] (const auto& e) {
775 return std::string(e.what()).find("do not match") != std::string::npos;
776 });
777
778 SafeBag invalidPriv(cert2, {0xCA, 0xFE});
779 BOOST_CHECK_EXCEPTION(m_keyChain.importSafeBag(invalidPriv, "1234", 4), KeyChain::Error, [] (const auto& e) {
780 return std::string(e.what()).find("Failed to import private key") == 0;
781 });
Yingdi Yufe4733a2015-10-22 14:24:12 -0700782}
783
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500784BOOST_FIXTURE_TEST_CASE(SelfSignedCertValidity, KeyChainFixture)
Alexander Afanasyevf8379172017-01-11 16:56:04 -0800785{
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500786 Certificate cert = m_keyChain.createIdentity("/Security/TestKeyChain/SelfSignedCertValidity")
Alexander Afanasyevf8379172017-01-11 16:56:04 -0800787 .getDefaultKey()
788 .getDefaultCertificate();
789 BOOST_CHECK(cert.isValid());
Davide Pesavento0f830802018-01-16 23:58:58 -0500790 BOOST_CHECK(cert.isValid(time::system_clock::now() + 10 * 365_days));
791 BOOST_CHECK_GT(cert.getValidityPeriod().getPeriod().second, time::system_clock::now() + 10 * 365_days);
Alexander Afanasyevf8379172017-01-11 16:56:04 -0800792}
793
Yingdi Yufe4733a2015-10-22 14:24:12 -0700794BOOST_AUTO_TEST_SUITE_END() // TestKeyChain
Yingdi Yufe4733a2015-10-22 14:24:12 -0700795BOOST_AUTO_TEST_SUITE_END() // Security
796
Davide Pesavento47ce2ee2023-05-09 01:33:33 -0400797} // namespace ndn::tests