blob: 1b0fe581bd00e56070405a3642c18b9840cfe67a [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
33const uint8_t sigInfoRsa[] = {
Davide Pesavento14c56cd2020-05-21 01:44:03 -040034 0x16, 0x1b, // SignatureInfo
35 0x1b, 0x01, // SignatureType
36 0x01, // Sha256WithRsa
37 0x1c, 0x16, // KeyLocator
38 0x07, 0x14, // Name
39 0x08, 0x04,
40 0x74, 0x65, 0x73, 0x74,
41 0x08, 0x03,
42 0x6b, 0x65, 0x79,
43 0x08, 0x07,
44 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72
Yingdi Yu4a557052014-07-09 16:40:37 -070045};
46
47BOOST_AUTO_TEST_CASE(Constructor)
48{
49 SignatureInfo info;
50 BOOST_CHECK_EQUAL(info.getSignatureType(), -1);
51 BOOST_CHECK_EQUAL(info.hasKeyLocator(), false);
52 BOOST_CHECK_THROW(info.getKeyLocator(), SignatureInfo::Error);
Davide Pesavento14c56cd2020-05-21 01:44:03 -040053 BOOST_CHECK_EQUAL(info.hasValidityPeriod(), false);
54 BOOST_CHECK_THROW(info.getValidityPeriod(), SignatureInfo::Error);
Yingdi Yu4a557052014-07-09 16:40:37 -070055
Davide Pesavento7e6f6f82017-10-31 18:05:28 -040056 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(info), "Invalid SignatureInfo");
57
Steve DiBenedetto54ce6682014-07-22 13:22:57 -060058 SignatureInfo sha256Info(tlv::DigestSha256);
59 BOOST_CHECK_EQUAL(sha256Info.getSignatureType(), tlv::DigestSha256);
Yingdi Yu4a557052014-07-09 16:40:37 -070060 BOOST_CHECK_EQUAL(sha256Info.hasKeyLocator(), false);
61 BOOST_CHECK_THROW(sha256Info.getKeyLocator(), SignatureInfo::Error);
62
Alexander Afanasyev41469752017-01-10 21:51:55 -080063 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(sha256Info), "DigestSha256");
64
Yingdi Yu4a557052014-07-09 16:40:37 -070065 KeyLocator keyLocator("/test/key/locator");
Steve DiBenedetto54ce6682014-07-22 13:22:57 -060066 SignatureInfo sha256RsaInfo(tlv::SignatureSha256WithRsa, keyLocator);
67 BOOST_CHECK_EQUAL(sha256RsaInfo.getSignatureType(), tlv::SignatureSha256WithRsa);
Yingdi Yu4a557052014-07-09 16:40:37 -070068 BOOST_CHECK_EQUAL(sha256RsaInfo.hasKeyLocator(), true);
Yingdi Yu4a557052014-07-09 16:40:37 -070069 BOOST_CHECK_EQUAL(sha256RsaInfo.getKeyLocator().getName(), Name("/test/key/locator"));
Davide Pesavento14c56cd2020-05-21 01:44:03 -040070 BOOST_CHECK_EQUAL(sha256RsaInfo.hasValidityPeriod(), false);
71 BOOST_CHECK_THROW(sha256RsaInfo.getValidityPeriod(), SignatureInfo::Error);
Yingdi Yu4a557052014-07-09 16:40:37 -070072
Davide Pesavento14c56cd2020-05-21 01:44:03 -040073 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(sha256RsaInfo),
74 "SignatureSha256WithRsa Name=/test/key/locator");
Alexander Afanasyev41469752017-01-10 21:51:55 -080075
Davide Pesavento14c56cd2020-05-21 01:44:03 -040076 auto encoded = sha256RsaInfo.wireEncode();
Yingdi Yu4a557052014-07-09 16:40:37 -070077 Block sigInfoBlock(sigInfoRsa, sizeof(sigInfoRsa));
Davide Pesavento14c56cd2020-05-21 01:44:03 -040078 BOOST_CHECK_EQUAL_COLLECTIONS(sigInfoBlock.wire(), sigInfoBlock.wire() + sigInfoBlock.size(),
79 encoded.wire(), encoded.wire() + encoded.size());
Yingdi Yu4a557052014-07-09 16:40:37 -070080
81 sha256RsaInfo = SignatureInfo(sigInfoBlock);
Steve DiBenedetto54ce6682014-07-22 13:22:57 -060082 BOOST_CHECK_EQUAL(sha256RsaInfo.getSignatureType(), tlv::SignatureSha256WithRsa);
Yingdi Yu4a557052014-07-09 16:40:37 -070083 BOOST_CHECK_EQUAL(sha256RsaInfo.hasKeyLocator(), true);
Yingdi Yu4a557052014-07-09 16:40:37 -070084 BOOST_CHECK_EQUAL(sha256RsaInfo.getKeyLocator().getName(), Name("/test/key/locator"));
85}
86
87BOOST_AUTO_TEST_CASE(ConstructorError)
88{
89 const uint8_t error1[] = {
90 0x15, 0x1b, // Wrong SignatureInfo (0x16, 0x1b)
91 0x1b, 0x01, // SignatureType
92 0x01, // Sha256WithRsa
93 0x1c, 0x16, // KeyLocator
94 0x07, 0x14, // Name
95 0x08, 0x04,
96 0x74, 0x65, 0x73, 0x74,
97 0x08, 0x03,
98 0x6b, 0x65, 0x79,
99 0x08, 0x07,
100 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72
101 };
102 Block errorBlock1(error1, sizeof(error1));
Steve DiBenedetto54ce6682014-07-22 13:22:57 -0600103 BOOST_CHECK_THROW(SignatureInfo info(errorBlock1), tlv::Error);
Yingdi Yu4a557052014-07-09 16:40:37 -0700104
105 const uint8_t error2[] = {
106 0x16, 0x01, // SignatureInfo
107 0x01 // Wrong SignatureInfo value
108 };
109 Block errorBlock2(error2, sizeof(error2));
Steve DiBenedetto54ce6682014-07-22 13:22:57 -0600110 BOOST_CHECK_THROW(SignatureInfo info(errorBlock2), tlv::Error);
Yingdi Yu4a557052014-07-09 16:40:37 -0700111
112 const uint8_t error3[] = {
113 0x16, 0x01, // SignatureInfo
114 0x1a, 0x01, // Wrong SignatureType (0x1b, 0x1b)
115 0x01, // Sha256WithRsa
116 0x1c, 0x16, // KeyLocator
117 0x07, 0x14, // Name
118 0x08, 0x04,
119 0x74, 0x65, 0x73, 0x74,
120 0x08, 0x03,
121 0x6b, 0x65, 0x79,
122 0x08, 0x07,
123 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72
124 };
125 Block errorBlock3(error3, sizeof(error3));
Steve DiBenedetto54ce6682014-07-22 13:22:57 -0600126 BOOST_CHECK_THROW(SignatureInfo info(errorBlock3), tlv::Error);
Yingdi Yu4a557052014-07-09 16:40:37 -0700127
128 const uint8_t error4[] = {
129 0x16, 0x00 // Empty SignatureInfo
130 };
131 Block errorBlock4(error4, sizeof(error4));
Steve DiBenedetto54ce6682014-07-22 13:22:57 -0600132 BOOST_CHECK_THROW(SignatureInfo info(errorBlock4), tlv::Error);
Yingdi Yu4a557052014-07-09 16:40:37 -0700133
134}
135
Davide Pesavento14c56cd2020-05-21 01:44:03 -0400136BOOST_AUTO_TEST_CASE(GetSetSignatureType)
Yingdi Yu4a557052014-07-09 16:40:37 -0700137{
138 SignatureInfo info;
139 BOOST_CHECK_EQUAL(info.getSignatureType(), -1);
Davide Pesavento14c56cd2020-05-21 01:44:03 -0400140 BOOST_CHECK_THROW(info.wireEncode(), SignatureInfo::Error);
Yingdi Yu4a557052014-07-09 16:40:37 -0700141
Davide Pesavento14c56cd2020-05-21 01:44:03 -0400142 info.setSignatureType(tlv::SignatureSha256WithEcdsa);
143 BOOST_CHECK_EQUAL(info.getSignatureType(), tlv::SignatureSha256WithEcdsa);
144 BOOST_CHECK_EQUAL(info.hasWire(), false);
Yingdi Yu4a557052014-07-09 16:40:37 -0700145
Davide Pesavento14c56cd2020-05-21 01:44:03 -0400146 info.wireEncode();
147 BOOST_CHECK_EQUAL(info.hasWire(), true);
148 info.setSignatureType(tlv::SignatureSha256WithEcdsa);
149 BOOST_CHECK_EQUAL(info.hasWire(), true);
Yingdi Yu4a557052014-07-09 16:40:37 -0700150
Davide Pesavento14c56cd2020-05-21 01:44:03 -0400151 info.setSignatureType(static_cast<tlv::SignatureTypeValue>(1234));
152 BOOST_CHECK_EQUAL(info.getSignatureType(), 1234);
153 BOOST_CHECK_EQUAL(info.hasWire(), false);
Yingdi Yu4a557052014-07-09 16:40:37 -0700154}
155
Davide Pesavento14c56cd2020-05-21 01:44:03 -0400156BOOST_AUTO_TEST_CASE(GetSetKeyLocator)
157{
158 SignatureInfo info(tlv::SignatureSha256WithEcdsa);
159 BOOST_CHECK_EQUAL(info.hasKeyLocator(), false);
160 BOOST_CHECK_THROW(info.getKeyLocator(), SignatureInfo::Error);
161
162 info.setKeyLocator(Name("/test/key/locator"));
163 BOOST_CHECK_EQUAL(info.hasKeyLocator(), true);
164 BOOST_CHECK_EQUAL(info.getKeyLocator().getName(), "/test/key/locator");
165 BOOST_CHECK_EQUAL(info.hasWire(), false);
166
167 info.wireEncode();
168 BOOST_CHECK_EQUAL(info.hasWire(), true);
169 info.setKeyLocator(Name("/test/key/locator"));
170 BOOST_CHECK_EQUAL(info.hasWire(), true);
171 info.setKeyLocator(Name("/another/key/locator"));
172 BOOST_CHECK_EQUAL(info.hasWire(), false);
173
174 info.wireEncode();
175 BOOST_CHECK_EQUAL(info.hasWire(), true);
176 info.setKeyLocator(nullopt);
177 BOOST_CHECK_EQUAL(info.hasKeyLocator(), false);
178 BOOST_CHECK_THROW(info.getKeyLocator(), SignatureInfo::Error);
179 BOOST_CHECK_EQUAL(info.hasWire(), false);
180}
181
182BOOST_AUTO_TEST_CASE(ValidityPeriod)
Yingdi Yu6be43f32015-06-09 14:19:54 -0700183{
184 const uint8_t sigInfo[] = {
185 0x16, 0x45, // 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 0xfd, 0x00, 0xfd, 0x26, // ValidityPeriod
197 0xfd, 0x00, 0xfe, 0x0f, // NotBefore
198 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x31, // 19700101T000000
199 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
200 0xfd, 0x00, 0xff, 0x0f, // NotAfter
201 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x32, // 19700102T000000
202 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30
203 };
204
Davide Pesavento14c56cd2020-05-21 01:44:03 -0400205 const auto notBefore = time::getUnixEpoch();
206 const auto notAfter = notBefore + 1_day;
Yingdi Yu6be43f32015-06-09 14:19:54 -0700207 security::ValidityPeriod vp1(notBefore, notAfter);
208
Davide Pesavento14c56cd2020-05-21 01:44:03 -0400209 SignatureInfo info(tlv::SignatureSha256WithRsa, KeyLocator("/test/key/locator"));
210 BOOST_CHECK_EQUAL(info.hasValidityPeriod(), false);
211 BOOST_CHECK_THROW(info.getValidityPeriod(), SignatureInfo::Error);
212
213 info.wireEncode();
Yingdi Yu6be43f32015-06-09 14:19:54 -0700214 info.setValidityPeriod(vp1);
Davide Pesavento14c56cd2020-05-21 01:44:03 -0400215 BOOST_CHECK_EQUAL(info.hasValidityPeriod(), true);
Junxiao Shi72c0c642018-04-20 15:41:09 +0000216 BOOST_CHECK_EQUAL(info.getValidityPeriod(), vp1);
Davide Pesavento14c56cd2020-05-21 01:44:03 -0400217 BOOST_CHECK_EQUAL(info.hasWire(), false);
Yingdi Yu6be43f32015-06-09 14:19:54 -0700218
Davide Pesavento14c56cd2020-05-21 01:44:03 -0400219 // encode
220 auto encoded = info.wireEncode();
221 BOOST_CHECK_EQUAL(info.hasWire(), true);
Yingdi Yu6be43f32015-06-09 14:19:54 -0700222 BOOST_CHECK_EQUAL_COLLECTIONS(sigInfo, sigInfo + sizeof(sigInfo),
223 encoded.wire(), encoded.wire() + encoded.size());
224
Davide Pesavento14c56cd2020-05-21 01:44:03 -0400225 info.setValidityPeriod(vp1);
226 BOOST_CHECK_EQUAL(info.hasWire(), true);
227
Yingdi Yu6be43f32015-06-09 14:19:54 -0700228 // decode
229 Block block(sigInfo, sizeof(sigInfo));
Davide Pesavento14c56cd2020-05-21 01:44:03 -0400230 SignatureInfo info2(block);
231 BOOST_CHECK_EQUAL(info2.hasValidityPeriod(), true);
Junxiao Shi72c0c642018-04-20 15:41:09 +0000232 BOOST_CHECK_EQUAL(info2.getValidityPeriod(), vp1);
Davide Pesavento14c56cd2020-05-21 01:44:03 -0400233 BOOST_CHECK_EQUAL(info2.hasWire(), true);
Yingdi Yu6be43f32015-06-09 14:19:54 -0700234
Davide Pesavento14c56cd2020-05-21 01:44:03 -0400235 info2.setValidityPeriod(security::ValidityPeriod(notBefore, notBefore + 42_days));
236 BOOST_CHECK_EQUAL(info2.hasValidityPeriod(), true);
237 BOOST_CHECK_NE(info2.getValidityPeriod(), vp1);
238 BOOST_CHECK(info2.getValidityPeriod().getPeriod() == std::make_pair(notBefore, notBefore + 42_days));
239 BOOST_CHECK_EQUAL(info2.hasWire(), false);
240
241 info2.wireEncode();
242 BOOST_CHECK_EQUAL(info2.hasWire(), true);
243 info2.setValidityPeriod(nullopt);
244 BOOST_CHECK_EQUAL(info2.hasValidityPeriod(), false);
245 BOOST_CHECK_THROW(info2.getValidityPeriod(), SignatureInfo::Error);
246 BOOST_CHECK_EQUAL(info2.hasWire(), false);
Yingdi Yu6be43f32015-06-09 14:19:54 -0700247}
248
Yingdi Yu4a557052014-07-09 16:40:37 -0700249BOOST_AUTO_TEST_CASE(OtherTlvs)
250{
Davide Pesavento14c56cd2020-05-21 01:44:03 -0400251 SignatureInfo info(tlv::SignatureSha256WithEcdsa);
252 info.appendTypeSpecificTlv("810101"_block);
Yingdi Yu4a557052014-07-09 16:40:37 -0700253 BOOST_CHECK_THROW(info.getTypeSpecificTlv(0x82), SignatureInfo::Error);
Davide Pesavento14c56cd2020-05-21 01:44:03 -0400254 BOOST_CHECK_EQUAL(info.getTypeSpecificTlv(0x81).type(), 0x81);
255
256 info.wireEncode();
257 BOOST_CHECK_EQUAL(info.hasWire(), true);
258 info.appendTypeSpecificTlv("82020202"_block);
259 BOOST_CHECK_EQUAL(info.hasWire(), false);
Yingdi Yu4a557052014-07-09 16:40:37 -0700260}
261
Alexander Afanasyev41469752017-01-10 21:51:55 -0800262BOOST_AUTO_TEST_CASE(OtherTlvsEncoding) // Bug #3914
263{
Junxiao Shi605671d2017-08-26 13:41:06 +0000264 SignatureInfo info1(tlv::SignatureSha256WithRsa);
Alexander Afanasyev41469752017-01-10 21:51:55 -0800265 info1.appendTypeSpecificTlv(makeStringBlock(101, "First"));
266 info1.appendTypeSpecificTlv(makeStringBlock(102, "Second"));
267 info1.appendTypeSpecificTlv(makeStringBlock(103, "Third"));
268
Junxiao Shi605671d2017-08-26 13:41:06 +0000269 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(info1), "SignatureSha256WithRsa { 101 102 103 }");
Alexander Afanasyev41469752017-01-10 21:51:55 -0800270
271 SignatureInfo info2;
272 info2.wireDecode(info1.wireEncode());
273 BOOST_CHECK_EQUAL(info1, info2);
274
Alexander Afanasyev41469752017-01-10 21:51:55 -0800275 const uint8_t infoBytes[] = {
Junxiao Shi605671d2017-08-26 13:41:06 +0000276 0x16, 0x19, // SignatureInfo
277 0x1b, 0x01, 0x01, // SignatureType=1
278 0x65, 0x05, 0x46, 0x69, 0x72, 0x73, 0x74, // 101 "First"
279 0x66, 0x06, 0x53, 0x65, 0x63, 0x6f, 0x6e, 0x64, // 102 "Second"
280 0x67, 0x05, 0x54, 0x68, 0x69, 0x72, 0x64 // 103 "Third"
Alexander Afanasyev41469752017-01-10 21:51:55 -0800281 };
282
283 SignatureInfo info3(Block(infoBytes, sizeof(infoBytes)));
284 BOOST_CHECK_EQUAL(info3, info1);
285 BOOST_CHECK_EQUAL_COLLECTIONS(infoBytes, infoBytes + sizeof(infoBytes),
286 info1.wireEncode().begin(), info1.wireEncode().end());
287}
288
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100289BOOST_AUTO_TEST_SUITE_END() // TestSignatureInfo
Yingdi Yu4a557052014-07-09 16:40:37 -0700290
Spyridon Mastorakis429634f2015-02-19 17:35:33 -0800291} // namespace tests
Yingdi Yu4a557052014-07-09 16:40:37 -0700292} // namespace ndn