blob: 8fc459c0ee7b05518d88e441589e8b04aaae7163 [file] [log] [blame]
Yingdi Yu4a557052014-07-09 16:40:37 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi605671d2017-08-26 13:41:06 +00002/*
Davide Pesaventof6b45892023-03-13 15:00:51 -04003 * Copyright (c) 2013-2023 Regents of the University of California.
Yingdi Yu4a557052014-07-09 16:40:37 -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
Davide Pesavento7e780642018-11-24 15:51:34 -050022#include "ndn-cxx/signature-info.hpp"
Yingdi Yu4a557052014-07-09 16:40:37 -070023
Davide Pesavento7e780642018-11-24 15:51:34 -050024#include "tests/boost-test.hpp"
Davide Pesavento14c56cd2020-05-21 01:44:03 -040025
Alexander Afanasyev41469752017-01-10 21:51:55 -080026#include <boost/lexical_cast.hpp>
Yingdi Yu4a557052014-07-09 16:40:37 -070027
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040028namespace ndn::tests {
Yingdi Yu4a557052014-07-09 16:40:37 -070029
Davide Pesavento152ef442023-04-22 02:02:29 -040030BOOST_CONCEPT_ASSERT((boost::EqualityComparable<SignatureInfo>));
31BOOST_CONCEPT_ASSERT((WireEncodable<SignatureInfo>));
32BOOST_CONCEPT_ASSERT((WireEncodableWithEncodingBuffer<SignatureInfo>));
33BOOST_CONCEPT_ASSERT((WireDecodable<SignatureInfo>));
34static_assert(std::is_convertible_v<SignatureInfo::Error*, tlv::Error*>,
35 "SignatureInfo::Error must inherit from tlv::Error");
36
Yingdi Yu4a557052014-07-09 16:40:37 -070037BOOST_AUTO_TEST_SUITE(TestSignatureInfo)
38
Yingdi Yu4a557052014-07-09 16:40:37 -070039BOOST_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 Pesavento14c56cd2020-05-21 01:44:03 -040045 BOOST_CHECK_THROW(info.getValidityPeriod(), SignatureInfo::Error);
Eric Newberryf165bb42020-05-22 11:28:45 -070046 BOOST_CHECK(!info.getNonce());
47 BOOST_CHECK(!info.getTime());
48 BOOST_CHECK(!info.getSeqNum());
Davide Pesavento7e6f6f82017-10-31 18:05:28 -040049
Steve DiBenedetto54ce6682014-07-22 13:22:57 -060050 SignatureInfo sha256Info(tlv::DigestSha256);
51 BOOST_CHECK_EQUAL(sha256Info.getSignatureType(), tlv::DigestSha256);
Yingdi Yu4a557052014-07-09 16:40:37 -070052 BOOST_CHECK_THROW(sha256Info.getKeyLocator(), SignatureInfo::Error);
Eric Newberryf165bb42020-05-22 11:28:45 -070053 BOOST_CHECK(!info.getNonce());
54 BOOST_CHECK(!info.getTime());
55 BOOST_CHECK(!info.getSeqNum());
Alexander Afanasyev41469752017-01-10 21:51:55 -080056
Yingdi Yu4a557052014-07-09 16:40:37 -070057 KeyLocator keyLocator("/test/key/locator");
Steve DiBenedetto54ce6682014-07-22 13:22:57 -060058 SignatureInfo sha256RsaInfo(tlv::SignatureSha256WithRsa, keyLocator);
59 BOOST_CHECK_EQUAL(sha256RsaInfo.getSignatureType(), tlv::SignatureSha256WithRsa);
Yingdi Yu4a557052014-07-09 16:40:37 -070060 BOOST_CHECK_EQUAL(sha256RsaInfo.hasKeyLocator(), true);
Yingdi Yu4a557052014-07-09 16:40:37 -070061 BOOST_CHECK_EQUAL(sha256RsaInfo.getKeyLocator().getName(), Name("/test/key/locator"));
Davide Pesavento14c56cd2020-05-21 01:44:03 -040062 BOOST_CHECK_THROW(sha256RsaInfo.getValidityPeriod(), SignatureInfo::Error);
Eric Newberryf165bb42020-05-22 11:28:45 -070063 BOOST_CHECK(!info.getNonce());
64 BOOST_CHECK(!info.getTime());
65 BOOST_CHECK(!info.getSeqNum());
Yingdi Yu4a557052014-07-09 16:40:37 -070066}
67
Eric Newberryf165bb42020-05-22 11:28:45 -070068BOOST_AUTO_TEST_CASE(SignatureType)
Yingdi Yu4a557052014-07-09 16:40:37 -070069{
70 SignatureInfo info;
71 BOOST_CHECK_EQUAL(info.getSignatureType(), -1);
Davide Pesavento14c56cd2020-05-21 01:44:03 -040072 BOOST_CHECK_THROW(info.wireEncode(), SignatureInfo::Error);
Yingdi Yu4a557052014-07-09 16:40:37 -070073
Davide Pesavento14c56cd2020-05-21 01:44:03 -040074 info.setSignatureType(tlv::SignatureSha256WithEcdsa);
75 BOOST_CHECK_EQUAL(info.getSignatureType(), tlv::SignatureSha256WithEcdsa);
76 BOOST_CHECK_EQUAL(info.hasWire(), false);
Yingdi Yu4a557052014-07-09 16:40:37 -070077
Davide Pesavento14c56cd2020-05-21 01:44:03 -040078 info.wireEncode();
79 BOOST_CHECK_EQUAL(info.hasWire(), true);
80 info.setSignatureType(tlv::SignatureSha256WithEcdsa);
81 BOOST_CHECK_EQUAL(info.hasWire(), true);
Yingdi Yu4a557052014-07-09 16:40:37 -070082
Davide Pesavento14c56cd2020-05-21 01:44:03 -040083 info.setSignatureType(static_cast<tlv::SignatureTypeValue>(1234));
84 BOOST_CHECK_EQUAL(info.getSignatureType(), 1234);
85 BOOST_CHECK_EQUAL(info.hasWire(), false);
Yingdi Yu4a557052014-07-09 16:40:37 -070086}
87
Eric Newberryf165bb42020-05-22 11:28:45 -070088// We must name this test case differently to avoid naming conflicts
89BOOST_AUTO_TEST_CASE(KeyLocatorField)
Davide Pesavento14c56cd2020-05-21 01:44:03 -040090{
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 Pesaventof6b45892023-03-13 15:00:51 -0400109 info.setKeyLocator(std::nullopt);
Davide Pesavento14c56cd2020-05-21 01:44:03 -0400110 BOOST_CHECK_EQUAL(info.hasKeyLocator(), false);
111 BOOST_CHECK_THROW(info.getKeyLocator(), SignatureInfo::Error);
112 BOOST_CHECK_EQUAL(info.hasWire(), false);
113}
114
Eric Newberryf165bb42020-05-22 11:28:45 -0700115BOOST_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 Pesaventof6b45892023-03-13 15:00:51 -0400136 info.setNonce(std::nullopt);
Eric Newberryf165bb42020-05-22 11:28:45 -0700137 BOOST_CHECK_EQUAL(info.hasWire(), false);
138 BOOST_CHECK(!info.getNonce());
139}
140
141BOOST_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 Pesaventof6b45892023-03-13 15:00:51 -0400161 info.setTime(std::nullopt);
Eric Newberryf165bb42020-05-22 11:28:45 -0700162 BOOST_CHECK_EQUAL(info.hasWire(), false);
163 BOOST_CHECK(!info.getTime());
164}
165
166BOOST_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 Pesaventof6b45892023-03-13 15:00:51 -0400185 info.setSeqNum(std::nullopt);
Eric Newberryf165bb42020-05-22 11:28:45 -0700186 BOOST_CHECK_EQUAL(info.hasWire(), false);
187 BOOST_CHECK(!info.getSeqNum());
188}
189
Davide Pesaventoa86300a2020-12-05 14:55:45 -0500190const 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
204const 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
224const 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 Newberryf165bb42020-05-22 11:28:45 -0700238BOOST_AUTO_TEST_CASE(EncodeDecode)
239{
Davide Pesaventoa86300a2020-12-05 14:55:45 -0500240 const KeyLocator keyLocator("/test/key/locator");
241
242 // RSA
Eric Newberryf165bb42020-05-22 11:28:45 -0700243 SignatureInfo info(tlv::SignatureSha256WithRsa, keyLocator);
244
Davide Pesaventoa86300a2020-12-05 14:55:45 -0500245 // Encode as (Data)SignatureInfo
Eric Newberryf165bb42020-05-22 11:28:45 -0700246 auto encodedData = info.wireEncode(SignatureInfo::Type::Data);
Eric Newberryf165bb42020-05-22 11:28:45 -0700247
Davide Pesaventoa86300a2020-12-05 14:55:45 -0500248 BOOST_CHECK_EQUAL_COLLECTIONS(sigInfoDataRsa, sigInfoDataRsa + sizeof(sigInfoDataRsa),
249 encodedData.begin(), encodedData.end());
Eric Newberryf165bb42020-05-22 11:28:45 -0700250
Davide Pesaventoa86300a2020-12-05 14:55:45 -0500251 // Decode as (Data)SignatureInfo
Davide Pesaventofbea4fc2022-02-08 07:26:04 -0500252 info = SignatureInfo(Block(sigInfoDataRsa), SignatureInfo::Type::Data);
Davide Pesaventoa86300a2020-12-05 14:55:45 -0500253
Eric Newberryf165bb42020-05-22 11:28:45 -0700254 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 Newberryf165bb42020-05-22 11:28:45 -0700257 BOOST_CHECK(!info.getNonce());
258 BOOST_CHECK(!info.getTime());
259 BOOST_CHECK(!info.getSeqNum());
260
261 // Encode as InterestSignatureInfo
Davide Pesaventoa86300a2020-12-05 14:55:45 -0500262 const std::vector<uint8_t> nonce{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
Eric Newberryf165bb42020-05-22 11:28:45 -0700263 info.setNonce(nonce);
Davide Pesaventoa86300a2020-12-05 14:55:45 -0500264 const time::system_clock::time_point timePoint(1590169108480_ms);
Eric Newberryf165bb42020-05-22 11:28:45 -0700265 info.setTime(timePoint);
266 info.setSeqNum(0x1020);
267 auto encodedInterest = info.wireEncode(SignatureInfo::Type::Interest);
Eric Newberryf165bb42020-05-22 11:28:45 -0700268
Davide Pesaventoa86300a2020-12-05 14:55:45 -0500269 BOOST_CHECK_EQUAL_COLLECTIONS(sigInfoInterestRsa, sigInfoInterestRsa + sizeof(sigInfoInterestRsa),
270 encodedInterest.begin(), encodedInterest.end());
Eric Newberryf165bb42020-05-22 11:28:45 -0700271
272 // Decode as InterestSignatureInfo
Davide Pesaventofbea4fc2022-02-08 07:26:04 -0500273 info = SignatureInfo(Block(sigInfoInterestRsa), SignatureInfo::Type::Interest);
Davide Pesaventoa86300a2020-12-05 14:55:45 -0500274
Eric Newberryf165bb42020-05-22 11:28:45 -0700275 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 Pesaventoa86300a2020-12-05 14:55:45 -0500281
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 Pesaventofbea4fc2022-02-08 07:26:04 -0500292 info = SignatureInfo(Block(sigInfoDataEcdsa), SignatureInfo::Type::Data);
Davide Pesaventoa86300a2020-12-05 14:55:45 -0500293
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 Newberryf165bb42020-05-22 11:28:45 -0700300}
301
302BOOST_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 Pesaventofbea4fc2022-02-08 07:26:04 -0500317 Block errorBlock1(error1);
Eric Newberryf165bb42020-05-22 11:28:45 -0700318 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 Pesaventofbea4fc2022-02-08 07:26:04 -0500326 Block errorBlock2(error2);
Eric Newberryf165bb42020-05-22 11:28:45 -0700327 BOOST_CHECK_THROW(SignatureInfo(errorBlock2, SignatureInfo::Type::Data), tlv::Error);
328
329 const uint8_t error3[] = {
330 0x16, 0x00 // Empty SignatureInfo
331 };
Davide Pesaventofbea4fc2022-02-08 07:26:04 -0500332 Block errorBlock3(error3);
Eric Newberryf165bb42020-05-22 11:28:45 -0700333 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 Pesaventofbea4fc2022-02-08 07:26:04 -0500349 Block errorBlock4(error4);
Eric Newberryf165bb42020-05-22 11:28:45 -0700350 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 Pesaventofbea4fc2022-02-08 07:26:04 -0500366 Block errorBlock5(error5);
Eric Newberryf165bb42020-05-22 11:28:45 -0700367 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 Pesaventofbea4fc2022-02-08 07:26:04 -0500391 Block errorBlock6(error6);
Eric Newberryf165bb42020-05-22 11:28:45 -0700392 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 Pesaventofbea4fc2022-02-08 07:26:04 -0500401 Block errorBlock7(error7);
Eric Newberryf165bb42020-05-22 11:28:45 -0700402 BOOST_CHECK_THROW(SignatureInfo(errorBlock7, SignatureInfo::Type::Interest), tlv::Error);
403}
404
Davide Pesavento14c56cd2020-05-21 01:44:03 -0400405BOOST_AUTO_TEST_CASE(ValidityPeriod)
Yingdi Yu6be43f32015-06-09 14:19:54 -0700406{
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 Pesavento14c56cd2020-05-21 01:44:03 -0400428 const auto notBefore = time::getUnixEpoch();
429 const auto notAfter = notBefore + 1_day;
Yingdi Yu6be43f32015-06-09 14:19:54 -0700430 security::ValidityPeriod vp1(notBefore, notAfter);
431
Davide Pesavento14c56cd2020-05-21 01:44:03 -0400432 SignatureInfo info(tlv::SignatureSha256WithRsa, KeyLocator("/test/key/locator"));
Davide Pesavento14c56cd2020-05-21 01:44:03 -0400433 BOOST_CHECK_THROW(info.getValidityPeriod(), SignatureInfo::Error);
434
435 info.wireEncode();
Yingdi Yu6be43f32015-06-09 14:19:54 -0700436 info.setValidityPeriod(vp1);
Junxiao Shi72c0c642018-04-20 15:41:09 +0000437 BOOST_CHECK_EQUAL(info.getValidityPeriod(), vp1);
Davide Pesavento14c56cd2020-05-21 01:44:03 -0400438 BOOST_CHECK_EQUAL(info.hasWire(), false);
Yingdi Yu6be43f32015-06-09 14:19:54 -0700439
Davide Pesavento14c56cd2020-05-21 01:44:03 -0400440 // encode
441 auto encoded = info.wireEncode();
442 BOOST_CHECK_EQUAL(info.hasWire(), true);
Davide Pesavento258d51a2022-02-27 21:26:28 -0500443 BOOST_TEST(encoded == sigInfo, boost::test_tools::per_element());
Yingdi Yu6be43f32015-06-09 14:19:54 -0700444
Davide Pesavento14c56cd2020-05-21 01:44:03 -0400445 info.setValidityPeriod(vp1);
446 BOOST_CHECK_EQUAL(info.hasWire(), true);
447
Yingdi Yu6be43f32015-06-09 14:19:54 -0700448 // decode
Davide Pesaventofbea4fc2022-02-08 07:26:04 -0500449 Block block(sigInfo);
Eric Newberryf165bb42020-05-22 11:28:45 -0700450 SignatureInfo info2(block, SignatureInfo::Type::Data);
Junxiao Shi72c0c642018-04-20 15:41:09 +0000451 BOOST_CHECK_EQUAL(info2.getValidityPeriod(), vp1);
Davide Pesavento14c56cd2020-05-21 01:44:03 -0400452 BOOST_CHECK_EQUAL(info2.hasWire(), true);
Yingdi Yu6be43f32015-06-09 14:19:54 -0700453
Davide Pesavento14c56cd2020-05-21 01:44:03 -0400454 info2.setValidityPeriod(security::ValidityPeriod(notBefore, notBefore + 42_days));
Davide Pesavento14c56cd2020-05-21 01:44:03 -0400455 BOOST_CHECK_NE(info2.getValidityPeriod(), vp1);
Davide Pesavento187e9f92023-03-20 22:46:22 -0400456 BOOST_CHECK(info2.getValidityPeriod().getPeriod() == std::pair(notBefore, notBefore + 42_days));
Davide Pesavento14c56cd2020-05-21 01:44:03 -0400457 BOOST_CHECK_EQUAL(info2.hasWire(), false);
458
459 info2.wireEncode();
460 BOOST_CHECK_EQUAL(info2.hasWire(), true);
Davide Pesaventof6b45892023-03-13 15:00:51 -0400461 info2.setValidityPeriod(std::nullopt);
Davide Pesavento14c56cd2020-05-21 01:44:03 -0400462 BOOST_CHECK_THROW(info2.getValidityPeriod(), SignatureInfo::Error);
463 BOOST_CHECK_EQUAL(info2.hasWire(), false);
Yingdi Yu6be43f32015-06-09 14:19:54 -0700464}
465
Eric Newberryf165bb42020-05-22 11:28:45 -0700466BOOST_AUTO_TEST_CASE(CustomTlvs)
Yingdi Yu4a557052014-07-09 16:40:37 -0700467{
Davide Pesavento14c56cd2020-05-21 01:44:03 -0400468 SignatureInfo info(tlv::SignatureSha256WithEcdsa);
Eric Newberryf165bb42020-05-22 11:28:45 -0700469 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 Pesavento14c56cd2020-05-21 01:44:03 -0400473
474 info.wireEncode();
475 BOOST_CHECK_EQUAL(info.hasWire(), true);
Eric Newberryf165bb42020-05-22 11:28:45 -0700476 info.addCustomTlv("82020202"_block);
Davide Pesavento14c56cd2020-05-21 01:44:03 -0400477 BOOST_CHECK_EQUAL(info.hasWire(), false);
Eric Newberryf165bb42020-05-22 11:28:45 -0700478
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 Yu4a557052014-07-09 16:40:37 -0700484}
485
Eric Newberryf165bb42020-05-22 11:28:45 -0700486BOOST_AUTO_TEST_CASE(CustomTlvsEncoding) // Bug #3914
Alexander Afanasyev41469752017-01-10 21:51:55 -0800487{
Junxiao Shi605671d2017-08-26 13:41:06 +0000488 SignatureInfo info1(tlv::SignatureSha256WithRsa);
Davide Pesaventoa86300a2020-12-05 14:55:45 -0500489 info1.addCustomTlv(makeStringBlock(102, "First"));
490 info1.addCustomTlv(makeStringBlock(104, "Second"));
491 info1.addCustomTlv(makeStringBlock(106, "Third"));
Alexander Afanasyev41469752017-01-10 21:51:55 -0800492
Davide Pesaventoa86300a2020-12-05 14:55:45 -0500493 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(info1),
494 "SignatureSha256WithRsa { 102 104 106 }");
Alexander Afanasyev41469752017-01-10 21:51:55 -0800495
496 SignatureInfo info2;
Eric Newberryf165bb42020-05-22 11:28:45 -0700497 info2.wireDecode(info1.wireEncode(), SignatureInfo::Type::Data);
Alexander Afanasyev41469752017-01-10 21:51:55 -0800498 BOOST_CHECK_EQUAL(info1, info2);
499
Alexander Afanasyev41469752017-01-10 21:51:55 -0800500 const uint8_t infoBytes[] = {
Junxiao Shi605671d2017-08-26 13:41:06 +0000501 0x16, 0x19, // SignatureInfo
502 0x1b, 0x01, 0x01, // SignatureType=1
Eric Newberryf165bb42020-05-22 11:28:45 -0700503 0x66, 0x05, 0x46, 0x69, 0x72, 0x73, 0x74, // 102 "First"
504 0x68, 0x06, 0x53, 0x65, 0x63, 0x6f, 0x6e, 0x64, // 104 "Second"
505 0x6a, 0x05, 0x54, 0x68, 0x69, 0x72, 0x64 // 106 "Third"
Alexander Afanasyev41469752017-01-10 21:51:55 -0800506 };
507
Davide Pesaventofbea4fc2022-02-08 07:26:04 -0500508 SignatureInfo info3(Block(infoBytes), SignatureInfo::Type::Data);
Alexander Afanasyev41469752017-01-10 21:51:55 -0800509 BOOST_CHECK_EQUAL(info3, info1);
510 BOOST_CHECK_EQUAL_COLLECTIONS(infoBytes, infoBytes + sizeof(infoBytes),
511 info1.wireEncode().begin(), info1.wireEncode().end());
512}
513
Eric Newberryf165bb42020-05-22 11:28:45 -0700514BOOST_AUTO_TEST_CASE(OutputStream)
515{
516 SignatureInfo info;
517 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(info), "Invalid SignatureInfo");
518
519 info.setSignatureType(tlv::DigestSha256);
520 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(info), "DigestSha256");
521
522 info.setSignatureType(tlv::SignatureSha256WithRsa);
523 info.setKeyLocator(KeyLocator("/test/key/locator"));
524 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(info),
525 "SignatureSha256WithRsa Name=/test/key/locator");
526
527 info.setNonce(std::vector<uint8_t>{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08});
528 info.setTime(time::system_clock::time_point(1590169108480_ms));
529 info.setSeqNum(0x1020);
530
531 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(info),
532 "SignatureSha256WithRsa Name=/test/key/locator "
533 "{ Nonce=0102030405060708 Time=1590169108480 SeqNum=4128 }");
534
535 info.setValidityPeriod(security::ValidityPeriod(time::getUnixEpoch(), time::getUnixEpoch() + 31_days));
536
537 info.addCustomTlv("82020102"_block);
538 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(info),
539 "SignatureSha256WithRsa Name=/test/key/locator "
540 "{ Nonce=0102030405060708 Time=1590169108480 SeqNum=4128 "
541 "ValidityPeriod=(19700101T000000, 19700201T000000) 130 }");
542
543 info.addCustomTlv("84020102"_block);
544 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(info),
545 "SignatureSha256WithRsa Name=/test/key/locator "
546 "{ Nonce=0102030405060708 Time=1590169108480 SeqNum=4128 "
547 "ValidityPeriod=(19700101T000000, 19700201T000000) 130 132 }");
548}
549
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100550BOOST_AUTO_TEST_SUITE_END() // TestSignatureInfo
Yingdi Yu4a557052014-07-09 16:40:37 -0700551
Davide Pesavento47ce2ee2023-05-09 01:33:33 -0400552} // namespace ndn::tests