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 | fbea4fc | 2022-02-08 07:26:04 -0500 | [diff] [blame] | 3 | * Copyright (c) 2013-2022 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 | |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 33 | BOOST_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 Pesavento | 14c56cd | 2020-05-21 01:44:03 -0400 | [diff] [blame] | 39 | BOOST_CHECK_THROW(info.getValidityPeriod(), SignatureInfo::Error); |
Eric Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 40 | BOOST_CHECK(!info.getNonce()); |
| 41 | BOOST_CHECK(!info.getTime()); |
| 42 | BOOST_CHECK(!info.getSeqNum()); |
Davide Pesavento | 7e6f6f8 | 2017-10-31 18:05:28 -0400 | [diff] [blame] | 43 | |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 44 | SignatureInfo sha256Info(tlv::DigestSha256); |
| 45 | BOOST_CHECK_EQUAL(sha256Info.getSignatureType(), tlv::DigestSha256); |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 46 | BOOST_CHECK_THROW(sha256Info.getKeyLocator(), SignatureInfo::Error); |
Eric Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 47 | BOOST_CHECK(!info.getNonce()); |
| 48 | BOOST_CHECK(!info.getTime()); |
| 49 | BOOST_CHECK(!info.getSeqNum()); |
Alexander Afanasyev | 4146975 | 2017-01-10 21:51:55 -0800 | [diff] [blame] | 50 | |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 51 | KeyLocator keyLocator("/test/key/locator"); |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 52 | SignatureInfo sha256RsaInfo(tlv::SignatureSha256WithRsa, keyLocator); |
| 53 | BOOST_CHECK_EQUAL(sha256RsaInfo.getSignatureType(), tlv::SignatureSha256WithRsa); |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 54 | BOOST_CHECK_EQUAL(sha256RsaInfo.hasKeyLocator(), true); |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 55 | BOOST_CHECK_EQUAL(sha256RsaInfo.getKeyLocator().getName(), Name("/test/key/locator")); |
Davide Pesavento | 14c56cd | 2020-05-21 01:44:03 -0400 | [diff] [blame] | 56 | BOOST_CHECK_THROW(sha256RsaInfo.getValidityPeriod(), SignatureInfo::Error); |
Eric Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 57 | BOOST_CHECK(!info.getNonce()); |
| 58 | BOOST_CHECK(!info.getTime()); |
| 59 | BOOST_CHECK(!info.getSeqNum()); |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 60 | } |
| 61 | |
Eric Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 62 | BOOST_AUTO_TEST_CASE(SignatureType) |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 63 | { |
| 64 | SignatureInfo info; |
| 65 | BOOST_CHECK_EQUAL(info.getSignatureType(), -1); |
Davide Pesavento | 14c56cd | 2020-05-21 01:44:03 -0400 | [diff] [blame] | 66 | BOOST_CHECK_THROW(info.wireEncode(), SignatureInfo::Error); |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 67 | |
Davide Pesavento | 14c56cd | 2020-05-21 01:44:03 -0400 | [diff] [blame] | 68 | info.setSignatureType(tlv::SignatureSha256WithEcdsa); |
| 69 | BOOST_CHECK_EQUAL(info.getSignatureType(), tlv::SignatureSha256WithEcdsa); |
| 70 | BOOST_CHECK_EQUAL(info.hasWire(), false); |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 71 | |
Davide Pesavento | 14c56cd | 2020-05-21 01:44:03 -0400 | [diff] [blame] | 72 | info.wireEncode(); |
| 73 | BOOST_CHECK_EQUAL(info.hasWire(), true); |
| 74 | info.setSignatureType(tlv::SignatureSha256WithEcdsa); |
| 75 | BOOST_CHECK_EQUAL(info.hasWire(), true); |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 76 | |
Davide Pesavento | 14c56cd | 2020-05-21 01:44:03 -0400 | [diff] [blame] | 77 | info.setSignatureType(static_cast<tlv::SignatureTypeValue>(1234)); |
| 78 | BOOST_CHECK_EQUAL(info.getSignatureType(), 1234); |
| 79 | BOOST_CHECK_EQUAL(info.hasWire(), false); |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Eric Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 82 | // We must name this test case differently to avoid naming conflicts |
| 83 | BOOST_AUTO_TEST_CASE(KeyLocatorField) |
Davide Pesavento | 14c56cd | 2020-05-21 01:44:03 -0400 | [diff] [blame] | 84 | { |
| 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 Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 109 | BOOST_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 | |
| 135 | BOOST_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 | |
| 160 | BOOST_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 Pesavento | a86300a | 2020-12-05 14:55:45 -0500 | [diff] [blame] | 184 | const 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 | |
| 198 | const 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 | |
| 218 | const 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 Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 232 | BOOST_AUTO_TEST_CASE(EncodeDecode) |
| 233 | { |
Davide Pesavento | a86300a | 2020-12-05 14:55:45 -0500 | [diff] [blame] | 234 | const KeyLocator keyLocator("/test/key/locator"); |
| 235 | |
| 236 | // RSA |
Eric Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 237 | SignatureInfo info(tlv::SignatureSha256WithRsa, keyLocator); |
| 238 | |
Davide Pesavento | a86300a | 2020-12-05 14:55:45 -0500 | [diff] [blame] | 239 | // Encode as (Data)SignatureInfo |
Eric Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 240 | auto encodedData = info.wireEncode(SignatureInfo::Type::Data); |
Eric Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 241 | |
Davide Pesavento | a86300a | 2020-12-05 14:55:45 -0500 | [diff] [blame] | 242 | BOOST_CHECK_EQUAL_COLLECTIONS(sigInfoDataRsa, sigInfoDataRsa + sizeof(sigInfoDataRsa), |
| 243 | encodedData.begin(), encodedData.end()); |
Eric Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 244 | |
Davide Pesavento | a86300a | 2020-12-05 14:55:45 -0500 | [diff] [blame] | 245 | // Decode as (Data)SignatureInfo |
Davide Pesavento | fbea4fc | 2022-02-08 07:26:04 -0500 | [diff] [blame] | 246 | info = SignatureInfo(Block(sigInfoDataRsa), SignatureInfo::Type::Data); |
Davide Pesavento | a86300a | 2020-12-05 14:55:45 -0500 | [diff] [blame] | 247 | |
Eric Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 248 | 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 Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 251 | BOOST_CHECK(!info.getNonce()); |
| 252 | BOOST_CHECK(!info.getTime()); |
| 253 | BOOST_CHECK(!info.getSeqNum()); |
| 254 | |
| 255 | // Encode as InterestSignatureInfo |
Davide Pesavento | a86300a | 2020-12-05 14:55:45 -0500 | [diff] [blame] | 256 | const std::vector<uint8_t> nonce{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}; |
Eric Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 257 | info.setNonce(nonce); |
Davide Pesavento | a86300a | 2020-12-05 14:55:45 -0500 | [diff] [blame] | 258 | const time::system_clock::time_point timePoint(1590169108480_ms); |
Eric Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 259 | info.setTime(timePoint); |
| 260 | info.setSeqNum(0x1020); |
| 261 | auto encodedInterest = info.wireEncode(SignatureInfo::Type::Interest); |
Eric Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 262 | |
Davide Pesavento | a86300a | 2020-12-05 14:55:45 -0500 | [diff] [blame] | 263 | BOOST_CHECK_EQUAL_COLLECTIONS(sigInfoInterestRsa, sigInfoInterestRsa + sizeof(sigInfoInterestRsa), |
| 264 | encodedInterest.begin(), encodedInterest.end()); |
Eric Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 265 | |
| 266 | // Decode as InterestSignatureInfo |
Davide Pesavento | fbea4fc | 2022-02-08 07:26:04 -0500 | [diff] [blame] | 267 | info = SignatureInfo(Block(sigInfoInterestRsa), SignatureInfo::Type::Interest); |
Davide Pesavento | a86300a | 2020-12-05 14:55:45 -0500 | [diff] [blame] | 268 | |
Eric Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 269 | 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 Pesavento | a86300a | 2020-12-05 14:55:45 -0500 | [diff] [blame] | 275 | |
| 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 Pesavento | fbea4fc | 2022-02-08 07:26:04 -0500 | [diff] [blame] | 286 | info = SignatureInfo(Block(sigInfoDataEcdsa), SignatureInfo::Type::Data); |
Davide Pesavento | a86300a | 2020-12-05 14:55:45 -0500 | [diff] [blame] | 287 | |
| 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 Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | BOOST_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 Pesavento | fbea4fc | 2022-02-08 07:26:04 -0500 | [diff] [blame] | 311 | Block errorBlock1(error1); |
Eric Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 312 | 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 Pesavento | fbea4fc | 2022-02-08 07:26:04 -0500 | [diff] [blame] | 320 | Block errorBlock2(error2); |
Eric Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 321 | BOOST_CHECK_THROW(SignatureInfo(errorBlock2, SignatureInfo::Type::Data), tlv::Error); |
| 322 | |
| 323 | const uint8_t error3[] = { |
| 324 | 0x16, 0x00 // Empty SignatureInfo |
| 325 | }; |
Davide Pesavento | fbea4fc | 2022-02-08 07:26:04 -0500 | [diff] [blame] | 326 | Block errorBlock3(error3); |
Eric Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 327 | 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 Pesavento | fbea4fc | 2022-02-08 07:26:04 -0500 | [diff] [blame] | 343 | Block errorBlock4(error4); |
Eric Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 344 | 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 Pesavento | fbea4fc | 2022-02-08 07:26:04 -0500 | [diff] [blame] | 360 | Block errorBlock5(error5); |
Eric Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 361 | 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 Pesavento | fbea4fc | 2022-02-08 07:26:04 -0500 | [diff] [blame] | 385 | Block errorBlock6(error6); |
Eric Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 386 | 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 Pesavento | fbea4fc | 2022-02-08 07:26:04 -0500 | [diff] [blame] | 395 | Block errorBlock7(error7); |
Eric Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 396 | BOOST_CHECK_THROW(SignatureInfo(errorBlock7, SignatureInfo::Type::Interest), tlv::Error); |
| 397 | } |
| 398 | |
Davide Pesavento | 14c56cd | 2020-05-21 01:44:03 -0400 | [diff] [blame] | 399 | BOOST_AUTO_TEST_CASE(ValidityPeriod) |
Yingdi Yu | 6be43f3 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 400 | { |
| 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 Pesavento | 14c56cd | 2020-05-21 01:44:03 -0400 | [diff] [blame] | 422 | const auto notBefore = time::getUnixEpoch(); |
| 423 | const auto notAfter = notBefore + 1_day; |
Yingdi Yu | 6be43f3 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 424 | security::ValidityPeriod vp1(notBefore, notAfter); |
| 425 | |
Davide Pesavento | 14c56cd | 2020-05-21 01:44:03 -0400 | [diff] [blame] | 426 | SignatureInfo info(tlv::SignatureSha256WithRsa, KeyLocator("/test/key/locator")); |
Davide Pesavento | 14c56cd | 2020-05-21 01:44:03 -0400 | [diff] [blame] | 427 | BOOST_CHECK_THROW(info.getValidityPeriod(), SignatureInfo::Error); |
| 428 | |
| 429 | info.wireEncode(); |
Yingdi Yu | 6be43f3 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 430 | info.setValidityPeriod(vp1); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 431 | BOOST_CHECK_EQUAL(info.getValidityPeriod(), vp1); |
Davide Pesavento | 14c56cd | 2020-05-21 01:44:03 -0400 | [diff] [blame] | 432 | BOOST_CHECK_EQUAL(info.hasWire(), false); |
Yingdi Yu | 6be43f3 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 433 | |
Davide Pesavento | 14c56cd | 2020-05-21 01:44:03 -0400 | [diff] [blame] | 434 | // encode |
| 435 | auto encoded = info.wireEncode(); |
| 436 | BOOST_CHECK_EQUAL(info.hasWire(), true); |
Davide Pesavento | 258d51a | 2022-02-27 21:26:28 -0500 | [diff] [blame^] | 437 | BOOST_TEST(encoded == sigInfo, boost::test_tools::per_element()); |
Yingdi Yu | 6be43f3 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 438 | |
Davide Pesavento | 14c56cd | 2020-05-21 01:44:03 -0400 | [diff] [blame] | 439 | info.setValidityPeriod(vp1); |
| 440 | BOOST_CHECK_EQUAL(info.hasWire(), true); |
| 441 | |
Yingdi Yu | 6be43f3 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 442 | // decode |
Davide Pesavento | fbea4fc | 2022-02-08 07:26:04 -0500 | [diff] [blame] | 443 | Block block(sigInfo); |
Eric Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 444 | SignatureInfo info2(block, SignatureInfo::Type::Data); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 445 | BOOST_CHECK_EQUAL(info2.getValidityPeriod(), vp1); |
Davide Pesavento | 14c56cd | 2020-05-21 01:44:03 -0400 | [diff] [blame] | 446 | BOOST_CHECK_EQUAL(info2.hasWire(), true); |
Yingdi Yu | 6be43f3 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 447 | |
Davide Pesavento | 14c56cd | 2020-05-21 01:44:03 -0400 | [diff] [blame] | 448 | info2.setValidityPeriod(security::ValidityPeriod(notBefore, notBefore + 42_days)); |
Davide Pesavento | 14c56cd | 2020-05-21 01:44:03 -0400 | [diff] [blame] | 449 | 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); |
| 455 | info2.setValidityPeriod(nullopt); |
Davide Pesavento | 14c56cd | 2020-05-21 01:44:03 -0400 | [diff] [blame] | 456 | BOOST_CHECK_THROW(info2.getValidityPeriod(), SignatureInfo::Error); |
| 457 | BOOST_CHECK_EQUAL(info2.hasWire(), false); |
Yingdi Yu | 6be43f3 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 458 | } |
| 459 | |
Eric Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 460 | BOOST_AUTO_TEST_CASE(CustomTlvs) |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 461 | { |
Davide Pesavento | 14c56cd | 2020-05-21 01:44:03 -0400 | [diff] [blame] | 462 | SignatureInfo info(tlv::SignatureSha256WithEcdsa); |
Eric Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 463 | 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 Pesavento | 14c56cd | 2020-05-21 01:44:03 -0400 | [diff] [blame] | 467 | |
| 468 | info.wireEncode(); |
| 469 | BOOST_CHECK_EQUAL(info.hasWire(), true); |
Eric Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 470 | info.addCustomTlv("82020202"_block); |
Davide Pesavento | 14c56cd | 2020-05-21 01:44:03 -0400 | [diff] [blame] | 471 | BOOST_CHECK_EQUAL(info.hasWire(), false); |
Eric Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 472 | |
| 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 Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 478 | } |
| 479 | |
Eric Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 480 | BOOST_AUTO_TEST_CASE(CustomTlvsEncoding) // Bug #3914 |
Alexander Afanasyev | 4146975 | 2017-01-10 21:51:55 -0800 | [diff] [blame] | 481 | { |
Junxiao Shi | 605671d | 2017-08-26 13:41:06 +0000 | [diff] [blame] | 482 | SignatureInfo info1(tlv::SignatureSha256WithRsa); |
Davide Pesavento | a86300a | 2020-12-05 14:55:45 -0500 | [diff] [blame] | 483 | info1.addCustomTlv(makeStringBlock(102, "First")); |
| 484 | info1.addCustomTlv(makeStringBlock(104, "Second")); |
| 485 | info1.addCustomTlv(makeStringBlock(106, "Third")); |
Alexander Afanasyev | 4146975 | 2017-01-10 21:51:55 -0800 | [diff] [blame] | 486 | |
Davide Pesavento | a86300a | 2020-12-05 14:55:45 -0500 | [diff] [blame] | 487 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(info1), |
| 488 | "SignatureSha256WithRsa { 102 104 106 }"); |
Alexander Afanasyev | 4146975 | 2017-01-10 21:51:55 -0800 | [diff] [blame] | 489 | |
| 490 | SignatureInfo info2; |
Eric Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 491 | info2.wireDecode(info1.wireEncode(), SignatureInfo::Type::Data); |
Alexander Afanasyev | 4146975 | 2017-01-10 21:51:55 -0800 | [diff] [blame] | 492 | BOOST_CHECK_EQUAL(info1, info2); |
| 493 | |
Alexander Afanasyev | 4146975 | 2017-01-10 21:51:55 -0800 | [diff] [blame] | 494 | const uint8_t infoBytes[] = { |
Junxiao Shi | 605671d | 2017-08-26 13:41:06 +0000 | [diff] [blame] | 495 | 0x16, 0x19, // SignatureInfo |
| 496 | 0x1b, 0x01, 0x01, // SignatureType=1 |
Eric Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 497 | 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 Afanasyev | 4146975 | 2017-01-10 21:51:55 -0800 | [diff] [blame] | 500 | }; |
| 501 | |
Davide Pesavento | fbea4fc | 2022-02-08 07:26:04 -0500 | [diff] [blame] | 502 | SignatureInfo info3(Block(infoBytes), SignatureInfo::Type::Data); |
Alexander Afanasyev | 4146975 | 2017-01-10 21:51:55 -0800 | [diff] [blame] | 503 | BOOST_CHECK_EQUAL(info3, info1); |
| 504 | BOOST_CHECK_EQUAL_COLLECTIONS(infoBytes, infoBytes + sizeof(infoBytes), |
| 505 | info1.wireEncode().begin(), info1.wireEncode().end()); |
| 506 | } |
| 507 | |
Eric Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 508 | BOOST_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 Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 544 | BOOST_AUTO_TEST_SUITE_END() // TestSignatureInfo |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 545 | |
Spyridon Mastorakis | 429634f | 2015-02-19 17:35:33 -0800 | [diff] [blame] | 546 | } // namespace tests |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 547 | } // namespace ndn |