blob: 29fd8ad45f1e0e92834af6a91d196a61b0836607 [file] [log] [blame]
Yingdi Yu1b0311c2015-06-10 14:58:47 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento7e6f6f82017-10-31 18:05:28 -04002/*
Davide Pesavento47ce2ee2023-05-09 01:33:33 -04003 * Copyright (c) 2013-2023 Regents of the University of California.
Yingdi Yu1b0311c2015-06-10 14:58:47 -07004 *
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 Pesavento7e780642018-11-24 15:51:34 -050022#include "ndn-cxx/security/signing-info.hpp"
Spencer Lee308bc442015-11-24 02:59:55 -070023
Davide Pesavento7e780642018-11-24 15:51:34 -050024#include "tests/boost-test.hpp"
Yingdi Yu1b0311c2015-06-10 14:58:47 -070025
Davide Pesavento80d671f2022-06-08 04:04:52 -040026#include <openssl/opensslv.h>
Davide Pesaventoeee3e822016-11-26 19:19:34 +010027#include <boost/lexical_cast.hpp>
28#include <sstream>
29
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040030namespace ndn::tests {
31
32using namespace ndn::security;
Yingdi Yu1b0311c2015-06-10 14:58:47 -070033
Davide Pesaventoeee3e822016-11-26 19:19:34 +010034BOOST_AUTO_TEST_SUITE(Security)
35BOOST_AUTO_TEST_SUITE(TestSigningInfo)
Yingdi Yu1b0311c2015-06-10 14:58:47 -070036
37BOOST_AUTO_TEST_CASE(Basic)
38{
Spencer Lee308bc442015-11-24 02:59:55 -070039 Name id("/my-identity");
40 Name key("/my-key");
41 Name cert("/my-cert");
Yingdi Yu1b0311c2015-06-10 14:58:47 -070042
43 SigningInfo info;
44
45 BOOST_CHECK_EQUAL(info.getSignerType(), SigningInfo::SIGNER_TYPE_NULL);
Eric Newberry78ffc632020-05-17 15:13:46 -070046 BOOST_CHECK_EQUAL(info.getSignerName(), Name());
Yingdi Yu99b2a002015-08-12 12:47:44 -070047 BOOST_CHECK_EQUAL(info.getDigestAlgorithm(), DigestAlgorithm::SHA256);
Eric Newberryb74bbda2020-06-18 19:33:58 -070048 BOOST_CHECK_EQUAL(info.getSignedInterestFormat(), SignedInterestFormat::V02);
Yingdi Yu1b0311c2015-06-10 14:58:47 -070049
50 const SignatureInfo& sigInfo = info.getSignatureInfo();
51 BOOST_CHECK_EQUAL(sigInfo.getSignatureType(), -1);
52 BOOST_CHECK_EQUAL(sigInfo.hasKeyLocator(), false);
53
54 info.setSigningIdentity(id);
55 BOOST_CHECK_EQUAL(info.getSignerType(), SigningInfo::SIGNER_TYPE_ID);
56 BOOST_CHECK_EQUAL(info.getSignerName(), id);
Yingdi Yu99b2a002015-08-12 12:47:44 -070057 BOOST_CHECK_EQUAL(info.getDigestAlgorithm(), DigestAlgorithm::SHA256);
Eric Newberryb74bbda2020-06-18 19:33:58 -070058 BOOST_CHECK_EQUAL(info.getSignedInterestFormat(), SignedInterestFormat::V02);
Yingdi Yu1b0311c2015-06-10 14:58:47 -070059
60 SigningInfo infoId(SigningInfo::SIGNER_TYPE_ID, id);
61 BOOST_CHECK_EQUAL(infoId.getSignerType(), SigningInfo::SIGNER_TYPE_ID);
62 BOOST_CHECK_EQUAL(infoId.getSignerName(), id);
Yingdi Yu99b2a002015-08-12 12:47:44 -070063 BOOST_CHECK_EQUAL(infoId.getDigestAlgorithm(), DigestAlgorithm::SHA256);
Eric Newberryb74bbda2020-06-18 19:33:58 -070064 BOOST_CHECK_EQUAL(infoId.getSignedInterestFormat(), SignedInterestFormat::V02);
Yingdi Yu1b0311c2015-06-10 14:58:47 -070065
66 info.setSigningKeyName(key);
67 BOOST_CHECK_EQUAL(info.getSignerType(), SigningInfo::SIGNER_TYPE_KEY);
68 BOOST_CHECK_EQUAL(info.getSignerName(), key);
Yingdi Yu99b2a002015-08-12 12:47:44 -070069 BOOST_CHECK_EQUAL(info.getDigestAlgorithm(), DigestAlgorithm::SHA256);
Eric Newberryb74bbda2020-06-18 19:33:58 -070070 BOOST_CHECK_EQUAL(info.getSignedInterestFormat(), SignedInterestFormat::V02);
Yingdi Yu1b0311c2015-06-10 14:58:47 -070071
72 SigningInfo infoKey(SigningInfo::SIGNER_TYPE_KEY, key);
73 BOOST_CHECK_EQUAL(infoKey.getSignerType(), SigningInfo::SIGNER_TYPE_KEY);
74 BOOST_CHECK_EQUAL(infoKey.getSignerName(), key);
Yingdi Yu99b2a002015-08-12 12:47:44 -070075 BOOST_CHECK_EQUAL(infoKey.getDigestAlgorithm(), DigestAlgorithm::SHA256);
Eric Newberryb74bbda2020-06-18 19:33:58 -070076 BOOST_CHECK_EQUAL(infoKey.getSignedInterestFormat(), SignedInterestFormat::V02);
Yingdi Yu1b0311c2015-06-10 14:58:47 -070077
78 info.setSigningCertName(cert);
79 BOOST_CHECK_EQUAL(info.getSignerType(), SigningInfo::SIGNER_TYPE_CERT);
80 BOOST_CHECK_EQUAL(info.getSignerName(), cert);
Yingdi Yu99b2a002015-08-12 12:47:44 -070081 BOOST_CHECK_EQUAL(info.getDigestAlgorithm(), DigestAlgorithm::SHA256);
Eric Newberryb74bbda2020-06-18 19:33:58 -070082 BOOST_CHECK_EQUAL(info.getSignedInterestFormat(), SignedInterestFormat::V02);
Yingdi Yu1b0311c2015-06-10 14:58:47 -070083
84 SigningInfo infoCert(SigningInfo::SIGNER_TYPE_CERT, cert);
85 BOOST_CHECK_EQUAL(infoCert.getSignerType(), SigningInfo::SIGNER_TYPE_CERT);
86 BOOST_CHECK_EQUAL(infoCert.getSignerName(), cert);
Yingdi Yu99b2a002015-08-12 12:47:44 -070087 BOOST_CHECK_EQUAL(infoCert.getDigestAlgorithm(), DigestAlgorithm::SHA256);
Eric Newberryb74bbda2020-06-18 19:33:58 -070088 BOOST_CHECK_EQUAL(infoCert.getSignedInterestFormat(), SignedInterestFormat::V02);
Yingdi Yu1b0311c2015-06-10 14:58:47 -070089
90 info.setSha256Signing();
91 BOOST_CHECK_EQUAL(info.getSignerType(), SigningInfo::SIGNER_TYPE_SHA256);
Eric Newberry78ffc632020-05-17 15:13:46 -070092 BOOST_CHECK_EQUAL(info.getSignerName(), Name());
Yingdi Yu99b2a002015-08-12 12:47:44 -070093 BOOST_CHECK_EQUAL(info.getDigestAlgorithm(), DigestAlgorithm::SHA256);
Eric Newberryb74bbda2020-06-18 19:33:58 -070094 BOOST_CHECK_EQUAL(info.getSignedInterestFormat(), SignedInterestFormat::V02);
Yingdi Yu1b0311c2015-06-10 14:58:47 -070095
96 SigningInfo infoSha(SigningInfo::SIGNER_TYPE_SHA256);
97 BOOST_CHECK_EQUAL(infoSha.getSignerType(), SigningInfo::SIGNER_TYPE_SHA256);
Eric Newberry78ffc632020-05-17 15:13:46 -070098 BOOST_CHECK_EQUAL(infoSha.getSignerName(), Name());
Yingdi Yu99b2a002015-08-12 12:47:44 -070099 BOOST_CHECK_EQUAL(infoSha.getDigestAlgorithm(), DigestAlgorithm::SHA256);
Eric Newberryb74bbda2020-06-18 19:33:58 -0700100 BOOST_CHECK_EQUAL(infoSha.getSignedInterestFormat(), SignedInterestFormat::V02);
laqinfan56a812d2019-06-03 15:33:58 -0500101
102 std::string encodedKey("QjM3NEEyNkE3MTQ5MDQzN0FBMDI0RTRGQURENUI0OTdGRE"
103 "ZGMUE4RUE2RkYxMkY2RkI2NUFGMjcyMEI1OUNDRg==");
104 info.setSigningHmacKey(encodedKey);
105 BOOST_CHECK_EQUAL(info.getSignerType(), SigningInfo::SIGNER_TYPE_HMAC);
106 BOOST_CHECK_EQUAL(info.getDigestAlgorithm(), DigestAlgorithm::SHA256);
Eric Newberryb74bbda2020-06-18 19:33:58 -0700107 BOOST_CHECK_EQUAL(info.getSignedInterestFormat(), SignedInterestFormat::V02);
laqinfan56a812d2019-06-03 15:33:58 -0500108
109 SigningInfo infoHmac(SigningInfo::SIGNER_TYPE_HMAC, info.getSignerName());
110 BOOST_CHECK_EQUAL(infoHmac.getSignerType(), SigningInfo::SIGNER_TYPE_HMAC);
111 BOOST_CHECK_EQUAL(infoHmac.getDigestAlgorithm(), DigestAlgorithm::SHA256);
Eric Newberryb74bbda2020-06-18 19:33:58 -0700112 BOOST_CHECK_EQUAL(infoHmac.getSignedInterestFormat(), SignedInterestFormat::V02);
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700113}
114
115BOOST_AUTO_TEST_CASE(CustomSignatureInfo)
116{
117 SigningInfo info1;
Junxiao Shi72c0c642018-04-20 15:41:09 +0000118 BOOST_CHECK_EQUAL(info1.getSignatureInfo(), SignatureInfo());
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700119
120 SignatureInfo si;
121 si.setKeyLocator(Name("ndn:/test/key/locator"));
122 info1.setSignatureInfo(si);
123
Junxiao Shi72c0c642018-04-20 15:41:09 +0000124 BOOST_CHECK_EQUAL(info1.getSignatureInfo(), si);
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700125
Eric Newberry78ffc632020-05-17 15:13:46 -0700126 SigningInfo info2(SigningInfo::SIGNER_TYPE_NULL, Name(), si);
Junxiao Shi72c0c642018-04-20 15:41:09 +0000127 BOOST_CHECK_EQUAL(info2.getSignatureInfo(), si);
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700128}
129
Spencer Lee308bc442015-11-24 02:59:55 -0700130BOOST_AUTO_TEST_CASE(FromString)
131{
132 SigningInfo infoDefault("");
133 BOOST_CHECK_EQUAL(infoDefault.getSignerType(), SigningInfo::SIGNER_TYPE_NULL);
Eric Newberry78ffc632020-05-17 15:13:46 -0700134 BOOST_CHECK_EQUAL(infoDefault.getSignerName(), Name());
Yingdi Yu99b2a002015-08-12 12:47:44 -0700135 BOOST_CHECK_EQUAL(infoDefault.getDigestAlgorithm(), DigestAlgorithm::SHA256);
Spencer Lee308bc442015-11-24 02:59:55 -0700136
137 SigningInfo infoId("id:/my-identity");
138 BOOST_CHECK_EQUAL(infoId.getSignerType(), SigningInfo::SIGNER_TYPE_ID);
139 BOOST_CHECK_EQUAL(infoId.getSignerName(), "/my-identity");
Yingdi Yu99b2a002015-08-12 12:47:44 -0700140 BOOST_CHECK_EQUAL(infoId.getDigestAlgorithm(), DigestAlgorithm::SHA256);
Spencer Lee308bc442015-11-24 02:59:55 -0700141
142 SigningInfo infoKey("key:/my-key");
143 BOOST_CHECK_EQUAL(infoKey.getSignerType(), SigningInfo::SIGNER_TYPE_KEY);
144 BOOST_CHECK_EQUAL(infoKey.getSignerName(), "/my-key");
Yingdi Yu99b2a002015-08-12 12:47:44 -0700145 BOOST_CHECK_EQUAL(infoKey.getDigestAlgorithm(), DigestAlgorithm::SHA256);
Spencer Lee308bc442015-11-24 02:59:55 -0700146
147 SigningInfo infoCert("cert:/my-cert");
148 BOOST_CHECK_EQUAL(infoCert.getSignerType(), SigningInfo::SIGNER_TYPE_CERT);
149 BOOST_CHECK_EQUAL(infoCert.getSignerName(), "/my-cert");
Yingdi Yu99b2a002015-08-12 12:47:44 -0700150 BOOST_CHECK_EQUAL(infoCert.getDigestAlgorithm(), DigestAlgorithm::SHA256);
Spencer Lee308bc442015-11-24 02:59:55 -0700151
laqinfan56a812d2019-06-03 15:33:58 -0500152 SigningInfo infoHmac("hmac-sha256:QjM3NEEyNkE3MTQ5MDQzN0FBMDI0RTRGQURENU"
153 "I0OTdGREZGMUE4RUE2RkYxMkY2RkI2NUFGMjcyMEI1OUNDRg==");
154 BOOST_CHECK_EQUAL(infoHmac.getSignerType(), SigningInfo::SIGNER_TYPE_HMAC);
155 BOOST_CHECK_EQUAL(infoHmac.getSignerName().getPrefix(3), SigningInfo::getHmacIdentity());
156 BOOST_CHECK_EQUAL(infoHmac.getDigestAlgorithm(), DigestAlgorithm::SHA256);
157
Spencer Lee308bc442015-11-24 02:59:55 -0700158 SigningInfo infoSha("id:/localhost/identity/digest-sha256");
159 BOOST_CHECK_EQUAL(infoSha.getSignerType(), SigningInfo::SIGNER_TYPE_SHA256);
Eric Newberry78ffc632020-05-17 15:13:46 -0700160 BOOST_CHECK_EQUAL(infoSha.getSignerName(), Name());
Yingdi Yu99b2a002015-08-12 12:47:44 -0700161 BOOST_CHECK_EQUAL(infoSha.getDigestAlgorithm(), DigestAlgorithm::SHA256);
Spencer Lee308bc442015-11-24 02:59:55 -0700162}
163
164BOOST_AUTO_TEST_CASE(ToString)
165{
166 // We can't use lexical_cast due to Boost Bug 6298.
167 std::stringstream ss;
168 ss << SigningInfo();
169 BOOST_CHECK_EQUAL(ss.str(), "");
170
171 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(
172 SigningInfo(SigningInfo::SIGNER_TYPE_ID, "/my-identity")), "id:/my-identity");
173 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(
174 SigningInfo(SigningInfo::SIGNER_TYPE_KEY, "/my-key")), "key:/my-key");
175 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(
176 SigningInfo(SigningInfo::SIGNER_TYPE_CERT, "/my-cert")), "cert:/my-cert");
177 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(
178 SigningInfo(SigningInfo::SIGNER_TYPE_SHA256)),
179 "id:/localhost/identity/digest-sha256");
laqinfan56a812d2019-06-03 15:33:58 -0500180 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(
181 SigningInfo(SigningInfo::SIGNER_TYPE_HMAC, "/localhost/identity/hmac/1234")),
182 "id:/localhost/identity/hmac/1234");
Spencer Lee308bc442015-11-24 02:59:55 -0700183}
184
Alexander Afanasyevc95f5642017-01-04 17:34:26 -0800185BOOST_AUTO_TEST_CASE(Chaining)
186{
187 SigningInfo info = SigningInfo()
188 .setSigningIdentity("/identity")
189 .setSigningKeyName("/key/name")
190 .setSigningCertName("/cert/name")
Alexander Afanasyevd6d78aa2017-01-02 18:14:23 -0800191 .setPibIdentity(Identity())
192 .setPibKey(Key())
Alexander Afanasyevc95f5642017-01-04 17:34:26 -0800193 .setSha256Signing()
194 .setDigestAlgorithm(DigestAlgorithm::SHA256)
195 .setSignatureInfo(SignatureInfo());
196
197 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(info), "id:/localhost/identity/digest-sha256");
198}
199
Nick Gordon2a6d45a2017-01-25 14:10:44 -0600200BOOST_AUTO_TEST_CASE(OperatorEquals)
201{
202 // Check name equality
203 SigningInfo info1("id:/my-id");
204 SigningInfo info2("id:/my-id");
205 BOOST_CHECK_EQUAL(info1, info2);
206 // Change name, check inequality
207 info2 = SigningInfo("id:/not-same-id");
208 BOOST_CHECK_NE(info1, info2);
209
210 // Check name, digest algo equality
211 info1 = SigningInfo("id:/my-id");
212 info2 = SigningInfo("id:/my-id");
213 info1.setDigestAlgorithm(DigestAlgorithm::SHA256);
214 info2.setDigestAlgorithm(DigestAlgorithm::SHA256);
215 BOOST_CHECK_EQUAL(info1, info2);
216 // Change digest algo, check inequality
217 info2.setDigestAlgorithm(DigestAlgorithm::NONE);
218 BOOST_CHECK_NE(info1, info2);
219
220 // Check name, digest algo, signature info equality
221 info1 = SigningInfo("id:/my-id");
222 info2 = SigningInfo("id:/my-id");
223 info1.setDigestAlgorithm(DigestAlgorithm::SHA256);
224 info2.setDigestAlgorithm(DigestAlgorithm::SHA256);
Davide Pesavento7e6f6f82017-10-31 18:05:28 -0400225 SignatureInfo sigInfo1(tlv::DigestSha256);
Nick Gordon2a6d45a2017-01-25 14:10:44 -0600226 info1.setSignatureInfo(sigInfo1);
227 info2.setSignatureInfo(sigInfo1);
228 BOOST_CHECK_EQUAL(info1, info2);
229 // Change signature info, check inequality
Davide Pesavento7e6f6f82017-10-31 18:05:28 -0400230 SignatureInfo sigInfo2(tlv::SignatureSha256WithRsa);
Nick Gordon2a6d45a2017-01-25 14:10:44 -0600231 info2.setSignatureInfo(sigInfo2);
232 BOOST_CHECK_NE(info1, info2);
233}
234
235BOOST_AUTO_TEST_CASE(OperatorEqualsDifferentTypes)
236{
237 SigningInfo info1("key:/my-id/KEY/1");
238 SigningInfo info2("key:/my-id/KEY/1");
239 // Check equality for key type
240 BOOST_CHECK_EQUAL(info1, info2);
241 info2 = SigningInfo("id:/my-id");
242 // Change signature type, check inequality
243 BOOST_CHECK_NE(info1, info2);
244 info2 = SigningInfo("key:/not-same-id/KEY/1");
245 // Change key name, check inequality
246 BOOST_CHECK_NE(info1, info2);
247
248 info1 = SigningInfo("cert:/my-id/KEY/1/self/%FD01");
249 info2 = SigningInfo("cert:/my-id/KEY/1/self/%FD01");
250 // Check equality for cert type
251 BOOST_CHECK_EQUAL(info1, info2);
252 info2 = SigningInfo("cert:/not-my-id/KEY/1/other/%FD01");
253 // Change cert name, check inequality
254 BOOST_CHECK_NE(info1, info2);
255 info2 = SigningInfo("id:/my-id");
256 // Change signature type, check inequality
257 BOOST_CHECK_NE(info1, info2);
258
259 info1 = SigningInfo(SigningInfo::SIGNER_TYPE_NULL);
260 info2 = SigningInfo(SigningInfo::SIGNER_TYPE_NULL);
261 // Check equality for null type
262 BOOST_CHECK_EQUAL(info1, info2);
263 info2 = SigningInfo("id:/my-id");
264 // Change signature type, check inequality
265 BOOST_CHECK_NE(info1, info2);
266
267 info1 = SigningInfo(SigningInfo::SIGNER_TYPE_SHA256);
268 info2 = SigningInfo(SigningInfo::SIGNER_TYPE_SHA256);
269 // Check equality for SHA256 digest type
270 BOOST_CHECK_EQUAL(info1, info2);
271 info2 = SigningInfo("id:/my-id");
272 // Change signature type, check inequality
273 BOOST_CHECK_NE(info1, info2);
Eric Newberryb74bbda2020-06-18 19:33:58 -0700274
275 info1 = SigningInfo(SigningInfo::SIGNER_TYPE_SHA256);
276 info2 = SigningInfo(SigningInfo::SIGNER_TYPE_SHA256);
277 // Check equality for signed Interest format
278 BOOST_CHECK_EQUAL(info1, info2);
279 info2.setSignedInterestFormat(SignedInterestFormat::V03);
280 // Change signed Interest format, check inequality
281 BOOST_CHECK_NE(info1, info2);
Nick Gordon2a6d45a2017-01-25 14:10:44 -0600282}
283
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100284BOOST_AUTO_TEST_SUITE_END() // TestSigningInfo
285BOOST_AUTO_TEST_SUITE_END() // Security
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700286
Davide Pesavento47ce2ee2023-05-09 01:33:33 -0400287} // namespace ndn::tests