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 | /* |
Alexander Afanasyev | 4146975 | 2017-01-10 21:51:55 -0800 | [diff] [blame] | 3 | * Copyright (c) 2013-2017 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 | |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 53 | SignatureInfo sha256Info(tlv::DigestSha256); |
| 54 | BOOST_CHECK_EQUAL(sha256Info.getSignatureType(), tlv::DigestSha256); |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 55 | BOOST_CHECK_EQUAL(sha256Info.hasKeyLocator(), false); |
| 56 | BOOST_CHECK_THROW(sha256Info.getKeyLocator(), SignatureInfo::Error); |
| 57 | |
Alexander Afanasyev | 4146975 | 2017-01-10 21:51:55 -0800 | [diff] [blame] | 58 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(sha256Info), "DigestSha256"); |
| 59 | |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 60 | KeyLocator keyLocator("/test/key/locator"); |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 61 | SignatureInfo sha256RsaInfo(tlv::SignatureSha256WithRsa, keyLocator); |
| 62 | BOOST_CHECK_EQUAL(sha256RsaInfo.getSignatureType(), tlv::SignatureSha256WithRsa); |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 63 | BOOST_CHECK_EQUAL(sha256RsaInfo.hasKeyLocator(), true); |
| 64 | BOOST_CHECK_NO_THROW(sha256RsaInfo.getKeyLocator()); |
| 65 | BOOST_CHECK_EQUAL(sha256RsaInfo.getKeyLocator().getName(), Name("/test/key/locator")); |
| 66 | |
Alexander Afanasyev | 4146975 | 2017-01-10 21:51:55 -0800 | [diff] [blame] | 67 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(sha256RsaInfo), "SignatureSha256WithRsa Name=/test/key/locator"); |
| 68 | |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 69 | const Block& encoded = sha256RsaInfo.wireEncode(); |
| 70 | Block sigInfoBlock(sigInfoRsa, sizeof(sigInfoRsa)); |
| 71 | |
| 72 | BOOST_CHECK_EQUAL_COLLECTIONS(sigInfoBlock.wire(), |
| 73 | sigInfoBlock.wire() + sigInfoBlock.size(), |
| 74 | encoded.wire(), |
| 75 | encoded.wire() + encoded.size()); |
| 76 | |
| 77 | sha256RsaInfo = SignatureInfo(sigInfoBlock); |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 78 | BOOST_CHECK_EQUAL(sha256RsaInfo.getSignatureType(), tlv::SignatureSha256WithRsa); |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 79 | BOOST_CHECK_EQUAL(sha256RsaInfo.hasKeyLocator(), true); |
| 80 | BOOST_CHECK_NO_THROW(sha256RsaInfo.getKeyLocator()); |
| 81 | BOOST_CHECK_EQUAL(sha256RsaInfo.getKeyLocator().getName(), Name("/test/key/locator")); |
| 82 | } |
| 83 | |
| 84 | BOOST_AUTO_TEST_CASE(ConstructorError) |
| 85 | { |
| 86 | const uint8_t error1[] = { |
| 87 | 0x15, 0x1b, // Wrong SignatureInfo (0x16, 0x1b) |
| 88 | 0x1b, 0x01, // SignatureType |
| 89 | 0x01, // Sha256WithRsa |
| 90 | 0x1c, 0x16, // KeyLocator |
| 91 | 0x07, 0x14, // Name |
| 92 | 0x08, 0x04, |
| 93 | 0x74, 0x65, 0x73, 0x74, |
| 94 | 0x08, 0x03, |
| 95 | 0x6b, 0x65, 0x79, |
| 96 | 0x08, 0x07, |
| 97 | 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72 |
| 98 | }; |
| 99 | Block errorBlock1(error1, sizeof(error1)); |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 100 | BOOST_CHECK_THROW(SignatureInfo info(errorBlock1), tlv::Error); |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 101 | |
| 102 | const uint8_t error2[] = { |
| 103 | 0x16, 0x01, // SignatureInfo |
| 104 | 0x01 // Wrong SignatureInfo value |
| 105 | }; |
| 106 | Block errorBlock2(error2, sizeof(error2)); |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 107 | BOOST_CHECK_THROW(SignatureInfo info(errorBlock2), tlv::Error); |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 108 | |
| 109 | const uint8_t error3[] = { |
| 110 | 0x16, 0x01, // SignatureInfo |
| 111 | 0x1a, 0x01, // Wrong SignatureType (0x1b, 0x1b) |
| 112 | 0x01, // Sha256WithRsa |
| 113 | 0x1c, 0x16, // KeyLocator |
| 114 | 0x07, 0x14, // Name |
| 115 | 0x08, 0x04, |
| 116 | 0x74, 0x65, 0x73, 0x74, |
| 117 | 0x08, 0x03, |
| 118 | 0x6b, 0x65, 0x79, |
| 119 | 0x08, 0x07, |
| 120 | 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72 |
| 121 | }; |
| 122 | Block errorBlock3(error3, sizeof(error3)); |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 123 | BOOST_CHECK_THROW(SignatureInfo info(errorBlock3), tlv::Error); |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 124 | |
| 125 | const uint8_t error4[] = { |
| 126 | 0x16, 0x00 // Empty SignatureInfo |
| 127 | }; |
| 128 | Block errorBlock4(error4, sizeof(error4)); |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 129 | BOOST_CHECK_THROW(SignatureInfo info(errorBlock4), tlv::Error); |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 130 | |
| 131 | } |
| 132 | |
| 133 | BOOST_AUTO_TEST_CASE(SetterGetter) |
| 134 | { |
| 135 | SignatureInfo info; |
| 136 | BOOST_CHECK_EQUAL(info.getSignatureType(), -1); |
| 137 | BOOST_CHECK_EQUAL(info.hasKeyLocator(), false); |
| 138 | BOOST_CHECK_THROW(info.getKeyLocator(), SignatureInfo::Error); |
| 139 | |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 140 | info.setSignatureType(tlv::SignatureSha256WithRsa); |
| 141 | BOOST_CHECK_EQUAL(info.getSignatureType(), tlv::SignatureSha256WithRsa); |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 142 | BOOST_CHECK_EQUAL(info.hasKeyLocator(), false); |
| 143 | |
| 144 | KeyLocator keyLocator("/test/key/locator"); |
| 145 | info.setKeyLocator(keyLocator); |
| 146 | BOOST_CHECK_EQUAL(info.hasKeyLocator(), true); |
| 147 | BOOST_CHECK_NO_THROW(info.getKeyLocator()); |
| 148 | BOOST_CHECK_EQUAL(info.getKeyLocator().getName(), Name("/test/key/locator")); |
| 149 | |
| 150 | const Block& encoded = info.wireEncode(); |
| 151 | Block sigInfoBlock(sigInfoRsa, sizeof(sigInfoRsa)); |
| 152 | |
| 153 | BOOST_CHECK_EQUAL_COLLECTIONS(sigInfoBlock.wire(), |
| 154 | sigInfoBlock.wire() + sigInfoBlock.size(), |
| 155 | encoded.wire(), |
| 156 | encoded.wire() + encoded.size()); |
Alexander Afanasyev | a7c7f9d | 2014-07-13 11:51:52 -0700 | [diff] [blame] | 157 | |
| 158 | info.unsetKeyLocator(); |
| 159 | BOOST_CHECK_EQUAL(info.hasKeyLocator(), false); |
| 160 | BOOST_CHECK_THROW(info.getKeyLocator(), SignatureInfo::Error); |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Yingdi Yu | 6be43f3 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 163 | BOOST_AUTO_TEST_CASE(ValidityPeriodExtension) |
| 164 | { |
| 165 | const uint8_t sigInfo[] = { |
| 166 | 0x16, 0x45, // SignatureInfo |
| 167 | 0x1b, 0x01, // SignatureType |
| 168 | 0x01, // Sha256WithRsa |
| 169 | 0x1c, 0x16, // KeyLocator |
| 170 | 0x07, 0x14, // Name |
| 171 | 0x08, 0x04, |
| 172 | 0x74, 0x65, 0x73, 0x74, |
| 173 | 0x08, 0x03, |
| 174 | 0x6b, 0x65, 0x79, |
| 175 | 0x08, 0x07, |
| 176 | 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72, |
| 177 | 0xfd, 0x00, 0xfd, 0x26, // ValidityPeriod |
| 178 | 0xfd, 0x00, 0xfe, 0x0f, // NotBefore |
| 179 | 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x31, // 19700101T000000 |
| 180 | 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, |
| 181 | 0xfd, 0x00, 0xff, 0x0f, // NotAfter |
| 182 | 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x32, // 19700102T000000 |
| 183 | 0x54, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30 |
| 184 | }; |
| 185 | |
| 186 | time::system_clock::TimePoint notBefore = time::getUnixEpoch(); |
| 187 | time::system_clock::TimePoint notAfter = notBefore + time::days(1); |
| 188 | security::ValidityPeriod vp1(notBefore, notAfter); |
| 189 | |
| 190 | // encode |
| 191 | SignatureInfo info; |
| 192 | info.setSignatureType(tlv::SignatureSha256WithRsa); |
| 193 | info.setKeyLocator(KeyLocator("/test/key/locator")); |
| 194 | info.setValidityPeriod(vp1); |
| 195 | |
| 196 | BOOST_CHECK(info.getValidityPeriod() == vp1); |
| 197 | |
| 198 | const Block& encoded = info.wireEncode(); |
| 199 | |
| 200 | BOOST_CHECK_EQUAL_COLLECTIONS(sigInfo, sigInfo + sizeof(sigInfo), |
| 201 | encoded.wire(), encoded.wire() + encoded.size()); |
| 202 | |
| 203 | // decode |
| 204 | Block block(sigInfo, sizeof(sigInfo)); |
| 205 | SignatureInfo info2; |
| 206 | info2.wireDecode(block); |
| 207 | BOOST_CHECK(info2.getValidityPeriod() == vp1); |
| 208 | |
| 209 | const security::ValidityPeriod& validityPeriod = info2.getValidityPeriod(); |
| 210 | BOOST_CHECK(validityPeriod.getPeriod() == std::make_pair(notBefore, notAfter)); |
| 211 | } |
| 212 | |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 213 | BOOST_AUTO_TEST_CASE(OtherTlvs) |
| 214 | { |
| 215 | SignatureInfo info; |
| 216 | BOOST_CHECK_EQUAL(info.getSignatureType(), -1); |
| 217 | BOOST_CHECK_EQUAL(info.hasKeyLocator(), false); |
| 218 | BOOST_CHECK_THROW(info.getKeyLocator(), SignatureInfo::Error); |
| 219 | |
| 220 | const uint8_t tlv1[] = { |
| 221 | 0x81, // T |
| 222 | 0x01, // L |
| 223 | 0x01, // V |
| 224 | }; |
| 225 | Block block1(tlv1, sizeof(tlv1)); |
| 226 | |
| 227 | info.appendTypeSpecificTlv(block1); |
| 228 | BOOST_CHECK_THROW(info.getTypeSpecificTlv(0x82), SignatureInfo::Error); |
| 229 | BOOST_REQUIRE_NO_THROW(info.getTypeSpecificTlv(0x81)); |
| 230 | } |
| 231 | |
Alexander Afanasyev | 4146975 | 2017-01-10 21:51:55 -0800 | [diff] [blame] | 232 | BOOST_AUTO_TEST_CASE(OtherTlvsEncoding) // Bug #3914 |
| 233 | { |
Junxiao Shi | 605671d | 2017-08-26 13:41:06 +0000 | [diff] [blame^] | 234 | SignatureInfo info1(tlv::SignatureSha256WithRsa); |
Alexander Afanasyev | 4146975 | 2017-01-10 21:51:55 -0800 | [diff] [blame] | 235 | info1.appendTypeSpecificTlv(makeStringBlock(101, "First")); |
| 236 | info1.appendTypeSpecificTlv(makeStringBlock(102, "Second")); |
| 237 | info1.appendTypeSpecificTlv(makeStringBlock(103, "Third")); |
| 238 | |
Junxiao Shi | 605671d | 2017-08-26 13:41:06 +0000 | [diff] [blame^] | 239 | 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] | 240 | |
| 241 | SignatureInfo info2; |
| 242 | info2.wireDecode(info1.wireEncode()); |
| 243 | BOOST_CHECK_EQUAL(info1, info2); |
| 244 | |
Alexander Afanasyev | 4146975 | 2017-01-10 21:51:55 -0800 | [diff] [blame] | 245 | const uint8_t infoBytes[] = { |
Junxiao Shi | 605671d | 2017-08-26 13:41:06 +0000 | [diff] [blame^] | 246 | 0x16, 0x19, // SignatureInfo |
| 247 | 0x1b, 0x01, 0x01, // SignatureType=1 |
| 248 | 0x65, 0x05, 0x46, 0x69, 0x72, 0x73, 0x74, // 101 "First" |
| 249 | 0x66, 0x06, 0x53, 0x65, 0x63, 0x6f, 0x6e, 0x64, // 102 "Second" |
| 250 | 0x67, 0x05, 0x54, 0x68, 0x69, 0x72, 0x64 // 103 "Third" |
Alexander Afanasyev | 4146975 | 2017-01-10 21:51:55 -0800 | [diff] [blame] | 251 | }; |
| 252 | |
| 253 | SignatureInfo info3(Block(infoBytes, sizeof(infoBytes))); |
| 254 | BOOST_CHECK_EQUAL(info3, info1); |
| 255 | BOOST_CHECK_EQUAL_COLLECTIONS(infoBytes, infoBytes + sizeof(infoBytes), |
| 256 | info1.wireEncode().begin(), info1.wireEncode().end()); |
| 257 | } |
| 258 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 259 | BOOST_AUTO_TEST_SUITE_END() // TestSignatureInfo |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 260 | |
Spyridon Mastorakis | 429634f | 2015-02-19 17:35:33 -0800 | [diff] [blame] | 261 | } // namespace tests |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 262 | } // namespace ndn |