blob: d5382173b166dd197a28154406e8e7ffb5104284 [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
28namespace ndn {
Spyridon Mastorakis429634f2015-02-19 17:35:33 -080029namespace tests {
Yingdi Yu4a557052014-07-09 16:40:37 -070030
Davide Pesavento152ef442023-04-22 02:02:29 -040031BOOST_CONCEPT_ASSERT((boost::EqualityComparable<SignatureInfo>));
32BOOST_CONCEPT_ASSERT((WireEncodable<SignatureInfo>));
33BOOST_CONCEPT_ASSERT((WireEncodableWithEncodingBuffer<SignatureInfo>));
34BOOST_CONCEPT_ASSERT((WireDecodable<SignatureInfo>));
35static_assert(std::is_convertible_v<SignatureInfo::Error*, tlv::Error*>,
36 "SignatureInfo::Error must inherit from tlv::Error");
37
Yingdi Yu4a557052014-07-09 16:40:37 -070038BOOST_AUTO_TEST_SUITE(TestSignatureInfo)
39
Yingdi Yu4a557052014-07-09 16:40:37 -070040BOOST_AUTO_TEST_CASE(Constructor)
41{
42 SignatureInfo info;
43 BOOST_CHECK_EQUAL(info.getSignatureType(), -1);
44 BOOST_CHECK_EQUAL(info.hasKeyLocator(), false);
45 BOOST_CHECK_THROW(info.getKeyLocator(), SignatureInfo::Error);
Davide Pesavento14c56cd2020-05-21 01:44:03 -040046 BOOST_CHECK_THROW(info.getValidityPeriod(), SignatureInfo::Error);
Eric Newberryf165bb42020-05-22 11:28:45 -070047 BOOST_CHECK(!info.getNonce());
48 BOOST_CHECK(!info.getTime());
49 BOOST_CHECK(!info.getSeqNum());
Davide Pesavento7e6f6f82017-10-31 18:05:28 -040050
Steve DiBenedetto54ce6682014-07-22 13:22:57 -060051 SignatureInfo sha256Info(tlv::DigestSha256);
52 BOOST_CHECK_EQUAL(sha256Info.getSignatureType(), tlv::DigestSha256);
Yingdi Yu4a557052014-07-09 16:40:37 -070053 BOOST_CHECK_THROW(sha256Info.getKeyLocator(), SignatureInfo::Error);
Eric Newberryf165bb42020-05-22 11:28:45 -070054 BOOST_CHECK(!info.getNonce());
55 BOOST_CHECK(!info.getTime());
56 BOOST_CHECK(!info.getSeqNum());
Alexander Afanasyev41469752017-01-10 21:51:55 -080057
Yingdi Yu4a557052014-07-09 16:40:37 -070058 KeyLocator keyLocator("/test/key/locator");
Steve DiBenedetto54ce6682014-07-22 13:22:57 -060059 SignatureInfo sha256RsaInfo(tlv::SignatureSha256WithRsa, keyLocator);
60 BOOST_CHECK_EQUAL(sha256RsaInfo.getSignatureType(), tlv::SignatureSha256WithRsa);
Yingdi Yu4a557052014-07-09 16:40:37 -070061 BOOST_CHECK_EQUAL(sha256RsaInfo.hasKeyLocator(), true);
Yingdi Yu4a557052014-07-09 16:40:37 -070062 BOOST_CHECK_EQUAL(sha256RsaInfo.getKeyLocator().getName(), Name("/test/key/locator"));
Davide Pesavento14c56cd2020-05-21 01:44:03 -040063 BOOST_CHECK_THROW(sha256RsaInfo.getValidityPeriod(), SignatureInfo::Error);
Eric Newberryf165bb42020-05-22 11:28:45 -070064 BOOST_CHECK(!info.getNonce());
65 BOOST_CHECK(!info.getTime());
66 BOOST_CHECK(!info.getSeqNum());
Yingdi Yu4a557052014-07-09 16:40:37 -070067}
68
Eric Newberryf165bb42020-05-22 11:28:45 -070069BOOST_AUTO_TEST_CASE(SignatureType)
Yingdi Yu4a557052014-07-09 16:40:37 -070070{
71 SignatureInfo info;
72 BOOST_CHECK_EQUAL(info.getSignatureType(), -1);
Davide Pesavento14c56cd2020-05-21 01:44:03 -040073 BOOST_CHECK_THROW(info.wireEncode(), SignatureInfo::Error);
Yingdi Yu4a557052014-07-09 16:40:37 -070074
Davide Pesavento14c56cd2020-05-21 01:44:03 -040075 info.setSignatureType(tlv::SignatureSha256WithEcdsa);
76 BOOST_CHECK_EQUAL(info.getSignatureType(), tlv::SignatureSha256WithEcdsa);
77 BOOST_CHECK_EQUAL(info.hasWire(), false);
Yingdi Yu4a557052014-07-09 16:40:37 -070078
Davide Pesavento14c56cd2020-05-21 01:44:03 -040079 info.wireEncode();
80 BOOST_CHECK_EQUAL(info.hasWire(), true);
81 info.setSignatureType(tlv::SignatureSha256WithEcdsa);
82 BOOST_CHECK_EQUAL(info.hasWire(), true);
Yingdi Yu4a557052014-07-09 16:40:37 -070083
Davide Pesavento14c56cd2020-05-21 01:44:03 -040084 info.setSignatureType(static_cast<tlv::SignatureTypeValue>(1234));
85 BOOST_CHECK_EQUAL(info.getSignatureType(), 1234);
86 BOOST_CHECK_EQUAL(info.hasWire(), false);
Yingdi Yu4a557052014-07-09 16:40:37 -070087}
88
Eric Newberryf165bb42020-05-22 11:28:45 -070089// We must name this test case differently to avoid naming conflicts
90BOOST_AUTO_TEST_CASE(KeyLocatorField)
Davide Pesavento14c56cd2020-05-21 01:44:03 -040091{
92 SignatureInfo info(tlv::SignatureSha256WithEcdsa);
93 BOOST_CHECK_EQUAL(info.hasKeyLocator(), false);
94 BOOST_CHECK_THROW(info.getKeyLocator(), SignatureInfo::Error);
95
96 info.setKeyLocator(Name("/test/key/locator"));
97 BOOST_CHECK_EQUAL(info.hasKeyLocator(), true);
98 BOOST_CHECK_EQUAL(info.getKeyLocator().getName(), "/test/key/locator");
99 BOOST_CHECK_EQUAL(info.hasWire(), false);
100
101 info.wireEncode();
102 BOOST_CHECK_EQUAL(info.hasWire(), true);
103 info.setKeyLocator(Name("/test/key/locator"));
104 BOOST_CHECK_EQUAL(info.hasWire(), true);
105 info.setKeyLocator(Name("/another/key/locator"));
106 BOOST_CHECK_EQUAL(info.hasWire(), false);
107
108 info.wireEncode();
109 BOOST_CHECK_EQUAL(info.hasWire(), true);
Davide Pesaventof6b45892023-03-13 15:00:51 -0400110 info.setKeyLocator(std::nullopt);
Davide Pesavento14c56cd2020-05-21 01:44:03 -0400111 BOOST_CHECK_EQUAL(info.hasKeyLocator(), false);
112 BOOST_CHECK_THROW(info.getKeyLocator(), SignatureInfo::Error);
113 BOOST_CHECK_EQUAL(info.hasWire(), false);
114}
115
Eric Newberryf165bb42020-05-22 11:28:45 -0700116BOOST_AUTO_TEST_CASE(SignatureNonce)
117{
118 SignatureInfo info(tlv::SignatureSha256WithEcdsa);
119 BOOST_CHECK(!info.getNonce());
120 info.wireEncode();
121 BOOST_CHECK_EQUAL(info.hasWire(), true);
122 std::vector<uint8_t> nonce{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
123 info.setNonce(nonce);
124 BOOST_CHECK_EQUAL(info.hasWire(), false);
125 BOOST_CHECK(info.getNonce() == nonce);
126
127 info.wireEncode();
128 info.setNonce(nonce);
129 BOOST_CHECK_EQUAL(info.hasWire(), true);
130 nonce[2] = 0xFF;
131 info.setNonce(nonce);
132 BOOST_CHECK_EQUAL(info.hasWire(), false);
133 BOOST_CHECK(info.getNonce() == nonce);
134
135 info.wireEncode();
136 BOOST_CHECK_EQUAL(info.hasWire(), true);
Davide Pesaventof6b45892023-03-13 15:00:51 -0400137 info.setNonce(std::nullopt);
Eric Newberryf165bb42020-05-22 11:28:45 -0700138 BOOST_CHECK_EQUAL(info.hasWire(), false);
139 BOOST_CHECK(!info.getNonce());
140}
141
142BOOST_AUTO_TEST_CASE(SignatureTime)
143{
144 SignatureInfo info(tlv::SignatureSha256WithEcdsa);
145 BOOST_CHECK(!info.getTime());
146 info.wireEncode();
147 BOOST_CHECK_EQUAL(info.hasWire(), true);
148 time::system_clock::time_point timePoint(1590169108480_ms);
149 info.setTime(timePoint);
150 BOOST_CHECK_EQUAL(info.hasWire(), false);
151 BOOST_CHECK(info.getTime() == timePoint);
152
153 info.wireEncode();
154 info.setTime(timePoint);
155 BOOST_CHECK_EQUAL(info.hasWire(), true);
156 info.setTime(timePoint + 2_s);
157 BOOST_CHECK_EQUAL(info.hasWire(), false);
158 BOOST_CHECK(info.getTime() == timePoint + 2_s);
159
160 info.wireEncode();
161 BOOST_CHECK_EQUAL(info.hasWire(), true);
Davide Pesaventof6b45892023-03-13 15:00:51 -0400162 info.setTime(std::nullopt);
Eric Newberryf165bb42020-05-22 11:28:45 -0700163 BOOST_CHECK_EQUAL(info.hasWire(), false);
164 BOOST_CHECK(!info.getTime());
165}
166
167BOOST_AUTO_TEST_CASE(SignatureSeqNum)
168{
169 SignatureInfo info(tlv::SignatureSha256WithEcdsa);
170 BOOST_CHECK(!info.getSeqNum());
171 info.wireEncode();
172 BOOST_CHECK_EQUAL(info.hasWire(), true);
173 info.setSeqNum(256);
174 BOOST_CHECK_EQUAL(info.hasWire(), false);
175 BOOST_CHECK(info.getSeqNum() == 256UL);
176
177 info.wireEncode();
178 info.setSeqNum(256);
179 BOOST_CHECK_EQUAL(info.hasWire(), true);
180 info.setSeqNum(512);
181 BOOST_CHECK_EQUAL(info.hasWire(), false);
182 BOOST_CHECK(info.getSeqNum() == 512UL);
183
184 info.wireEncode();
185 BOOST_CHECK_EQUAL(info.hasWire(), true);
Davide Pesaventof6b45892023-03-13 15:00:51 -0400186 info.setSeqNum(std::nullopt);
Eric Newberryf165bb42020-05-22 11:28:45 -0700187 BOOST_CHECK_EQUAL(info.hasWire(), false);
188 BOOST_CHECK(!info.getSeqNum());
189}
190
Davide Pesaventoa86300a2020-12-05 14:55:45 -0500191const uint8_t sigInfoDataRsa[] = {
192 0x16, 0x1b, // SignatureInfo
193 0x1b, 0x01, // SignatureType
194 0x01, // Sha256WithRsa
195 0x1c, 0x16, // KeyLocator
196 0x07, 0x14, // Name
197 0x08, 0x04,
198 0x74, 0x65, 0x73, 0x74,
199 0x08, 0x03,
200 0x6b, 0x65, 0x79,
201 0x08, 0x07,
202 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72,
203};
204
205const uint8_t sigInfoInterestRsa[] = {
206 0x2c, 0x33, // InterestSignatureInfo
207 0x1b, 0x01, // SignatureType
208 0x01, // Sha256WithRsa
209 0x1c, 0x16, // KeyLocator
210 0x07, 0x14, // Name
211 0x08, 0x04,
212 0x74, 0x65, 0x73, 0x74,
213 0x08, 0x03,
214 0x6b, 0x65, 0x79,
215 0x08, 0x07,
216 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72,
217 0x26, 0x08, // SignatureNonce
218 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
219 0x28, 0x08, // SignatureTime
220 0x00, 0x00, 0x01, 0x72, 0x3d, 0x77, 0x00, 0x00,
221 0x2a, 0x02, // SignatureSeqNum
222 0x10, 0x20,
223};
224
225const uint8_t sigInfoDataEcdsa[] = {
226 0x16, 0x1b, // SignatureInfo
227 0x1b, 0x01, // SignatureType
228 0x03, // Sha256WithEcdsa
229 0x1c, 0x16, // KeyLocator
230 0x07, 0x14, // Name
231 0x08, 0x04,
232 0x74, 0x65, 0x73, 0x74,
233 0x08, 0x03,
234 0x6b, 0x65, 0x79,
235 0x08, 0x07,
236 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72,
237};
238
Eric Newberryf165bb42020-05-22 11:28:45 -0700239BOOST_AUTO_TEST_CASE(EncodeDecode)
240{
Davide Pesaventoa86300a2020-12-05 14:55:45 -0500241 const KeyLocator keyLocator("/test/key/locator");
242
243 // RSA
Eric Newberryf165bb42020-05-22 11:28:45 -0700244 SignatureInfo info(tlv::SignatureSha256WithRsa, keyLocator);
245
Davide Pesaventoa86300a2020-12-05 14:55:45 -0500246 // Encode as (Data)SignatureInfo
Eric Newberryf165bb42020-05-22 11:28:45 -0700247 auto encodedData = info.wireEncode(SignatureInfo::Type::Data);
Eric Newberryf165bb42020-05-22 11:28:45 -0700248
Davide Pesaventoa86300a2020-12-05 14:55:45 -0500249 BOOST_CHECK_EQUAL_COLLECTIONS(sigInfoDataRsa, sigInfoDataRsa + sizeof(sigInfoDataRsa),
250 encodedData.begin(), encodedData.end());
Eric Newberryf165bb42020-05-22 11:28:45 -0700251
Davide Pesaventoa86300a2020-12-05 14:55:45 -0500252 // Decode as (Data)SignatureInfo
Davide Pesaventofbea4fc2022-02-08 07:26:04 -0500253 info = SignatureInfo(Block(sigInfoDataRsa), SignatureInfo::Type::Data);
Davide Pesaventoa86300a2020-12-05 14:55:45 -0500254
Eric Newberryf165bb42020-05-22 11:28:45 -0700255 BOOST_CHECK_EQUAL(info.getSignatureType(), tlv::SignatureSha256WithRsa);
256 BOOST_CHECK_EQUAL(info.hasKeyLocator(), true);
257 BOOST_CHECK_EQUAL(info.getKeyLocator().getName(), Name("/test/key/locator"));
Eric Newberryf165bb42020-05-22 11:28:45 -0700258 BOOST_CHECK(!info.getNonce());
259 BOOST_CHECK(!info.getTime());
260 BOOST_CHECK(!info.getSeqNum());
261
262 // Encode as InterestSignatureInfo
Davide Pesaventoa86300a2020-12-05 14:55:45 -0500263 const std::vector<uint8_t> nonce{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
Eric Newberryf165bb42020-05-22 11:28:45 -0700264 info.setNonce(nonce);
Davide Pesaventoa86300a2020-12-05 14:55:45 -0500265 const time::system_clock::time_point timePoint(1590169108480_ms);
Eric Newberryf165bb42020-05-22 11:28:45 -0700266 info.setTime(timePoint);
267 info.setSeqNum(0x1020);
268 auto encodedInterest = info.wireEncode(SignatureInfo::Type::Interest);
Eric Newberryf165bb42020-05-22 11:28:45 -0700269
Davide Pesaventoa86300a2020-12-05 14:55:45 -0500270 BOOST_CHECK_EQUAL_COLLECTIONS(sigInfoInterestRsa, sigInfoInterestRsa + sizeof(sigInfoInterestRsa),
271 encodedInterest.begin(), encodedInterest.end());
Eric Newberryf165bb42020-05-22 11:28:45 -0700272
273 // Decode as InterestSignatureInfo
Davide Pesaventofbea4fc2022-02-08 07:26:04 -0500274 info = SignatureInfo(Block(sigInfoInterestRsa), SignatureInfo::Type::Interest);
Davide Pesaventoa86300a2020-12-05 14:55:45 -0500275
Eric Newberryf165bb42020-05-22 11:28:45 -0700276 BOOST_CHECK_EQUAL(info.getSignatureType(), tlv::SignatureSha256WithRsa);
277 BOOST_CHECK_EQUAL(info.hasKeyLocator(), true);
278 BOOST_CHECK_EQUAL(info.getKeyLocator().getName(), Name("/test/key/locator"));
279 BOOST_CHECK(info.getNonce() == nonce);
280 BOOST_CHECK(info.getTime() == timePoint);
281 BOOST_CHECK(info.getSeqNum() == 0x1020UL);
Davide Pesaventoa86300a2020-12-05 14:55:45 -0500282
283 // ECDSA
284 info = SignatureInfo(tlv::SignatureSha256WithEcdsa, keyLocator);
285
286 // Encode as (Data)SignatureInfo
287 auto encodedDataEcdsa = info.wireEncode(SignatureInfo::Type::Data);
288
289 BOOST_CHECK_EQUAL_COLLECTIONS(sigInfoDataEcdsa, sigInfoDataEcdsa + sizeof(sigInfoDataEcdsa),
290 encodedDataEcdsa.begin(), encodedDataEcdsa.end());
291
292 // Decode as (Data)SignatureInfo
Davide Pesaventofbea4fc2022-02-08 07:26:04 -0500293 info = SignatureInfo(Block(sigInfoDataEcdsa), SignatureInfo::Type::Data);
Davide Pesaventoa86300a2020-12-05 14:55:45 -0500294
295 BOOST_CHECK_EQUAL(info.getSignatureType(), tlv::SignatureSha256WithEcdsa);
296 BOOST_CHECK_EQUAL(info.hasKeyLocator(), true);
297 BOOST_CHECK_EQUAL(info.getKeyLocator().getName(), Name("/test/key/locator"));
298 BOOST_CHECK(!info.getNonce());
299 BOOST_CHECK(!info.getTime());
300 BOOST_CHECK(!info.getSeqNum());
Eric Newberryf165bb42020-05-22 11:28:45 -0700301}
302
303BOOST_AUTO_TEST_CASE(DecodeError)
304{
305 const uint8_t error1[] = {
306 0x15, 0x1b, // Wrong SignatureInfo (0x16, 0x1b)
307 0x1b, 0x01, // SignatureType
308 0x01, // Sha256WithRsa
309 0x1c, 0x16, // KeyLocator
310 0x07, 0x14, // Name
311 0x08, 0x04,
312 0x74, 0x65, 0x73, 0x74,
313 0x08, 0x03,
314 0x6b, 0x65, 0x79,
315 0x08, 0x07,
316 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72
317 };
Davide Pesaventofbea4fc2022-02-08 07:26:04 -0500318 Block errorBlock1(error1);
Eric Newberryf165bb42020-05-22 11:28:45 -0700319 BOOST_CHECK_THROW(SignatureInfo(errorBlock1, SignatureInfo::Type::Data), tlv::Error);
320
321 const uint8_t error2[] = {
322 0x16, 0x05, // SignatureInfo
323 0x1b, 0x01, // SignatureType
324 0x01, // Sha256WithRsa
325 0x83, 0x00, // Unrecognized critical TLV
326 };
Davide Pesaventofbea4fc2022-02-08 07:26:04 -0500327 Block errorBlock2(error2);
Eric Newberryf165bb42020-05-22 11:28:45 -0700328 BOOST_CHECK_THROW(SignatureInfo(errorBlock2, SignatureInfo::Type::Data), tlv::Error);
329
330 const uint8_t error3[] = {
331 0x16, 0x00 // Empty SignatureInfo
332 };
Davide Pesaventofbea4fc2022-02-08 07:26:04 -0500333 Block errorBlock3(error3);
Eric Newberryf165bb42020-05-22 11:28:45 -0700334 BOOST_CHECK_THROW(SignatureInfo(errorBlock3, SignatureInfo::Type::Data), tlv::Error);
335
336 // Encoding is correct for SignatureInfo, but decoder is expecting InterestSignatureInfo
337 const uint8_t error4[] = {
338 0x16, 0x1b, // SignatureInfo
339 0x1b, 0x01, // SignatureType
340 0x01, // Sha256WithRsa
341 0x1c, 0x16, // KeyLocator
342 0x07, 0x14, // Name
343 0x08, 0x04,
344 0x74, 0x65, 0x73, 0x74,
345 0x08, 0x03,
346 0x6b, 0x65, 0x79,
347 0x08, 0x07,
348 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72
349 };
Davide Pesaventofbea4fc2022-02-08 07:26:04 -0500350 Block errorBlock4(error4);
Eric Newberryf165bb42020-05-22 11:28:45 -0700351 BOOST_CHECK_THROW(SignatureInfo(errorBlock4, SignatureInfo::Type::Interest), tlv::Error);
352
353 // SignatureType and KeyLocator out-of-order
354 const uint8_t error5[] = {
355 0x2c, 0x1b, // SignatureInfo
356 0x1c, 0x16, // KeyLocator
357 0x07, 0x14, // Name
358 0x08, 0x04,
359 0x74, 0x65, 0x73, 0x74,
360 0x08, 0x03,
361 0x6b, 0x65, 0x79,
362 0x08, 0x07,
363 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72,
364 0x1b, 0x01, // SignatureType
365 0x01, // Sha256WithRsa
366 };
Davide Pesaventofbea4fc2022-02-08 07:26:04 -0500367 Block errorBlock5(error5);
Eric Newberryf165bb42020-05-22 11:28:45 -0700368 BOOST_CHECK_THROW(SignatureInfo(errorBlock5, SignatureInfo::Type::Interest), tlv::Error);
369
370 // Repeated KeyLocator
371 const uint8_t error6[] = {
372 0x2c, 0x33, // SignatureInfo
373 0x1b, 0x01, // SignatureType
374 0x01, // Sha256WithRsa
375 0x1c, 0x16, // KeyLocator
376 0x07, 0x14, // Name
377 0x08, 0x04,
378 0x74, 0x65, 0x73, 0x74,
379 0x08, 0x03,
380 0x6b, 0x65, 0x79,
381 0x08, 0x07,
382 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72,
383 0x1c, 0x16, // KeyLocator
384 0x07, 0x14, // Name
385 0x08, 0x04,
386 0x74, 0x65, 0x73, 0x74,
387 0x08, 0x03,
388 0x6b, 0x65, 0x79,
389 0x08, 0x07,
390 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72
391 };
Davide Pesaventofbea4fc2022-02-08 07:26:04 -0500392 Block errorBlock6(error6);
Eric Newberryf165bb42020-05-22 11:28:45 -0700393 BOOST_CHECK_THROW(SignatureInfo(errorBlock6, SignatureInfo::Type::Interest), tlv::Error);
394
395 // Zero-length SignatureNonce
396 const uint8_t error7[] = {
397 0x2c, 0x05, // SignatureInfo
398 0x1b, 0x01, // SignatureType
399 0x01, // Sha256WithRsa
400 0x26, 0x00 // SignatureNonce
401 };
Davide Pesaventofbea4fc2022-02-08 07:26:04 -0500402 Block errorBlock7(error7);
Eric Newberryf165bb42020-05-22 11:28:45 -0700403 BOOST_CHECK_THROW(SignatureInfo(errorBlock7, SignatureInfo::Type::Interest), tlv::Error);
404}
405
Davide Pesavento14c56cd2020-05-21 01:44:03 -0400406BOOST_AUTO_TEST_CASE(ValidityPeriod)
Yingdi Yu6be43f32015-06-09 14:19:54 -0700407{
408 const uint8_t sigInfo[] = {
409 0x16, 0x45, // SignatureInfo
410 0x1b, 0x01, // SignatureType
411 0x01, // Sha256WithRsa
412 0x1c, 0x16, // KeyLocator
413 0x07, 0x14, // Name
414 0x08, 0x04,
415 0x74, 0x65, 0x73, 0x74,
416 0x08, 0x03,
417 0x6b, 0x65, 0x79,
418 0x08, 0x07,
419 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72,
420 0xfd, 0x00, 0xfd, 0x26, // ValidityPeriod
421 0xfd, 0x00, 0xfe, 0x0f, // NotBefore
422 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x31, // 19700101T000000
423 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
424 0xfd, 0x00, 0xff, 0x0f, // NotAfter
425 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x32, // 19700102T000000
426 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30
427 };
428
Davide Pesavento14c56cd2020-05-21 01:44:03 -0400429 const auto notBefore = time::getUnixEpoch();
430 const auto notAfter = notBefore + 1_day;
Yingdi Yu6be43f32015-06-09 14:19:54 -0700431 security::ValidityPeriod vp1(notBefore, notAfter);
432
Davide Pesavento14c56cd2020-05-21 01:44:03 -0400433 SignatureInfo info(tlv::SignatureSha256WithRsa, KeyLocator("/test/key/locator"));
Davide Pesavento14c56cd2020-05-21 01:44:03 -0400434 BOOST_CHECK_THROW(info.getValidityPeriod(), SignatureInfo::Error);
435
436 info.wireEncode();
Yingdi Yu6be43f32015-06-09 14:19:54 -0700437 info.setValidityPeriod(vp1);
Junxiao Shi72c0c642018-04-20 15:41:09 +0000438 BOOST_CHECK_EQUAL(info.getValidityPeriod(), vp1);
Davide Pesavento14c56cd2020-05-21 01:44:03 -0400439 BOOST_CHECK_EQUAL(info.hasWire(), false);
Yingdi Yu6be43f32015-06-09 14:19:54 -0700440
Davide Pesavento14c56cd2020-05-21 01:44:03 -0400441 // encode
442 auto encoded = info.wireEncode();
443 BOOST_CHECK_EQUAL(info.hasWire(), true);
Davide Pesavento258d51a2022-02-27 21:26:28 -0500444 BOOST_TEST(encoded == sigInfo, boost::test_tools::per_element());
Yingdi Yu6be43f32015-06-09 14:19:54 -0700445
Davide Pesavento14c56cd2020-05-21 01:44:03 -0400446 info.setValidityPeriod(vp1);
447 BOOST_CHECK_EQUAL(info.hasWire(), true);
448
Yingdi Yu6be43f32015-06-09 14:19:54 -0700449 // decode
Davide Pesaventofbea4fc2022-02-08 07:26:04 -0500450 Block block(sigInfo);
Eric Newberryf165bb42020-05-22 11:28:45 -0700451 SignatureInfo info2(block, SignatureInfo::Type::Data);
Junxiao Shi72c0c642018-04-20 15:41:09 +0000452 BOOST_CHECK_EQUAL(info2.getValidityPeriod(), vp1);
Davide Pesavento14c56cd2020-05-21 01:44:03 -0400453 BOOST_CHECK_EQUAL(info2.hasWire(), true);
Yingdi Yu6be43f32015-06-09 14:19:54 -0700454
Davide Pesavento14c56cd2020-05-21 01:44:03 -0400455 info2.setValidityPeriod(security::ValidityPeriod(notBefore, notBefore + 42_days));
Davide Pesavento14c56cd2020-05-21 01:44:03 -0400456 BOOST_CHECK_NE(info2.getValidityPeriod(), vp1);
Davide Pesavento187e9f92023-03-20 22:46:22 -0400457 BOOST_CHECK(info2.getValidityPeriod().getPeriod() == std::pair(notBefore, notBefore + 42_days));
Davide Pesavento14c56cd2020-05-21 01:44:03 -0400458 BOOST_CHECK_EQUAL(info2.hasWire(), false);
459
460 info2.wireEncode();
461 BOOST_CHECK_EQUAL(info2.hasWire(), true);
Davide Pesaventof6b45892023-03-13 15:00:51 -0400462 info2.setValidityPeriod(std::nullopt);
Davide Pesavento14c56cd2020-05-21 01:44:03 -0400463 BOOST_CHECK_THROW(info2.getValidityPeriod(), SignatureInfo::Error);
464 BOOST_CHECK_EQUAL(info2.hasWire(), false);
Yingdi Yu6be43f32015-06-09 14:19:54 -0700465}
466
Eric Newberryf165bb42020-05-22 11:28:45 -0700467BOOST_AUTO_TEST_CASE(CustomTlvs)
Yingdi Yu4a557052014-07-09 16:40:37 -0700468{
Davide Pesavento14c56cd2020-05-21 01:44:03 -0400469 SignatureInfo info(tlv::SignatureSha256WithEcdsa);
Eric Newberryf165bb42020-05-22 11:28:45 -0700470 info.addCustomTlv("810101"_block);
471 BOOST_CHECK(!info.getCustomTlv(0x82));
472 BOOST_REQUIRE(info.getCustomTlv(0x81));
473 BOOST_CHECK_EQUAL(info.getCustomTlv(0x81)->type(), 0x81);
Davide Pesavento14c56cd2020-05-21 01:44:03 -0400474
475 info.wireEncode();
476 BOOST_CHECK_EQUAL(info.hasWire(), true);
Eric Newberryf165bb42020-05-22 11:28:45 -0700477 info.addCustomTlv("82020202"_block);
Davide Pesavento14c56cd2020-05-21 01:44:03 -0400478 BOOST_CHECK_EQUAL(info.hasWire(), false);
Eric Newberryf165bb42020-05-22 11:28:45 -0700479
480 info.wireEncode();
481 BOOST_CHECK_EQUAL(info.hasWire(), true);
482 info.removeCustomTlv(0x81);
483 BOOST_CHECK_EQUAL(info.hasWire(), false);
484 BOOST_CHECK(!info.getCustomTlv(0x81));
Yingdi Yu4a557052014-07-09 16:40:37 -0700485}
486
Eric Newberryf165bb42020-05-22 11:28:45 -0700487BOOST_AUTO_TEST_CASE(CustomTlvsEncoding) // Bug #3914
Alexander Afanasyev41469752017-01-10 21:51:55 -0800488{
Junxiao Shi605671d2017-08-26 13:41:06 +0000489 SignatureInfo info1(tlv::SignatureSha256WithRsa);
Davide Pesaventoa86300a2020-12-05 14:55:45 -0500490 info1.addCustomTlv(makeStringBlock(102, "First"));
491 info1.addCustomTlv(makeStringBlock(104, "Second"));
492 info1.addCustomTlv(makeStringBlock(106, "Third"));
Alexander Afanasyev41469752017-01-10 21:51:55 -0800493
Davide Pesaventoa86300a2020-12-05 14:55:45 -0500494 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(info1),
495 "SignatureSha256WithRsa { 102 104 106 }");
Alexander Afanasyev41469752017-01-10 21:51:55 -0800496
497 SignatureInfo info2;
Eric Newberryf165bb42020-05-22 11:28:45 -0700498 info2.wireDecode(info1.wireEncode(), SignatureInfo::Type::Data);
Alexander Afanasyev41469752017-01-10 21:51:55 -0800499 BOOST_CHECK_EQUAL(info1, info2);
500
Alexander Afanasyev41469752017-01-10 21:51:55 -0800501 const uint8_t infoBytes[] = {
Junxiao Shi605671d2017-08-26 13:41:06 +0000502 0x16, 0x19, // SignatureInfo
503 0x1b, 0x01, 0x01, // SignatureType=1
Eric Newberryf165bb42020-05-22 11:28:45 -0700504 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 Afanasyev41469752017-01-10 21:51:55 -0800507 };
508
Davide Pesaventofbea4fc2022-02-08 07:26:04 -0500509 SignatureInfo info3(Block(infoBytes), SignatureInfo::Type::Data);
Alexander Afanasyev41469752017-01-10 21:51:55 -0800510 BOOST_CHECK_EQUAL(info3, info1);
511 BOOST_CHECK_EQUAL_COLLECTIONS(infoBytes, infoBytes + sizeof(infoBytes),
512 info1.wireEncode().begin(), info1.wireEncode().end());
513}
514
Eric Newberryf165bb42020-05-22 11:28:45 -0700515BOOST_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 Pesaventoeee3e822016-11-26 19:19:34 +0100551BOOST_AUTO_TEST_SUITE_END() // TestSignatureInfo
Yingdi Yu4a557052014-07-09 16:40:37 -0700552
Spyridon Mastorakis429634f2015-02-19 17:35:33 -0800553} // namespace tests
Yingdi Yu4a557052014-07-09 16:40:37 -0700554} // namespace ndn