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 | |
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 |
| 246 | info = SignatureInfo(Block(sigInfoDataRsa, sizeof(sigInfoDataRsa)), |
| 247 | SignatureInfo::Type::Data); |
| 248 | |
Eric Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 249 | 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 Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 252 | BOOST_CHECK(!info.getNonce()); |
| 253 | BOOST_CHECK(!info.getTime()); |
| 254 | BOOST_CHECK(!info.getSeqNum()); |
| 255 | |
| 256 | // Encode as InterestSignatureInfo |
Davide Pesavento | a86300a | 2020-12-05 14:55:45 -0500 | [diff] [blame] | 257 | 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] | 258 | info.setNonce(nonce); |
Davide Pesavento | a86300a | 2020-12-05 14:55:45 -0500 | [diff] [blame] | 259 | const time::system_clock::time_point timePoint(1590169108480_ms); |
Eric Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 260 | info.setTime(timePoint); |
| 261 | info.setSeqNum(0x1020); |
| 262 | auto encodedInterest = info.wireEncode(SignatureInfo::Type::Interest); |
Eric Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 263 | |
Davide Pesavento | a86300a | 2020-12-05 14:55:45 -0500 | [diff] [blame] | 264 | BOOST_CHECK_EQUAL_COLLECTIONS(sigInfoInterestRsa, sigInfoInterestRsa + sizeof(sigInfoInterestRsa), |
| 265 | encodedInterest.begin(), encodedInterest.end()); |
Eric Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 266 | |
| 267 | // Decode as InterestSignatureInfo |
Davide Pesavento | a86300a | 2020-12-05 14:55:45 -0500 | [diff] [blame] | 268 | info = SignatureInfo(Block(sigInfoInterestRsa, sizeof(sigInfoInterestRsa)), |
| 269 | SignatureInfo::Type::Interest); |
| 270 | |
Eric Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 271 | 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 Pesavento | a86300a | 2020-12-05 14:55:45 -0500 | [diff] [blame] | 277 | |
| 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 Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | BOOST_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 Pesavento | 14c56cd | 2020-05-21 01:44:03 -0400 | [diff] [blame] | 402 | BOOST_AUTO_TEST_CASE(ValidityPeriod) |
Yingdi Yu | 6be43f3 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 403 | { |
| 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 Pesavento | 14c56cd | 2020-05-21 01:44:03 -0400 | [diff] [blame] | 425 | const auto notBefore = time::getUnixEpoch(); |
| 426 | const auto notAfter = notBefore + 1_day; |
Yingdi Yu | 6be43f3 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 427 | security::ValidityPeriod vp1(notBefore, notAfter); |
| 428 | |
Davide Pesavento | 14c56cd | 2020-05-21 01:44:03 -0400 | [diff] [blame] | 429 | SignatureInfo info(tlv::SignatureSha256WithRsa, KeyLocator("/test/key/locator")); |
Davide Pesavento | 14c56cd | 2020-05-21 01:44:03 -0400 | [diff] [blame] | 430 | BOOST_CHECK_THROW(info.getValidityPeriod(), SignatureInfo::Error); |
| 431 | |
| 432 | info.wireEncode(); |
Yingdi Yu | 6be43f3 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 433 | info.setValidityPeriod(vp1); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 434 | BOOST_CHECK_EQUAL(info.getValidityPeriod(), vp1); |
Davide Pesavento | 14c56cd | 2020-05-21 01:44:03 -0400 | [diff] [blame] | 435 | BOOST_CHECK_EQUAL(info.hasWire(), false); |
Yingdi Yu | 6be43f3 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 436 | |
Davide Pesavento | 14c56cd | 2020-05-21 01:44:03 -0400 | [diff] [blame] | 437 | // encode |
| 438 | auto encoded = info.wireEncode(); |
| 439 | BOOST_CHECK_EQUAL(info.hasWire(), true); |
Yingdi Yu | 6be43f3 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 440 | BOOST_CHECK_EQUAL_COLLECTIONS(sigInfo, sigInfo + sizeof(sigInfo), |
| 441 | encoded.wire(), encoded.wire() + encoded.size()); |
| 442 | |
Davide Pesavento | 14c56cd | 2020-05-21 01:44:03 -0400 | [diff] [blame] | 443 | info.setValidityPeriod(vp1); |
| 444 | BOOST_CHECK_EQUAL(info.hasWire(), true); |
| 445 | |
Yingdi Yu | 6be43f3 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 446 | // decode |
| 447 | Block block(sigInfo, sizeof(sigInfo)); |
Eric Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 448 | SignatureInfo info2(block, SignatureInfo::Type::Data); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 449 | BOOST_CHECK_EQUAL(info2.getValidityPeriod(), vp1); |
Davide Pesavento | 14c56cd | 2020-05-21 01:44:03 -0400 | [diff] [blame] | 450 | BOOST_CHECK_EQUAL(info2.hasWire(), true); |
Yingdi Yu | 6be43f3 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 451 | |
Davide Pesavento | 14c56cd | 2020-05-21 01:44:03 -0400 | [diff] [blame] | 452 | info2.setValidityPeriod(security::ValidityPeriod(notBefore, notBefore + 42_days)); |
Davide Pesavento | 14c56cd | 2020-05-21 01:44:03 -0400 | [diff] [blame] | 453 | 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 Pesavento | 14c56cd | 2020-05-21 01:44:03 -0400 | [diff] [blame] | 460 | BOOST_CHECK_THROW(info2.getValidityPeriod(), SignatureInfo::Error); |
| 461 | BOOST_CHECK_EQUAL(info2.hasWire(), false); |
Yingdi Yu | 6be43f3 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 462 | } |
| 463 | |
Eric Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 464 | BOOST_AUTO_TEST_CASE(CustomTlvs) |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 465 | { |
Davide Pesavento | 14c56cd | 2020-05-21 01:44:03 -0400 | [diff] [blame] | 466 | SignatureInfo info(tlv::SignatureSha256WithEcdsa); |
Eric Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 467 | 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 Pesavento | 14c56cd | 2020-05-21 01:44:03 -0400 | [diff] [blame] | 471 | |
| 472 | info.wireEncode(); |
| 473 | BOOST_CHECK_EQUAL(info.hasWire(), true); |
Eric Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 474 | info.addCustomTlv("82020202"_block); |
Davide Pesavento | 14c56cd | 2020-05-21 01:44:03 -0400 | [diff] [blame] | 475 | BOOST_CHECK_EQUAL(info.hasWire(), false); |
Eric Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 476 | |
| 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 Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 482 | } |
| 483 | |
Eric Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 484 | BOOST_AUTO_TEST_CASE(CustomTlvsEncoding) // Bug #3914 |
Alexander Afanasyev | 4146975 | 2017-01-10 21:51:55 -0800 | [diff] [blame] | 485 | { |
Junxiao Shi | 605671d | 2017-08-26 13:41:06 +0000 | [diff] [blame] | 486 | SignatureInfo info1(tlv::SignatureSha256WithRsa); |
Davide Pesavento | a86300a | 2020-12-05 14:55:45 -0500 | [diff] [blame] | 487 | info1.addCustomTlv(makeStringBlock(102, "First")); |
| 488 | info1.addCustomTlv(makeStringBlock(104, "Second")); |
| 489 | info1.addCustomTlv(makeStringBlock(106, "Third")); |
Alexander Afanasyev | 4146975 | 2017-01-10 21:51:55 -0800 | [diff] [blame] | 490 | |
Davide Pesavento | a86300a | 2020-12-05 14:55:45 -0500 | [diff] [blame] | 491 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(info1), |
| 492 | "SignatureSha256WithRsa { 102 104 106 }"); |
Alexander Afanasyev | 4146975 | 2017-01-10 21:51:55 -0800 | [diff] [blame] | 493 | |
| 494 | SignatureInfo info2; |
Eric Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 495 | info2.wireDecode(info1.wireEncode(), SignatureInfo::Type::Data); |
Alexander Afanasyev | 4146975 | 2017-01-10 21:51:55 -0800 | [diff] [blame] | 496 | BOOST_CHECK_EQUAL(info1, info2); |
| 497 | |
Alexander Afanasyev | 4146975 | 2017-01-10 21:51:55 -0800 | [diff] [blame] | 498 | const uint8_t infoBytes[] = { |
Junxiao Shi | 605671d | 2017-08-26 13:41:06 +0000 | [diff] [blame] | 499 | 0x16, 0x19, // SignatureInfo |
| 500 | 0x1b, 0x01, 0x01, // SignatureType=1 |
Eric Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 501 | 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 Afanasyev | 4146975 | 2017-01-10 21:51:55 -0800 | [diff] [blame] | 504 | }; |
| 505 | |
Eric Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 506 | SignatureInfo info3(Block(infoBytes, sizeof(infoBytes)), SignatureInfo::Type::Data); |
Alexander Afanasyev | 4146975 | 2017-01-10 21:51:55 -0800 | [diff] [blame] | 507 | BOOST_CHECK_EQUAL(info3, info1); |
| 508 | BOOST_CHECK_EQUAL_COLLECTIONS(infoBytes, infoBytes + sizeof(infoBytes), |
| 509 | info1.wireEncode().begin(), info1.wireEncode().end()); |
| 510 | } |
| 511 | |
Eric Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 512 | BOOST_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 Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 548 | BOOST_AUTO_TEST_SUITE_END() // TestSignatureInfo |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 549 | |
Spyridon Mastorakis | 429634f | 2015-02-19 17:35:33 -0800 | [diff] [blame] | 550 | } // namespace tests |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 551 | } // namespace ndn |