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 | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 3 | * Copyright (c) 2013-2018 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 | |
| 22 | #include "signature-info.hpp" |
| 23 | |
| 24 | #include "boost-test.hpp" |
Alexander Afanasyev | 4146975 | 2017-01-10 21:51:55 -0800 | [diff] [blame] | 25 | #include <boost/lexical_cast.hpp> |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 26 | |
| 27 | namespace ndn { |
Spyridon Mastorakis | 429634f | 2015-02-19 17:35:33 -0800 | [diff] [blame] | 28 | namespace tests { |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 29 | |
| 30 | BOOST_AUTO_TEST_SUITE(TestSignatureInfo) |
| 31 | |
| 32 | const uint8_t sigInfoRsa[] = { |
| 33 | 0x16, 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 | |
| 46 | BOOST_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 Pesavento | 7e6f6f8 | 2017-10-31 18:05:28 -0400 | [diff] [blame] | 53 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(info), "Invalid SignatureInfo"); |
| 54 | |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 55 | SignatureInfo sha256Info(tlv::DigestSha256); |
| 56 | BOOST_CHECK_EQUAL(sha256Info.getSignatureType(), tlv::DigestSha256); |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 57 | BOOST_CHECK_EQUAL(sha256Info.hasKeyLocator(), false); |
| 58 | BOOST_CHECK_THROW(sha256Info.getKeyLocator(), SignatureInfo::Error); |
| 59 | |
Alexander Afanasyev | 4146975 | 2017-01-10 21:51:55 -0800 | [diff] [blame] | 60 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(sha256Info), "DigestSha256"); |
| 61 | |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 62 | KeyLocator keyLocator("/test/key/locator"); |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 63 | SignatureInfo sha256RsaInfo(tlv::SignatureSha256WithRsa, keyLocator); |
| 64 | BOOST_CHECK_EQUAL(sha256RsaInfo.getSignatureType(), tlv::SignatureSha256WithRsa); |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 65 | 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 Afanasyev | 4146975 | 2017-01-10 21:51:55 -0800 | [diff] [blame] | 69 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(sha256RsaInfo), "SignatureSha256WithRsa Name=/test/key/locator"); |
| 70 | |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 71 | 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 DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 80 | BOOST_CHECK_EQUAL(sha256RsaInfo.getSignatureType(), tlv::SignatureSha256WithRsa); |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 81 | 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 | |
| 86 | BOOST_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 DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 102 | BOOST_CHECK_THROW(SignatureInfo info(errorBlock1), tlv::Error); |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 103 | |
| 104 | const uint8_t error2[] = { |
| 105 | 0x16, 0x01, // SignatureInfo |
| 106 | 0x01 // Wrong SignatureInfo value |
| 107 | }; |
| 108 | Block errorBlock2(error2, sizeof(error2)); |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 109 | BOOST_CHECK_THROW(SignatureInfo info(errorBlock2), tlv::Error); |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 110 | |
| 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 DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 125 | BOOST_CHECK_THROW(SignatureInfo info(errorBlock3), tlv::Error); |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 126 | |
| 127 | const uint8_t error4[] = { |
| 128 | 0x16, 0x00 // Empty SignatureInfo |
| 129 | }; |
| 130 | Block errorBlock4(error4, sizeof(error4)); |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 131 | BOOST_CHECK_THROW(SignatureInfo info(errorBlock4), tlv::Error); |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 132 | |
| 133 | } |
| 134 | |
| 135 | BOOST_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 DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 142 | info.setSignatureType(tlv::SignatureSha256WithRsa); |
| 143 | BOOST_CHECK_EQUAL(info.getSignatureType(), tlv::SignatureSha256WithRsa); |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 144 | 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 Afanasyev | a7c7f9d | 2014-07-13 11:51:52 -0700 | [diff] [blame] | 159 | |
| 160 | info.unsetKeyLocator(); |
| 161 | BOOST_CHECK_EQUAL(info.hasKeyLocator(), false); |
| 162 | BOOST_CHECK_THROW(info.getKeyLocator(), SignatureInfo::Error); |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 163 | } |
| 164 | |
Yingdi Yu | 6be43f3 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 165 | BOOST_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 Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 189 | time::system_clock::TimePoint notAfter = notBefore + 1_day; |
Yingdi Yu | 6be43f3 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 190 | 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 Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 215 | BOOST_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 Afanasyev | 4146975 | 2017-01-10 21:51:55 -0800 | [diff] [blame] | 234 | BOOST_AUTO_TEST_CASE(OtherTlvsEncoding) // Bug #3914 |
| 235 | { |
Junxiao Shi | 605671d | 2017-08-26 13:41:06 +0000 | [diff] [blame] | 236 | SignatureInfo info1(tlv::SignatureSha256WithRsa); |
Alexander Afanasyev | 4146975 | 2017-01-10 21:51:55 -0800 | [diff] [blame] | 237 | info1.appendTypeSpecificTlv(makeStringBlock(101, "First")); |
| 238 | info1.appendTypeSpecificTlv(makeStringBlock(102, "Second")); |
| 239 | info1.appendTypeSpecificTlv(makeStringBlock(103, "Third")); |
| 240 | |
Junxiao Shi | 605671d | 2017-08-26 13:41:06 +0000 | [diff] [blame] | 241 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(info1), "SignatureSha256WithRsa { 101 102 103 }"); |
Alexander Afanasyev | 4146975 | 2017-01-10 21:51:55 -0800 | [diff] [blame] | 242 | |
| 243 | SignatureInfo info2; |
| 244 | info2.wireDecode(info1.wireEncode()); |
| 245 | BOOST_CHECK_EQUAL(info1, info2); |
| 246 | |
Alexander Afanasyev | 4146975 | 2017-01-10 21:51:55 -0800 | [diff] [blame] | 247 | const uint8_t infoBytes[] = { |
Junxiao Shi | 605671d | 2017-08-26 13:41:06 +0000 | [diff] [blame] | 248 | 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 Afanasyev | 4146975 | 2017-01-10 21:51:55 -0800 | [diff] [blame] | 253 | }; |
| 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 Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 261 | BOOST_AUTO_TEST_SUITE_END() // TestSignatureInfo |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 262 | |
Spyridon Mastorakis | 429634f | 2015-02-19 17:35:33 -0800 | [diff] [blame] | 263 | } // namespace tests |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 264 | } // namespace ndn |