Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 7e6f6f8 | 2017-10-31 18:05:28 -0400 | [diff] [blame] | 2 | /* |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 3 | * Copyright (c) 2013-2018 Regents of the University of California. |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -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 "security/signing-info.hpp" |
Spencer Lee | 308bc44 | 2015-11-24 02:59:55 -0700 | [diff] [blame] | 23 | |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 24 | #include "boost-test.hpp" |
| 25 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 26 | #include <boost/lexical_cast.hpp> |
| 27 | #include <sstream> |
| 28 | |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 29 | namespace ndn { |
| 30 | namespace security { |
| 31 | namespace tests { |
| 32 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 33 | BOOST_AUTO_TEST_SUITE(Security) |
| 34 | BOOST_AUTO_TEST_SUITE(TestSigningInfo) |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 35 | |
| 36 | BOOST_AUTO_TEST_CASE(Basic) |
| 37 | { |
Spencer Lee | 308bc44 | 2015-11-24 02:59:55 -0700 | [diff] [blame] | 38 | Name id("/my-identity"); |
| 39 | Name key("/my-key"); |
| 40 | Name cert("/my-cert"); |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 41 | |
| 42 | SigningInfo info; |
| 43 | |
| 44 | BOOST_CHECK_EQUAL(info.getSignerType(), SigningInfo::SIGNER_TYPE_NULL); |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 45 | BOOST_CHECK_EQUAL(info.getSignerName(), SigningInfo::getEmptyName()); |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 46 | BOOST_CHECK_EQUAL(info.getDigestAlgorithm(), DigestAlgorithm::SHA256); |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 47 | |
| 48 | const SignatureInfo& sigInfo = info.getSignatureInfo(); |
| 49 | BOOST_CHECK_EQUAL(sigInfo.getSignatureType(), -1); |
| 50 | BOOST_CHECK_EQUAL(sigInfo.hasKeyLocator(), false); |
| 51 | |
| 52 | info.setSigningIdentity(id); |
| 53 | BOOST_CHECK_EQUAL(info.getSignerType(), SigningInfo::SIGNER_TYPE_ID); |
| 54 | BOOST_CHECK_EQUAL(info.getSignerName(), id); |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 55 | BOOST_CHECK_EQUAL(info.getDigestAlgorithm(), DigestAlgorithm::SHA256); |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 56 | |
| 57 | SigningInfo infoId(SigningInfo::SIGNER_TYPE_ID, id); |
| 58 | BOOST_CHECK_EQUAL(infoId.getSignerType(), SigningInfo::SIGNER_TYPE_ID); |
| 59 | BOOST_CHECK_EQUAL(infoId.getSignerName(), id); |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 60 | BOOST_CHECK_EQUAL(infoId.getDigestAlgorithm(), DigestAlgorithm::SHA256); |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 61 | |
| 62 | info.setSigningKeyName(key); |
| 63 | BOOST_CHECK_EQUAL(info.getSignerType(), SigningInfo::SIGNER_TYPE_KEY); |
| 64 | BOOST_CHECK_EQUAL(info.getSignerName(), key); |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 65 | BOOST_CHECK_EQUAL(info.getDigestAlgorithm(), DigestAlgorithm::SHA256); |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 66 | |
| 67 | SigningInfo infoKey(SigningInfo::SIGNER_TYPE_KEY, key); |
| 68 | BOOST_CHECK_EQUAL(infoKey.getSignerType(), SigningInfo::SIGNER_TYPE_KEY); |
| 69 | BOOST_CHECK_EQUAL(infoKey.getSignerName(), key); |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 70 | BOOST_CHECK_EQUAL(infoKey.getDigestAlgorithm(), DigestAlgorithm::SHA256); |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 71 | |
| 72 | info.setSigningCertName(cert); |
| 73 | BOOST_CHECK_EQUAL(info.getSignerType(), SigningInfo::SIGNER_TYPE_CERT); |
| 74 | BOOST_CHECK_EQUAL(info.getSignerName(), cert); |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 75 | BOOST_CHECK_EQUAL(info.getDigestAlgorithm(), DigestAlgorithm::SHA256); |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 76 | |
| 77 | SigningInfo infoCert(SigningInfo::SIGNER_TYPE_CERT, cert); |
| 78 | BOOST_CHECK_EQUAL(infoCert.getSignerType(), SigningInfo::SIGNER_TYPE_CERT); |
| 79 | BOOST_CHECK_EQUAL(infoCert.getSignerName(), cert); |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 80 | BOOST_CHECK_EQUAL(infoCert.getDigestAlgorithm(), DigestAlgorithm::SHA256); |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 81 | |
| 82 | info.setSha256Signing(); |
| 83 | BOOST_CHECK_EQUAL(info.getSignerType(), SigningInfo::SIGNER_TYPE_SHA256); |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 84 | BOOST_CHECK_EQUAL(info.getSignerName(), SigningInfo::getEmptyName()); |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 85 | BOOST_CHECK_EQUAL(info.getDigestAlgorithm(), DigestAlgorithm::SHA256); |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 86 | |
| 87 | SigningInfo infoSha(SigningInfo::SIGNER_TYPE_SHA256); |
| 88 | BOOST_CHECK_EQUAL(infoSha.getSignerType(), SigningInfo::SIGNER_TYPE_SHA256); |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 89 | BOOST_CHECK_EQUAL(infoSha.getSignerName(), SigningInfo::getEmptyName()); |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 90 | BOOST_CHECK_EQUAL(infoSha.getDigestAlgorithm(), DigestAlgorithm::SHA256); |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | BOOST_AUTO_TEST_CASE(CustomSignatureInfo) |
| 94 | { |
| 95 | SigningInfo info1; |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 96 | BOOST_CHECK_EQUAL(info1.getSignatureInfo(), SignatureInfo()); |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 97 | |
| 98 | SignatureInfo si; |
| 99 | si.setKeyLocator(Name("ndn:/test/key/locator")); |
| 100 | info1.setSignatureInfo(si); |
| 101 | |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 102 | BOOST_CHECK_EQUAL(info1.getSignatureInfo(), si); |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 103 | |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 104 | SigningInfo info2(SigningInfo::SIGNER_TYPE_NULL, SigningInfo::getEmptyName(), si); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 105 | BOOST_CHECK_EQUAL(info2.getSignatureInfo(), si); |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Spencer Lee | 308bc44 | 2015-11-24 02:59:55 -0700 | [diff] [blame] | 108 | BOOST_AUTO_TEST_CASE(FromString) |
| 109 | { |
| 110 | SigningInfo infoDefault(""); |
| 111 | BOOST_CHECK_EQUAL(infoDefault.getSignerType(), SigningInfo::SIGNER_TYPE_NULL); |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 112 | BOOST_CHECK_EQUAL(infoDefault.getSignerName(), SigningInfo::getEmptyName()); |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 113 | BOOST_CHECK_EQUAL(infoDefault.getDigestAlgorithm(), DigestAlgorithm::SHA256); |
Spencer Lee | 308bc44 | 2015-11-24 02:59:55 -0700 | [diff] [blame] | 114 | |
| 115 | SigningInfo infoId("id:/my-identity"); |
| 116 | BOOST_CHECK_EQUAL(infoId.getSignerType(), SigningInfo::SIGNER_TYPE_ID); |
| 117 | BOOST_CHECK_EQUAL(infoId.getSignerName(), "/my-identity"); |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 118 | BOOST_CHECK_EQUAL(infoId.getDigestAlgorithm(), DigestAlgorithm::SHA256); |
Spencer Lee | 308bc44 | 2015-11-24 02:59:55 -0700 | [diff] [blame] | 119 | |
| 120 | SigningInfo infoKey("key:/my-key"); |
| 121 | BOOST_CHECK_EQUAL(infoKey.getSignerType(), SigningInfo::SIGNER_TYPE_KEY); |
| 122 | BOOST_CHECK_EQUAL(infoKey.getSignerName(), "/my-key"); |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 123 | BOOST_CHECK_EQUAL(infoKey.getDigestAlgorithm(), DigestAlgorithm::SHA256); |
Spencer Lee | 308bc44 | 2015-11-24 02:59:55 -0700 | [diff] [blame] | 124 | |
| 125 | SigningInfo infoCert("cert:/my-cert"); |
| 126 | BOOST_CHECK_EQUAL(infoCert.getSignerType(), SigningInfo::SIGNER_TYPE_CERT); |
| 127 | BOOST_CHECK_EQUAL(infoCert.getSignerName(), "/my-cert"); |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 128 | BOOST_CHECK_EQUAL(infoCert.getDigestAlgorithm(), DigestAlgorithm::SHA256); |
Spencer Lee | 308bc44 | 2015-11-24 02:59:55 -0700 | [diff] [blame] | 129 | |
| 130 | SigningInfo infoSha("id:/localhost/identity/digest-sha256"); |
| 131 | BOOST_CHECK_EQUAL(infoSha.getSignerType(), SigningInfo::SIGNER_TYPE_SHA256); |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 132 | BOOST_CHECK_EQUAL(infoSha.getSignerName(), SigningInfo::getEmptyName()); |
Yingdi Yu | 99b2a00 | 2015-08-12 12:47:44 -0700 | [diff] [blame] | 133 | BOOST_CHECK_EQUAL(infoSha.getDigestAlgorithm(), DigestAlgorithm::SHA256); |
Spencer Lee | 308bc44 | 2015-11-24 02:59:55 -0700 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | BOOST_AUTO_TEST_CASE(ToString) |
| 137 | { |
| 138 | // We can't use lexical_cast due to Boost Bug 6298. |
| 139 | std::stringstream ss; |
| 140 | ss << SigningInfo(); |
| 141 | BOOST_CHECK_EQUAL(ss.str(), ""); |
| 142 | |
| 143 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>( |
| 144 | SigningInfo(SigningInfo::SIGNER_TYPE_ID, "/my-identity")), "id:/my-identity"); |
| 145 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>( |
| 146 | SigningInfo(SigningInfo::SIGNER_TYPE_KEY, "/my-key")), "key:/my-key"); |
| 147 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>( |
| 148 | SigningInfo(SigningInfo::SIGNER_TYPE_CERT, "/my-cert")), "cert:/my-cert"); |
| 149 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>( |
| 150 | SigningInfo(SigningInfo::SIGNER_TYPE_SHA256)), |
| 151 | "id:/localhost/identity/digest-sha256"); |
| 152 | } |
| 153 | |
Alexander Afanasyev | c95f564 | 2017-01-04 17:34:26 -0800 | [diff] [blame] | 154 | BOOST_AUTO_TEST_CASE(Chaining) |
| 155 | { |
| 156 | SigningInfo info = SigningInfo() |
| 157 | .setSigningIdentity("/identity") |
| 158 | .setSigningKeyName("/key/name") |
| 159 | .setSigningCertName("/cert/name") |
Alexander Afanasyev | d6d78aa | 2017-01-02 18:14:23 -0800 | [diff] [blame] | 160 | .setPibIdentity(Identity()) |
| 161 | .setPibKey(Key()) |
Alexander Afanasyev | c95f564 | 2017-01-04 17:34:26 -0800 | [diff] [blame] | 162 | .setSha256Signing() |
| 163 | .setDigestAlgorithm(DigestAlgorithm::SHA256) |
| 164 | .setSignatureInfo(SignatureInfo()); |
| 165 | |
| 166 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(info), "id:/localhost/identity/digest-sha256"); |
| 167 | } |
| 168 | |
Nick Gordon | 2a6d45a | 2017-01-25 14:10:44 -0600 | [diff] [blame] | 169 | BOOST_AUTO_TEST_CASE(OperatorEquals) |
| 170 | { |
| 171 | // Check name equality |
| 172 | SigningInfo info1("id:/my-id"); |
| 173 | SigningInfo info2("id:/my-id"); |
| 174 | BOOST_CHECK_EQUAL(info1, info2); |
| 175 | // Change name, check inequality |
| 176 | info2 = SigningInfo("id:/not-same-id"); |
| 177 | BOOST_CHECK_NE(info1, info2); |
| 178 | |
| 179 | // Check name, digest algo equality |
| 180 | info1 = SigningInfo("id:/my-id"); |
| 181 | info2 = SigningInfo("id:/my-id"); |
| 182 | info1.setDigestAlgorithm(DigestAlgorithm::SHA256); |
| 183 | info2.setDigestAlgorithm(DigestAlgorithm::SHA256); |
| 184 | BOOST_CHECK_EQUAL(info1, info2); |
| 185 | // Change digest algo, check inequality |
| 186 | info2.setDigestAlgorithm(DigestAlgorithm::NONE); |
| 187 | BOOST_CHECK_NE(info1, info2); |
| 188 | |
| 189 | // Check name, digest algo, signature info equality |
| 190 | info1 = SigningInfo("id:/my-id"); |
| 191 | info2 = SigningInfo("id:/my-id"); |
| 192 | info1.setDigestAlgorithm(DigestAlgorithm::SHA256); |
| 193 | info2.setDigestAlgorithm(DigestAlgorithm::SHA256); |
Davide Pesavento | 7e6f6f8 | 2017-10-31 18:05:28 -0400 | [diff] [blame] | 194 | SignatureInfo sigInfo1(tlv::DigestSha256); |
Nick Gordon | 2a6d45a | 2017-01-25 14:10:44 -0600 | [diff] [blame] | 195 | info1.setSignatureInfo(sigInfo1); |
| 196 | info2.setSignatureInfo(sigInfo1); |
| 197 | BOOST_CHECK_EQUAL(info1, info2); |
| 198 | // Change signature info, check inequality |
Davide Pesavento | 7e6f6f8 | 2017-10-31 18:05:28 -0400 | [diff] [blame] | 199 | SignatureInfo sigInfo2(tlv::SignatureSha256WithRsa); |
Nick Gordon | 2a6d45a | 2017-01-25 14:10:44 -0600 | [diff] [blame] | 200 | info2.setSignatureInfo(sigInfo2); |
| 201 | BOOST_CHECK_NE(info1, info2); |
| 202 | } |
| 203 | |
| 204 | BOOST_AUTO_TEST_CASE(OperatorEqualsDifferentTypes) |
| 205 | { |
| 206 | SigningInfo info1("key:/my-id/KEY/1"); |
| 207 | SigningInfo info2("key:/my-id/KEY/1"); |
| 208 | // Check equality for key type |
| 209 | BOOST_CHECK_EQUAL(info1, info2); |
| 210 | info2 = SigningInfo("id:/my-id"); |
| 211 | // Change signature type, check inequality |
| 212 | BOOST_CHECK_NE(info1, info2); |
| 213 | info2 = SigningInfo("key:/not-same-id/KEY/1"); |
| 214 | // Change key name, check inequality |
| 215 | BOOST_CHECK_NE(info1, info2); |
| 216 | |
| 217 | info1 = SigningInfo("cert:/my-id/KEY/1/self/%FD01"); |
| 218 | info2 = SigningInfo("cert:/my-id/KEY/1/self/%FD01"); |
| 219 | // Check equality for cert type |
| 220 | BOOST_CHECK_EQUAL(info1, info2); |
| 221 | info2 = SigningInfo("cert:/not-my-id/KEY/1/other/%FD01"); |
| 222 | // Change cert name, check inequality |
| 223 | BOOST_CHECK_NE(info1, info2); |
| 224 | info2 = SigningInfo("id:/my-id"); |
| 225 | // Change signature type, check inequality |
| 226 | BOOST_CHECK_NE(info1, info2); |
| 227 | |
| 228 | info1 = SigningInfo(SigningInfo::SIGNER_TYPE_NULL); |
| 229 | info2 = SigningInfo(SigningInfo::SIGNER_TYPE_NULL); |
| 230 | // Check equality for null type |
| 231 | BOOST_CHECK_EQUAL(info1, info2); |
| 232 | info2 = SigningInfo("id:/my-id"); |
| 233 | // Change signature type, check inequality |
| 234 | BOOST_CHECK_NE(info1, info2); |
| 235 | |
| 236 | info1 = SigningInfo(SigningInfo::SIGNER_TYPE_SHA256); |
| 237 | info2 = SigningInfo(SigningInfo::SIGNER_TYPE_SHA256); |
| 238 | // Check equality for SHA256 digest type |
| 239 | BOOST_CHECK_EQUAL(info1, info2); |
| 240 | info2 = SigningInfo("id:/my-id"); |
| 241 | // Change signature type, check inequality |
| 242 | BOOST_CHECK_NE(info1, info2); |
| 243 | } |
| 244 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 245 | BOOST_AUTO_TEST_SUITE_END() // TestSigningInfo |
| 246 | BOOST_AUTO_TEST_SUITE_END() // Security |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 247 | |
| 248 | } // namespace tests |
| 249 | } // namespace security |
| 250 | } // namespace ndn |