Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 605671d | 2017-08-26 13:41:06 +0000 | [diff] [blame] | 2 | /* |
Davide Pesavento | 0c52603 | 2024-01-31 21:14:01 -0500 | [diff] [blame] | 3 | * Copyright (c) 2013-2024 Regents of the University of California. |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [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 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 22 | #include "ndn-cxx/signature-info.hpp" |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 23 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 24 | #include "tests/boost-test.hpp" |
Davide Pesavento | 14c56cd | 2020-05-21 01:44:03 -0400 | [diff] [blame] | 25 | |
Alexander Afanasyev | 4146975 | 2017-01-10 21:51:55 -0800 | [diff] [blame] | 26 | #include <boost/lexical_cast.hpp> |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 27 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 28 | namespace ndn::tests { |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 29 | |
Davide Pesavento | 152ef44 | 2023-04-22 02:02:29 -0400 | [diff] [blame] | 30 | BOOST_CONCEPT_ASSERT((boost::EqualityComparable<SignatureInfo>)); |
| 31 | BOOST_CONCEPT_ASSERT((WireEncodable<SignatureInfo>)); |
| 32 | BOOST_CONCEPT_ASSERT((WireEncodableWithEncodingBuffer<SignatureInfo>)); |
| 33 | BOOST_CONCEPT_ASSERT((WireDecodable<SignatureInfo>)); |
| 34 | static_assert(std::is_convertible_v<SignatureInfo::Error*, tlv::Error*>, |
| 35 | "SignatureInfo::Error must inherit from tlv::Error"); |
| 36 | |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 37 | BOOST_AUTO_TEST_SUITE(TestSignatureInfo) |
| 38 | |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 39 | BOOST_AUTO_TEST_CASE(Constructor) |
| 40 | { |
| 41 | SignatureInfo info; |
| 42 | BOOST_CHECK_EQUAL(info.getSignatureType(), -1); |
| 43 | BOOST_CHECK_EQUAL(info.hasKeyLocator(), false); |
| 44 | BOOST_CHECK_THROW(info.getKeyLocator(), SignatureInfo::Error); |
Davide Pesavento | 14c56cd | 2020-05-21 01:44:03 -0400 | [diff] [blame] | 45 | BOOST_CHECK_THROW(info.getValidityPeriod(), SignatureInfo::Error); |
Eric Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 46 | BOOST_CHECK(!info.getNonce()); |
| 47 | BOOST_CHECK(!info.getTime()); |
| 48 | BOOST_CHECK(!info.getSeqNum()); |
Davide Pesavento | 7e6f6f8 | 2017-10-31 18:05:28 -0400 | [diff] [blame] | 49 | |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 50 | SignatureInfo sha256Info(tlv::DigestSha256); |
| 51 | BOOST_CHECK_EQUAL(sha256Info.getSignatureType(), tlv::DigestSha256); |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 52 | BOOST_CHECK_THROW(sha256Info.getKeyLocator(), SignatureInfo::Error); |
Eric Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 53 | BOOST_CHECK(!info.getNonce()); |
| 54 | BOOST_CHECK(!info.getTime()); |
| 55 | BOOST_CHECK(!info.getSeqNum()); |
Alexander Afanasyev | 4146975 | 2017-01-10 21:51:55 -0800 | [diff] [blame] | 56 | |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 57 | KeyLocator keyLocator("/test/key/locator"); |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 58 | SignatureInfo sha256RsaInfo(tlv::SignatureSha256WithRsa, keyLocator); |
| 59 | BOOST_CHECK_EQUAL(sha256RsaInfo.getSignatureType(), tlv::SignatureSha256WithRsa); |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 60 | BOOST_CHECK_EQUAL(sha256RsaInfo.hasKeyLocator(), true); |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 61 | BOOST_CHECK_EQUAL(sha256RsaInfo.getKeyLocator().getName(), Name("/test/key/locator")); |
Davide Pesavento | 14c56cd | 2020-05-21 01:44:03 -0400 | [diff] [blame] | 62 | BOOST_CHECK_THROW(sha256RsaInfo.getValidityPeriod(), SignatureInfo::Error); |
Eric Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 63 | BOOST_CHECK(!info.getNonce()); |
| 64 | BOOST_CHECK(!info.getTime()); |
| 65 | BOOST_CHECK(!info.getSeqNum()); |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 66 | } |
| 67 | |
Eric Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 68 | BOOST_AUTO_TEST_CASE(SignatureType) |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 69 | { |
| 70 | SignatureInfo info; |
| 71 | BOOST_CHECK_EQUAL(info.getSignatureType(), -1); |
Davide Pesavento | 14c56cd | 2020-05-21 01:44:03 -0400 | [diff] [blame] | 72 | BOOST_CHECK_THROW(info.wireEncode(), SignatureInfo::Error); |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 73 | |
Davide Pesavento | 14c56cd | 2020-05-21 01:44:03 -0400 | [diff] [blame] | 74 | info.setSignatureType(tlv::SignatureSha256WithEcdsa); |
| 75 | BOOST_CHECK_EQUAL(info.getSignatureType(), tlv::SignatureSha256WithEcdsa); |
| 76 | BOOST_CHECK_EQUAL(info.hasWire(), false); |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 77 | |
Davide Pesavento | 14c56cd | 2020-05-21 01:44:03 -0400 | [diff] [blame] | 78 | info.wireEncode(); |
| 79 | BOOST_CHECK_EQUAL(info.hasWire(), true); |
| 80 | info.setSignatureType(tlv::SignatureSha256WithEcdsa); |
| 81 | BOOST_CHECK_EQUAL(info.hasWire(), true); |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 82 | |
Davide Pesavento | 14c56cd | 2020-05-21 01:44:03 -0400 | [diff] [blame] | 83 | info.setSignatureType(static_cast<tlv::SignatureTypeValue>(1234)); |
| 84 | BOOST_CHECK_EQUAL(info.getSignatureType(), 1234); |
| 85 | BOOST_CHECK_EQUAL(info.hasWire(), false); |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 86 | } |
| 87 | |
Eric Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 88 | // We must name this test case differently to avoid naming conflicts |
| 89 | BOOST_AUTO_TEST_CASE(KeyLocatorField) |
Davide Pesavento | 14c56cd | 2020-05-21 01:44:03 -0400 | [diff] [blame] | 90 | { |
| 91 | SignatureInfo info(tlv::SignatureSha256WithEcdsa); |
| 92 | BOOST_CHECK_EQUAL(info.hasKeyLocator(), false); |
| 93 | BOOST_CHECK_THROW(info.getKeyLocator(), SignatureInfo::Error); |
| 94 | |
| 95 | info.setKeyLocator(Name("/test/key/locator")); |
| 96 | BOOST_CHECK_EQUAL(info.hasKeyLocator(), true); |
| 97 | BOOST_CHECK_EQUAL(info.getKeyLocator().getName(), "/test/key/locator"); |
| 98 | BOOST_CHECK_EQUAL(info.hasWire(), false); |
| 99 | |
| 100 | info.wireEncode(); |
| 101 | BOOST_CHECK_EQUAL(info.hasWire(), true); |
| 102 | info.setKeyLocator(Name("/test/key/locator")); |
| 103 | BOOST_CHECK_EQUAL(info.hasWire(), true); |
| 104 | info.setKeyLocator(Name("/another/key/locator")); |
| 105 | BOOST_CHECK_EQUAL(info.hasWire(), false); |
| 106 | |
| 107 | info.wireEncode(); |
| 108 | BOOST_CHECK_EQUAL(info.hasWire(), true); |
Davide Pesavento | f6b4589 | 2023-03-13 15:00:51 -0400 | [diff] [blame] | 109 | info.setKeyLocator(std::nullopt); |
Davide Pesavento | 14c56cd | 2020-05-21 01:44:03 -0400 | [diff] [blame] | 110 | BOOST_CHECK_EQUAL(info.hasKeyLocator(), false); |
| 111 | BOOST_CHECK_THROW(info.getKeyLocator(), SignatureInfo::Error); |
| 112 | BOOST_CHECK_EQUAL(info.hasWire(), false); |
| 113 | } |
| 114 | |
Eric Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 115 | BOOST_AUTO_TEST_CASE(SignatureNonce) |
| 116 | { |
| 117 | SignatureInfo info(tlv::SignatureSha256WithEcdsa); |
| 118 | BOOST_CHECK(!info.getNonce()); |
| 119 | info.wireEncode(); |
| 120 | BOOST_CHECK_EQUAL(info.hasWire(), true); |
| 121 | std::vector<uint8_t> nonce{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}; |
| 122 | info.setNonce(nonce); |
| 123 | BOOST_CHECK_EQUAL(info.hasWire(), false); |
| 124 | BOOST_CHECK(info.getNonce() == nonce); |
| 125 | |
| 126 | info.wireEncode(); |
| 127 | info.setNonce(nonce); |
| 128 | BOOST_CHECK_EQUAL(info.hasWire(), true); |
| 129 | nonce[2] = 0xFF; |
| 130 | info.setNonce(nonce); |
| 131 | BOOST_CHECK_EQUAL(info.hasWire(), false); |
| 132 | BOOST_CHECK(info.getNonce() == nonce); |
| 133 | |
| 134 | info.wireEncode(); |
| 135 | BOOST_CHECK_EQUAL(info.hasWire(), true); |
Davide Pesavento | f6b4589 | 2023-03-13 15:00:51 -0400 | [diff] [blame] | 136 | info.setNonce(std::nullopt); |
Eric Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 137 | BOOST_CHECK_EQUAL(info.hasWire(), false); |
| 138 | BOOST_CHECK(!info.getNonce()); |
| 139 | } |
| 140 | |
| 141 | BOOST_AUTO_TEST_CASE(SignatureTime) |
| 142 | { |
| 143 | SignatureInfo info(tlv::SignatureSha256WithEcdsa); |
| 144 | BOOST_CHECK(!info.getTime()); |
| 145 | info.wireEncode(); |
| 146 | BOOST_CHECK_EQUAL(info.hasWire(), true); |
| 147 | time::system_clock::time_point timePoint(1590169108480_ms); |
| 148 | info.setTime(timePoint); |
| 149 | BOOST_CHECK_EQUAL(info.hasWire(), false); |
| 150 | BOOST_CHECK(info.getTime() == timePoint); |
| 151 | |
| 152 | info.wireEncode(); |
| 153 | info.setTime(timePoint); |
| 154 | BOOST_CHECK_EQUAL(info.hasWire(), true); |
| 155 | info.setTime(timePoint + 2_s); |
| 156 | BOOST_CHECK_EQUAL(info.hasWire(), false); |
| 157 | BOOST_CHECK(info.getTime() == timePoint + 2_s); |
| 158 | |
| 159 | info.wireEncode(); |
| 160 | BOOST_CHECK_EQUAL(info.hasWire(), true); |
Davide Pesavento | f6b4589 | 2023-03-13 15:00:51 -0400 | [diff] [blame] | 161 | info.setTime(std::nullopt); |
Eric Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 162 | BOOST_CHECK_EQUAL(info.hasWire(), false); |
| 163 | BOOST_CHECK(!info.getTime()); |
| 164 | } |
| 165 | |
| 166 | BOOST_AUTO_TEST_CASE(SignatureSeqNum) |
| 167 | { |
| 168 | SignatureInfo info(tlv::SignatureSha256WithEcdsa); |
| 169 | BOOST_CHECK(!info.getSeqNum()); |
| 170 | info.wireEncode(); |
| 171 | BOOST_CHECK_EQUAL(info.hasWire(), true); |
| 172 | info.setSeqNum(256); |
| 173 | BOOST_CHECK_EQUAL(info.hasWire(), false); |
| 174 | BOOST_CHECK(info.getSeqNum() == 256UL); |
| 175 | |
| 176 | info.wireEncode(); |
| 177 | info.setSeqNum(256); |
| 178 | BOOST_CHECK_EQUAL(info.hasWire(), true); |
| 179 | info.setSeqNum(512); |
| 180 | BOOST_CHECK_EQUAL(info.hasWire(), false); |
| 181 | BOOST_CHECK(info.getSeqNum() == 512UL); |
| 182 | |
| 183 | info.wireEncode(); |
| 184 | BOOST_CHECK_EQUAL(info.hasWire(), true); |
Davide Pesavento | f6b4589 | 2023-03-13 15:00:51 -0400 | [diff] [blame] | 185 | info.setSeqNum(std::nullopt); |
Eric Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 186 | BOOST_CHECK_EQUAL(info.hasWire(), false); |
| 187 | BOOST_CHECK(!info.getSeqNum()); |
| 188 | } |
| 189 | |
Davide Pesavento | a86300a | 2020-12-05 14:55:45 -0500 | [diff] [blame] | 190 | const uint8_t sigInfoDataRsa[] = { |
| 191 | 0x16, 0x1b, // SignatureInfo |
| 192 | 0x1b, 0x01, // SignatureType |
| 193 | 0x01, // Sha256WithRsa |
| 194 | 0x1c, 0x16, // KeyLocator |
| 195 | 0x07, 0x14, // Name |
| 196 | 0x08, 0x04, |
| 197 | 0x74, 0x65, 0x73, 0x74, |
| 198 | 0x08, 0x03, |
| 199 | 0x6b, 0x65, 0x79, |
| 200 | 0x08, 0x07, |
| 201 | 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72, |
| 202 | }; |
| 203 | |
| 204 | const uint8_t sigInfoInterestRsa[] = { |
| 205 | 0x2c, 0x33, // InterestSignatureInfo |
| 206 | 0x1b, 0x01, // SignatureType |
| 207 | 0x01, // Sha256WithRsa |
| 208 | 0x1c, 0x16, // KeyLocator |
| 209 | 0x07, 0x14, // Name |
| 210 | 0x08, 0x04, |
| 211 | 0x74, 0x65, 0x73, 0x74, |
| 212 | 0x08, 0x03, |
| 213 | 0x6b, 0x65, 0x79, |
| 214 | 0x08, 0x07, |
| 215 | 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72, |
| 216 | 0x26, 0x08, // SignatureNonce |
| 217 | 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, |
| 218 | 0x28, 0x08, // SignatureTime |
| 219 | 0x00, 0x00, 0x01, 0x72, 0x3d, 0x77, 0x00, 0x00, |
| 220 | 0x2a, 0x02, // SignatureSeqNum |
| 221 | 0x10, 0x20, |
| 222 | }; |
| 223 | |
| 224 | const uint8_t sigInfoDataEcdsa[] = { |
| 225 | 0x16, 0x1b, // SignatureInfo |
| 226 | 0x1b, 0x01, // SignatureType |
| 227 | 0x03, // Sha256WithEcdsa |
| 228 | 0x1c, 0x16, // KeyLocator |
| 229 | 0x07, 0x14, // Name |
| 230 | 0x08, 0x04, |
| 231 | 0x74, 0x65, 0x73, 0x74, |
| 232 | 0x08, 0x03, |
| 233 | 0x6b, 0x65, 0x79, |
| 234 | 0x08, 0x07, |
| 235 | 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72, |
| 236 | }; |
| 237 | |
Eric Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 238 | BOOST_AUTO_TEST_CASE(EncodeDecode) |
| 239 | { |
Davide Pesavento | a86300a | 2020-12-05 14:55:45 -0500 | [diff] [blame] | 240 | const KeyLocator keyLocator("/test/key/locator"); |
| 241 | |
| 242 | // RSA |
Eric Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 243 | SignatureInfo info(tlv::SignatureSha256WithRsa, keyLocator); |
| 244 | |
Davide Pesavento | a86300a | 2020-12-05 14:55:45 -0500 | [diff] [blame] | 245 | // Encode as (Data)SignatureInfo |
Eric Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 246 | auto encodedData = info.wireEncode(SignatureInfo::Type::Data); |
Eric Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 247 | |
Davide Pesavento | a86300a | 2020-12-05 14:55:45 -0500 | [diff] [blame] | 248 | BOOST_CHECK_EQUAL_COLLECTIONS(sigInfoDataRsa, sigInfoDataRsa + sizeof(sigInfoDataRsa), |
| 249 | encodedData.begin(), encodedData.end()); |
Eric Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 250 | |
Davide Pesavento | a86300a | 2020-12-05 14:55:45 -0500 | [diff] [blame] | 251 | // Decode as (Data)SignatureInfo |
Davide Pesavento | fbea4fc | 2022-02-08 07:26:04 -0500 | [diff] [blame] | 252 | info = SignatureInfo(Block(sigInfoDataRsa), SignatureInfo::Type::Data); |
Davide Pesavento | a86300a | 2020-12-05 14:55:45 -0500 | [diff] [blame] | 253 | |
Eric Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 254 | BOOST_CHECK_EQUAL(info.getSignatureType(), tlv::SignatureSha256WithRsa); |
| 255 | BOOST_CHECK_EQUAL(info.hasKeyLocator(), true); |
| 256 | BOOST_CHECK_EQUAL(info.getKeyLocator().getName(), Name("/test/key/locator")); |
Eric Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 257 | BOOST_CHECK(!info.getNonce()); |
| 258 | BOOST_CHECK(!info.getTime()); |
| 259 | BOOST_CHECK(!info.getSeqNum()); |
| 260 | |
| 261 | // Encode as InterestSignatureInfo |
Davide Pesavento | a86300a | 2020-12-05 14:55:45 -0500 | [diff] [blame] | 262 | const std::vector<uint8_t> nonce{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}; |
Eric Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 263 | info.setNonce(nonce); |
Davide Pesavento | a86300a | 2020-12-05 14:55:45 -0500 | [diff] [blame] | 264 | const time::system_clock::time_point timePoint(1590169108480_ms); |
Eric Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 265 | info.setTime(timePoint); |
| 266 | info.setSeqNum(0x1020); |
| 267 | auto encodedInterest = info.wireEncode(SignatureInfo::Type::Interest); |
Eric Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 268 | |
Davide Pesavento | a86300a | 2020-12-05 14:55:45 -0500 | [diff] [blame] | 269 | BOOST_CHECK_EQUAL_COLLECTIONS(sigInfoInterestRsa, sigInfoInterestRsa + sizeof(sigInfoInterestRsa), |
| 270 | encodedInterest.begin(), encodedInterest.end()); |
Eric Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 271 | |
| 272 | // Decode as InterestSignatureInfo |
Davide Pesavento | fbea4fc | 2022-02-08 07:26:04 -0500 | [diff] [blame] | 273 | info = SignatureInfo(Block(sigInfoInterestRsa), SignatureInfo::Type::Interest); |
Davide Pesavento | a86300a | 2020-12-05 14:55:45 -0500 | [diff] [blame] | 274 | |
Eric Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 275 | BOOST_CHECK_EQUAL(info.getSignatureType(), tlv::SignatureSha256WithRsa); |
| 276 | BOOST_CHECK_EQUAL(info.hasKeyLocator(), true); |
| 277 | BOOST_CHECK_EQUAL(info.getKeyLocator().getName(), Name("/test/key/locator")); |
| 278 | BOOST_CHECK(info.getNonce() == nonce); |
| 279 | BOOST_CHECK(info.getTime() == timePoint); |
| 280 | BOOST_CHECK(info.getSeqNum() == 0x1020UL); |
Davide Pesavento | a86300a | 2020-12-05 14:55:45 -0500 | [diff] [blame] | 281 | |
| 282 | // ECDSA |
| 283 | info = SignatureInfo(tlv::SignatureSha256WithEcdsa, keyLocator); |
| 284 | |
| 285 | // Encode as (Data)SignatureInfo |
| 286 | auto encodedDataEcdsa = info.wireEncode(SignatureInfo::Type::Data); |
| 287 | |
| 288 | BOOST_CHECK_EQUAL_COLLECTIONS(sigInfoDataEcdsa, sigInfoDataEcdsa + sizeof(sigInfoDataEcdsa), |
| 289 | encodedDataEcdsa.begin(), encodedDataEcdsa.end()); |
| 290 | |
| 291 | // Decode as (Data)SignatureInfo |
Davide Pesavento | fbea4fc | 2022-02-08 07:26:04 -0500 | [diff] [blame] | 292 | info = SignatureInfo(Block(sigInfoDataEcdsa), SignatureInfo::Type::Data); |
Davide Pesavento | a86300a | 2020-12-05 14:55:45 -0500 | [diff] [blame] | 293 | |
| 294 | BOOST_CHECK_EQUAL(info.getSignatureType(), tlv::SignatureSha256WithEcdsa); |
| 295 | BOOST_CHECK_EQUAL(info.hasKeyLocator(), true); |
| 296 | BOOST_CHECK_EQUAL(info.getKeyLocator().getName(), Name("/test/key/locator")); |
| 297 | BOOST_CHECK(!info.getNonce()); |
| 298 | BOOST_CHECK(!info.getTime()); |
| 299 | BOOST_CHECK(!info.getSeqNum()); |
Eric Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | BOOST_AUTO_TEST_CASE(DecodeError) |
| 303 | { |
| 304 | const uint8_t error1[] = { |
| 305 | 0x15, 0x1b, // Wrong SignatureInfo (0x16, 0x1b) |
| 306 | 0x1b, 0x01, // SignatureType |
| 307 | 0x01, // Sha256WithRsa |
| 308 | 0x1c, 0x16, // KeyLocator |
| 309 | 0x07, 0x14, // Name |
| 310 | 0x08, 0x04, |
| 311 | 0x74, 0x65, 0x73, 0x74, |
| 312 | 0x08, 0x03, |
| 313 | 0x6b, 0x65, 0x79, |
| 314 | 0x08, 0x07, |
| 315 | 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72 |
| 316 | }; |
Davide Pesavento | fbea4fc | 2022-02-08 07:26:04 -0500 | [diff] [blame] | 317 | Block errorBlock1(error1); |
Eric Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 318 | BOOST_CHECK_THROW(SignatureInfo(errorBlock1, SignatureInfo::Type::Data), tlv::Error); |
| 319 | |
| 320 | const uint8_t error2[] = { |
| 321 | 0x16, 0x05, // SignatureInfo |
| 322 | 0x1b, 0x01, // SignatureType |
| 323 | 0x01, // Sha256WithRsa |
| 324 | 0x83, 0x00, // Unrecognized critical TLV |
| 325 | }; |
Davide Pesavento | fbea4fc | 2022-02-08 07:26:04 -0500 | [diff] [blame] | 326 | Block errorBlock2(error2); |
Eric Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 327 | BOOST_CHECK_THROW(SignatureInfo(errorBlock2, SignatureInfo::Type::Data), tlv::Error); |
| 328 | |
| 329 | const uint8_t error3[] = { |
| 330 | 0x16, 0x00 // Empty SignatureInfo |
| 331 | }; |
Davide Pesavento | fbea4fc | 2022-02-08 07:26:04 -0500 | [diff] [blame] | 332 | Block errorBlock3(error3); |
Eric Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 333 | BOOST_CHECK_THROW(SignatureInfo(errorBlock3, SignatureInfo::Type::Data), tlv::Error); |
| 334 | |
| 335 | // Encoding is correct for SignatureInfo, but decoder is expecting InterestSignatureInfo |
| 336 | const uint8_t error4[] = { |
| 337 | 0x16, 0x1b, // SignatureInfo |
| 338 | 0x1b, 0x01, // SignatureType |
| 339 | 0x01, // Sha256WithRsa |
| 340 | 0x1c, 0x16, // KeyLocator |
| 341 | 0x07, 0x14, // Name |
| 342 | 0x08, 0x04, |
| 343 | 0x74, 0x65, 0x73, 0x74, |
| 344 | 0x08, 0x03, |
| 345 | 0x6b, 0x65, 0x79, |
| 346 | 0x08, 0x07, |
| 347 | 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72 |
| 348 | }; |
Davide Pesavento | fbea4fc | 2022-02-08 07:26:04 -0500 | [diff] [blame] | 349 | Block errorBlock4(error4); |
Eric Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 350 | BOOST_CHECK_THROW(SignatureInfo(errorBlock4, SignatureInfo::Type::Interest), tlv::Error); |
| 351 | |
| 352 | // SignatureType and KeyLocator out-of-order |
| 353 | const uint8_t error5[] = { |
| 354 | 0x2c, 0x1b, // SignatureInfo |
| 355 | 0x1c, 0x16, // KeyLocator |
| 356 | 0x07, 0x14, // Name |
| 357 | 0x08, 0x04, |
| 358 | 0x74, 0x65, 0x73, 0x74, |
| 359 | 0x08, 0x03, |
| 360 | 0x6b, 0x65, 0x79, |
| 361 | 0x08, 0x07, |
| 362 | 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72, |
| 363 | 0x1b, 0x01, // SignatureType |
| 364 | 0x01, // Sha256WithRsa |
| 365 | }; |
Davide Pesavento | fbea4fc | 2022-02-08 07:26:04 -0500 | [diff] [blame] | 366 | Block errorBlock5(error5); |
Eric Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 367 | BOOST_CHECK_THROW(SignatureInfo(errorBlock5, SignatureInfo::Type::Interest), tlv::Error); |
| 368 | |
| 369 | // Repeated KeyLocator |
| 370 | const uint8_t error6[] = { |
| 371 | 0x2c, 0x33, // SignatureInfo |
| 372 | 0x1b, 0x01, // SignatureType |
| 373 | 0x01, // Sha256WithRsa |
| 374 | 0x1c, 0x16, // KeyLocator |
| 375 | 0x07, 0x14, // Name |
| 376 | 0x08, 0x04, |
| 377 | 0x74, 0x65, 0x73, 0x74, |
| 378 | 0x08, 0x03, |
| 379 | 0x6b, 0x65, 0x79, |
| 380 | 0x08, 0x07, |
| 381 | 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72, |
| 382 | 0x1c, 0x16, // KeyLocator |
| 383 | 0x07, 0x14, // Name |
| 384 | 0x08, 0x04, |
| 385 | 0x74, 0x65, 0x73, 0x74, |
| 386 | 0x08, 0x03, |
| 387 | 0x6b, 0x65, 0x79, |
| 388 | 0x08, 0x07, |
| 389 | 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72 |
| 390 | }; |
Davide Pesavento | fbea4fc | 2022-02-08 07:26:04 -0500 | [diff] [blame] | 391 | Block errorBlock6(error6); |
Eric Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 392 | BOOST_CHECK_THROW(SignatureInfo(errorBlock6, SignatureInfo::Type::Interest), tlv::Error); |
| 393 | |
| 394 | // Zero-length SignatureNonce |
| 395 | const uint8_t error7[] = { |
| 396 | 0x2c, 0x05, // SignatureInfo |
| 397 | 0x1b, 0x01, // SignatureType |
| 398 | 0x01, // Sha256WithRsa |
| 399 | 0x26, 0x00 // SignatureNonce |
| 400 | }; |
Davide Pesavento | fbea4fc | 2022-02-08 07:26:04 -0500 | [diff] [blame] | 401 | Block errorBlock7(error7); |
Eric Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 402 | BOOST_CHECK_THROW(SignatureInfo(errorBlock7, SignatureInfo::Type::Interest), tlv::Error); |
| 403 | } |
| 404 | |
Davide Pesavento | 14c56cd | 2020-05-21 01:44:03 -0400 | [diff] [blame] | 405 | BOOST_AUTO_TEST_CASE(ValidityPeriod) |
Yingdi Yu | 6be43f3 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 406 | { |
| 407 | const uint8_t sigInfo[] = { |
| 408 | 0x16, 0x45, // SignatureInfo |
| 409 | 0x1b, 0x01, // SignatureType |
| 410 | 0x01, // Sha256WithRsa |
| 411 | 0x1c, 0x16, // KeyLocator |
| 412 | 0x07, 0x14, // Name |
| 413 | 0x08, 0x04, |
| 414 | 0x74, 0x65, 0x73, 0x74, |
| 415 | 0x08, 0x03, |
| 416 | 0x6b, 0x65, 0x79, |
| 417 | 0x08, 0x07, |
| 418 | 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72, |
| 419 | 0xfd, 0x00, 0xfd, 0x26, // ValidityPeriod |
| 420 | 0xfd, 0x00, 0xfe, 0x0f, // NotBefore |
| 421 | 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x31, // 19700101T000000 |
| 422 | 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, |
| 423 | 0xfd, 0x00, 0xff, 0x0f, // NotAfter |
| 424 | 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x32, // 19700102T000000 |
| 425 | 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30 |
| 426 | }; |
| 427 | |
Davide Pesavento | 14c56cd | 2020-05-21 01:44:03 -0400 | [diff] [blame] | 428 | const auto notBefore = time::getUnixEpoch(); |
| 429 | const auto notAfter = notBefore + 1_day; |
Yingdi Yu | 6be43f3 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 430 | security::ValidityPeriod vp1(notBefore, notAfter); |
| 431 | |
Davide Pesavento | 14c56cd | 2020-05-21 01:44:03 -0400 | [diff] [blame] | 432 | SignatureInfo info(tlv::SignatureSha256WithRsa, KeyLocator("/test/key/locator")); |
Davide Pesavento | 14c56cd | 2020-05-21 01:44:03 -0400 | [diff] [blame] | 433 | BOOST_CHECK_THROW(info.getValidityPeriod(), SignatureInfo::Error); |
| 434 | |
| 435 | info.wireEncode(); |
Yingdi Yu | 6be43f3 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 436 | info.setValidityPeriod(vp1); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 437 | BOOST_CHECK_EQUAL(info.getValidityPeriod(), vp1); |
Davide Pesavento | 14c56cd | 2020-05-21 01:44:03 -0400 | [diff] [blame] | 438 | BOOST_CHECK_EQUAL(info.hasWire(), false); |
Yingdi Yu | 6be43f3 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 439 | |
Davide Pesavento | 14c56cd | 2020-05-21 01:44:03 -0400 | [diff] [blame] | 440 | // encode |
| 441 | auto encoded = info.wireEncode(); |
| 442 | BOOST_CHECK_EQUAL(info.hasWire(), true); |
Davide Pesavento | 258d51a | 2022-02-27 21:26:28 -0500 | [diff] [blame] | 443 | BOOST_TEST(encoded == sigInfo, boost::test_tools::per_element()); |
Yingdi Yu | 6be43f3 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 444 | |
Davide Pesavento | 14c56cd | 2020-05-21 01:44:03 -0400 | [diff] [blame] | 445 | info.setValidityPeriod(vp1); |
| 446 | BOOST_CHECK_EQUAL(info.hasWire(), true); |
| 447 | |
Yingdi Yu | 6be43f3 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 448 | // decode |
Davide Pesavento | fbea4fc | 2022-02-08 07:26:04 -0500 | [diff] [blame] | 449 | Block block(sigInfo); |
Eric Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 450 | SignatureInfo info2(block, SignatureInfo::Type::Data); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 451 | BOOST_CHECK_EQUAL(info2.getValidityPeriod(), vp1); |
Davide Pesavento | 14c56cd | 2020-05-21 01:44:03 -0400 | [diff] [blame] | 452 | BOOST_CHECK_EQUAL(info2.hasWire(), true); |
Yingdi Yu | 6be43f3 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 453 | |
Davide Pesavento | 14c56cd | 2020-05-21 01:44:03 -0400 | [diff] [blame] | 454 | info2.setValidityPeriod(security::ValidityPeriod(notBefore, notBefore + 42_days)); |
Davide Pesavento | 14c56cd | 2020-05-21 01:44:03 -0400 | [diff] [blame] | 455 | BOOST_CHECK_NE(info2.getValidityPeriod(), vp1); |
Davide Pesavento | 187e9f9 | 2023-03-20 22:46:22 -0400 | [diff] [blame] | 456 | BOOST_CHECK(info2.getValidityPeriod().getPeriod() == std::pair(notBefore, notBefore + 42_days)); |
Davide Pesavento | 14c56cd | 2020-05-21 01:44:03 -0400 | [diff] [blame] | 457 | BOOST_CHECK_EQUAL(info2.hasWire(), false); |
| 458 | |
| 459 | info2.wireEncode(); |
| 460 | BOOST_CHECK_EQUAL(info2.hasWire(), true); |
Davide Pesavento | f6b4589 | 2023-03-13 15:00:51 -0400 | [diff] [blame] | 461 | info2.setValidityPeriod(std::nullopt); |
Davide Pesavento | 14c56cd | 2020-05-21 01:44:03 -0400 | [diff] [blame] | 462 | BOOST_CHECK_THROW(info2.getValidityPeriod(), SignatureInfo::Error); |
| 463 | BOOST_CHECK_EQUAL(info2.hasWire(), false); |
Yingdi Yu | 6be43f3 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 464 | } |
| 465 | |
Eric Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 466 | BOOST_AUTO_TEST_CASE(CustomTlvs) |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 467 | { |
Davide Pesavento | 14c56cd | 2020-05-21 01:44:03 -0400 | [diff] [blame] | 468 | SignatureInfo info(tlv::SignatureSha256WithEcdsa); |
Eric Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 469 | info.addCustomTlv("810101"_block); |
| 470 | BOOST_CHECK(!info.getCustomTlv(0x82)); |
| 471 | BOOST_REQUIRE(info.getCustomTlv(0x81)); |
| 472 | BOOST_CHECK_EQUAL(info.getCustomTlv(0x81)->type(), 0x81); |
Davide Pesavento | 14c56cd | 2020-05-21 01:44:03 -0400 | [diff] [blame] | 473 | |
| 474 | info.wireEncode(); |
| 475 | BOOST_CHECK_EQUAL(info.hasWire(), true); |
Eric Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 476 | info.addCustomTlv("82020202"_block); |
Davide Pesavento | 14c56cd | 2020-05-21 01:44:03 -0400 | [diff] [blame] | 477 | BOOST_CHECK_EQUAL(info.hasWire(), false); |
Eric Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 478 | |
| 479 | info.wireEncode(); |
| 480 | BOOST_CHECK_EQUAL(info.hasWire(), true); |
| 481 | info.removeCustomTlv(0x81); |
| 482 | BOOST_CHECK_EQUAL(info.hasWire(), false); |
| 483 | BOOST_CHECK(!info.getCustomTlv(0x81)); |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 484 | } |
| 485 | |
Davide Pesavento | 0c52603 | 2024-01-31 21:14:01 -0500 | [diff] [blame] | 486 | BOOST_AUTO_TEST_CASE(CustomTlvsEncoding, |
| 487 | * ut::description("test for bug #3914")) |
Alexander Afanasyev | 4146975 | 2017-01-10 21:51:55 -0800 | [diff] [blame] | 488 | { |
Junxiao Shi | 605671d | 2017-08-26 13:41:06 +0000 | [diff] [blame] | 489 | SignatureInfo info1(tlv::SignatureSha256WithRsa); |
Davide Pesavento | a86300a | 2020-12-05 14:55:45 -0500 | [diff] [blame] | 490 | info1.addCustomTlv(makeStringBlock(102, "First")); |
| 491 | info1.addCustomTlv(makeStringBlock(104, "Second")); |
| 492 | info1.addCustomTlv(makeStringBlock(106, "Third")); |
Alexander Afanasyev | 4146975 | 2017-01-10 21:51:55 -0800 | [diff] [blame] | 493 | |
Davide Pesavento | a86300a | 2020-12-05 14:55:45 -0500 | [diff] [blame] | 494 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(info1), |
| 495 | "SignatureSha256WithRsa { 102 104 106 }"); |
Alexander Afanasyev | 4146975 | 2017-01-10 21:51:55 -0800 | [diff] [blame] | 496 | |
| 497 | SignatureInfo info2; |
Eric Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 498 | info2.wireDecode(info1.wireEncode(), SignatureInfo::Type::Data); |
Alexander Afanasyev | 4146975 | 2017-01-10 21:51:55 -0800 | [diff] [blame] | 499 | BOOST_CHECK_EQUAL(info1, info2); |
| 500 | |
Alexander Afanasyev | 4146975 | 2017-01-10 21:51:55 -0800 | [diff] [blame] | 501 | const uint8_t infoBytes[] = { |
Junxiao Shi | 605671d | 2017-08-26 13:41:06 +0000 | [diff] [blame] | 502 | 0x16, 0x19, // SignatureInfo |
| 503 | 0x1b, 0x01, 0x01, // SignatureType=1 |
Eric Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 504 | 0x66, 0x05, 0x46, 0x69, 0x72, 0x73, 0x74, // 102 "First" |
| 505 | 0x68, 0x06, 0x53, 0x65, 0x63, 0x6f, 0x6e, 0x64, // 104 "Second" |
| 506 | 0x6a, 0x05, 0x54, 0x68, 0x69, 0x72, 0x64 // 106 "Third" |
Alexander Afanasyev | 4146975 | 2017-01-10 21:51:55 -0800 | [diff] [blame] | 507 | }; |
| 508 | |
Davide Pesavento | fbea4fc | 2022-02-08 07:26:04 -0500 | [diff] [blame] | 509 | SignatureInfo info3(Block(infoBytes), SignatureInfo::Type::Data); |
Alexander Afanasyev | 4146975 | 2017-01-10 21:51:55 -0800 | [diff] [blame] | 510 | BOOST_CHECK_EQUAL(info3, info1); |
| 511 | BOOST_CHECK_EQUAL_COLLECTIONS(infoBytes, infoBytes + sizeof(infoBytes), |
| 512 | info1.wireEncode().begin(), info1.wireEncode().end()); |
| 513 | } |
| 514 | |
Eric Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 515 | BOOST_AUTO_TEST_CASE(OutputStream) |
| 516 | { |
| 517 | SignatureInfo info; |
| 518 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(info), "Invalid SignatureInfo"); |
| 519 | |
| 520 | info.setSignatureType(tlv::DigestSha256); |
| 521 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(info), "DigestSha256"); |
| 522 | |
| 523 | info.setSignatureType(tlv::SignatureSha256WithRsa); |
| 524 | info.setKeyLocator(KeyLocator("/test/key/locator")); |
| 525 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(info), |
| 526 | "SignatureSha256WithRsa Name=/test/key/locator"); |
| 527 | |
| 528 | info.setNonce(std::vector<uint8_t>{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}); |
| 529 | info.setTime(time::system_clock::time_point(1590169108480_ms)); |
| 530 | info.setSeqNum(0x1020); |
| 531 | |
| 532 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(info), |
| 533 | "SignatureSha256WithRsa Name=/test/key/locator " |
| 534 | "{ Nonce=0102030405060708 Time=1590169108480 SeqNum=4128 }"); |
| 535 | |
| 536 | info.setValidityPeriod(security::ValidityPeriod(time::getUnixEpoch(), time::getUnixEpoch() + 31_days)); |
| 537 | |
| 538 | info.addCustomTlv("82020102"_block); |
| 539 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(info), |
| 540 | "SignatureSha256WithRsa Name=/test/key/locator " |
| 541 | "{ Nonce=0102030405060708 Time=1590169108480 SeqNum=4128 " |
| 542 | "ValidityPeriod=(19700101T000000, 19700201T000000) 130 }"); |
| 543 | |
| 544 | info.addCustomTlv("84020102"_block); |
| 545 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(info), |
| 546 | "SignatureSha256WithRsa Name=/test/key/locator " |
| 547 | "{ Nonce=0102030405060708 Time=1590169108480 SeqNum=4128 " |
| 548 | "ValidityPeriod=(19700101T000000, 19700201T000000) 130 132 }"); |
| 549 | } |
| 550 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 551 | BOOST_AUTO_TEST_SUITE_END() // TestSignatureInfo |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 552 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 553 | } // namespace ndn::tests |