Alexander Afanasyev | 1b58a03 | 2017-01-04 14:00:47 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Alexander Afanasyev | adc7184 | 2017-01-26 22:17:58 -0500 | [diff] [blame] | 2 | /* |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2023 Regents of the University of California. |
Alexander Afanasyev | 1b58a03 | 2017-01-04 14:00:47 -0800 | [diff] [blame] | 4 | * |
| 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 | |
Eric Newberry | 17d7c47 | 2020-06-18 21:29:22 -0700 | [diff] [blame] | 22 | #include "ndn-cxx/security/interest-signer.hpp" |
Alexander Afanasyev | 1b58a03 | 2017-01-04 14:00:47 -0800 | [diff] [blame] | 23 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 24 | #include "tests/boost-test.hpp" |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 25 | #include "tests/key-chain-fixture.hpp" |
| 26 | #include "tests/unit/clock-fixture.hpp" |
Alexander Afanasyev | 1b58a03 | 2017-01-04 14:00:47 -0800 | [diff] [blame] | 27 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 28 | namespace ndn::tests { |
Alexander Afanasyev | 1b58a03 | 2017-01-04 14:00:47 -0800 | [diff] [blame] | 29 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 30 | using ndn::security::InterestSigner; |
| 31 | using ndn::security::SigningInfo; |
Alexander Afanasyev | 1b58a03 | 2017-01-04 14:00:47 -0800 | [diff] [blame] | 32 | |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 33 | class InterestSignerFixture : public ClockFixture, public KeyChainFixture |
| 34 | { |
| 35 | }; |
| 36 | |
Alexander Afanasyev | 1b58a03 | 2017-01-04 14:00:47 -0800 | [diff] [blame] | 37 | BOOST_AUTO_TEST_SUITE(Security) |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 38 | BOOST_FIXTURE_TEST_SUITE(TestInterestSigner, InterestSignerFixture) |
Alexander Afanasyev | 1b58a03 | 2017-01-04 14:00:47 -0800 | [diff] [blame] | 39 | |
Eric Newberry | 17d7c47 | 2020-06-18 21:29:22 -0700 | [diff] [blame] | 40 | BOOST_AUTO_TEST_CASE(V02) |
Alexander Afanasyev | 1b58a03 | 2017-01-04 14:00:47 -0800 | [diff] [blame] | 41 | { |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 42 | m_keyChain.createIdentity("/test"); |
Alexander Afanasyev | 1b58a03 | 2017-01-04 14:00:47 -0800 | [diff] [blame] | 43 | |
Eric Newberry | 17d7c47 | 2020-06-18 21:29:22 -0700 | [diff] [blame] | 44 | InterestSigner signer(m_keyChain); |
Alexander Afanasyev | 1b58a03 | 2017-01-04 14:00:47 -0800 | [diff] [blame] | 45 | Interest i1 = signer.makeCommandInterest("/hello/world"); |
Eric Newberry | 17d7c47 | 2020-06-18 21:29:22 -0700 | [diff] [blame] | 46 | BOOST_REQUIRE_EQUAL(i1.getName().size(), 6); |
Eric Newberry | 1caa634 | 2020-08-23 19:29:08 -0700 | [diff] [blame] | 47 | BOOST_TEST(i1.getName().at(command_interest::POS_SIG_VALUE).blockFromValue().type() == tlv::SignatureValue); |
| 48 | BOOST_TEST(i1.getName().at(command_interest::POS_SIG_INFO).blockFromValue().type() == tlv::SignatureInfo); |
Alexander Afanasyev | 1b58a03 | 2017-01-04 14:00:47 -0800 | [diff] [blame] | 49 | |
| 50 | time::milliseconds timestamp = toUnixTimestamp(time::system_clock::now()); |
Eric Newberry | 1caa634 | 2020-08-23 19:29:08 -0700 | [diff] [blame] | 51 | BOOST_TEST(i1.getName().at(command_interest::POS_TIMESTAMP).toNumber() == timestamp.count()); |
Alexander Afanasyev | 1b58a03 | 2017-01-04 14:00:47 -0800 | [diff] [blame] | 52 | |
| 53 | Interest i2 = signer.makeCommandInterest("/hello/world/!", signingByIdentity("/test")); |
Eric Newberry | 17d7c47 | 2020-06-18 21:29:22 -0700 | [diff] [blame] | 54 | BOOST_REQUIRE_EQUAL(i2.getName().size(), 7); |
Eric Newberry | 1caa634 | 2020-08-23 19:29:08 -0700 | [diff] [blame] | 55 | BOOST_TEST(i2.getName().at(command_interest::POS_SIG_VALUE).blockFromValue().type() == tlv::SignatureValue); |
| 56 | BOOST_TEST(i2.getName().at(command_interest::POS_SIG_INFO).blockFromValue().type() == tlv::SignatureInfo); |
| 57 | // These doesn't play well with BOOST_TEST for some reason |
| 58 | BOOST_CHECK_GT(i2.getName().at(command_interest::POS_TIMESTAMP), |
| 59 | i1.getName().at(command_interest::POS_TIMESTAMP)); |
Alexander Afanasyev | 70244f4 | 2017-01-04 12:47:12 -0800 | [diff] [blame] | 60 | BOOST_CHECK_NE(i2.getName().at(command_interest::POS_RANDOM_VAL), |
| 61 | i1.getName().at(command_interest::POS_RANDOM_VAL)); // this sometimes can fail |
Alexander Afanasyev | 1b58a03 | 2017-01-04 14:00:47 -0800 | [diff] [blame] | 62 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 63 | advanceClocks(100_s); |
Alexander Afanasyev | 1b58a03 | 2017-01-04 14:00:47 -0800 | [diff] [blame] | 64 | |
| 65 | i2 = signer.makeCommandInterest("/hello/world/!"); |
Eric Newberry | 1caa634 | 2020-08-23 19:29:08 -0700 | [diff] [blame] | 66 | // This doesn't play well with BOOST_TEST for some reason |
| 67 | BOOST_CHECK_GT(i2.getName().at(command_interest::POS_TIMESTAMP), |
| 68 | i1.getName().at(command_interest::POS_TIMESTAMP)); |
Alexander Afanasyev | 1b58a03 | 2017-01-04 14:00:47 -0800 | [diff] [blame] | 69 | } |
| 70 | |
Eric Newberry | 17d7c47 | 2020-06-18 21:29:22 -0700 | [diff] [blame] | 71 | BOOST_AUTO_TEST_CASE(V03) |
| 72 | { |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 73 | m_keyChain.createIdentity("/test"); |
Eric Newberry | 17d7c47 | 2020-06-18 21:29:22 -0700 | [diff] [blame] | 74 | |
| 75 | InterestSigner signer(m_keyChain); |
| 76 | Interest i1("/hello/world"); |
Eric Newberry | 1caa634 | 2020-08-23 19:29:08 -0700 | [diff] [blame] | 77 | signer.makeSignedInterest(i1, SigningInfo(), |
| 78 | InterestSigner::SigningFlags::WantNonce | |
Davide Pesavento | aee2ada | 2022-02-18 14:43:02 -0500 | [diff] [blame] | 79 | InterestSigner::SigningFlags::WantTime); |
Eric Newberry | 1caa634 | 2020-08-23 19:29:08 -0700 | [diff] [blame] | 80 | BOOST_TEST(i1.isSigned() == true); |
| 81 | BOOST_TEST_REQUIRE(i1.getName().size() == 3); |
| 82 | BOOST_TEST_REQUIRE(i1.getSignatureInfo().has_value()); |
Eric Newberry | 17d7c47 | 2020-06-18 21:29:22 -0700 | [diff] [blame] | 83 | |
Eric Newberry | 1caa634 | 2020-08-23 19:29:08 -0700 | [diff] [blame] | 84 | BOOST_TEST(i1.getSignatureInfo()->getNonce().has_value() == true); |
Eric Newberry | 17d7c47 | 2020-06-18 21:29:22 -0700 | [diff] [blame] | 85 | BOOST_TEST(*i1.getSignatureInfo()->getTime() == time::system_clock::now()); |
Eric Newberry | 1caa634 | 2020-08-23 19:29:08 -0700 | [diff] [blame] | 86 | BOOST_TEST(i1.getSignatureInfo()->getSeqNum().has_value() == false); |
Eric Newberry | 17d7c47 | 2020-06-18 21:29:22 -0700 | [diff] [blame] | 87 | |
| 88 | Interest i2("/hello/world/!"); |
Eric Newberry | 1caa634 | 2020-08-23 19:29:08 -0700 | [diff] [blame] | 89 | signer.makeSignedInterest(i2, signingByIdentity("/test"), |
| 90 | InterestSigner::SigningFlags::WantNonce | |
Davide Pesavento | aee2ada | 2022-02-18 14:43:02 -0500 | [diff] [blame] | 91 | InterestSigner::SigningFlags::WantTime | |
| 92 | InterestSigner::SigningFlags::WantSeqNum); |
Eric Newberry | 1caa634 | 2020-08-23 19:29:08 -0700 | [diff] [blame] | 93 | BOOST_TEST(i2.isSigned() == true); |
Eric Newberry | 17d7c47 | 2020-06-18 21:29:22 -0700 | [diff] [blame] | 94 | BOOST_REQUIRE_EQUAL(i2.getName().size(), 4); |
| 95 | BOOST_REQUIRE(i2.getSignatureInfo()); |
| 96 | |
Eric Newberry | 17d7c47 | 2020-06-18 21:29:22 -0700 | [diff] [blame] | 97 | BOOST_TEST(*i2.getSignatureInfo()->getNonce() != *i1.getSignatureInfo()->getNonce()); |
Eric Newberry | 1caa634 | 2020-08-23 19:29:08 -0700 | [diff] [blame] | 98 | BOOST_TEST(*i2.getSignatureInfo()->getTime() > *i1.getSignatureInfo()->getTime()); |
| 99 | BOOST_TEST_REQUIRE(i2.getSignatureInfo()->getSeqNum().has_value() == true); |
Eric Newberry | 17d7c47 | 2020-06-18 21:29:22 -0700 | [diff] [blame] | 100 | |
| 101 | advanceClocks(100_s); |
| 102 | |
Eric Newberry | 1caa634 | 2020-08-23 19:29:08 -0700 | [diff] [blame] | 103 | Interest i3("/hello/world/2"); |
Eric Newberry | 1caa634 | 2020-08-23 19:29:08 -0700 | [diff] [blame] | 104 | signer.makeSignedInterest(i3, SigningInfo(), InterestSigner::SigningFlags::WantSeqNum); |
| 105 | BOOST_TEST(i3.isSigned() == true); |
| 106 | BOOST_REQUIRE_EQUAL(i3.getName().size(), 4); |
| 107 | BOOST_REQUIRE(i3.getSignatureInfo()); |
Eric Newberry | 17d7c47 | 2020-06-18 21:29:22 -0700 | [diff] [blame] | 108 | |
Eric Newberry | 1caa634 | 2020-08-23 19:29:08 -0700 | [diff] [blame] | 109 | BOOST_TEST(i3.getSignatureInfo()->getNonce().has_value() == false); |
| 110 | BOOST_TEST(i3.getSignatureInfo()->getTime().has_value() == false); |
| 111 | BOOST_TEST_REQUIRE(i3.getSignatureInfo()->getSeqNum().has_value() == true); |
| 112 | BOOST_TEST(*i3.getSignatureInfo()->getSeqNum() > *i2.getSignatureInfo()->getSeqNum()); |
| 113 | |
| 114 | signer.makeSignedInterest(i3); |
| 115 | BOOST_TEST(i3.isSigned() == true); |
| 116 | |
| 117 | BOOST_TEST(*i3.getSignatureInfo()->getTime() == time::system_clock::now()); |
| 118 | |
| 119 | BOOST_CHECK_THROW(signer.makeSignedInterest(i3, SigningInfo(), 0), std::invalid_argument); |
Eric Newberry | 17d7c47 | 2020-06-18 21:29:22 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | BOOST_AUTO_TEST_SUITE_END() // TestInterestSigner |
Alexander Afanasyev | 1b58a03 | 2017-01-04 14:00:47 -0800 | [diff] [blame] | 123 | BOOST_AUTO_TEST_SUITE_END() // Security |
| 124 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 125 | } // namespace ndn::tests |