blob: 1257851aab8d38e20bce914326233951614a156b [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
31BOOST_AUTO_TEST_SUITE(TestSignatureInfo)
32
Yingdi Yu4a557052014-07-09 16:40:37 -070033BOOST_AUTO_TEST_CASE(Constructor)
34{
35 SignatureInfo info;
36 BOOST_CHECK_EQUAL(info.getSignatureType(), -1);
37 BOOST_CHECK_EQUAL(info.hasKeyLocator(), false);
38 BOOST_CHECK_THROW(info.getKeyLocator(), SignatureInfo::Error);
Davide Pesavento14c56cd2020-05-21 01:44:03 -040039 BOOST_CHECK_THROW(info.getValidityPeriod(), SignatureInfo::Error);
Eric Newberryf165bb42020-05-22 11:28:45 -070040 BOOST_CHECK(!info.getNonce());
41 BOOST_CHECK(!info.getTime());
42 BOOST_CHECK(!info.getSeqNum());
Davide Pesavento7e6f6f82017-10-31 18:05:28 -040043
Steve DiBenedetto54ce6682014-07-22 13:22:57 -060044 SignatureInfo sha256Info(tlv::DigestSha256);
45 BOOST_CHECK_EQUAL(sha256Info.getSignatureType(), tlv::DigestSha256);
Yingdi Yu4a557052014-07-09 16:40:37 -070046 BOOST_CHECK_THROW(sha256Info.getKeyLocator(), SignatureInfo::Error);
Eric Newberryf165bb42020-05-22 11:28:45 -070047 BOOST_CHECK(!info.getNonce());
48 BOOST_CHECK(!info.getTime());
49 BOOST_CHECK(!info.getSeqNum());
Alexander Afanasyev41469752017-01-10 21:51:55 -080050
Yingdi Yu4a557052014-07-09 16:40:37 -070051 KeyLocator keyLocator("/test/key/locator");
Steve DiBenedetto54ce6682014-07-22 13:22:57 -060052 SignatureInfo sha256RsaInfo(tlv::SignatureSha256WithRsa, keyLocator);
53 BOOST_CHECK_EQUAL(sha256RsaInfo.getSignatureType(), tlv::SignatureSha256WithRsa);
Yingdi Yu4a557052014-07-09 16:40:37 -070054 BOOST_CHECK_EQUAL(sha256RsaInfo.hasKeyLocator(), true);
Yingdi Yu4a557052014-07-09 16:40:37 -070055 BOOST_CHECK_EQUAL(sha256RsaInfo.getKeyLocator().getName(), Name("/test/key/locator"));
Davide Pesavento14c56cd2020-05-21 01:44:03 -040056 BOOST_CHECK_THROW(sha256RsaInfo.getValidityPeriod(), SignatureInfo::Error);
Eric Newberryf165bb42020-05-22 11:28:45 -070057 BOOST_CHECK(!info.getNonce());
58 BOOST_CHECK(!info.getTime());
59 BOOST_CHECK(!info.getSeqNum());
Yingdi Yu4a557052014-07-09 16:40:37 -070060}
61
Eric Newberryf165bb42020-05-22 11:28:45 -070062BOOST_AUTO_TEST_CASE(SignatureType)
Yingdi Yu4a557052014-07-09 16:40:37 -070063{
64 SignatureInfo info;
65 BOOST_CHECK_EQUAL(info.getSignatureType(), -1);
Davide Pesavento14c56cd2020-05-21 01:44:03 -040066 BOOST_CHECK_THROW(info.wireEncode(), SignatureInfo::Error);
Yingdi Yu4a557052014-07-09 16:40:37 -070067
Davide Pesavento14c56cd2020-05-21 01:44:03 -040068 info.setSignatureType(tlv::SignatureSha256WithEcdsa);
69 BOOST_CHECK_EQUAL(info.getSignatureType(), tlv::SignatureSha256WithEcdsa);
70 BOOST_CHECK_EQUAL(info.hasWire(), false);
Yingdi Yu4a557052014-07-09 16:40:37 -070071
Davide Pesavento14c56cd2020-05-21 01:44:03 -040072 info.wireEncode();
73 BOOST_CHECK_EQUAL(info.hasWire(), true);
74 info.setSignatureType(tlv::SignatureSha256WithEcdsa);
75 BOOST_CHECK_EQUAL(info.hasWire(), true);
Yingdi Yu4a557052014-07-09 16:40:37 -070076
Davide Pesavento14c56cd2020-05-21 01:44:03 -040077 info.setSignatureType(static_cast<tlv::SignatureTypeValue>(1234));
78 BOOST_CHECK_EQUAL(info.getSignatureType(), 1234);
79 BOOST_CHECK_EQUAL(info.hasWire(), false);
Yingdi Yu4a557052014-07-09 16:40:37 -070080}
81
Eric Newberryf165bb42020-05-22 11:28:45 -070082// We must name this test case differently to avoid naming conflicts
83BOOST_AUTO_TEST_CASE(KeyLocatorField)
Davide Pesavento14c56cd2020-05-21 01:44:03 -040084{
85 SignatureInfo info(tlv::SignatureSha256WithEcdsa);
86 BOOST_CHECK_EQUAL(info.hasKeyLocator(), false);
87 BOOST_CHECK_THROW(info.getKeyLocator(), SignatureInfo::Error);
88
89 info.setKeyLocator(Name("/test/key/locator"));
90 BOOST_CHECK_EQUAL(info.hasKeyLocator(), true);
91 BOOST_CHECK_EQUAL(info.getKeyLocator().getName(), "/test/key/locator");
92 BOOST_CHECK_EQUAL(info.hasWire(), false);
93
94 info.wireEncode();
95 BOOST_CHECK_EQUAL(info.hasWire(), true);
96 info.setKeyLocator(Name("/test/key/locator"));
97 BOOST_CHECK_EQUAL(info.hasWire(), true);
98 info.setKeyLocator(Name("/another/key/locator"));
99 BOOST_CHECK_EQUAL(info.hasWire(), false);
100
101 info.wireEncode();
102 BOOST_CHECK_EQUAL(info.hasWire(), true);
Davide Pesaventof6b45892023-03-13 15:00:51 -0400103 info.setKeyLocator(std::nullopt);
Davide Pesavento14c56cd2020-05-21 01:44:03 -0400104 BOOST_CHECK_EQUAL(info.hasKeyLocator(), false);
105 BOOST_CHECK_THROW(info.getKeyLocator(), SignatureInfo::Error);
106 BOOST_CHECK_EQUAL(info.hasWire(), false);
107}
108
Eric Newberryf165bb42020-05-22 11:28:45 -0700109BOOST_AUTO_TEST_CASE(SignatureNonce)
110{
111 SignatureInfo info(tlv::SignatureSha256WithEcdsa);
112 BOOST_CHECK(!info.getNonce());
113 info.wireEncode();
114 BOOST_CHECK_EQUAL(info.hasWire(), true);
115 std::vector<uint8_t> nonce{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
116 info.setNonce(nonce);
117 BOOST_CHECK_EQUAL(info.hasWire(), false);
118 BOOST_CHECK(info.getNonce() == nonce);
119
120 info.wireEncode();
121 info.setNonce(nonce);
122 BOOST_CHECK_EQUAL(info.hasWire(), true);
123 nonce[2] = 0xFF;
124 info.setNonce(nonce);
125 BOOST_CHECK_EQUAL(info.hasWire(), false);
126 BOOST_CHECK(info.getNonce() == nonce);
127
128 info.wireEncode();
129 BOOST_CHECK_EQUAL(info.hasWire(), true);
Davide Pesaventof6b45892023-03-13 15:00:51 -0400130 info.setNonce(std::nullopt);
Eric Newberryf165bb42020-05-22 11:28:45 -0700131 BOOST_CHECK_EQUAL(info.hasWire(), false);
132 BOOST_CHECK(!info.getNonce());
133}
134
135BOOST_AUTO_TEST_CASE(SignatureTime)
136{
137 SignatureInfo info(tlv::SignatureSha256WithEcdsa);
138 BOOST_CHECK(!info.getTime());
139 info.wireEncode();
140 BOOST_CHECK_EQUAL(info.hasWire(), true);
141 time::system_clock::time_point timePoint(1590169108480_ms);
142 info.setTime(timePoint);
143 BOOST_CHECK_EQUAL(info.hasWire(), false);
144 BOOST_CHECK(info.getTime() == timePoint);
145
146 info.wireEncode();
147 info.setTime(timePoint);
148 BOOST_CHECK_EQUAL(info.hasWire(), true);
149 info.setTime(timePoint + 2_s);
150 BOOST_CHECK_EQUAL(info.hasWire(), false);
151 BOOST_CHECK(info.getTime() == timePoint + 2_s);
152
153 info.wireEncode();
154 BOOST_CHECK_EQUAL(info.hasWire(), true);
Davide Pesaventof6b45892023-03-13 15:00:51 -0400155 info.setTime(std::nullopt);
Eric Newberryf165bb42020-05-22 11:28:45 -0700156 BOOST_CHECK_EQUAL(info.hasWire(), false);
157 BOOST_CHECK(!info.getTime());
158}
159
160BOOST_AUTO_TEST_CASE(SignatureSeqNum)
161{
162 SignatureInfo info(tlv::SignatureSha256WithEcdsa);
163 BOOST_CHECK(!info.getSeqNum());
164 info.wireEncode();
165 BOOST_CHECK_EQUAL(info.hasWire(), true);
166 info.setSeqNum(256);
167 BOOST_CHECK_EQUAL(info.hasWire(), false);
168 BOOST_CHECK(info.getSeqNum() == 256UL);
169
170 info.wireEncode();
171 info.setSeqNum(256);
172 BOOST_CHECK_EQUAL(info.hasWire(), true);
173 info.setSeqNum(512);
174 BOOST_CHECK_EQUAL(info.hasWire(), false);
175 BOOST_CHECK(info.getSeqNum() == 512UL);
176
177 info.wireEncode();
178 BOOST_CHECK_EQUAL(info.hasWire(), true);
Davide Pesaventof6b45892023-03-13 15:00:51 -0400179 info.setSeqNum(std::nullopt);
Eric Newberryf165bb42020-05-22 11:28:45 -0700180 BOOST_CHECK_EQUAL(info.hasWire(), false);
181 BOOST_CHECK(!info.getSeqNum());
182}
183
Davide Pesaventoa86300a2020-12-05 14:55:45 -0500184const uint8_t sigInfoDataRsa[] = {
185 0x16, 0x1b, // SignatureInfo
186 0x1b, 0x01, // SignatureType
187 0x01, // Sha256WithRsa
188 0x1c, 0x16, // KeyLocator
189 0x07, 0x14, // Name
190 0x08, 0x04,
191 0x74, 0x65, 0x73, 0x74,
192 0x08, 0x03,
193 0x6b, 0x65, 0x79,
194 0x08, 0x07,
195 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72,
196};
197
198const uint8_t sigInfoInterestRsa[] = {
199 0x2c, 0x33, // InterestSignatureInfo
200 0x1b, 0x01, // SignatureType
201 0x01, // Sha256WithRsa
202 0x1c, 0x16, // KeyLocator
203 0x07, 0x14, // Name
204 0x08, 0x04,
205 0x74, 0x65, 0x73, 0x74,
206 0x08, 0x03,
207 0x6b, 0x65, 0x79,
208 0x08, 0x07,
209 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72,
210 0x26, 0x08, // SignatureNonce
211 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
212 0x28, 0x08, // SignatureTime
213 0x00, 0x00, 0x01, 0x72, 0x3d, 0x77, 0x00, 0x00,
214 0x2a, 0x02, // SignatureSeqNum
215 0x10, 0x20,
216};
217
218const uint8_t sigInfoDataEcdsa[] = {
219 0x16, 0x1b, // SignatureInfo
220 0x1b, 0x01, // SignatureType
221 0x03, // Sha256WithEcdsa
222 0x1c, 0x16, // KeyLocator
223 0x07, 0x14, // Name
224 0x08, 0x04,
225 0x74, 0x65, 0x73, 0x74,
226 0x08, 0x03,
227 0x6b, 0x65, 0x79,
228 0x08, 0x07,
229 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72,
230};
231
Eric Newberryf165bb42020-05-22 11:28:45 -0700232BOOST_AUTO_TEST_CASE(EncodeDecode)
233{
Davide Pesaventoa86300a2020-12-05 14:55:45 -0500234 const KeyLocator keyLocator("/test/key/locator");
235
236 // RSA
Eric Newberryf165bb42020-05-22 11:28:45 -0700237 SignatureInfo info(tlv::SignatureSha256WithRsa, keyLocator);
238
Davide Pesaventoa86300a2020-12-05 14:55:45 -0500239 // Encode as (Data)SignatureInfo
Eric Newberryf165bb42020-05-22 11:28:45 -0700240 auto encodedData = info.wireEncode(SignatureInfo::Type::Data);
Eric Newberryf165bb42020-05-22 11:28:45 -0700241
Davide Pesaventoa86300a2020-12-05 14:55:45 -0500242 BOOST_CHECK_EQUAL_COLLECTIONS(sigInfoDataRsa, sigInfoDataRsa + sizeof(sigInfoDataRsa),
243 encodedData.begin(), encodedData.end());
Eric Newberryf165bb42020-05-22 11:28:45 -0700244
Davide Pesaventoa86300a2020-12-05 14:55:45 -0500245 // Decode as (Data)SignatureInfo
Davide Pesaventofbea4fc2022-02-08 07:26:04 -0500246 info = SignatureInfo(Block(sigInfoDataRsa), SignatureInfo::Type::Data);
Davide Pesaventoa86300a2020-12-05 14:55:45 -0500247
Eric Newberryf165bb42020-05-22 11:28:45 -0700248 BOOST_CHECK_EQUAL(info.getSignatureType(), tlv::SignatureSha256WithRsa);
249 BOOST_CHECK_EQUAL(info.hasKeyLocator(), true);
250 BOOST_CHECK_EQUAL(info.getKeyLocator().getName(), Name("/test/key/locator"));
Eric Newberryf165bb42020-05-22 11:28:45 -0700251 BOOST_CHECK(!info.getNonce());
252 BOOST_CHECK(!info.getTime());
253 BOOST_CHECK(!info.getSeqNum());
254
255 // Encode as InterestSignatureInfo
Davide Pesaventoa86300a2020-12-05 14:55:45 -0500256 const std::vector<uint8_t> nonce{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
Eric Newberryf165bb42020-05-22 11:28:45 -0700257 info.setNonce(nonce);
Davide Pesaventoa86300a2020-12-05 14:55:45 -0500258 const time::system_clock::time_point timePoint(1590169108480_ms);
Eric Newberryf165bb42020-05-22 11:28:45 -0700259 info.setTime(timePoint);
260 info.setSeqNum(0x1020);
261 auto encodedInterest = info.wireEncode(SignatureInfo::Type::Interest);
Eric Newberryf165bb42020-05-22 11:28:45 -0700262
Davide Pesaventoa86300a2020-12-05 14:55:45 -0500263 BOOST_CHECK_EQUAL_COLLECTIONS(sigInfoInterestRsa, sigInfoInterestRsa + sizeof(sigInfoInterestRsa),
264 encodedInterest.begin(), encodedInterest.end());
Eric Newberryf165bb42020-05-22 11:28:45 -0700265
266 // Decode as InterestSignatureInfo
Davide Pesaventofbea4fc2022-02-08 07:26:04 -0500267 info = SignatureInfo(Block(sigInfoInterestRsa), SignatureInfo::Type::Interest);
Davide Pesaventoa86300a2020-12-05 14:55:45 -0500268
Eric Newberryf165bb42020-05-22 11:28:45 -0700269 BOOST_CHECK_EQUAL(info.getSignatureType(), tlv::SignatureSha256WithRsa);
270 BOOST_CHECK_EQUAL(info.hasKeyLocator(), true);
271 BOOST_CHECK_EQUAL(info.getKeyLocator().getName(), Name("/test/key/locator"));
272 BOOST_CHECK(info.getNonce() == nonce);
273 BOOST_CHECK(info.getTime() == timePoint);
274 BOOST_CHECK(info.getSeqNum() == 0x1020UL);
Davide Pesaventoa86300a2020-12-05 14:55:45 -0500275
276 // ECDSA
277 info = SignatureInfo(tlv::SignatureSha256WithEcdsa, keyLocator);
278
279 // Encode as (Data)SignatureInfo
280 auto encodedDataEcdsa = info.wireEncode(SignatureInfo::Type::Data);
281
282 BOOST_CHECK_EQUAL_COLLECTIONS(sigInfoDataEcdsa, sigInfoDataEcdsa + sizeof(sigInfoDataEcdsa),
283 encodedDataEcdsa.begin(), encodedDataEcdsa.end());
284
285 // Decode as (Data)SignatureInfo
Davide Pesaventofbea4fc2022-02-08 07:26:04 -0500286 info = SignatureInfo(Block(sigInfoDataEcdsa), SignatureInfo::Type::Data);
Davide Pesaventoa86300a2020-12-05 14:55:45 -0500287
288 BOOST_CHECK_EQUAL(info.getSignatureType(), tlv::SignatureSha256WithEcdsa);
289 BOOST_CHECK_EQUAL(info.hasKeyLocator(), true);
290 BOOST_CHECK_EQUAL(info.getKeyLocator().getName(), Name("/test/key/locator"));
291 BOOST_CHECK(!info.getNonce());
292 BOOST_CHECK(!info.getTime());
293 BOOST_CHECK(!info.getSeqNum());
Eric Newberryf165bb42020-05-22 11:28:45 -0700294}
295
296BOOST_AUTO_TEST_CASE(DecodeError)
297{
298 const uint8_t error1[] = {
299 0x15, 0x1b, // Wrong SignatureInfo (0x16, 0x1b)
300 0x1b, 0x01, // SignatureType
301 0x01, // Sha256WithRsa
302 0x1c, 0x16, // KeyLocator
303 0x07, 0x14, // Name
304 0x08, 0x04,
305 0x74, 0x65, 0x73, 0x74,
306 0x08, 0x03,
307 0x6b, 0x65, 0x79,
308 0x08, 0x07,
309 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72
310 };
Davide Pesaventofbea4fc2022-02-08 07:26:04 -0500311 Block errorBlock1(error1);
Eric Newberryf165bb42020-05-22 11:28:45 -0700312 BOOST_CHECK_THROW(SignatureInfo(errorBlock1, SignatureInfo::Type::Data), tlv::Error);
313
314 const uint8_t error2[] = {
315 0x16, 0x05, // SignatureInfo
316 0x1b, 0x01, // SignatureType
317 0x01, // Sha256WithRsa
318 0x83, 0x00, // Unrecognized critical TLV
319 };
Davide Pesaventofbea4fc2022-02-08 07:26:04 -0500320 Block errorBlock2(error2);
Eric Newberryf165bb42020-05-22 11:28:45 -0700321 BOOST_CHECK_THROW(SignatureInfo(errorBlock2, SignatureInfo::Type::Data), tlv::Error);
322
323 const uint8_t error3[] = {
324 0x16, 0x00 // Empty SignatureInfo
325 };
Davide Pesaventofbea4fc2022-02-08 07:26:04 -0500326 Block errorBlock3(error3);
Eric Newberryf165bb42020-05-22 11:28:45 -0700327 BOOST_CHECK_THROW(SignatureInfo(errorBlock3, SignatureInfo::Type::Data), tlv::Error);
328
329 // Encoding is correct for SignatureInfo, but decoder is expecting InterestSignatureInfo
330 const uint8_t error4[] = {
331 0x16, 0x1b, // SignatureInfo
332 0x1b, 0x01, // SignatureType
333 0x01, // Sha256WithRsa
334 0x1c, 0x16, // KeyLocator
335 0x07, 0x14, // Name
336 0x08, 0x04,
337 0x74, 0x65, 0x73, 0x74,
338 0x08, 0x03,
339 0x6b, 0x65, 0x79,
340 0x08, 0x07,
341 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72
342 };
Davide Pesaventofbea4fc2022-02-08 07:26:04 -0500343 Block errorBlock4(error4);
Eric Newberryf165bb42020-05-22 11:28:45 -0700344 BOOST_CHECK_THROW(SignatureInfo(errorBlock4, SignatureInfo::Type::Interest), tlv::Error);
345
346 // SignatureType and KeyLocator out-of-order
347 const uint8_t error5[] = {
348 0x2c, 0x1b, // SignatureInfo
349 0x1c, 0x16, // KeyLocator
350 0x07, 0x14, // Name
351 0x08, 0x04,
352 0x74, 0x65, 0x73, 0x74,
353 0x08, 0x03,
354 0x6b, 0x65, 0x79,
355 0x08, 0x07,
356 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72,
357 0x1b, 0x01, // SignatureType
358 0x01, // Sha256WithRsa
359 };
Davide Pesaventofbea4fc2022-02-08 07:26:04 -0500360 Block errorBlock5(error5);
Eric Newberryf165bb42020-05-22 11:28:45 -0700361 BOOST_CHECK_THROW(SignatureInfo(errorBlock5, SignatureInfo::Type::Interest), tlv::Error);
362
363 // Repeated KeyLocator
364 const uint8_t error6[] = {
365 0x2c, 0x33, // SignatureInfo
366 0x1b, 0x01, // SignatureType
367 0x01, // Sha256WithRsa
368 0x1c, 0x16, // KeyLocator
369 0x07, 0x14, // Name
370 0x08, 0x04,
371 0x74, 0x65, 0x73, 0x74,
372 0x08, 0x03,
373 0x6b, 0x65, 0x79,
374 0x08, 0x07,
375 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72,
376 0x1c, 0x16, // KeyLocator
377 0x07, 0x14, // Name
378 0x08, 0x04,
379 0x74, 0x65, 0x73, 0x74,
380 0x08, 0x03,
381 0x6b, 0x65, 0x79,
382 0x08, 0x07,
383 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72
384 };
Davide Pesaventofbea4fc2022-02-08 07:26:04 -0500385 Block errorBlock6(error6);
Eric Newberryf165bb42020-05-22 11:28:45 -0700386 BOOST_CHECK_THROW(SignatureInfo(errorBlock6, SignatureInfo::Type::Interest), tlv::Error);
387
388 // Zero-length SignatureNonce
389 const uint8_t error7[] = {
390 0x2c, 0x05, // SignatureInfo
391 0x1b, 0x01, // SignatureType
392 0x01, // Sha256WithRsa
393 0x26, 0x00 // SignatureNonce
394 };
Davide Pesaventofbea4fc2022-02-08 07:26:04 -0500395 Block errorBlock7(error7);
Eric Newberryf165bb42020-05-22 11:28:45 -0700396 BOOST_CHECK_THROW(SignatureInfo(errorBlock7, SignatureInfo::Type::Interest), tlv::Error);
397}
398
Davide Pesavento14c56cd2020-05-21 01:44:03 -0400399BOOST_AUTO_TEST_CASE(ValidityPeriod)
Yingdi Yu6be43f32015-06-09 14:19:54 -0700400{
401 const uint8_t sigInfo[] = {
402 0x16, 0x45, // SignatureInfo
403 0x1b, 0x01, // SignatureType
404 0x01, // Sha256WithRsa
405 0x1c, 0x16, // KeyLocator
406 0x07, 0x14, // Name
407 0x08, 0x04,
408 0x74, 0x65, 0x73, 0x74,
409 0x08, 0x03,
410 0x6b, 0x65, 0x79,
411 0x08, 0x07,
412 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72,
413 0xfd, 0x00, 0xfd, 0x26, // ValidityPeriod
414 0xfd, 0x00, 0xfe, 0x0f, // NotBefore
415 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x31, // 19700101T000000
416 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
417 0xfd, 0x00, 0xff, 0x0f, // NotAfter
418 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x32, // 19700102T000000
419 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30
420 };
421
Davide Pesavento14c56cd2020-05-21 01:44:03 -0400422 const auto notBefore = time::getUnixEpoch();
423 const auto notAfter = notBefore + 1_day;
Yingdi Yu6be43f32015-06-09 14:19:54 -0700424 security::ValidityPeriod vp1(notBefore, notAfter);
425
Davide Pesavento14c56cd2020-05-21 01:44:03 -0400426 SignatureInfo info(tlv::SignatureSha256WithRsa, KeyLocator("/test/key/locator"));
Davide Pesavento14c56cd2020-05-21 01:44:03 -0400427 BOOST_CHECK_THROW(info.getValidityPeriod(), SignatureInfo::Error);
428
429 info.wireEncode();
Yingdi Yu6be43f32015-06-09 14:19:54 -0700430 info.setValidityPeriod(vp1);
Junxiao Shi72c0c642018-04-20 15:41:09 +0000431 BOOST_CHECK_EQUAL(info.getValidityPeriod(), vp1);
Davide Pesavento14c56cd2020-05-21 01:44:03 -0400432 BOOST_CHECK_EQUAL(info.hasWire(), false);
Yingdi Yu6be43f32015-06-09 14:19:54 -0700433
Davide Pesavento14c56cd2020-05-21 01:44:03 -0400434 // encode
435 auto encoded = info.wireEncode();
436 BOOST_CHECK_EQUAL(info.hasWire(), true);
Davide Pesavento258d51a2022-02-27 21:26:28 -0500437 BOOST_TEST(encoded == sigInfo, boost::test_tools::per_element());
Yingdi Yu6be43f32015-06-09 14:19:54 -0700438
Davide Pesavento14c56cd2020-05-21 01:44:03 -0400439 info.setValidityPeriod(vp1);
440 BOOST_CHECK_EQUAL(info.hasWire(), true);
441
Yingdi Yu6be43f32015-06-09 14:19:54 -0700442 // decode
Davide Pesaventofbea4fc2022-02-08 07:26:04 -0500443 Block block(sigInfo);
Eric Newberryf165bb42020-05-22 11:28:45 -0700444 SignatureInfo info2(block, SignatureInfo::Type::Data);
Junxiao Shi72c0c642018-04-20 15:41:09 +0000445 BOOST_CHECK_EQUAL(info2.getValidityPeriod(), vp1);
Davide Pesavento14c56cd2020-05-21 01:44:03 -0400446 BOOST_CHECK_EQUAL(info2.hasWire(), true);
Yingdi Yu6be43f32015-06-09 14:19:54 -0700447
Davide Pesavento14c56cd2020-05-21 01:44:03 -0400448 info2.setValidityPeriod(security::ValidityPeriod(notBefore, notBefore + 42_days));
Davide Pesavento14c56cd2020-05-21 01:44:03 -0400449 BOOST_CHECK_NE(info2.getValidityPeriod(), vp1);
450 BOOST_CHECK(info2.getValidityPeriod().getPeriod() == std::make_pair(notBefore, notBefore + 42_days));
451 BOOST_CHECK_EQUAL(info2.hasWire(), false);
452
453 info2.wireEncode();
454 BOOST_CHECK_EQUAL(info2.hasWire(), true);
Davide Pesaventof6b45892023-03-13 15:00:51 -0400455 info2.setValidityPeriod(std::nullopt);
Davide Pesavento14c56cd2020-05-21 01:44:03 -0400456 BOOST_CHECK_THROW(info2.getValidityPeriod(), SignatureInfo::Error);
457 BOOST_CHECK_EQUAL(info2.hasWire(), false);
Yingdi Yu6be43f32015-06-09 14:19:54 -0700458}
459
Eric Newberryf165bb42020-05-22 11:28:45 -0700460BOOST_AUTO_TEST_CASE(CustomTlvs)
Yingdi Yu4a557052014-07-09 16:40:37 -0700461{
Davide Pesavento14c56cd2020-05-21 01:44:03 -0400462 SignatureInfo info(tlv::SignatureSha256WithEcdsa);
Eric Newberryf165bb42020-05-22 11:28:45 -0700463 info.addCustomTlv("810101"_block);
464 BOOST_CHECK(!info.getCustomTlv(0x82));
465 BOOST_REQUIRE(info.getCustomTlv(0x81));
466 BOOST_CHECK_EQUAL(info.getCustomTlv(0x81)->type(), 0x81);
Davide Pesavento14c56cd2020-05-21 01:44:03 -0400467
468 info.wireEncode();
469 BOOST_CHECK_EQUAL(info.hasWire(), true);
Eric Newberryf165bb42020-05-22 11:28:45 -0700470 info.addCustomTlv("82020202"_block);
Davide Pesavento14c56cd2020-05-21 01:44:03 -0400471 BOOST_CHECK_EQUAL(info.hasWire(), false);
Eric Newberryf165bb42020-05-22 11:28:45 -0700472
473 info.wireEncode();
474 BOOST_CHECK_EQUAL(info.hasWire(), true);
475 info.removeCustomTlv(0x81);
476 BOOST_CHECK_EQUAL(info.hasWire(), false);
477 BOOST_CHECK(!info.getCustomTlv(0x81));
Yingdi Yu4a557052014-07-09 16:40:37 -0700478}
479
Eric Newberryf165bb42020-05-22 11:28:45 -0700480BOOST_AUTO_TEST_CASE(CustomTlvsEncoding) // Bug #3914
Alexander Afanasyev41469752017-01-10 21:51:55 -0800481{
Junxiao Shi605671d2017-08-26 13:41:06 +0000482 SignatureInfo info1(tlv::SignatureSha256WithRsa);
Davide Pesaventoa86300a2020-12-05 14:55:45 -0500483 info1.addCustomTlv(makeStringBlock(102, "First"));
484 info1.addCustomTlv(makeStringBlock(104, "Second"));
485 info1.addCustomTlv(makeStringBlock(106, "Third"));
Alexander Afanasyev41469752017-01-10 21:51:55 -0800486
Davide Pesaventoa86300a2020-12-05 14:55:45 -0500487 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(info1),
488 "SignatureSha256WithRsa { 102 104 106 }");
Alexander Afanasyev41469752017-01-10 21:51:55 -0800489
490 SignatureInfo info2;
Eric Newberryf165bb42020-05-22 11:28:45 -0700491 info2.wireDecode(info1.wireEncode(), SignatureInfo::Type::Data);
Alexander Afanasyev41469752017-01-10 21:51:55 -0800492 BOOST_CHECK_EQUAL(info1, info2);
493
Alexander Afanasyev41469752017-01-10 21:51:55 -0800494 const uint8_t infoBytes[] = {
Junxiao Shi605671d2017-08-26 13:41:06 +0000495 0x16, 0x19, // SignatureInfo
496 0x1b, 0x01, 0x01, // SignatureType=1
Eric Newberryf165bb42020-05-22 11:28:45 -0700497 0x66, 0x05, 0x46, 0x69, 0x72, 0x73, 0x74, // 102 "First"
498 0x68, 0x06, 0x53, 0x65, 0x63, 0x6f, 0x6e, 0x64, // 104 "Second"
499 0x6a, 0x05, 0x54, 0x68, 0x69, 0x72, 0x64 // 106 "Third"
Alexander Afanasyev41469752017-01-10 21:51:55 -0800500 };
501
Davide Pesaventofbea4fc2022-02-08 07:26:04 -0500502 SignatureInfo info3(Block(infoBytes), SignatureInfo::Type::Data);
Alexander Afanasyev41469752017-01-10 21:51:55 -0800503 BOOST_CHECK_EQUAL(info3, info1);
504 BOOST_CHECK_EQUAL_COLLECTIONS(infoBytes, infoBytes + sizeof(infoBytes),
505 info1.wireEncode().begin(), info1.wireEncode().end());
506}
507
Eric Newberryf165bb42020-05-22 11:28:45 -0700508BOOST_AUTO_TEST_CASE(OutputStream)
509{
510 SignatureInfo info;
511 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(info), "Invalid SignatureInfo");
512
513 info.setSignatureType(tlv::DigestSha256);
514 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(info), "DigestSha256");
515
516 info.setSignatureType(tlv::SignatureSha256WithRsa);
517 info.setKeyLocator(KeyLocator("/test/key/locator"));
518 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(info),
519 "SignatureSha256WithRsa Name=/test/key/locator");
520
521 info.setNonce(std::vector<uint8_t>{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08});
522 info.setTime(time::system_clock::time_point(1590169108480_ms));
523 info.setSeqNum(0x1020);
524
525 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(info),
526 "SignatureSha256WithRsa Name=/test/key/locator "
527 "{ Nonce=0102030405060708 Time=1590169108480 SeqNum=4128 }");
528
529 info.setValidityPeriod(security::ValidityPeriod(time::getUnixEpoch(), time::getUnixEpoch() + 31_days));
530
531 info.addCustomTlv("82020102"_block);
532 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(info),
533 "SignatureSha256WithRsa Name=/test/key/locator "
534 "{ Nonce=0102030405060708 Time=1590169108480 SeqNum=4128 "
535 "ValidityPeriod=(19700101T000000, 19700201T000000) 130 }");
536
537 info.addCustomTlv("84020102"_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 132 }");
542}
543
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100544BOOST_AUTO_TEST_SUITE_END() // TestSignatureInfo
Yingdi Yu4a557052014-07-09 16:40:37 -0700545
Spyridon Mastorakis429634f2015-02-19 17:35:33 -0800546} // namespace tests
Yingdi Yu4a557052014-07-09 16:40:37 -0700547} // namespace ndn