Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 605671d | 2017-08-26 13:41:06 +0000 | [diff] [blame] | 2 | /* |
Davide Pesavento | 14c56cd | 2020-05-21 01:44:03 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2020 Regents of the University of California. |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 4 | * |
| 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 Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 22 | #include "ndn-cxx/signature-info.hpp" |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 23 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 24 | #include "tests/boost-test.hpp" |
Davide Pesavento | 14c56cd | 2020-05-21 01:44:03 -0400 | [diff] [blame] | 25 | |
Alexander Afanasyev | 4146975 | 2017-01-10 21:51:55 -0800 | [diff] [blame] | 26 | #include <boost/lexical_cast.hpp> |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 27 | |
| 28 | namespace ndn { |
Spyridon Mastorakis | 429634f | 2015-02-19 17:35:33 -0800 | [diff] [blame] | 29 | namespace tests { |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 30 | |
| 31 | BOOST_AUTO_TEST_SUITE(TestSignatureInfo) |
| 32 | |
Eric Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 33 | const uint8_t sigInfoDataRsa[] = { |
Davide Pesavento | 14c56cd | 2020-05-21 01:44:03 -0400 | [diff] [blame] | 34 | 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 Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 45 | }; |
| 46 | |
Eric Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 47 | const uint8_t sigInfoInterestRsa[] = { |
| 48 | 0x2c, 0x33, // InterestSignatureInfo |
| 49 | 0x1b, 0x01, // SignatureType |
| 50 | 0x01, // Sha256WithRsa |
| 51 | 0x1c, 0x16, // KeyLocator |
| 52 | 0x07, 0x14, // Name |
| 53 | 0x08, 0x04, |
| 54 | 0x74, 0x65, 0x73, 0x74, |
| 55 | 0x08, 0x03, |
| 56 | 0x6b, 0x65, 0x79, |
| 57 | 0x08, 0x07, |
| 58 | 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72, |
| 59 | 0x26, 0x08, // SignatureNonce |
| 60 | 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, |
| 61 | 0x28, 0x08, // SignatureTime |
| 62 | 0x00, 0x00, 0x01, 0x72, 0x3d, 0x77, 0x00, 0x00, |
| 63 | 0x2a, 0x02, // SignatureSeqNum |
| 64 | 0x10, 0x20, |
| 65 | }; |
| 66 | |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 67 | BOOST_AUTO_TEST_CASE(Constructor) |
| 68 | { |
| 69 | SignatureInfo info; |
| 70 | BOOST_CHECK_EQUAL(info.getSignatureType(), -1); |
| 71 | BOOST_CHECK_EQUAL(info.hasKeyLocator(), false); |
| 72 | BOOST_CHECK_THROW(info.getKeyLocator(), SignatureInfo::Error); |
Davide Pesavento | 14c56cd | 2020-05-21 01:44:03 -0400 | [diff] [blame] | 73 | BOOST_CHECK_THROW(info.getValidityPeriod(), SignatureInfo::Error); |
Eric Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 74 | BOOST_CHECK(!info.getNonce()); |
| 75 | BOOST_CHECK(!info.getTime()); |
| 76 | BOOST_CHECK(!info.getSeqNum()); |
Davide Pesavento | 7e6f6f8 | 2017-10-31 18:05:28 -0400 | [diff] [blame] | 77 | |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 78 | SignatureInfo sha256Info(tlv::DigestSha256); |
| 79 | BOOST_CHECK_EQUAL(sha256Info.getSignatureType(), tlv::DigestSha256); |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 80 | BOOST_CHECK_THROW(sha256Info.getKeyLocator(), SignatureInfo::Error); |
Eric Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 81 | BOOST_CHECK(!info.getNonce()); |
| 82 | BOOST_CHECK(!info.getTime()); |
| 83 | BOOST_CHECK(!info.getSeqNum()); |
Alexander Afanasyev | 4146975 | 2017-01-10 21:51:55 -0800 | [diff] [blame] | 84 | |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 85 | KeyLocator keyLocator("/test/key/locator"); |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 86 | SignatureInfo sha256RsaInfo(tlv::SignatureSha256WithRsa, keyLocator); |
| 87 | BOOST_CHECK_EQUAL(sha256RsaInfo.getSignatureType(), tlv::SignatureSha256WithRsa); |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 88 | BOOST_CHECK_EQUAL(sha256RsaInfo.hasKeyLocator(), true); |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 89 | BOOST_CHECK_EQUAL(sha256RsaInfo.getKeyLocator().getName(), Name("/test/key/locator")); |
Davide Pesavento | 14c56cd | 2020-05-21 01:44:03 -0400 | [diff] [blame] | 90 | BOOST_CHECK_THROW(sha256RsaInfo.getValidityPeriod(), SignatureInfo::Error); |
Eric Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 91 | BOOST_CHECK(!info.getNonce()); |
| 92 | BOOST_CHECK(!info.getTime()); |
| 93 | BOOST_CHECK(!info.getSeqNum()); |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Eric Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 96 | BOOST_AUTO_TEST_CASE(SignatureType) |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 97 | { |
| 98 | SignatureInfo info; |
| 99 | BOOST_CHECK_EQUAL(info.getSignatureType(), -1); |
Davide Pesavento | 14c56cd | 2020-05-21 01:44:03 -0400 | [diff] [blame] | 100 | BOOST_CHECK_THROW(info.wireEncode(), SignatureInfo::Error); |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 101 | |
Davide Pesavento | 14c56cd | 2020-05-21 01:44:03 -0400 | [diff] [blame] | 102 | info.setSignatureType(tlv::SignatureSha256WithEcdsa); |
| 103 | BOOST_CHECK_EQUAL(info.getSignatureType(), tlv::SignatureSha256WithEcdsa); |
| 104 | BOOST_CHECK_EQUAL(info.hasWire(), false); |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 105 | |
Davide Pesavento | 14c56cd | 2020-05-21 01:44:03 -0400 | [diff] [blame] | 106 | info.wireEncode(); |
| 107 | BOOST_CHECK_EQUAL(info.hasWire(), true); |
| 108 | info.setSignatureType(tlv::SignatureSha256WithEcdsa); |
| 109 | BOOST_CHECK_EQUAL(info.hasWire(), true); |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 110 | |
Davide Pesavento | 14c56cd | 2020-05-21 01:44:03 -0400 | [diff] [blame] | 111 | info.setSignatureType(static_cast<tlv::SignatureTypeValue>(1234)); |
| 112 | BOOST_CHECK_EQUAL(info.getSignatureType(), 1234); |
| 113 | BOOST_CHECK_EQUAL(info.hasWire(), false); |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 114 | } |
| 115 | |
Eric Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 116 | // We must name this test case differently to avoid naming conflicts |
| 117 | BOOST_AUTO_TEST_CASE(KeyLocatorField) |
Davide Pesavento | 14c56cd | 2020-05-21 01:44:03 -0400 | [diff] [blame] | 118 | { |
| 119 | SignatureInfo info(tlv::SignatureSha256WithEcdsa); |
| 120 | BOOST_CHECK_EQUAL(info.hasKeyLocator(), false); |
| 121 | BOOST_CHECK_THROW(info.getKeyLocator(), SignatureInfo::Error); |
| 122 | |
| 123 | info.setKeyLocator(Name("/test/key/locator")); |
| 124 | BOOST_CHECK_EQUAL(info.hasKeyLocator(), true); |
| 125 | BOOST_CHECK_EQUAL(info.getKeyLocator().getName(), "/test/key/locator"); |
| 126 | BOOST_CHECK_EQUAL(info.hasWire(), false); |
| 127 | |
| 128 | info.wireEncode(); |
| 129 | BOOST_CHECK_EQUAL(info.hasWire(), true); |
| 130 | info.setKeyLocator(Name("/test/key/locator")); |
| 131 | BOOST_CHECK_EQUAL(info.hasWire(), true); |
| 132 | info.setKeyLocator(Name("/another/key/locator")); |
| 133 | BOOST_CHECK_EQUAL(info.hasWire(), false); |
| 134 | |
| 135 | info.wireEncode(); |
| 136 | BOOST_CHECK_EQUAL(info.hasWire(), true); |
| 137 | info.setKeyLocator(nullopt); |
| 138 | BOOST_CHECK_EQUAL(info.hasKeyLocator(), false); |
| 139 | BOOST_CHECK_THROW(info.getKeyLocator(), SignatureInfo::Error); |
| 140 | BOOST_CHECK_EQUAL(info.hasWire(), false); |
| 141 | } |
| 142 | |
Eric Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 143 | BOOST_AUTO_TEST_CASE(SignatureNonce) |
| 144 | { |
| 145 | SignatureInfo info(tlv::SignatureSha256WithEcdsa); |
| 146 | BOOST_CHECK(!info.getNonce()); |
| 147 | info.wireEncode(); |
| 148 | BOOST_CHECK_EQUAL(info.hasWire(), true); |
| 149 | std::vector<uint8_t> nonce{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}; |
| 150 | info.setNonce(nonce); |
| 151 | BOOST_CHECK_EQUAL(info.hasWire(), false); |
| 152 | BOOST_CHECK(info.getNonce() == nonce); |
| 153 | |
| 154 | info.wireEncode(); |
| 155 | info.setNonce(nonce); |
| 156 | BOOST_CHECK_EQUAL(info.hasWire(), true); |
| 157 | nonce[2] = 0xFF; |
| 158 | info.setNonce(nonce); |
| 159 | BOOST_CHECK_EQUAL(info.hasWire(), false); |
| 160 | BOOST_CHECK(info.getNonce() == nonce); |
| 161 | |
| 162 | info.wireEncode(); |
| 163 | BOOST_CHECK_EQUAL(info.hasWire(), true); |
| 164 | info.setNonce(nullopt); |
| 165 | BOOST_CHECK_EQUAL(info.hasWire(), false); |
| 166 | BOOST_CHECK(!info.getNonce()); |
| 167 | } |
| 168 | |
| 169 | BOOST_AUTO_TEST_CASE(SignatureTime) |
| 170 | { |
| 171 | SignatureInfo info(tlv::SignatureSha256WithEcdsa); |
| 172 | BOOST_CHECK(!info.getTime()); |
| 173 | info.wireEncode(); |
| 174 | BOOST_CHECK_EQUAL(info.hasWire(), true); |
| 175 | time::system_clock::time_point timePoint(1590169108480_ms); |
| 176 | info.setTime(timePoint); |
| 177 | BOOST_CHECK_EQUAL(info.hasWire(), false); |
| 178 | BOOST_CHECK(info.getTime() == timePoint); |
| 179 | |
| 180 | info.wireEncode(); |
| 181 | info.setTime(timePoint); |
| 182 | BOOST_CHECK_EQUAL(info.hasWire(), true); |
| 183 | info.setTime(timePoint + 2_s); |
| 184 | BOOST_CHECK_EQUAL(info.hasWire(), false); |
| 185 | BOOST_CHECK(info.getTime() == timePoint + 2_s); |
| 186 | |
| 187 | info.wireEncode(); |
| 188 | BOOST_CHECK_EQUAL(info.hasWire(), true); |
| 189 | info.setTime(nullopt); |
| 190 | BOOST_CHECK_EQUAL(info.hasWire(), false); |
| 191 | BOOST_CHECK(!info.getTime()); |
| 192 | } |
| 193 | |
| 194 | BOOST_AUTO_TEST_CASE(SignatureSeqNum) |
| 195 | { |
| 196 | SignatureInfo info(tlv::SignatureSha256WithEcdsa); |
| 197 | BOOST_CHECK(!info.getSeqNum()); |
| 198 | info.wireEncode(); |
| 199 | BOOST_CHECK_EQUAL(info.hasWire(), true); |
| 200 | info.setSeqNum(256); |
| 201 | BOOST_CHECK_EQUAL(info.hasWire(), false); |
| 202 | BOOST_CHECK(info.getSeqNum() == 256UL); |
| 203 | |
| 204 | info.wireEncode(); |
| 205 | info.setSeqNum(256); |
| 206 | BOOST_CHECK_EQUAL(info.hasWire(), true); |
| 207 | info.setSeqNum(512); |
| 208 | BOOST_CHECK_EQUAL(info.hasWire(), false); |
| 209 | BOOST_CHECK(info.getSeqNum() == 512UL); |
| 210 | |
| 211 | info.wireEncode(); |
| 212 | BOOST_CHECK_EQUAL(info.hasWire(), true); |
| 213 | info.setSeqNum(nullopt); |
| 214 | BOOST_CHECK_EQUAL(info.hasWire(), false); |
| 215 | BOOST_CHECK(!info.getSeqNum()); |
| 216 | } |
| 217 | |
| 218 | BOOST_AUTO_TEST_CASE(EncodeDecode) |
| 219 | { |
| 220 | KeyLocator keyLocator("/test/key/locator"); |
| 221 | SignatureInfo info(tlv::SignatureSha256WithRsa, keyLocator); |
| 222 | |
| 223 | // Encode as SignatureInfo (for Data packets) |
| 224 | auto encodedData = info.wireEncode(SignatureInfo::Type::Data); |
| 225 | Block sigInfoDataBlock(sigInfoDataRsa, sizeof(sigInfoDataRsa)); |
| 226 | |
| 227 | BOOST_CHECK_EQUAL_COLLECTIONS(sigInfoDataBlock.wire(), sigInfoDataBlock.wire() + sigInfoDataBlock.size(), |
| 228 | encodedData.wire(), encodedData.wire() + encodedData.size()); |
| 229 | |
| 230 | // Decode as SignatureInfo (for Data packets) |
| 231 | info = SignatureInfo(sigInfoDataBlock, SignatureInfo::Type::Data); |
| 232 | BOOST_CHECK_EQUAL(info.getSignatureType(), tlv::SignatureSha256WithRsa); |
| 233 | BOOST_CHECK_EQUAL(info.hasKeyLocator(), true); |
| 234 | BOOST_CHECK_EQUAL(info.getKeyLocator().getName(), Name("/test/key/locator")); |
| 235 | |
| 236 | BOOST_CHECK(!info.getNonce()); |
| 237 | BOOST_CHECK(!info.getTime()); |
| 238 | BOOST_CHECK(!info.getSeqNum()); |
| 239 | |
| 240 | // Encode as InterestSignatureInfo |
| 241 | std::vector<uint8_t> nonce{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}; |
| 242 | info.setNonce(nonce); |
| 243 | time::system_clock::time_point timePoint(1590169108480_ms); |
| 244 | info.setTime(timePoint); |
| 245 | info.setSeqNum(0x1020); |
| 246 | auto encodedInterest = info.wireEncode(SignatureInfo::Type::Interest); |
| 247 | Block sigInfoInterestBlock(sigInfoInterestRsa, sizeof(sigInfoInterestRsa)); |
| 248 | |
| 249 | BOOST_CHECK_EQUAL_COLLECTIONS(sigInfoInterestBlock.wire(), |
| 250 | sigInfoInterestBlock.wire() + sigInfoInterestBlock.size(), |
| 251 | encodedInterest.wire(), |
| 252 | encodedInterest.wire() + encodedInterest.size()); |
| 253 | |
| 254 | // Decode as InterestSignatureInfo |
| 255 | info = SignatureInfo(sigInfoInterestBlock, SignatureInfo::Type::Interest); |
| 256 | BOOST_CHECK_EQUAL(info.getSignatureType(), tlv::SignatureSha256WithRsa); |
| 257 | BOOST_CHECK_EQUAL(info.hasKeyLocator(), true); |
| 258 | BOOST_CHECK_EQUAL(info.getKeyLocator().getName(), Name("/test/key/locator")); |
| 259 | BOOST_CHECK(info.getNonce() == nonce); |
| 260 | BOOST_CHECK(info.getTime() == timePoint); |
| 261 | BOOST_CHECK(info.getSeqNum() == 0x1020UL); |
| 262 | } |
| 263 | |
| 264 | BOOST_AUTO_TEST_CASE(DecodeError) |
| 265 | { |
| 266 | const uint8_t error1[] = { |
| 267 | 0x15, 0x1b, // Wrong SignatureInfo (0x16, 0x1b) |
| 268 | 0x1b, 0x01, // SignatureType |
| 269 | 0x01, // Sha256WithRsa |
| 270 | 0x1c, 0x16, // KeyLocator |
| 271 | 0x07, 0x14, // Name |
| 272 | 0x08, 0x04, |
| 273 | 0x74, 0x65, 0x73, 0x74, |
| 274 | 0x08, 0x03, |
| 275 | 0x6b, 0x65, 0x79, |
| 276 | 0x08, 0x07, |
| 277 | 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72 |
| 278 | }; |
| 279 | Block errorBlock1(error1, sizeof(error1)); |
| 280 | BOOST_CHECK_THROW(SignatureInfo(errorBlock1, SignatureInfo::Type::Data), tlv::Error); |
| 281 | |
| 282 | const uint8_t error2[] = { |
| 283 | 0x16, 0x05, // SignatureInfo |
| 284 | 0x1b, 0x01, // SignatureType |
| 285 | 0x01, // Sha256WithRsa |
| 286 | 0x83, 0x00, // Unrecognized critical TLV |
| 287 | }; |
| 288 | Block errorBlock2(error2, sizeof(error2)); |
| 289 | BOOST_CHECK_THROW(SignatureInfo(errorBlock2, SignatureInfo::Type::Data), tlv::Error); |
| 290 | |
| 291 | const uint8_t error3[] = { |
| 292 | 0x16, 0x00 // Empty SignatureInfo |
| 293 | }; |
| 294 | Block errorBlock3(error3, sizeof(error3)); |
| 295 | BOOST_CHECK_THROW(SignatureInfo(errorBlock3, SignatureInfo::Type::Data), tlv::Error); |
| 296 | |
| 297 | // Encoding is correct for SignatureInfo, but decoder is expecting InterestSignatureInfo |
| 298 | const uint8_t error4[] = { |
| 299 | 0x16, 0x1b, // SignatureInfo |
| 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 | }; |
| 311 | Block errorBlock4(error4, sizeof(error4)); |
| 312 | BOOST_CHECK_THROW(SignatureInfo(errorBlock4, SignatureInfo::Type::Interest), tlv::Error); |
| 313 | |
| 314 | // SignatureType and KeyLocator out-of-order |
| 315 | const uint8_t error5[] = { |
| 316 | 0x2c, 0x1b, // SignatureInfo |
| 317 | 0x1c, 0x16, // KeyLocator |
| 318 | 0x07, 0x14, // Name |
| 319 | 0x08, 0x04, |
| 320 | 0x74, 0x65, 0x73, 0x74, |
| 321 | 0x08, 0x03, |
| 322 | 0x6b, 0x65, 0x79, |
| 323 | 0x08, 0x07, |
| 324 | 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72, |
| 325 | 0x1b, 0x01, // SignatureType |
| 326 | 0x01, // Sha256WithRsa |
| 327 | }; |
| 328 | Block errorBlock5(error5, sizeof(error5)); |
| 329 | BOOST_CHECK_THROW(SignatureInfo(errorBlock5, SignatureInfo::Type::Interest), tlv::Error); |
| 330 | |
| 331 | // Repeated KeyLocator |
| 332 | const uint8_t error6[] = { |
| 333 | 0x2c, 0x33, // SignatureInfo |
| 334 | 0x1b, 0x01, // SignatureType |
| 335 | 0x01, // Sha256WithRsa |
| 336 | 0x1c, 0x16, // KeyLocator |
| 337 | 0x07, 0x14, // Name |
| 338 | 0x08, 0x04, |
| 339 | 0x74, 0x65, 0x73, 0x74, |
| 340 | 0x08, 0x03, |
| 341 | 0x6b, 0x65, 0x79, |
| 342 | 0x08, 0x07, |
| 343 | 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72, |
| 344 | 0x1c, 0x16, // KeyLocator |
| 345 | 0x07, 0x14, // Name |
| 346 | 0x08, 0x04, |
| 347 | 0x74, 0x65, 0x73, 0x74, |
| 348 | 0x08, 0x03, |
| 349 | 0x6b, 0x65, 0x79, |
| 350 | 0x08, 0x07, |
| 351 | 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72 |
| 352 | }; |
| 353 | Block errorBlock6(error6, sizeof(error6)); |
| 354 | BOOST_CHECK_THROW(SignatureInfo(errorBlock6, SignatureInfo::Type::Interest), tlv::Error); |
| 355 | |
| 356 | // Zero-length SignatureNonce |
| 357 | const uint8_t error7[] = { |
| 358 | 0x2c, 0x05, // SignatureInfo |
| 359 | 0x1b, 0x01, // SignatureType |
| 360 | 0x01, // Sha256WithRsa |
| 361 | 0x26, 0x00 // SignatureNonce |
| 362 | }; |
| 363 | Block errorBlock7(error7, sizeof(error7)); |
| 364 | BOOST_CHECK_THROW(SignatureInfo(errorBlock7, SignatureInfo::Type::Interest), tlv::Error); |
| 365 | } |
| 366 | |
Davide Pesavento | 14c56cd | 2020-05-21 01:44:03 -0400 | [diff] [blame] | 367 | BOOST_AUTO_TEST_CASE(ValidityPeriod) |
Yingdi Yu | 6be43f3 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 368 | { |
| 369 | const uint8_t sigInfo[] = { |
| 370 | 0x16, 0x45, // SignatureInfo |
| 371 | 0x1b, 0x01, // SignatureType |
| 372 | 0x01, // Sha256WithRsa |
| 373 | 0x1c, 0x16, // KeyLocator |
| 374 | 0x07, 0x14, // Name |
| 375 | 0x08, 0x04, |
| 376 | 0x74, 0x65, 0x73, 0x74, |
| 377 | 0x08, 0x03, |
| 378 | 0x6b, 0x65, 0x79, |
| 379 | 0x08, 0x07, |
| 380 | 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72, |
| 381 | 0xfd, 0x00, 0xfd, 0x26, // ValidityPeriod |
| 382 | 0xfd, 0x00, 0xfe, 0x0f, // NotBefore |
| 383 | 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x31, // 19700101T000000 |
| 384 | 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, |
| 385 | 0xfd, 0x00, 0xff, 0x0f, // NotAfter |
| 386 | 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x32, // 19700102T000000 |
| 387 | 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30 |
| 388 | }; |
| 389 | |
Davide Pesavento | 14c56cd | 2020-05-21 01:44:03 -0400 | [diff] [blame] | 390 | const auto notBefore = time::getUnixEpoch(); |
| 391 | const auto notAfter = notBefore + 1_day; |
Yingdi Yu | 6be43f3 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 392 | security::ValidityPeriod vp1(notBefore, notAfter); |
| 393 | |
Davide Pesavento | 14c56cd | 2020-05-21 01:44:03 -0400 | [diff] [blame] | 394 | SignatureInfo info(tlv::SignatureSha256WithRsa, KeyLocator("/test/key/locator")); |
Davide Pesavento | 14c56cd | 2020-05-21 01:44:03 -0400 | [diff] [blame] | 395 | BOOST_CHECK_THROW(info.getValidityPeriod(), SignatureInfo::Error); |
| 396 | |
| 397 | info.wireEncode(); |
Yingdi Yu | 6be43f3 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 398 | info.setValidityPeriod(vp1); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 399 | BOOST_CHECK_EQUAL(info.getValidityPeriod(), vp1); |
Davide Pesavento | 14c56cd | 2020-05-21 01:44:03 -0400 | [diff] [blame] | 400 | BOOST_CHECK_EQUAL(info.hasWire(), false); |
Yingdi Yu | 6be43f3 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 401 | |
Davide Pesavento | 14c56cd | 2020-05-21 01:44:03 -0400 | [diff] [blame] | 402 | // encode |
| 403 | auto encoded = info.wireEncode(); |
| 404 | BOOST_CHECK_EQUAL(info.hasWire(), true); |
Yingdi Yu | 6be43f3 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 405 | BOOST_CHECK_EQUAL_COLLECTIONS(sigInfo, sigInfo + sizeof(sigInfo), |
| 406 | encoded.wire(), encoded.wire() + encoded.size()); |
| 407 | |
Davide Pesavento | 14c56cd | 2020-05-21 01:44:03 -0400 | [diff] [blame] | 408 | info.setValidityPeriod(vp1); |
| 409 | BOOST_CHECK_EQUAL(info.hasWire(), true); |
| 410 | |
Yingdi Yu | 6be43f3 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 411 | // decode |
| 412 | Block block(sigInfo, sizeof(sigInfo)); |
Eric Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 413 | SignatureInfo info2(block, SignatureInfo::Type::Data); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 414 | BOOST_CHECK_EQUAL(info2.getValidityPeriod(), vp1); |
Davide Pesavento | 14c56cd | 2020-05-21 01:44:03 -0400 | [diff] [blame] | 415 | BOOST_CHECK_EQUAL(info2.hasWire(), true); |
Yingdi Yu | 6be43f3 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 416 | |
Davide Pesavento | 14c56cd | 2020-05-21 01:44:03 -0400 | [diff] [blame] | 417 | info2.setValidityPeriod(security::ValidityPeriod(notBefore, notBefore + 42_days)); |
Davide Pesavento | 14c56cd | 2020-05-21 01:44:03 -0400 | [diff] [blame] | 418 | BOOST_CHECK_NE(info2.getValidityPeriod(), vp1); |
| 419 | BOOST_CHECK(info2.getValidityPeriod().getPeriod() == std::make_pair(notBefore, notBefore + 42_days)); |
| 420 | BOOST_CHECK_EQUAL(info2.hasWire(), false); |
| 421 | |
| 422 | info2.wireEncode(); |
| 423 | BOOST_CHECK_EQUAL(info2.hasWire(), true); |
| 424 | info2.setValidityPeriod(nullopt); |
Davide Pesavento | 14c56cd | 2020-05-21 01:44:03 -0400 | [diff] [blame] | 425 | BOOST_CHECK_THROW(info2.getValidityPeriod(), SignatureInfo::Error); |
| 426 | BOOST_CHECK_EQUAL(info2.hasWire(), false); |
Yingdi Yu | 6be43f3 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 427 | } |
| 428 | |
Eric Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 429 | BOOST_AUTO_TEST_CASE(CustomTlvs) |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 430 | { |
Davide Pesavento | 14c56cd | 2020-05-21 01:44:03 -0400 | [diff] [blame] | 431 | SignatureInfo info(tlv::SignatureSha256WithEcdsa); |
Eric Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 432 | info.addCustomTlv("810101"_block); |
| 433 | BOOST_CHECK(!info.getCustomTlv(0x82)); |
| 434 | BOOST_REQUIRE(info.getCustomTlv(0x81)); |
| 435 | BOOST_CHECK_EQUAL(info.getCustomTlv(0x81)->type(), 0x81); |
Davide Pesavento | 14c56cd | 2020-05-21 01:44:03 -0400 | [diff] [blame] | 436 | |
| 437 | info.wireEncode(); |
| 438 | BOOST_CHECK_EQUAL(info.hasWire(), true); |
Eric Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 439 | info.addCustomTlv("82020202"_block); |
Davide Pesavento | 14c56cd | 2020-05-21 01:44:03 -0400 | [diff] [blame] | 440 | BOOST_CHECK_EQUAL(info.hasWire(), false); |
Eric Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 441 | |
| 442 | info.wireEncode(); |
| 443 | BOOST_CHECK_EQUAL(info.hasWire(), true); |
| 444 | info.removeCustomTlv(0x81); |
| 445 | BOOST_CHECK_EQUAL(info.hasWire(), false); |
| 446 | BOOST_CHECK(!info.getCustomTlv(0x81)); |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 447 | } |
| 448 | |
Eric Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 449 | BOOST_AUTO_TEST_CASE(CustomTlvsEncoding) // Bug #3914 |
Alexander Afanasyev | 4146975 | 2017-01-10 21:51:55 -0800 | [diff] [blame] | 450 | { |
Junxiao Shi | 605671d | 2017-08-26 13:41:06 +0000 | [diff] [blame] | 451 | SignatureInfo info1(tlv::SignatureSha256WithRsa); |
Eric Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 452 | info1.appendTypeSpecificTlv(makeStringBlock(102, "First")); |
| 453 | info1.appendTypeSpecificTlv(makeStringBlock(104, "Second")); |
| 454 | info1.appendTypeSpecificTlv(makeStringBlock(106, "Third")); |
Alexander Afanasyev | 4146975 | 2017-01-10 21:51:55 -0800 | [diff] [blame] | 455 | |
Eric Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 456 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(info1), "SignatureSha256WithRsa { 102 104 106 }"); |
Alexander Afanasyev | 4146975 | 2017-01-10 21:51:55 -0800 | [diff] [blame] | 457 | |
| 458 | SignatureInfo info2; |
Eric Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 459 | info2.wireDecode(info1.wireEncode(), SignatureInfo::Type::Data); |
Alexander Afanasyev | 4146975 | 2017-01-10 21:51:55 -0800 | [diff] [blame] | 460 | BOOST_CHECK_EQUAL(info1, info2); |
| 461 | |
Alexander Afanasyev | 4146975 | 2017-01-10 21:51:55 -0800 | [diff] [blame] | 462 | const uint8_t infoBytes[] = { |
Junxiao Shi | 605671d | 2017-08-26 13:41:06 +0000 | [diff] [blame] | 463 | 0x16, 0x19, // SignatureInfo |
| 464 | 0x1b, 0x01, 0x01, // SignatureType=1 |
Eric Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 465 | 0x66, 0x05, 0x46, 0x69, 0x72, 0x73, 0x74, // 102 "First" |
| 466 | 0x68, 0x06, 0x53, 0x65, 0x63, 0x6f, 0x6e, 0x64, // 104 "Second" |
| 467 | 0x6a, 0x05, 0x54, 0x68, 0x69, 0x72, 0x64 // 106 "Third" |
Alexander Afanasyev | 4146975 | 2017-01-10 21:51:55 -0800 | [diff] [blame] | 468 | }; |
| 469 | |
Eric Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 470 | SignatureInfo info3(Block(infoBytes, sizeof(infoBytes)), SignatureInfo::Type::Data); |
Alexander Afanasyev | 4146975 | 2017-01-10 21:51:55 -0800 | [diff] [blame] | 471 | BOOST_CHECK_EQUAL(info3, info1); |
| 472 | BOOST_CHECK_EQUAL_COLLECTIONS(infoBytes, infoBytes + sizeof(infoBytes), |
| 473 | info1.wireEncode().begin(), info1.wireEncode().end()); |
| 474 | } |
| 475 | |
Eric Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 476 | BOOST_AUTO_TEST_CASE(OutputStream) |
| 477 | { |
| 478 | SignatureInfo info; |
| 479 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(info), "Invalid SignatureInfo"); |
| 480 | |
| 481 | info.setSignatureType(tlv::DigestSha256); |
| 482 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(info), "DigestSha256"); |
| 483 | |
| 484 | info.setSignatureType(tlv::SignatureSha256WithRsa); |
| 485 | info.setKeyLocator(KeyLocator("/test/key/locator")); |
| 486 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(info), |
| 487 | "SignatureSha256WithRsa Name=/test/key/locator"); |
| 488 | |
| 489 | info.setNonce(std::vector<uint8_t>{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}); |
| 490 | info.setTime(time::system_clock::time_point(1590169108480_ms)); |
| 491 | info.setSeqNum(0x1020); |
| 492 | |
| 493 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(info), |
| 494 | "SignatureSha256WithRsa Name=/test/key/locator " |
| 495 | "{ Nonce=0102030405060708 Time=1590169108480 SeqNum=4128 }"); |
| 496 | |
| 497 | info.setValidityPeriod(security::ValidityPeriod(time::getUnixEpoch(), time::getUnixEpoch() + 31_days)); |
| 498 | |
| 499 | info.addCustomTlv("82020102"_block); |
| 500 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(info), |
| 501 | "SignatureSha256WithRsa Name=/test/key/locator " |
| 502 | "{ Nonce=0102030405060708 Time=1590169108480 SeqNum=4128 " |
| 503 | "ValidityPeriod=(19700101T000000, 19700201T000000) 130 }"); |
| 504 | |
| 505 | info.addCustomTlv("84020102"_block); |
| 506 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(info), |
| 507 | "SignatureSha256WithRsa Name=/test/key/locator " |
| 508 | "{ Nonce=0102030405060708 Time=1590169108480 SeqNum=4128 " |
| 509 | "ValidityPeriod=(19700101T000000, 19700201T000000) 130 132 }"); |
| 510 | } |
| 511 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 512 | BOOST_AUTO_TEST_SUITE_END() // TestSignatureInfo |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 513 | |
Spyridon Mastorakis | 429634f | 2015-02-19 17:35:33 -0800 | [diff] [blame] | 514 | } // namespace tests |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 515 | } // namespace ndn |