blob: 836c11496c00d8186bee92020ad224663f78ac22 [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 Pesavento0f830802018-01-16 23:58:58 -05003 * Copyright (c) 2013-2018 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
22#include "signature-info.hpp"
23
24#include "boost-test.hpp"
Alexander Afanasyev41469752017-01-10 21:51:55 -080025#include <boost/lexical_cast.hpp>
Yingdi Yu4a557052014-07-09 16:40:37 -070026
27namespace ndn {
Spyridon Mastorakis429634f2015-02-19 17:35:33 -080028namespace tests {
Yingdi Yu4a557052014-07-09 16:40:37 -070029
30BOOST_AUTO_TEST_SUITE(TestSignatureInfo)
31
32const uint8_t sigInfoRsa[] = {
330x16, 0x1b, // SignatureInfo
34 0x1b, 0x01, // SignatureType
35 0x01, // Sha256WithRsa
36 0x1c, 0x16, // KeyLocator
37 0x07, 0x14, // Name
38 0x08, 0x04,
39 0x74, 0x65, 0x73, 0x74,
40 0x08, 0x03,
41 0x6b, 0x65, 0x79,
42 0x08, 0x07,
43 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72
44};
45
46BOOST_AUTO_TEST_CASE(Constructor)
47{
48 SignatureInfo info;
49 BOOST_CHECK_EQUAL(info.getSignatureType(), -1);
50 BOOST_CHECK_EQUAL(info.hasKeyLocator(), false);
51 BOOST_CHECK_THROW(info.getKeyLocator(), SignatureInfo::Error);
52
Davide Pesavento7e6f6f82017-10-31 18:05:28 -040053 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(info), "Invalid SignatureInfo");
54
Steve DiBenedetto54ce6682014-07-22 13:22:57 -060055 SignatureInfo sha256Info(tlv::DigestSha256);
56 BOOST_CHECK_EQUAL(sha256Info.getSignatureType(), tlv::DigestSha256);
Yingdi Yu4a557052014-07-09 16:40:37 -070057 BOOST_CHECK_EQUAL(sha256Info.hasKeyLocator(), false);
58 BOOST_CHECK_THROW(sha256Info.getKeyLocator(), SignatureInfo::Error);
59
Alexander Afanasyev41469752017-01-10 21:51:55 -080060 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(sha256Info), "DigestSha256");
61
Yingdi Yu4a557052014-07-09 16:40:37 -070062 KeyLocator keyLocator("/test/key/locator");
Steve DiBenedetto54ce6682014-07-22 13:22:57 -060063 SignatureInfo sha256RsaInfo(tlv::SignatureSha256WithRsa, keyLocator);
64 BOOST_CHECK_EQUAL(sha256RsaInfo.getSignatureType(), tlv::SignatureSha256WithRsa);
Yingdi Yu4a557052014-07-09 16:40:37 -070065 BOOST_CHECK_EQUAL(sha256RsaInfo.hasKeyLocator(), true);
66 BOOST_CHECK_NO_THROW(sha256RsaInfo.getKeyLocator());
67 BOOST_CHECK_EQUAL(sha256RsaInfo.getKeyLocator().getName(), Name("/test/key/locator"));
68
Alexander Afanasyev41469752017-01-10 21:51:55 -080069 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(sha256RsaInfo), "SignatureSha256WithRsa Name=/test/key/locator");
70
Yingdi Yu4a557052014-07-09 16:40:37 -070071 const Block& encoded = sha256RsaInfo.wireEncode();
72 Block sigInfoBlock(sigInfoRsa, sizeof(sigInfoRsa));
73
74 BOOST_CHECK_EQUAL_COLLECTIONS(sigInfoBlock.wire(),
75 sigInfoBlock.wire() + sigInfoBlock.size(),
76 encoded.wire(),
77 encoded.wire() + encoded.size());
78
79 sha256RsaInfo = SignatureInfo(sigInfoBlock);
Steve DiBenedetto54ce6682014-07-22 13:22:57 -060080 BOOST_CHECK_EQUAL(sha256RsaInfo.getSignatureType(), tlv::SignatureSha256WithRsa);
Yingdi Yu4a557052014-07-09 16:40:37 -070081 BOOST_CHECK_EQUAL(sha256RsaInfo.hasKeyLocator(), true);
82 BOOST_CHECK_NO_THROW(sha256RsaInfo.getKeyLocator());
83 BOOST_CHECK_EQUAL(sha256RsaInfo.getKeyLocator().getName(), Name("/test/key/locator"));
84}
85
86BOOST_AUTO_TEST_CASE(ConstructorError)
87{
88 const uint8_t error1[] = {
89 0x15, 0x1b, // Wrong SignatureInfo (0x16, 0x1b)
90 0x1b, 0x01, // SignatureType
91 0x01, // Sha256WithRsa
92 0x1c, 0x16, // KeyLocator
93 0x07, 0x14, // Name
94 0x08, 0x04,
95 0x74, 0x65, 0x73, 0x74,
96 0x08, 0x03,
97 0x6b, 0x65, 0x79,
98 0x08, 0x07,
99 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72
100 };
101 Block errorBlock1(error1, sizeof(error1));
Steve DiBenedetto54ce6682014-07-22 13:22:57 -0600102 BOOST_CHECK_THROW(SignatureInfo info(errorBlock1), tlv::Error);
Yingdi Yu4a557052014-07-09 16:40:37 -0700103
104 const uint8_t error2[] = {
105 0x16, 0x01, // SignatureInfo
106 0x01 // Wrong SignatureInfo value
107 };
108 Block errorBlock2(error2, sizeof(error2));
Steve DiBenedetto54ce6682014-07-22 13:22:57 -0600109 BOOST_CHECK_THROW(SignatureInfo info(errorBlock2), tlv::Error);
Yingdi Yu4a557052014-07-09 16:40:37 -0700110
111 const uint8_t error3[] = {
112 0x16, 0x01, // SignatureInfo
113 0x1a, 0x01, // Wrong SignatureType (0x1b, 0x1b)
114 0x01, // Sha256WithRsa
115 0x1c, 0x16, // KeyLocator
116 0x07, 0x14, // Name
117 0x08, 0x04,
118 0x74, 0x65, 0x73, 0x74,
119 0x08, 0x03,
120 0x6b, 0x65, 0x79,
121 0x08, 0x07,
122 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72
123 };
124 Block errorBlock3(error3, sizeof(error3));
Steve DiBenedetto54ce6682014-07-22 13:22:57 -0600125 BOOST_CHECK_THROW(SignatureInfo info(errorBlock3), tlv::Error);
Yingdi Yu4a557052014-07-09 16:40:37 -0700126
127 const uint8_t error4[] = {
128 0x16, 0x00 // Empty SignatureInfo
129 };
130 Block errorBlock4(error4, sizeof(error4));
Steve DiBenedetto54ce6682014-07-22 13:22:57 -0600131 BOOST_CHECK_THROW(SignatureInfo info(errorBlock4), tlv::Error);
Yingdi Yu4a557052014-07-09 16:40:37 -0700132
133}
134
135BOOST_AUTO_TEST_CASE(SetterGetter)
136{
137 SignatureInfo info;
138 BOOST_CHECK_EQUAL(info.getSignatureType(), -1);
139 BOOST_CHECK_EQUAL(info.hasKeyLocator(), false);
140 BOOST_CHECK_THROW(info.getKeyLocator(), SignatureInfo::Error);
141
Steve DiBenedetto54ce6682014-07-22 13:22:57 -0600142 info.setSignatureType(tlv::SignatureSha256WithRsa);
143 BOOST_CHECK_EQUAL(info.getSignatureType(), tlv::SignatureSha256WithRsa);
Yingdi Yu4a557052014-07-09 16:40:37 -0700144 BOOST_CHECK_EQUAL(info.hasKeyLocator(), false);
145
146 KeyLocator keyLocator("/test/key/locator");
147 info.setKeyLocator(keyLocator);
148 BOOST_CHECK_EQUAL(info.hasKeyLocator(), true);
149 BOOST_CHECK_NO_THROW(info.getKeyLocator());
150 BOOST_CHECK_EQUAL(info.getKeyLocator().getName(), Name("/test/key/locator"));
151
152 const Block& encoded = info.wireEncode();
153 Block sigInfoBlock(sigInfoRsa, sizeof(sigInfoRsa));
154
155 BOOST_CHECK_EQUAL_COLLECTIONS(sigInfoBlock.wire(),
156 sigInfoBlock.wire() + sigInfoBlock.size(),
157 encoded.wire(),
158 encoded.wire() + encoded.size());
Alexander Afanasyeva7c7f9d2014-07-13 11:51:52 -0700159
160 info.unsetKeyLocator();
161 BOOST_CHECK_EQUAL(info.hasKeyLocator(), false);
162 BOOST_CHECK_THROW(info.getKeyLocator(), SignatureInfo::Error);
Yingdi Yu4a557052014-07-09 16:40:37 -0700163}
164
Yingdi Yu6be43f32015-06-09 14:19:54 -0700165BOOST_AUTO_TEST_CASE(ValidityPeriodExtension)
166{
167 const uint8_t sigInfo[] = {
168 0x16, 0x45, // SignatureInfo
169 0x1b, 0x01, // SignatureType
170 0x01, // Sha256WithRsa
171 0x1c, 0x16, // KeyLocator
172 0x07, 0x14, // Name
173 0x08, 0x04,
174 0x74, 0x65, 0x73, 0x74,
175 0x08, 0x03,
176 0x6b, 0x65, 0x79,
177 0x08, 0x07,
178 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72,
179 0xfd, 0x00, 0xfd, 0x26, // ValidityPeriod
180 0xfd, 0x00, 0xfe, 0x0f, // NotBefore
181 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x31, // 19700101T000000
182 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
183 0xfd, 0x00, 0xff, 0x0f, // NotAfter
184 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x32, // 19700102T000000
185 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30
186 };
187
188 time::system_clock::TimePoint notBefore = time::getUnixEpoch();
Davide Pesavento0f830802018-01-16 23:58:58 -0500189 time::system_clock::TimePoint notAfter = notBefore + 1_day;
Yingdi Yu6be43f32015-06-09 14:19:54 -0700190 security::ValidityPeriod vp1(notBefore, notAfter);
191
192 // encode
193 SignatureInfo info;
194 info.setSignatureType(tlv::SignatureSha256WithRsa);
195 info.setKeyLocator(KeyLocator("/test/key/locator"));
196 info.setValidityPeriod(vp1);
197
198 BOOST_CHECK(info.getValidityPeriod() == vp1);
199
200 const Block& encoded = info.wireEncode();
201
202 BOOST_CHECK_EQUAL_COLLECTIONS(sigInfo, sigInfo + sizeof(sigInfo),
203 encoded.wire(), encoded.wire() + encoded.size());
204
205 // decode
206 Block block(sigInfo, sizeof(sigInfo));
207 SignatureInfo info2;
208 info2.wireDecode(block);
209 BOOST_CHECK(info2.getValidityPeriod() == vp1);
210
211 const security::ValidityPeriod& validityPeriod = info2.getValidityPeriod();
212 BOOST_CHECK(validityPeriod.getPeriod() == std::make_pair(notBefore, notAfter));
213}
214
Yingdi Yu4a557052014-07-09 16:40:37 -0700215BOOST_AUTO_TEST_CASE(OtherTlvs)
216{
217 SignatureInfo info;
218 BOOST_CHECK_EQUAL(info.getSignatureType(), -1);
219 BOOST_CHECK_EQUAL(info.hasKeyLocator(), false);
220 BOOST_CHECK_THROW(info.getKeyLocator(), SignatureInfo::Error);
221
222 const uint8_t tlv1[] = {
223 0x81, // T
224 0x01, // L
225 0x01, // V
226 };
227 Block block1(tlv1, sizeof(tlv1));
228
229 info.appendTypeSpecificTlv(block1);
230 BOOST_CHECK_THROW(info.getTypeSpecificTlv(0x82), SignatureInfo::Error);
231 BOOST_REQUIRE_NO_THROW(info.getTypeSpecificTlv(0x81));
232}
233
Alexander Afanasyev41469752017-01-10 21:51:55 -0800234BOOST_AUTO_TEST_CASE(OtherTlvsEncoding) // Bug #3914
235{
Junxiao Shi605671d2017-08-26 13:41:06 +0000236 SignatureInfo info1(tlv::SignatureSha256WithRsa);
Alexander Afanasyev41469752017-01-10 21:51:55 -0800237 info1.appendTypeSpecificTlv(makeStringBlock(101, "First"));
238 info1.appendTypeSpecificTlv(makeStringBlock(102, "Second"));
239 info1.appendTypeSpecificTlv(makeStringBlock(103, "Third"));
240
Junxiao Shi605671d2017-08-26 13:41:06 +0000241 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(info1), "SignatureSha256WithRsa { 101 102 103 }");
Alexander Afanasyev41469752017-01-10 21:51:55 -0800242
243 SignatureInfo info2;
244 info2.wireDecode(info1.wireEncode());
245 BOOST_CHECK_EQUAL(info1, info2);
246
Alexander Afanasyev41469752017-01-10 21:51:55 -0800247 const uint8_t infoBytes[] = {
Junxiao Shi605671d2017-08-26 13:41:06 +0000248 0x16, 0x19, // SignatureInfo
249 0x1b, 0x01, 0x01, // SignatureType=1
250 0x65, 0x05, 0x46, 0x69, 0x72, 0x73, 0x74, // 101 "First"
251 0x66, 0x06, 0x53, 0x65, 0x63, 0x6f, 0x6e, 0x64, // 102 "Second"
252 0x67, 0x05, 0x54, 0x68, 0x69, 0x72, 0x64 // 103 "Third"
Alexander Afanasyev41469752017-01-10 21:51:55 -0800253 };
254
255 SignatureInfo info3(Block(infoBytes, sizeof(infoBytes)));
256 BOOST_CHECK_EQUAL(info3, info1);
257 BOOST_CHECK_EQUAL_COLLECTIONS(infoBytes, infoBytes + sizeof(infoBytes),
258 info1.wireEncode().begin(), info1.wireEncode().end());
259}
260
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100261BOOST_AUTO_TEST_SUITE_END() // TestSignatureInfo
Yingdi Yu4a557052014-07-09 16:40:37 -0700262
Spyridon Mastorakis429634f2015-02-19 17:35:33 -0800263} // namespace tests
Yingdi Yu4a557052014-07-09 16:40:37 -0700264} // namespace ndn