blob: eb08285f49bf0605968a0f8d78fa7e9b3af6cfb2 [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 Pesavento14c56cd2020-05-21 01:44:03 -04003 * Copyright (c) 2013-2020 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);
103 info.setKeyLocator(nullopt);
104 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);
130 info.setNonce(nullopt);
131 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);
155 info.setTime(nullopt);
156 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);
179 info.setSeqNum(nullopt);
180 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
246 info = SignatureInfo(Block(sigInfoDataRsa, sizeof(sigInfoDataRsa)),
247 SignatureInfo::Type::Data);
248
Eric Newberryf165bb42020-05-22 11:28:45 -0700249 BOOST_CHECK_EQUAL(info.getSignatureType(), tlv::SignatureSha256WithRsa);
250 BOOST_CHECK_EQUAL(info.hasKeyLocator(), true);
251 BOOST_CHECK_EQUAL(info.getKeyLocator().getName(), Name("/test/key/locator"));
Eric Newberryf165bb42020-05-22 11:28:45 -0700252 BOOST_CHECK(!info.getNonce());
253 BOOST_CHECK(!info.getTime());
254 BOOST_CHECK(!info.getSeqNum());
255
256 // Encode as InterestSignatureInfo
Davide Pesaventoa86300a2020-12-05 14:55:45 -0500257 const std::vector<uint8_t> nonce{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
Eric Newberryf165bb42020-05-22 11:28:45 -0700258 info.setNonce(nonce);
Davide Pesaventoa86300a2020-12-05 14:55:45 -0500259 const time::system_clock::time_point timePoint(1590169108480_ms);
Eric Newberryf165bb42020-05-22 11:28:45 -0700260 info.setTime(timePoint);
261 info.setSeqNum(0x1020);
262 auto encodedInterest = info.wireEncode(SignatureInfo::Type::Interest);
Eric Newberryf165bb42020-05-22 11:28:45 -0700263
Davide Pesaventoa86300a2020-12-05 14:55:45 -0500264 BOOST_CHECK_EQUAL_COLLECTIONS(sigInfoInterestRsa, sigInfoInterestRsa + sizeof(sigInfoInterestRsa),
265 encodedInterest.begin(), encodedInterest.end());
Eric Newberryf165bb42020-05-22 11:28:45 -0700266
267 // Decode as InterestSignatureInfo
Davide Pesaventoa86300a2020-12-05 14:55:45 -0500268 info = SignatureInfo(Block(sigInfoInterestRsa, sizeof(sigInfoInterestRsa)),
269 SignatureInfo::Type::Interest);
270
Eric Newberryf165bb42020-05-22 11:28:45 -0700271 BOOST_CHECK_EQUAL(info.getSignatureType(), tlv::SignatureSha256WithRsa);
272 BOOST_CHECK_EQUAL(info.hasKeyLocator(), true);
273 BOOST_CHECK_EQUAL(info.getKeyLocator().getName(), Name("/test/key/locator"));
274 BOOST_CHECK(info.getNonce() == nonce);
275 BOOST_CHECK(info.getTime() == timePoint);
276 BOOST_CHECK(info.getSeqNum() == 0x1020UL);
Davide Pesaventoa86300a2020-12-05 14:55:45 -0500277
278 // ECDSA
279 info = SignatureInfo(tlv::SignatureSha256WithEcdsa, keyLocator);
280
281 // Encode as (Data)SignatureInfo
282 auto encodedDataEcdsa = info.wireEncode(SignatureInfo::Type::Data);
283
284 BOOST_CHECK_EQUAL_COLLECTIONS(sigInfoDataEcdsa, sigInfoDataEcdsa + sizeof(sigInfoDataEcdsa),
285 encodedDataEcdsa.begin(), encodedDataEcdsa.end());
286
287 // Decode as (Data)SignatureInfo
288 info = SignatureInfo(Block(sigInfoDataEcdsa, sizeof(sigInfoDataEcdsa)),
289 SignatureInfo::Type::Data);
290
291 BOOST_CHECK_EQUAL(info.getSignatureType(), tlv::SignatureSha256WithEcdsa);
292 BOOST_CHECK_EQUAL(info.hasKeyLocator(), true);
293 BOOST_CHECK_EQUAL(info.getKeyLocator().getName(), Name("/test/key/locator"));
294 BOOST_CHECK(!info.getNonce());
295 BOOST_CHECK(!info.getTime());
296 BOOST_CHECK(!info.getSeqNum());
Eric Newberryf165bb42020-05-22 11:28:45 -0700297}
298
299BOOST_AUTO_TEST_CASE(DecodeError)
300{
301 const uint8_t error1[] = {
302 0x15, 0x1b, // Wrong SignatureInfo (0x16, 0x1b)
303 0x1b, 0x01, // SignatureType
304 0x01, // Sha256WithRsa
305 0x1c, 0x16, // KeyLocator
306 0x07, 0x14, // Name
307 0x08, 0x04,
308 0x74, 0x65, 0x73, 0x74,
309 0x08, 0x03,
310 0x6b, 0x65, 0x79,
311 0x08, 0x07,
312 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72
313 };
314 Block errorBlock1(error1, sizeof(error1));
315 BOOST_CHECK_THROW(SignatureInfo(errorBlock1, SignatureInfo::Type::Data), tlv::Error);
316
317 const uint8_t error2[] = {
318 0x16, 0x05, // SignatureInfo
319 0x1b, 0x01, // SignatureType
320 0x01, // Sha256WithRsa
321 0x83, 0x00, // Unrecognized critical TLV
322 };
323 Block errorBlock2(error2, sizeof(error2));
324 BOOST_CHECK_THROW(SignatureInfo(errorBlock2, SignatureInfo::Type::Data), tlv::Error);
325
326 const uint8_t error3[] = {
327 0x16, 0x00 // Empty SignatureInfo
328 };
329 Block errorBlock3(error3, sizeof(error3));
330 BOOST_CHECK_THROW(SignatureInfo(errorBlock3, SignatureInfo::Type::Data), tlv::Error);
331
332 // Encoding is correct for SignatureInfo, but decoder is expecting InterestSignatureInfo
333 const uint8_t error4[] = {
334 0x16, 0x1b, // SignatureInfo
335 0x1b, 0x01, // SignatureType
336 0x01, // Sha256WithRsa
337 0x1c, 0x16, // KeyLocator
338 0x07, 0x14, // Name
339 0x08, 0x04,
340 0x74, 0x65, 0x73, 0x74,
341 0x08, 0x03,
342 0x6b, 0x65, 0x79,
343 0x08, 0x07,
344 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72
345 };
346 Block errorBlock4(error4, sizeof(error4));
347 BOOST_CHECK_THROW(SignatureInfo(errorBlock4, SignatureInfo::Type::Interest), tlv::Error);
348
349 // SignatureType and KeyLocator out-of-order
350 const uint8_t error5[] = {
351 0x2c, 0x1b, // SignatureInfo
352 0x1c, 0x16, // KeyLocator
353 0x07, 0x14, // Name
354 0x08, 0x04,
355 0x74, 0x65, 0x73, 0x74,
356 0x08, 0x03,
357 0x6b, 0x65, 0x79,
358 0x08, 0x07,
359 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72,
360 0x1b, 0x01, // SignatureType
361 0x01, // Sha256WithRsa
362 };
363 Block errorBlock5(error5, sizeof(error5));
364 BOOST_CHECK_THROW(SignatureInfo(errorBlock5, SignatureInfo::Type::Interest), tlv::Error);
365
366 // Repeated KeyLocator
367 const uint8_t error6[] = {
368 0x2c, 0x33, // SignatureInfo
369 0x1b, 0x01, // SignatureType
370 0x01, // Sha256WithRsa
371 0x1c, 0x16, // KeyLocator
372 0x07, 0x14, // Name
373 0x08, 0x04,
374 0x74, 0x65, 0x73, 0x74,
375 0x08, 0x03,
376 0x6b, 0x65, 0x79,
377 0x08, 0x07,
378 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72,
379 0x1c, 0x16, // KeyLocator
380 0x07, 0x14, // Name
381 0x08, 0x04,
382 0x74, 0x65, 0x73, 0x74,
383 0x08, 0x03,
384 0x6b, 0x65, 0x79,
385 0x08, 0x07,
386 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72
387 };
388 Block errorBlock6(error6, sizeof(error6));
389 BOOST_CHECK_THROW(SignatureInfo(errorBlock6, SignatureInfo::Type::Interest), tlv::Error);
390
391 // Zero-length SignatureNonce
392 const uint8_t error7[] = {
393 0x2c, 0x05, // SignatureInfo
394 0x1b, 0x01, // SignatureType
395 0x01, // Sha256WithRsa
396 0x26, 0x00 // SignatureNonce
397 };
398 Block errorBlock7(error7, sizeof(error7));
399 BOOST_CHECK_THROW(SignatureInfo(errorBlock7, SignatureInfo::Type::Interest), tlv::Error);
400}
401
Davide Pesavento14c56cd2020-05-21 01:44:03 -0400402BOOST_AUTO_TEST_CASE(ValidityPeriod)
Yingdi Yu6be43f32015-06-09 14:19:54 -0700403{
404 const uint8_t sigInfo[] = {
405 0x16, 0x45, // SignatureInfo
406 0x1b, 0x01, // SignatureType
407 0x01, // Sha256WithRsa
408 0x1c, 0x16, // KeyLocator
409 0x07, 0x14, // Name
410 0x08, 0x04,
411 0x74, 0x65, 0x73, 0x74,
412 0x08, 0x03,
413 0x6b, 0x65, 0x79,
414 0x08, 0x07,
415 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72,
416 0xfd, 0x00, 0xfd, 0x26, // ValidityPeriod
417 0xfd, 0x00, 0xfe, 0x0f, // NotBefore
418 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x31, // 19700101T000000
419 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
420 0xfd, 0x00, 0xff, 0x0f, // NotAfter
421 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x32, // 19700102T000000
422 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30
423 };
424
Davide Pesavento14c56cd2020-05-21 01:44:03 -0400425 const auto notBefore = time::getUnixEpoch();
426 const auto notAfter = notBefore + 1_day;
Yingdi Yu6be43f32015-06-09 14:19:54 -0700427 security::ValidityPeriod vp1(notBefore, notAfter);
428
Davide Pesavento14c56cd2020-05-21 01:44:03 -0400429 SignatureInfo info(tlv::SignatureSha256WithRsa, KeyLocator("/test/key/locator"));
Davide Pesavento14c56cd2020-05-21 01:44:03 -0400430 BOOST_CHECK_THROW(info.getValidityPeriod(), SignatureInfo::Error);
431
432 info.wireEncode();
Yingdi Yu6be43f32015-06-09 14:19:54 -0700433 info.setValidityPeriod(vp1);
Junxiao Shi72c0c642018-04-20 15:41:09 +0000434 BOOST_CHECK_EQUAL(info.getValidityPeriod(), vp1);
Davide Pesavento14c56cd2020-05-21 01:44:03 -0400435 BOOST_CHECK_EQUAL(info.hasWire(), false);
Yingdi Yu6be43f32015-06-09 14:19:54 -0700436
Davide Pesavento14c56cd2020-05-21 01:44:03 -0400437 // encode
438 auto encoded = info.wireEncode();
439 BOOST_CHECK_EQUAL(info.hasWire(), true);
Yingdi Yu6be43f32015-06-09 14:19:54 -0700440 BOOST_CHECK_EQUAL_COLLECTIONS(sigInfo, sigInfo + sizeof(sigInfo),
441 encoded.wire(), encoded.wire() + encoded.size());
442
Davide Pesavento14c56cd2020-05-21 01:44:03 -0400443 info.setValidityPeriod(vp1);
444 BOOST_CHECK_EQUAL(info.hasWire(), true);
445
Yingdi Yu6be43f32015-06-09 14:19:54 -0700446 // decode
447 Block block(sigInfo, sizeof(sigInfo));
Eric Newberryf165bb42020-05-22 11:28:45 -0700448 SignatureInfo info2(block, SignatureInfo::Type::Data);
Junxiao Shi72c0c642018-04-20 15:41:09 +0000449 BOOST_CHECK_EQUAL(info2.getValidityPeriod(), vp1);
Davide Pesavento14c56cd2020-05-21 01:44:03 -0400450 BOOST_CHECK_EQUAL(info2.hasWire(), true);
Yingdi Yu6be43f32015-06-09 14:19:54 -0700451
Davide Pesavento14c56cd2020-05-21 01:44:03 -0400452 info2.setValidityPeriod(security::ValidityPeriod(notBefore, notBefore + 42_days));
Davide Pesavento14c56cd2020-05-21 01:44:03 -0400453 BOOST_CHECK_NE(info2.getValidityPeriod(), vp1);
454 BOOST_CHECK(info2.getValidityPeriod().getPeriod() == std::make_pair(notBefore, notBefore + 42_days));
455 BOOST_CHECK_EQUAL(info2.hasWire(), false);
456
457 info2.wireEncode();
458 BOOST_CHECK_EQUAL(info2.hasWire(), true);
459 info2.setValidityPeriod(nullopt);
Davide Pesavento14c56cd2020-05-21 01:44:03 -0400460 BOOST_CHECK_THROW(info2.getValidityPeriod(), SignatureInfo::Error);
461 BOOST_CHECK_EQUAL(info2.hasWire(), false);
Yingdi Yu6be43f32015-06-09 14:19:54 -0700462}
463
Eric Newberryf165bb42020-05-22 11:28:45 -0700464BOOST_AUTO_TEST_CASE(CustomTlvs)
Yingdi Yu4a557052014-07-09 16:40:37 -0700465{
Davide Pesavento14c56cd2020-05-21 01:44:03 -0400466 SignatureInfo info(tlv::SignatureSha256WithEcdsa);
Eric Newberryf165bb42020-05-22 11:28:45 -0700467 info.addCustomTlv("810101"_block);
468 BOOST_CHECK(!info.getCustomTlv(0x82));
469 BOOST_REQUIRE(info.getCustomTlv(0x81));
470 BOOST_CHECK_EQUAL(info.getCustomTlv(0x81)->type(), 0x81);
Davide Pesavento14c56cd2020-05-21 01:44:03 -0400471
472 info.wireEncode();
473 BOOST_CHECK_EQUAL(info.hasWire(), true);
Eric Newberryf165bb42020-05-22 11:28:45 -0700474 info.addCustomTlv("82020202"_block);
Davide Pesavento14c56cd2020-05-21 01:44:03 -0400475 BOOST_CHECK_EQUAL(info.hasWire(), false);
Eric Newberryf165bb42020-05-22 11:28:45 -0700476
477 info.wireEncode();
478 BOOST_CHECK_EQUAL(info.hasWire(), true);
479 info.removeCustomTlv(0x81);
480 BOOST_CHECK_EQUAL(info.hasWire(), false);
481 BOOST_CHECK(!info.getCustomTlv(0x81));
Yingdi Yu4a557052014-07-09 16:40:37 -0700482}
483
Eric Newberryf165bb42020-05-22 11:28:45 -0700484BOOST_AUTO_TEST_CASE(CustomTlvsEncoding) // Bug #3914
Alexander Afanasyev41469752017-01-10 21:51:55 -0800485{
Junxiao Shi605671d2017-08-26 13:41:06 +0000486 SignatureInfo info1(tlv::SignatureSha256WithRsa);
Davide Pesaventoa86300a2020-12-05 14:55:45 -0500487 info1.addCustomTlv(makeStringBlock(102, "First"));
488 info1.addCustomTlv(makeStringBlock(104, "Second"));
489 info1.addCustomTlv(makeStringBlock(106, "Third"));
Alexander Afanasyev41469752017-01-10 21:51:55 -0800490
Davide Pesaventoa86300a2020-12-05 14:55:45 -0500491 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(info1),
492 "SignatureSha256WithRsa { 102 104 106 }");
Alexander Afanasyev41469752017-01-10 21:51:55 -0800493
494 SignatureInfo info2;
Eric Newberryf165bb42020-05-22 11:28:45 -0700495 info2.wireDecode(info1.wireEncode(), SignatureInfo::Type::Data);
Alexander Afanasyev41469752017-01-10 21:51:55 -0800496 BOOST_CHECK_EQUAL(info1, info2);
497
Alexander Afanasyev41469752017-01-10 21:51:55 -0800498 const uint8_t infoBytes[] = {
Junxiao Shi605671d2017-08-26 13:41:06 +0000499 0x16, 0x19, // SignatureInfo
500 0x1b, 0x01, 0x01, // SignatureType=1
Eric Newberryf165bb42020-05-22 11:28:45 -0700501 0x66, 0x05, 0x46, 0x69, 0x72, 0x73, 0x74, // 102 "First"
502 0x68, 0x06, 0x53, 0x65, 0x63, 0x6f, 0x6e, 0x64, // 104 "Second"
503 0x6a, 0x05, 0x54, 0x68, 0x69, 0x72, 0x64 // 106 "Third"
Alexander Afanasyev41469752017-01-10 21:51:55 -0800504 };
505
Eric Newberryf165bb42020-05-22 11:28:45 -0700506 SignatureInfo info3(Block(infoBytes, sizeof(infoBytes)), SignatureInfo::Type::Data);
Alexander Afanasyev41469752017-01-10 21:51:55 -0800507 BOOST_CHECK_EQUAL(info3, info1);
508 BOOST_CHECK_EQUAL_COLLECTIONS(infoBytes, infoBytes + sizeof(infoBytes),
509 info1.wireEncode().begin(), info1.wireEncode().end());
510}
511
Eric Newberryf165bb42020-05-22 11:28:45 -0700512BOOST_AUTO_TEST_CASE(OutputStream)
513{
514 SignatureInfo info;
515 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(info), "Invalid SignatureInfo");
516
517 info.setSignatureType(tlv::DigestSha256);
518 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(info), "DigestSha256");
519
520 info.setSignatureType(tlv::SignatureSha256WithRsa);
521 info.setKeyLocator(KeyLocator("/test/key/locator"));
522 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(info),
523 "SignatureSha256WithRsa Name=/test/key/locator");
524
525 info.setNonce(std::vector<uint8_t>{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08});
526 info.setTime(time::system_clock::time_point(1590169108480_ms));
527 info.setSeqNum(0x1020);
528
529 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(info),
530 "SignatureSha256WithRsa Name=/test/key/locator "
531 "{ Nonce=0102030405060708 Time=1590169108480 SeqNum=4128 }");
532
533 info.setValidityPeriod(security::ValidityPeriod(time::getUnixEpoch(), time::getUnixEpoch() + 31_days));
534
535 info.addCustomTlv("82020102"_block);
536 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(info),
537 "SignatureSha256WithRsa Name=/test/key/locator "
538 "{ Nonce=0102030405060708 Time=1590169108480 SeqNum=4128 "
539 "ValidityPeriod=(19700101T000000, 19700201T000000) 130 }");
540
541 info.addCustomTlv("84020102"_block);
542 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(info),
543 "SignatureSha256WithRsa Name=/test/key/locator "
544 "{ Nonce=0102030405060708 Time=1590169108480 SeqNum=4128 "
545 "ValidityPeriod=(19700101T000000, 19700201T000000) 130 132 }");
546}
547
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100548BOOST_AUTO_TEST_SUITE_END() // TestSignatureInfo
Yingdi Yu4a557052014-07-09 16:40:37 -0700549
Spyridon Mastorakis429634f2015-02-19 17:35:33 -0800550} // namespace tests
Yingdi Yu4a557052014-07-09 16:40:37 -0700551} // namespace ndn