blob: 15bf4cd8b8e9427504f5f8c81708b6d2421287b1 [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 Pesavento80d671f2022-06-08 04:04:52 -040031#include <openssl/opensslv.h>
Davide Pesavento73710362020-12-04 16:10:03 -050032#include <boost/mpl/vector.hpp>
33
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040034namespace ndn::tests {
Yingdi Yufe4733a2015-10-22 14:24:12 -070035
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040036using namespace ndn::security;
Yingdi Yufe4733a2015-10-22 14:24:12 -070037
38BOOST_AUTO_TEST_SUITE(Security)
Yingdi Yufe4733a2015-10-22 14:24:12 -070039BOOST_FIXTURE_TEST_SUITE(TestKeyChain, TestHomeEnvSaver)
40
41template<class Path>
42class TestHomeAndPibFixture : public TestHomeFixture<Path>
43{
44public:
45 TestHomeAndPibFixture()
46 {
47 unsetenv("NDN_CLIENT_PIB");
48 unsetenv("NDN_CLIENT_TPM");
49 }
Alexander Afanasyev80782e02017-01-04 13:16:54 -080050
51 ~TestHomeAndPibFixture()
52 {
Davide Pesavento0e768ef2022-05-09 20:03:44 -040053 KeyChain::resetDefaultLocators();
Alexander Afanasyev80782e02017-01-04 13:16:54 -080054 }
Yingdi Yufe4733a2015-10-22 14:24:12 -070055};
56
57struct PibPathConfigFileHome
58{
Davide Pesavento3fdb02f2023-04-12 02:32:38 -040059 static constexpr std::string_view PATH = "build/config-file-home/";
Yingdi Yufe4733a2015-10-22 14:24:12 -070060};
61
62BOOST_FIXTURE_TEST_CASE(ConstructorNormalConfig, TestHomeAndPibFixture<PibPathConfigFileHome>)
63{
64 createClientConf({"pib=pib-memory:", "tpm=tpm-memory:"});
65
Yingdi Yufe4733a2015-10-22 14:24:12 -070066 KeyChain keyChain;
67 BOOST_CHECK_EQUAL(keyChain.getPib().getPibLocator(), "pib-memory:");
68 BOOST_CHECK_EQUAL(keyChain.getPib().getTpmLocator(), "tpm-memory:");
69 BOOST_CHECK_EQUAL(keyChain.getTpm().getTpmLocator(), "tpm-memory:");
70}
71
72struct PibPathConfigFileEmptyHome
73{
Davide Pesavento3fdb02f2023-04-12 02:32:38 -040074 static constexpr std::string_view PATH = "build/config-file-empty-home/";
Yingdi Yufe4733a2015-10-22 14:24:12 -070075};
76
77BOOST_FIXTURE_TEST_CASE(ConstructorEmptyConfig, TestHomeAndPibFixture<PibPathConfigFileEmptyHome>)
78{
79 createClientConf({"pib=pib-memory:"});
80
Alexander Afanasyev0cf887d2017-03-26 16:58:59 -050081#if defined(NDN_CXX_HAVE_OSX_FRAMEWORKS)
Yingdi Yufe4733a2015-10-22 14:24:12 -070082 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())
91 setenv("HOME", oldHOME.c_str(), 1);
92 else
93 unsetenv("HOME");
94#endif
95
Yingdi Yufe4733a2015-10-22 14:24:12 -070096 KeyChain keyChain;
97 BOOST_CHECK_EQUAL(keyChain.getPib().getPibLocator(), "pib-memory:");
98
Davide Pesaventodc3575f2022-07-30 21:10:34 -040099#if defined(NDN_CXX_HAVE_OSX_FRAMEWORKS) && defined(NDN_CXX_WITH_OSX_KEYCHAIN)
Yingdi Yufe4733a2015-10-22 14:24:12 -0700100 BOOST_CHECK_EQUAL(keyChain.getPib().getTpmLocator(), "tpm-osxkeychain:");
101 BOOST_CHECK_EQUAL(keyChain.getTpm().getTpmLocator(), "tpm-osxkeychain:");
102#else
103 BOOST_CHECK_EQUAL(keyChain.getPib().getTpmLocator(), "tpm-file:");
104 BOOST_CHECK_EQUAL(keyChain.getTpm().getTpmLocator(), "tpm-file:");
105#endif
106
Alexander Afanasyev0cf887d2017-03-26 16:58:59 -0500107#if defined(NDN_CXX_HAVE_OSX_FRAMEWORKS)
Yingdi Yufe4733a2015-10-22 14:24:12 -0700108 if (!HOME.empty())
109 setenv("HOME", HOME.c_str(), 1);
110 else
111 unsetenv("HOME");
112
113 if (!oldHOME.empty())
114 setenv("OLD_HOME", oldHOME.c_str(), 1);
115 else
116 unsetenv("OLD_HOME");
117#endif
118}
119
120struct PibPathConfigFileEmpty2Home
121{
Davide Pesavento3fdb02f2023-04-12 02:32:38 -0400122 static constexpr std::string_view PATH = "build/config-file-empty2-home/";
Yingdi Yufe4733a2015-10-22 14:24:12 -0700123};
124
Davide Pesavento73710362020-12-04 16:10:03 -0500125BOOST_FIXTURE_TEST_CASE(ConstructorEmptyConfig2, TestHomeAndPibFixture<PibPathConfigFileEmpty2Home>)
Yingdi Yufe4733a2015-10-22 14:24:12 -0700126{
127 createClientConf({"tpm=tpm-memory:"});
128
Yingdi Yufe4733a2015-10-22 14:24:12 -0700129 KeyChain keyChain;
130 BOOST_CHECK_EQUAL(keyChain.getPib().getPibLocator(), "pib-sqlite3:");
131 BOOST_CHECK_EQUAL(keyChain.getPib().getTpmLocator(), "tpm-memory:");
132 BOOST_CHECK_EQUAL(keyChain.getTpm().getTpmLocator(), "tpm-memory:");
133}
134
135struct PibPathConfigFileMalformedHome
136{
Davide Pesavento3fdb02f2023-04-12 02:32:38 -0400137 static constexpr std::string_view PATH = "build/config-file-malformed-home/";
Yingdi Yufe4733a2015-10-22 14:24:12 -0700138};
139
Davide Pesavento73710362020-12-04 16:10:03 -0500140BOOST_FIXTURE_TEST_CASE(ConstructorBadConfig, TestHomeAndPibFixture<PibPathConfigFileMalformedHome>)
Yingdi Yufe4733a2015-10-22 14:24:12 -0700141{
142 createClientConf({"pib=lord", "tpm=ring"});
Davide Pesavento73710362020-12-04 16:10:03 -0500143 BOOST_CHECK_THROW(KeyChain(), KeyChain::Error); // Wrong configuration. Error expected.
Yingdi Yufe4733a2015-10-22 14:24:12 -0700144}
145
146struct PibPathConfigFileMalformed2Home
147{
Davide Pesavento3fdb02f2023-04-12 02:32:38 -0400148 static constexpr std::string_view PATH = "build/config-file-malformed2-home/";
Yingdi Yufe4733a2015-10-22 14:24:12 -0700149};
150
Davide Pesavento73710362020-12-04 16:10:03 -0500151BOOST_FIXTURE_TEST_CASE(ConstructorBadConfig2, TestHomeAndPibFixture<PibPathConfigFileMalformed2Home>)
Yingdi Yufe4733a2015-10-22 14:24:12 -0700152{
153 createClientConf({"pib=pib-sqlite3:%PATH%", "tpm=just-wrong"});
Davide Pesavento73710362020-12-04 16:10:03 -0500154 BOOST_CHECK_THROW(KeyChain(), KeyChain::Error); // Wrong configuration. Error expected.
Yingdi Yufe4733a2015-10-22 14:24:12 -0700155}
156
Alexander Afanasyev57d02b62018-06-15 18:19:50 -0400157struct PibPathConfigFileNonCanonicalTpm
158{
Davide Pesavento3fdb02f2023-04-12 02:32:38 -0400159 static constexpr std::string_view PATH = "build/config-file-non-canonical-tpm/";
Alexander Afanasyev57d02b62018-06-15 18:19:50 -0400160};
161
162BOOST_FIXTURE_TEST_CASE(ConstructorNonCanonicalTpm, TestHomeAndPibFixture<PibPathConfigFileNonCanonicalTpm>) // Bug 4297
163{
164 createClientConf({"pib=pib-sqlite3:", "tpm=tpm-file"});
165
166 {
167 KeyChain keyChain;
168 keyChain.createIdentity("/test");
169 BOOST_CHECK_EQUAL(keyChain.getPib().getPibLocator(), "pib-sqlite3:");
170 BOOST_CHECK_EQUAL(keyChain.getTpm().getTpmLocator(), "tpm-file:");
171 }
172
173 {
174 KeyChain keyChain;
175 BOOST_CHECK_EQUAL(keyChain.getPib().getPibLocator(), "pib-sqlite3:");
176 BOOST_CHECK_EQUAL(keyChain.getTpm().getTpmLocator(), "tpm-file:");
177 BOOST_CHECK(keyChain.getPib().getIdentities().find("/test") != keyChain.getPib().getIdentities().end());
178 }
179}
180
Yingdi Yufe4733a2015-10-22 14:24:12 -0700181BOOST_AUTO_TEST_CASE(KeyChainWithCustomTpmAndPib)
182{
Yingdi Yufe4733a2015-10-22 14:24:12 -0700183 KeyChain keyChain("pib-memory", "tpm-memory");
184 BOOST_CHECK_EQUAL(keyChain.getPib().getPibLocator(), "pib-memory:");
185 BOOST_CHECK_EQUAL(keyChain.getPib().getTpmLocator(), "tpm-memory:");
186 BOOST_CHECK_EQUAL(keyChain.getTpm().getTpmLocator(), "tpm-memory:");
Davide Pesavento0e768ef2022-05-09 20:03:44 -0400187
188 BOOST_CHECK_NO_THROW(KeyChain("pib-memory:", "tpm-memory:"));
189 BOOST_CHECK_NO_THROW(KeyChain("pib-memory:/something", "tpm-memory:/something"));
Yingdi Yufe4733a2015-10-22 14:24:12 -0700190}
191
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500192BOOST_FIXTURE_TEST_CASE(SigningWithCorruptedPibTpm, KeyChainFixture)
Alexander Afanasyevf601e192020-06-02 16:41:07 -0400193{
194 Identity id = m_keyChain.createIdentity("/test");
195
196 Data data("/foobar");
197 BOOST_CHECK_NO_THROW(m_keyChain.sign(data, signingByIdentity(id)));
198
199 // now, "corrupting TPM"
200 const_cast<Tpm&>(m_keyChain.getTpm()).deleteKey(id.getDefaultKey().getName());
201
202 BOOST_CHECK_NO_THROW(id.getDefaultKey());
203 BOOST_CHECK_THROW(m_keyChain.sign(data, signingByIdentity(id)), KeyChain::InvalidSigningInfoError);
204}
205
Davide Pesavento73710362020-12-04 16:10:03 -0500206BOOST_FIXTURE_TEST_CASE(SigningWithNonExistingIdentity, KeyChainFixture)
207{
208 Data data("/test/data");
209 BOOST_CHECK_THROW(m_keyChain.sign(data, signingByIdentity("/non-existing/identity")),
210 KeyChain::InvalidSigningInfoError);
211}
212
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500213BOOST_FIXTURE_TEST_CASE(Management, KeyChainFixture)
Yingdi Yufe4733a2015-10-22 14:24:12 -0700214{
Yingdi Yufe4733a2015-10-22 14:24:12 -0700215 BOOST_CHECK_EQUAL(m_keyChain.getPib().getIdentities().size(), 0);
Davide Pesaventoe30389c2022-05-31 17:23:33 -0400216 BOOST_CHECK_THROW(m_keyChain.getPib().getDefaultIdentity(), Pib::Error);
Yingdi Yufe4733a2015-10-22 14:24:12 -0700217
Davide Pesaventoe30389c2022-05-31 17:23:33 -0400218 // Create an identity
219 Name idName("/test/id");
220 Identity id = m_keyChain.createIdentity(idName);
221 BOOST_REQUIRE(id);
222 BOOST_CHECK_EQUAL(m_keyChain.getPib().getIdentities().size(), 1);
223 BOOST_CHECK_EQUAL(m_keyChain.getPib().getIdentity(idName), id);
224
Yingdi Yufe4733a2015-10-22 14:24:12 -0700225 // The first added identity becomes the default identity
Davide Pesaventoe30389c2022-05-31 17:23:33 -0400226 BOOST_CHECK_EQUAL(m_keyChain.getPib().getDefaultIdentity(), id);
Yingdi Yufe4733a2015-10-22 14:24:12 -0700227 // The default key of the added identity must exist
laqinfan56a812d2019-06-03 15:33:58 -0500228 Key key = id.getDefaultKey();
Davide Pesaventoe30389c2022-05-31 17:23:33 -0400229 BOOST_REQUIRE(key);
230 BOOST_CHECK_EQUAL(id.getKeys().size(), 1);
Yingdi Yufe4733a2015-10-22 14:24:12 -0700231 // The default certificate of the default key must exist
laqinfan56a812d2019-06-03 15:33:58 -0500232 BOOST_CHECK_NO_THROW(key.getDefaultCertificate());
Davide Pesaventoe30389c2022-05-31 17:23:33 -0400233 BOOST_CHECK_EQUAL(key.getCertificates().size(), 1);
Yingdi Yufe4733a2015-10-22 14:24:12 -0700234
235 // Delete key
236 Name key1Name = key.getName();
Yingdi Yufe4733a2015-10-22 14:24:12 -0700237 m_keyChain.deleteKey(id, key);
238 // The key instance should not be valid any more
239 BOOST_CHECK(!key);
Davide Pesaventoe30389c2022-05-31 17:23:33 -0400240 BOOST_CHECK_THROW(id.getDefaultKey(), Pib::Error);
Yingdi Yufe4733a2015-10-22 14:24:12 -0700241 BOOST_CHECK_THROW(id.getKey(key1Name), Pib::Error);
242 BOOST_CHECK_EQUAL(id.getKeys().size(), 0);
243
244 // Create another key
245 m_keyChain.createKey(id);
Davide Pesaventoe30389c2022-05-31 17:23:33 -0400246 // The added key becomes the default key
Yingdi Yufe4733a2015-10-22 14:24:12 -0700247 Key key2 = id.getDefaultKey();
248 BOOST_REQUIRE(key2);
249 BOOST_CHECK_NE(key2.getName(), key1Name);
250 BOOST_CHECK_EQUAL(id.getKeys().size(), 1);
laqinfan56a812d2019-06-03 15:33:58 -0500251 BOOST_CHECK_NO_THROW(key2.getDefaultCertificate());
Davide Pesaventoe30389c2022-05-31 17:23:33 -0400252 BOOST_CHECK_EQUAL(key2.getCertificates().size(), 1);
Yingdi Yufe4733a2015-10-22 14:24:12 -0700253
Davide Pesaventoe30389c2022-05-31 17:23:33 -0400254 // Create a third key
Yingdi Yufe4733a2015-10-22 14:24:12 -0700255 Key key3 = m_keyChain.createKey(id);
Junxiao Shi72c0c642018-04-20 15:41:09 +0000256 BOOST_CHECK_NE(key3.getName(), key2.getName());
Yingdi Yufe4733a2015-10-22 14:24:12 -0700257 BOOST_CHECK_EQUAL(id.getKeys().size(), 2);
Davide Pesaventoe30389c2022-05-31 17:23:33 -0400258 // The added key will not be the default key, because the default was already set
259 BOOST_CHECK_EQUAL(id.getDefaultKey().getName(), key2.getName());
Yingdi Yufe4733a2015-10-22 14:24:12 -0700260
261 // Delete cert
Davide Pesaventoe30389c2022-05-31 17:23:33 -0400262 BOOST_REQUIRE_EQUAL(key3.getCertificates().size(), 1);
Yingdi Yufe4733a2015-10-22 14:24:12 -0700263 Certificate key3Cert1 = *key3.getCertificates().begin();
264 Name key3CertName = key3Cert1.getName();
265 m_keyChain.deleteCertificate(key3, key3CertName);
266 BOOST_CHECK_EQUAL(key3.getCertificates().size(), 0);
Davide Pesaventoe30389c2022-05-31 17:23:33 -0400267 BOOST_CHECK_THROW(key3.getDefaultCertificate(), Pib::Error);
Yingdi Yufe4733a2015-10-22 14:24:12 -0700268
269 // Add cert
270 m_keyChain.addCertificate(key3, key3Cert1);
271 BOOST_CHECK_EQUAL(key3.getCertificates().size(), 1);
laqinfan56a812d2019-06-03 15:33:58 -0500272 BOOST_CHECK_NO_THROW(key3.getDefaultCertificate());
Alexander Afanasyeva10b2ff2017-01-30 12:44:15 -0800273 m_keyChain.addCertificate(key3, key3Cert1); // overwriting the cert should work
Yingdi Yufe4733a2015-10-22 14:24:12 -0700274 BOOST_CHECK_EQUAL(key3.getCertificates().size(), 1);
Davide Pesaventoe30389c2022-05-31 17:23:33 -0400275
Yingdi Yufe4733a2015-10-22 14:24:12 -0700276 // Add another cert
277 Certificate key3Cert2 = key3Cert1;
278 Name key3Cert2Name = key3.getName();
Davide Pesaventoe30389c2022-05-31 17:23:33 -0400279 key3Cert2Name.append("Self").appendVersion();
Yingdi Yufe4733a2015-10-22 14:24:12 -0700280 key3Cert2.setName(key3Cert2Name);
281 m_keyChain.addCertificate(key3, key3Cert2);
282 BOOST_CHECK_EQUAL(key3.getCertificates().size(), 2);
Davide Pesaventoe30389c2022-05-31 17:23:33 -0400283
Davide Pesavento81bd6962020-06-17 16:03:23 -0400284 // Add empty cert
285 Certificate key3Cert3 = key3Cert1;
286 key3Cert3.unsetContent();
287 BOOST_CHECK_THROW(m_keyChain.addCertificate(key3, key3Cert3), std::invalid_argument);
Yingdi Yufe4733a2015-10-22 14:24:12 -0700288
Davide Pesaventoe30389c2022-05-31 17:23:33 -0400289 // Create another identity
290 Name id2Name("/test/id2");
291 Identity id2 = m_keyChain.createIdentity(id2Name);
292 BOOST_REQUIRE(id2);
293 BOOST_CHECK_EQUAL(m_keyChain.getPib().getIdentities().size(), 2);
294 BOOST_CHECK_EQUAL(m_keyChain.getPib().getIdentity(id2Name), id2);
295
296 // Default identity setting
297 BOOST_CHECK_EQUAL(m_keyChain.getPib().getDefaultIdentity().getName(), id.getName());
298 m_keyChain.setDefaultIdentity(id2);
299 BOOST_CHECK_EQUAL(m_keyChain.getPib().getDefaultIdentity().getName(), id2.getName());
Yingdi Yufe4733a2015-10-22 14:24:12 -0700300
301 // Default key setting
302 BOOST_CHECK_EQUAL(id.getDefaultKey().getName(), key2.getName());
303 m_keyChain.setDefaultKey(id, key3);
304 BOOST_CHECK_EQUAL(id.getDefaultKey().getName(), key3.getName());
Davide Pesaventoe30389c2022-05-31 17:23:33 -0400305 // Set key of a different identity as default
306 BOOST_CHECK_THROW(m_keyChain.setDefaultKey(id, id2.getDefaultKey()), std::invalid_argument);
Yingdi Yufe4733a2015-10-22 14:24:12 -0700307
Davide Pesaventoe30389c2022-05-31 17:23:33 -0400308 // Default certificate setting
309 BOOST_CHECK_EQUAL(key3.getDefaultCertificate().getName(), key3CertName);
310 m_keyChain.setDefaultCertificate(key3, key3Cert2);
311 BOOST_CHECK_EQUAL(key3.getDefaultCertificate().getName(), key3Cert2Name);
312 // Set certificate of a different key as default
313 BOOST_CHECK_THROW(m_keyChain.setDefaultCertificate(key3, key2.getDefaultCertificate()),
314 std::invalid_argument);
315
316 // Delete certificate name mismatch
317 BOOST_CHECK_THROW(m_keyChain.deleteCertificate(key2, key3CertName), std::invalid_argument);
318 // Delete key name mismatch
319 BOOST_CHECK_THROW(m_keyChain.deleteKey(id, id2.getDefaultKey()), std::invalid_argument);
Yingdi Yufe4733a2015-10-22 14:24:12 -0700320
321 // Delete identity
322 m_keyChain.deleteIdentity(id);
323 // The identity instance should not be valid any more
324 BOOST_CHECK(!id);
Davide Pesaventoe30389c2022-05-31 17:23:33 -0400325 BOOST_CHECK_THROW(m_keyChain.getPib().getIdentity(idName), Pib::Error);
326 BOOST_CHECK_EQUAL(m_keyChain.getPib().getIdentities().size(), 1);
Yingdi Yufe4733a2015-10-22 14:24:12 -0700327}
328
Davide Pesavento73710362020-12-04 16:10:03 -0500329struct DataPkt
Yingdi Yufe4733a2015-10-22 14:24:12 -0700330{
Davide Pesavento73710362020-12-04 16:10:03 -0500331 Data packet{"/data"};
332 SignedInterestFormat sigFormat = SignedInterestFormat::V02; // irrelevant for Data
Yingdi Yufe4733a2015-10-22 14:24:12 -0700333
Davide Pesavento73710362020-12-04 16:10:03 -0500334 SignatureInfo
335 getSignatureInfo() const
336 {
337 return packet.getSignatureInfo();
338 }
339};
laqinfan56a812d2019-06-03 15:33:58 -0500340
Davide Pesavento73710362020-12-04 16:10:03 -0500341struct InterestV02Pkt
342{
Davide Pesaventoaee2ada2022-02-18 14:43:02 -0500343 Interest packet{"/interest02"};
Davide Pesavento73710362020-12-04 16:10:03 -0500344 SignedInterestFormat sigFormat = SignedInterestFormat::V02;
Eric Newberryb74bbda2020-06-18 19:33:58 -0700345
Davide Pesavento73710362020-12-04 16:10:03 -0500346 SignatureInfo
347 getSignatureInfo() const
348 {
349 return SignatureInfo(packet.getName()[signed_interest::POS_SIG_INFO].blockFromValue());
350 }
351};
Eric Newberryb74bbda2020-06-18 19:33:58 -0700352
Davide Pesavento73710362020-12-04 16:10:03 -0500353struct InterestV03Pkt
354{
Davide Pesaventoaee2ada2022-02-18 14:43:02 -0500355 Interest packet{"/interest03"};
Davide Pesavento73710362020-12-04 16:10:03 -0500356 SignedInterestFormat sigFormat = SignedInterestFormat::V03;
Eric Newberryb74bbda2020-06-18 19:33:58 -0700357
Davide Pesavento73710362020-12-04 16:10:03 -0500358 SignatureInfo
359 getSignatureInfo() const
360 {
361 return packet.getSignatureInfo().value(); // use .value() for checked access
362 }
363};
Eric Newberryb74bbda2020-06-18 19:33:58 -0700364
Davide Pesavento73710362020-12-04 16:10:03 -0500365template<typename KeyParams>
366struct DefaultIdentity
367{
368 Identity
369 operator()(KeyChain& keyChain) const
370 {
371 auto id = keyChain.createIdentity("/id", KeyParams());
372 BOOST_ASSERT(keyChain.getPib().getDefaultIdentity() == id);
373 return id;
374 }
375};
Eric Newberryb74bbda2020-06-18 19:33:58 -0700376
Davide Pesavento73710362020-12-04 16:10:03 -0500377template<typename KeyParams>
378struct NonDefaultIdentity
379{
380 Identity
381 operator()(KeyChain& keyChain) const
382 {
383 auto id = keyChain.createIdentity("/id");
384 auto id2 = keyChain.createIdentity("/id2", KeyParams());
385 BOOST_ASSERT(keyChain.getPib().getDefaultIdentity() == id);
386 return id2;
387 }
388};
Eric Newberryb74bbda2020-06-18 19:33:58 -0700389
Davide Pesavento73710362020-12-04 16:10:03 -0500390template<typename KeyParams>
391struct DefaultKey
392{
393 Key
394 operator()(KeyChain&, const Identity& id) const
395 {
396 auto key = id.getDefaultKey();
397 BOOST_ASSERT(key.getKeyType() == KeyParams().getKeyType());
398 return key;
399 }
400};
Eric Newberryb74bbda2020-06-18 19:33:58 -0700401
Davide Pesavento73710362020-12-04 16:10:03 -0500402template<typename KeyParams>
403struct NonDefaultKey
404{
405 Key
406 operator()(KeyChain& keyChain, const Identity& id) const
407 {
408 auto key2 = keyChain.createKey(id, KeyParams());
409 BOOST_ASSERT(id.getDefaultKey() != key2);
410 return key2;
411 }
412};
Eric Newberryb74bbda2020-06-18 19:33:58 -0700413
Davide Pesavento73710362020-12-04 16:10:03 -0500414template<typename PacketType,
415 template<typename> class IdentityMaker = DefaultIdentity,
416 template<typename> class KeyMaker = DefaultKey,
417 typename AsymmetricKeyParams = EcKeyParams,
418 uint32_t SignatureTypeTlvValue = tlv::SignatureSha256WithEcdsa>
419struct AsymmetricSigningBase : protected KeyChainFixture, protected PacketType
420{
421 const Identity id = IdentityMaker<AsymmetricKeyParams>()(m_keyChain);
422 const Key key = KeyMaker<AsymmetricKeyParams>()(m_keyChain, id);
423 const Certificate cert = key.getDefaultCertificate();
424
425 const uint32_t expectedSigType = SignatureTypeTlvValue;
426 const bool shouldHaveKeyLocator = true;
Davide Pesaventof6b45892023-03-13 15:00:51 -0400427 const std::optional<KeyLocator> expectedKeyLocator = cert.getName();
Davide Pesavento73710362020-12-04 16:10:03 -0500428
429 bool
430 verify(const SigningInfo&) const
431 {
432 return verifySignature(this->packet, key);
433 }
434};
435
436template<typename PacketType,
437 typename AsymmetricKeyParams,
438 uint32_t SignatureTypeTlvValue>
439struct AsymmetricSigning : protected AsymmetricSigningBase<PacketType, DefaultIdentity, DefaultKey,
440 AsymmetricKeyParams, SignatureTypeTlvValue>
441{
442 const std::vector<SigningInfo> signingInfos = {
Yingdi Yufe4733a2015-10-22 14:24:12 -0700443 SigningInfo(),
Davide Pesavento73710362020-12-04 16:10:03 -0500444 SigningInfo(""),
Yingdi Yufe4733a2015-10-22 14:24:12 -0700445
Davide Pesavento73710362020-12-04 16:10:03 -0500446 SigningInfo(this->id),
447 SigningInfo(SigningInfo::SIGNER_TYPE_ID, this->id.getName()),
448 SigningInfo("id:" + this->id.getName().toUri()),
449 signingByIdentity(this->id),
450 signingByIdentity(this->id.getName()),
Yingdi Yufe4733a2015-10-22 14:24:12 -0700451
Davide Pesavento73710362020-12-04 16:10:03 -0500452 SigningInfo(this->key),
453 SigningInfo(SigningInfo::SIGNER_TYPE_KEY, this->key.getName()),
454 SigningInfo("key:" + this->key.getName().toUri()),
455 signingByKey(this->key),
456 signingByKey(this->key.getName()),
Alexander Afanasyevd6d78aa2017-01-02 18:14:23 -0800457
Davide Pesavento73710362020-12-04 16:10:03 -0500458 SigningInfo(SigningInfo::SIGNER_TYPE_CERT, this->cert.getName()),
459 SigningInfo("cert:" + this->cert.getName().toUri()),
460 signingByCertificate(this->cert),
461 signingByCertificate(this->cert.getName()),
462 };
463};
Yingdi Yufe4733a2015-10-22 14:24:12 -0700464
Davide Pesavento73710362020-12-04 16:10:03 -0500465template<typename PacketType>
466using RsaSigning = AsymmetricSigning<PacketType, RsaKeyParams, tlv::SignatureSha256WithRsa>;
Alexander Afanasyevd6d78aa2017-01-02 18:14:23 -0800467
Davide Pesavento73710362020-12-04 16:10:03 -0500468template<typename PacketType>
469using EcdsaSigning = AsymmetricSigning<PacketType, EcKeyParams, tlv::SignatureSha256WithEcdsa>;
Yingdi Yufe4733a2015-10-22 14:24:12 -0700470
Davide Pesavento73710362020-12-04 16:10:03 -0500471template<typename PacketType>
472struct SigningWithNonDefaultIdentity : protected AsymmetricSigningBase<PacketType, NonDefaultIdentity>
473{
474 const std::vector<SigningInfo> signingInfos = {
475 signingByIdentity(this->id),
476 signingByIdentity(this->id.getName()),
477 signingByKey(this->key),
478 signingByCertificate(this->cert),
479 };
480};
481
482template<typename PacketType>
483struct SigningWithNonDefaultKey : protected AsymmetricSigningBase<PacketType, NonDefaultIdentity, NonDefaultKey>
484{
485 const std::vector<SigningInfo> signingInfos = {
486 signingByKey(this->key),
487 signingByKey(this->key.getName()),
488 signingByCertificate(this->cert),
489 };
490};
491
492template<typename PacketType,
493 DigestAlgorithm DigestAlgo = DigestAlgorithm::SHA256,
494 uint32_t SignatureTypeTlvValue = tlv::SignatureHmacWithSha256>
495struct HmacSigning : protected KeyChainFixture, protected PacketType
496{
497 const std::vector<SigningInfo> signingInfos = {
498 SigningInfo(SigningInfo::SIGNER_TYPE_HMAC, m_keyChain.createHmacKey()),
laqinfan56a812d2019-06-03 15:33:58 -0500499 SigningInfo("hmac-sha256:QjM3NEEyNkE3MTQ5MDQzN0FBMDI0RTRGQURENUI0OTdGREZGMUE4RUE2RkYxMkY2RkI2NUFGMjcyMEI1OUNDRg=="),
Davide Pesavento73710362020-12-04 16:10:03 -0500500 };
laqinfan56a812d2019-06-03 15:33:58 -0500501
Davide Pesavento73710362020-12-04 16:10:03 -0500502 const uint32_t expectedSigType = SignatureTypeTlvValue;
503 const bool shouldHaveKeyLocator = true;
Davide Pesaventof6b45892023-03-13 15:00:51 -0400504 const std::optional<KeyLocator> expectedKeyLocator = std::nullopt; // don't check KeyLocator value
Davide Pesavento73710362020-12-04 16:10:03 -0500505
506 bool
507 verify(const SigningInfo& si) const
508 {
509 return verifySignature(this->packet, m_keyChain.getTpm(), si.getSignerName(), DigestAlgo);
510 }
511};
512
513template<typename PacketType>
514struct Sha256Signing : protected KeyChainFixture, protected PacketType
515{
516 const std::vector<SigningInfo> signingInfos = {
Yingdi Yufe4733a2015-10-22 14:24:12 -0700517 SigningInfo(SigningInfo::SIGNER_TYPE_SHA256),
Davide Pesavento73710362020-12-04 16:10:03 -0500518 SigningInfo("id:" + SigningInfo::getDigestSha256Identity().toUri()),
Yingdi Yufe4733a2015-10-22 14:24:12 -0700519 signingWithSha256()
520 };
521
Davide Pesavento73710362020-12-04 16:10:03 -0500522 const uint32_t expectedSigType = tlv::DigestSha256;
523 const bool shouldHaveKeyLocator = false;
Davide Pesaventof6b45892023-03-13 15:00:51 -0400524 const std::optional<KeyLocator> expectedKeyLocator = std::nullopt;
Yingdi Yufe4733a2015-10-22 14:24:12 -0700525
Davide Pesavento73710362020-12-04 16:10:03 -0500526 bool
527 verify(const SigningInfo&) const
528 {
Davide Pesaventof6b45892023-03-13 15:00:51 -0400529 return verifySignature(this->packet, std::nullopt);
Davide Pesavento73710362020-12-04 16:10:03 -0500530 }
531};
Yingdi Yufe4733a2015-10-22 14:24:12 -0700532
Davide Pesavento73710362020-12-04 16:10:03 -0500533using SigningTests = boost::mpl::vector<
534 RsaSigning<DataPkt>,
535 RsaSigning<InterestV02Pkt>,
536 RsaSigning<InterestV03Pkt>,
537 EcdsaSigning<DataPkt>,
538 EcdsaSigning<InterestV02Pkt>,
539 EcdsaSigning<InterestV03Pkt>,
540 HmacSigning<DataPkt>,
541 HmacSigning<InterestV02Pkt>,
542 HmacSigning<InterestV03Pkt>,
543 Sha256Signing<DataPkt>,
544 Sha256Signing<InterestV02Pkt>,
545 Sha256Signing<InterestV03Pkt>,
546 SigningWithNonDefaultIdentity<DataPkt>,
Davide Pesavento809f7542021-03-24 18:53:05 -0400547 SigningWithNonDefaultIdentity<InterestV03Pkt>,
548 SigningWithNonDefaultKey<DataPkt>,
549 SigningWithNonDefaultKey<InterestV03Pkt>
Davide Pesavento73710362020-12-04 16:10:03 -0500550>;
Eric Newberryb74bbda2020-06-18 19:33:58 -0700551
Davide Pesavento73710362020-12-04 16:10:03 -0500552BOOST_FIXTURE_TEST_CASE_TEMPLATE(SigningInterface, T, SigningTests, T)
553{
554 BOOST_TEST_CONTEXT("Packet = " << this->packet.getName()) {
555 for (auto signingInfo : this->signingInfos) {
556 signingInfo.setSignedInterestFormat(this->sigFormat);
557
558 BOOST_TEST_CONTEXT("SigningInfo = " << signingInfo) {
559 this->m_keyChain.sign(this->packet, signingInfo);
560
561 auto sigInfo = this->getSignatureInfo();
562 BOOST_CHECK_EQUAL(sigInfo.getSignatureType(), this->expectedSigType);
563 BOOST_CHECK_EQUAL(sigInfo.hasKeyLocator(), this->shouldHaveKeyLocator);
564 if (this->expectedKeyLocator) {
565 BOOST_CHECK_EQUAL(sigInfo.getKeyLocator(), *this->expectedKeyLocator);
566 }
567 BOOST_CHECK(this->verify(signingInfo));
Eric Newberryb74bbda2020-06-18 19:33:58 -0700568 }
Yingdi Yufe4733a2015-10-22 14:24:12 -0700569 }
570 }
571}
572
Junxiao Shi9ee770b2022-04-25 23:33:33 +0000573class MakeCertificateFixture : public ClockFixture
574{
575public:
576 MakeCertificateFixture()
577 : requesterKeyChain("pib-memory:", "tpm-memory:")
578 , signerKeyChain("pib-memory:", "tpm-memory:")
579 {
580 m_systemClock->setNow(time::fromIsoString("20091117T203458,651387237").time_since_epoch());
581
582 requester = requesterKeyChain.createIdentity("/requester").getDefaultKey();
583 Name signerIdentityName("/signer");
584 signerKey = signerKeyChain.createIdentity(signerIdentityName).getDefaultKey();
585 signerParams = signingByIdentity(signerIdentityName);
586 }
587
588 void
Davide Pesaventof6b45892023-03-13 15:00:51 -0400589 checkKeyLocatorName(const Certificate& cert,
590 const std::optional<Name>& klName = std::nullopt) const
Junxiao Shi9ee770b2022-04-25 23:33:33 +0000591 {
592 auto kl = cert.getKeyLocator();
593 if (!kl.has_value()) {
594 BOOST_ERROR("KeyLocator is missing");
595 return;
596 }
Davide Pesavento07db0732022-05-06 15:20:26 -0400597 BOOST_CHECK_EQUAL(kl->getName(), klName.value_or(signerKey.getDefaultCertificate().getName()));
Junxiao Shi9ee770b2022-04-25 23:33:33 +0000598 }
599
600 void
601 checkCertFromDefaults(const Certificate& cert) const
602 {
603 BOOST_CHECK(Certificate::isValidName(cert.getName()));
604 BOOST_CHECK_EQUAL(cert.getKeyName(), requester.getName());
605 BOOST_CHECK_EQUAL(cert.getName()[-2], name::Component("NA"));
606 BOOST_CHECK(cert.getName()[-1].isVersion());
607
608 BOOST_CHECK_EQUAL(cert.getContentType(), tlv::ContentType_Key);
609 BOOST_CHECK_EQUAL(cert.getFreshnessPeriod(), 1_h);
610
Davide Pesavento8e2a61d2022-05-13 18:44:03 -0400611 BOOST_TEST(cert.getPublicKey() == requester.getPublicKey(), boost::test_tools::per_element());
Junxiao Shi9ee770b2022-04-25 23:33:33 +0000612
613 checkKeyLocatorName(cert);
614
615 BOOST_CHECK(cert.isValid());
616 auto vp = cert.getValidityPeriod().getPeriod();
617 BOOST_CHECK_EQUAL(vp.first, time::fromIsoString("20091117T203458"));
618 BOOST_CHECK_EQUAL(vp.second, time::fromIsoString("20101117T203458"));
619
620 auto adBlock = cert.getSignatureInfo().getCustomTlv(tlv::AdditionalDescription);
621 BOOST_CHECK(!adBlock.has_value());
622 }
623
624public:
625 KeyChain requesterKeyChain;
626 pib::Key requester;
627
628 KeyChain signerKeyChain;
629 pib::Key signerKey;
630 Name signerCertificateName;
631 SigningInfo signerParams;
632};
633
634BOOST_FIXTURE_TEST_SUITE(MakeCertificate, MakeCertificateFixture)
635
636BOOST_AUTO_TEST_CASE(DefaultsFromKey)
637{
638 auto cert = signerKeyChain.makeCertificate(requester, signerParams);
639 checkCertFromDefaults(cert);
640}
641
642BOOST_AUTO_TEST_CASE(DefaultsFromCert)
643{
644 auto cert = signerKeyChain.makeCertificate(requester.getDefaultCertificate(), signerParams);
645 checkCertFromDefaults(cert);
646}
647
648BOOST_AUTO_TEST_CASE(Options)
649{
650 MakeCertificateOptions opts;
651 opts.issuerId = name::Component::fromEscapedString("ISSUER");
652 opts.version = 41218268;
653 opts.freshnessPeriod = 321_s;
654 opts.validity.emplace(time::fromIsoString("20060702T150405"),
655 time::fromIsoString("20160702T150405"));
656
657 SignatureInfo sigInfo;
658 sigInfo.setKeyLocator(signerKey.getName());
659 sigInfo.setValidityPeriod(ValidityPeriod(time::fromIsoString("20060102T150405"),
660 time::fromIsoString("20160102T150405")));
661 sigInfo.addCustomTlv(Block(0xF0));
662 signerParams.setSignatureInfo(sigInfo);
663
664 auto cert = signerKeyChain.makeCertificate(requester, signerParams, opts);
665
666 BOOST_CHECK_EQUAL(cert.getName(),
667 Name(requester.getName()).append(PartialName("ISSUER/v=41218268")));
668 BOOST_CHECK_EQUAL(cert.getFreshnessPeriod(), 321_s);
669 checkKeyLocatorName(cert, signerKey.getName());
670
671 auto vp = cert.getValidityPeriod().getPeriod();
672 BOOST_CHECK_EQUAL(vp.first, time::fromIsoString("20060702T150405"));
673 BOOST_CHECK_EQUAL(vp.second, time::fromIsoString("20160702T150405"));
674
675 BOOST_CHECK(cert.getSignatureInfo().getCustomTlv(0xF0).has_value());
676}
677
678BOOST_AUTO_TEST_CASE(ErrSigner)
679{
680 signerParams = signingByIdentity("/nonexistent");
681 BOOST_CHECK_THROW(signerKeyChain.makeCertificate(requester, signerParams), KeyChain::Error);
682}
683
684BOOST_AUTO_TEST_CASE(ErrZeroFreshness)
685{
686 MakeCertificateOptions opts;
687 opts.freshnessPeriod = 0_ms;
688 BOOST_CHECK_THROW(signerKeyChain.makeCertificate(requester, signerParams, opts),
689 std::invalid_argument);
690}
691
692BOOST_AUTO_TEST_CASE(ErrNegativeFreshness)
693{
694 MakeCertificateOptions opts;
695 opts.freshnessPeriod = -1_ms;
696 BOOST_CHECK_THROW(signerKeyChain.makeCertificate(requester, signerParams, opts),
697 std::invalid_argument);
698}
699
700BOOST_AUTO_TEST_CASE(ErrContent)
701{
702 Certificate request(requester.getDefaultCertificate());
Davide Pesavento07db0732022-05-06 15:20:26 -0400703
704 // malformed public key
Junxiao Shi9ee770b2022-04-25 23:33:33 +0000705 const auto& oldContent = request.getContent();
706 std::vector<uint8_t> content(oldContent.value_begin(), oldContent.value_end());
707 content[0] ^= 0x80;
708 request.setContent(content);
709 BOOST_CHECK_THROW(signerKeyChain.makeCertificate(request, signerParams), std::invalid_argument);
Davide Pesavento07db0732022-05-06 15:20:26 -0400710
711 // empty content
Davide Pesavento296c3a12023-05-04 21:40:40 -0400712 request.setContent("");
Davide Pesavento07db0732022-05-06 15:20:26 -0400713 BOOST_CHECK_THROW(signerKeyChain.makeCertificate(request, signerParams), std::invalid_argument);
Junxiao Shi9ee770b2022-04-25 23:33:33 +0000714}
715
716BOOST_AUTO_TEST_SUITE_END() // MakeCertificate
717
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500718BOOST_FIXTURE_TEST_CASE(ImportPrivateKey, KeyChainFixture)
laqinfan56a812d2019-06-03 15:33:58 -0500719{
Davide Pesavento765abc92021-12-27 00:44:04 -0500720 const Name keyName("/test/device2");
721 const uint8_t rawKey[] = "nPSNOHyZKsg2WLqHAs7MXGb0sjQb4zCT";
laqinfan56a812d2019-06-03 15:33:58 -0500722 auto key = make_shared<transform::PrivateKey>();
Davide Pesavento765abc92021-12-27 00:44:04 -0500723 key->loadRaw(KeyType::HMAC, rawKey);
laqinfan56a812d2019-06-03 15:33:58 -0500724
725 m_keyChain.importPrivateKey(keyName, key);
726 BOOST_CHECK_EQUAL(m_keyChain.getTpm().hasKey(keyName), true);
727 BOOST_CHECK_THROW(m_keyChain.importPrivateKey(keyName, key), KeyChain::Error);
728}
729
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500730BOOST_FIXTURE_TEST_CASE(ExportImport, KeyChainFixture)
Yingdi Yufe4733a2015-10-22 14:24:12 -0700731{
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500732 Identity id = m_keyChain.createIdentity("/TestKeyChain/ExportIdentity");
Yingdi Yufe4733a2015-10-22 14:24:12 -0700733 Certificate cert = id.getDefaultKey().getDefaultCertificate();
734
735 shared_ptr<SafeBag> exported = m_keyChain.exportSafeBag(cert, "1234", 4);
Davide Pesaventoe30389c2022-05-31 17:23:33 -0400736 const auto& block = exported->wireEncode();
Yingdi Yufe4733a2015-10-22 14:24:12 -0700737
738 m_keyChain.deleteIdentity(id);
Davide Pesaventoe30389c2022-05-31 17:23:33 -0400739 BOOST_CHECK_EXCEPTION(m_keyChain.exportSafeBag(cert, "1234", 4), KeyChain::Error, [] (const auto& e) {
740 return std::string(e.what()).find("Failed to export private key") == 0;
741 });
Yingdi Yufe4733a2015-10-22 14:24:12 -0700742
743 BOOST_CHECK_EQUAL(m_keyChain.getTpm().hasKey(cert.getKeyName()), false);
744 BOOST_CHECK_EQUAL(m_keyChain.getPib().getIdentities().size(), 0);
745
Davide Pesaventoe30389c2022-05-31 17:23:33 -0400746 SafeBag imported(block);
Yingdi Yufe4733a2015-10-22 14:24:12 -0700747 m_keyChain.importSafeBag(imported, "1234", 4);
Davide Pesaventoe30389c2022-05-31 17:23:33 -0400748 BOOST_CHECK_EXCEPTION(m_keyChain.importSafeBag(imported, "1234", 4), KeyChain::Error, [] (const auto& e) {
749 return std::string(e.what()).find("already exists") != std::string::npos;
750 });
Yingdi Yufe4733a2015-10-22 14:24:12 -0700751
752 BOOST_CHECK_EQUAL(m_keyChain.getTpm().hasKey(cert.getKeyName()), true);
753 BOOST_CHECK_EQUAL(m_keyChain.getPib().getIdentities().size(), 1);
Yingdi Yufe4733a2015-10-22 14:24:12 -0700754 Identity newId = m_keyChain.getPib().getIdentity(cert.getIdentity());
755 BOOST_CHECK_EQUAL(newId.getKeys().size(), 1);
Yingdi Yufe4733a2015-10-22 14:24:12 -0700756 Key newKey = newId.getKey(cert.getKeyName());
757 BOOST_CHECK_EQUAL(newKey.getCertificates().size(), 1);
Davide Pesaventoe30389c2022-05-31 17:23:33 -0400758 Certificate newCert = newKey.getCertificate(cert.getName());
759 BOOST_CHECK_EQUAL(newCert, cert);
760}
Yingdi Yufe4733a2015-10-22 14:24:12 -0700761
Davide Pesaventoe30389c2022-05-31 17:23:33 -0400762BOOST_FIXTURE_TEST_CASE(ImportInvalid, KeyChainFixture)
763{
764 Identity id = m_keyChain.createIdentity("/TestKeyChain/ExportIdentity");
765 Certificate cert = id.getDefaultKey().getDefaultCertificate();
766
767 auto exported = m_keyChain.exportSafeBag(cert, "1234", 4);
768 m_keyChain.deleteIdentity(id);
769
770 Identity id2 = m_keyChain.createIdentity("/TestKeyChain/AnotherIdentity");
771 Certificate cert2 = id2.getDefaultKey().getDefaultCertificate();
772 m_keyChain.deleteIdentity(id2);
773
774 SafeBag mismatch(cert2, exported->getEncryptedKey());
775 BOOST_CHECK_EXCEPTION(m_keyChain.importSafeBag(mismatch, "1234", 4), KeyChain::Error, [] (const auto& e) {
776 return std::string(e.what()).find("do not match") != std::string::npos;
777 });
778
779 SafeBag invalidPriv(cert2, {0xCA, 0xFE});
780 BOOST_CHECK_EXCEPTION(m_keyChain.importSafeBag(invalidPriv, "1234", 4), KeyChain::Error, [] (const auto& e) {
781 return std::string(e.what()).find("Failed to import private key") == 0;
782 });
Yingdi Yufe4733a2015-10-22 14:24:12 -0700783}
784
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500785BOOST_FIXTURE_TEST_CASE(SelfSignedCertValidity, KeyChainFixture)
Alexander Afanasyevf8379172017-01-11 16:56:04 -0800786{
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500787 Certificate cert = m_keyChain.createIdentity("/Security/TestKeyChain/SelfSignedCertValidity")
Alexander Afanasyevf8379172017-01-11 16:56:04 -0800788 .getDefaultKey()
789 .getDefaultCertificate();
790 BOOST_CHECK(cert.isValid());
Davide Pesavento0f830802018-01-16 23:58:58 -0500791 BOOST_CHECK(cert.isValid(time::system_clock::now() + 10 * 365_days));
792 BOOST_CHECK_GT(cert.getValidityPeriod().getPeriod().second, time::system_clock::now() + 10 * 365_days);
Alexander Afanasyevf8379172017-01-11 16:56:04 -0800793}
794
Yingdi Yufe4733a2015-10-22 14:24:12 -0700795BOOST_AUTO_TEST_SUITE_END() // TestKeyChain
Yingdi Yufe4733a2015-10-22 14:24:12 -0700796BOOST_AUTO_TEST_SUITE_END() // Security
797
Davide Pesavento47ce2ee2023-05-09 01:33:33 -0400798} // namespace ndn::tests