blob: 9b59a13dad2bf8dee8f8dd99a3b7f8e9da3fd27e [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/*
Eric Newberry78ffc632020-05-17 15:13:46 -07003 * Copyright (c) 2013-2020 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 Pesaventoeee3e822016-11-26 19:19:34 +010026#include <boost/lexical_cast.hpp>
27#include <sstream>
28
Yingdi Yu1b0311c2015-06-10 14:58:47 -070029namespace ndn {
30namespace security {
31namespace tests {
32
Davide Pesaventoeee3e822016-11-26 19:19:34 +010033BOOST_AUTO_TEST_SUITE(Security)
34BOOST_AUTO_TEST_SUITE(TestSigningInfo)
Yingdi Yu1b0311c2015-06-10 14:58:47 -070035
36BOOST_AUTO_TEST_CASE(Basic)
37{
Spencer Lee308bc442015-11-24 02:59:55 -070038 Name id("/my-identity");
39 Name key("/my-key");
40 Name cert("/my-cert");
Yingdi Yu1b0311c2015-06-10 14:58:47 -070041
42 SigningInfo info;
43
44 BOOST_CHECK_EQUAL(info.getSignerType(), SigningInfo::SIGNER_TYPE_NULL);
Eric Newberry78ffc632020-05-17 15:13:46 -070045 BOOST_CHECK_EQUAL(info.getSignerName(), Name());
Yingdi Yu99b2a002015-08-12 12:47:44 -070046 BOOST_CHECK_EQUAL(info.getDigestAlgorithm(), DigestAlgorithm::SHA256);
Yingdi Yu1b0311c2015-06-10 14:58:47 -070047
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 Yu99b2a002015-08-12 12:47:44 -070055 BOOST_CHECK_EQUAL(info.getDigestAlgorithm(), DigestAlgorithm::SHA256);
Yingdi Yu1b0311c2015-06-10 14:58:47 -070056
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 Yu99b2a002015-08-12 12:47:44 -070060 BOOST_CHECK_EQUAL(infoId.getDigestAlgorithm(), DigestAlgorithm::SHA256);
Yingdi Yu1b0311c2015-06-10 14:58:47 -070061
62 info.setSigningKeyName(key);
63 BOOST_CHECK_EQUAL(info.getSignerType(), SigningInfo::SIGNER_TYPE_KEY);
64 BOOST_CHECK_EQUAL(info.getSignerName(), key);
Yingdi Yu99b2a002015-08-12 12:47:44 -070065 BOOST_CHECK_EQUAL(info.getDigestAlgorithm(), DigestAlgorithm::SHA256);
Yingdi Yu1b0311c2015-06-10 14:58:47 -070066
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 Yu99b2a002015-08-12 12:47:44 -070070 BOOST_CHECK_EQUAL(infoKey.getDigestAlgorithm(), DigestAlgorithm::SHA256);
Yingdi Yu1b0311c2015-06-10 14:58:47 -070071
72 info.setSigningCertName(cert);
73 BOOST_CHECK_EQUAL(info.getSignerType(), SigningInfo::SIGNER_TYPE_CERT);
74 BOOST_CHECK_EQUAL(info.getSignerName(), cert);
Yingdi Yu99b2a002015-08-12 12:47:44 -070075 BOOST_CHECK_EQUAL(info.getDigestAlgorithm(), DigestAlgorithm::SHA256);
Yingdi Yu1b0311c2015-06-10 14:58:47 -070076
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 Yu99b2a002015-08-12 12:47:44 -070080 BOOST_CHECK_EQUAL(infoCert.getDigestAlgorithm(), DigestAlgorithm::SHA256);
Yingdi Yu1b0311c2015-06-10 14:58:47 -070081
82 info.setSha256Signing();
83 BOOST_CHECK_EQUAL(info.getSignerType(), SigningInfo::SIGNER_TYPE_SHA256);
Eric Newberry78ffc632020-05-17 15:13:46 -070084 BOOST_CHECK_EQUAL(info.getSignerName(), Name());
Yingdi Yu99b2a002015-08-12 12:47:44 -070085 BOOST_CHECK_EQUAL(info.getDigestAlgorithm(), DigestAlgorithm::SHA256);
Yingdi Yu1b0311c2015-06-10 14:58:47 -070086
87 SigningInfo infoSha(SigningInfo::SIGNER_TYPE_SHA256);
88 BOOST_CHECK_EQUAL(infoSha.getSignerType(), SigningInfo::SIGNER_TYPE_SHA256);
Eric Newberry78ffc632020-05-17 15:13:46 -070089 BOOST_CHECK_EQUAL(infoSha.getSignerName(), Name());
Yingdi Yu99b2a002015-08-12 12:47:44 -070090 BOOST_CHECK_EQUAL(infoSha.getDigestAlgorithm(), DigestAlgorithm::SHA256);
laqinfan56a812d2019-06-03 15:33:58 -050091
92 std::string encodedKey("QjM3NEEyNkE3MTQ5MDQzN0FBMDI0RTRGQURENUI0OTdGRE"
93 "ZGMUE4RUE2RkYxMkY2RkI2NUFGMjcyMEI1OUNDRg==");
94 info.setSigningHmacKey(encodedKey);
95 BOOST_CHECK_EQUAL(info.getSignerType(), SigningInfo::SIGNER_TYPE_HMAC);
96 BOOST_CHECK_EQUAL(info.getDigestAlgorithm(), DigestAlgorithm::SHA256);
97
98 SigningInfo infoHmac(SigningInfo::SIGNER_TYPE_HMAC, info.getSignerName());
99 BOOST_CHECK_EQUAL(infoHmac.getSignerType(), SigningInfo::SIGNER_TYPE_HMAC);
100 BOOST_CHECK_EQUAL(infoHmac.getDigestAlgorithm(), DigestAlgorithm::SHA256);
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700101}
102
103BOOST_AUTO_TEST_CASE(CustomSignatureInfo)
104{
105 SigningInfo info1;
Junxiao Shi72c0c642018-04-20 15:41:09 +0000106 BOOST_CHECK_EQUAL(info1.getSignatureInfo(), SignatureInfo());
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700107
108 SignatureInfo si;
109 si.setKeyLocator(Name("ndn:/test/key/locator"));
110 info1.setSignatureInfo(si);
111
Junxiao Shi72c0c642018-04-20 15:41:09 +0000112 BOOST_CHECK_EQUAL(info1.getSignatureInfo(), si);
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700113
Eric Newberry78ffc632020-05-17 15:13:46 -0700114 SigningInfo info2(SigningInfo::SIGNER_TYPE_NULL, Name(), si);
Junxiao Shi72c0c642018-04-20 15:41:09 +0000115 BOOST_CHECK_EQUAL(info2.getSignatureInfo(), si);
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700116}
117
Spencer Lee308bc442015-11-24 02:59:55 -0700118BOOST_AUTO_TEST_CASE(FromString)
119{
120 SigningInfo infoDefault("");
121 BOOST_CHECK_EQUAL(infoDefault.getSignerType(), SigningInfo::SIGNER_TYPE_NULL);
Eric Newberry78ffc632020-05-17 15:13:46 -0700122 BOOST_CHECK_EQUAL(infoDefault.getSignerName(), Name());
Yingdi Yu99b2a002015-08-12 12:47:44 -0700123 BOOST_CHECK_EQUAL(infoDefault.getDigestAlgorithm(), DigestAlgorithm::SHA256);
Spencer Lee308bc442015-11-24 02:59:55 -0700124
125 SigningInfo infoId("id:/my-identity");
126 BOOST_CHECK_EQUAL(infoId.getSignerType(), SigningInfo::SIGNER_TYPE_ID);
127 BOOST_CHECK_EQUAL(infoId.getSignerName(), "/my-identity");
Yingdi Yu99b2a002015-08-12 12:47:44 -0700128 BOOST_CHECK_EQUAL(infoId.getDigestAlgorithm(), DigestAlgorithm::SHA256);
Spencer Lee308bc442015-11-24 02:59:55 -0700129
130 SigningInfo infoKey("key:/my-key");
131 BOOST_CHECK_EQUAL(infoKey.getSignerType(), SigningInfo::SIGNER_TYPE_KEY);
132 BOOST_CHECK_EQUAL(infoKey.getSignerName(), "/my-key");
Yingdi Yu99b2a002015-08-12 12:47:44 -0700133 BOOST_CHECK_EQUAL(infoKey.getDigestAlgorithm(), DigestAlgorithm::SHA256);
Spencer Lee308bc442015-11-24 02:59:55 -0700134
135 SigningInfo infoCert("cert:/my-cert");
136 BOOST_CHECK_EQUAL(infoCert.getSignerType(), SigningInfo::SIGNER_TYPE_CERT);
137 BOOST_CHECK_EQUAL(infoCert.getSignerName(), "/my-cert");
Yingdi Yu99b2a002015-08-12 12:47:44 -0700138 BOOST_CHECK_EQUAL(infoCert.getDigestAlgorithm(), DigestAlgorithm::SHA256);
Spencer Lee308bc442015-11-24 02:59:55 -0700139
laqinfan56a812d2019-06-03 15:33:58 -0500140 SigningInfo infoHmac("hmac-sha256:QjM3NEEyNkE3MTQ5MDQzN0FBMDI0RTRGQURENU"
141 "I0OTdGREZGMUE4RUE2RkYxMkY2RkI2NUFGMjcyMEI1OUNDRg==");
142 BOOST_CHECK_EQUAL(infoHmac.getSignerType(), SigningInfo::SIGNER_TYPE_HMAC);
143 BOOST_CHECK_EQUAL(infoHmac.getSignerName().getPrefix(3), SigningInfo::getHmacIdentity());
144 BOOST_CHECK_EQUAL(infoHmac.getDigestAlgorithm(), DigestAlgorithm::SHA256);
145
Spencer Lee308bc442015-11-24 02:59:55 -0700146 SigningInfo infoSha("id:/localhost/identity/digest-sha256");
147 BOOST_CHECK_EQUAL(infoSha.getSignerType(), SigningInfo::SIGNER_TYPE_SHA256);
Eric Newberry78ffc632020-05-17 15:13:46 -0700148 BOOST_CHECK_EQUAL(infoSha.getSignerName(), Name());
Yingdi Yu99b2a002015-08-12 12:47:44 -0700149 BOOST_CHECK_EQUAL(infoSha.getDigestAlgorithm(), DigestAlgorithm::SHA256);
Spencer Lee308bc442015-11-24 02:59:55 -0700150}
151
152BOOST_AUTO_TEST_CASE(ToString)
153{
154 // We can't use lexical_cast due to Boost Bug 6298.
155 std::stringstream ss;
156 ss << SigningInfo();
157 BOOST_CHECK_EQUAL(ss.str(), "");
158
159 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(
160 SigningInfo(SigningInfo::SIGNER_TYPE_ID, "/my-identity")), "id:/my-identity");
161 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(
162 SigningInfo(SigningInfo::SIGNER_TYPE_KEY, "/my-key")), "key:/my-key");
163 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(
164 SigningInfo(SigningInfo::SIGNER_TYPE_CERT, "/my-cert")), "cert:/my-cert");
165 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(
166 SigningInfo(SigningInfo::SIGNER_TYPE_SHA256)),
167 "id:/localhost/identity/digest-sha256");
laqinfan56a812d2019-06-03 15:33:58 -0500168 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(
169 SigningInfo(SigningInfo::SIGNER_TYPE_HMAC, "/localhost/identity/hmac/1234")),
170 "id:/localhost/identity/hmac/1234");
Spencer Lee308bc442015-11-24 02:59:55 -0700171}
172
Alexander Afanasyevc95f5642017-01-04 17:34:26 -0800173BOOST_AUTO_TEST_CASE(Chaining)
174{
175 SigningInfo info = SigningInfo()
176 .setSigningIdentity("/identity")
177 .setSigningKeyName("/key/name")
178 .setSigningCertName("/cert/name")
Alexander Afanasyevd6d78aa2017-01-02 18:14:23 -0800179 .setPibIdentity(Identity())
180 .setPibKey(Key())
Alexander Afanasyevc95f5642017-01-04 17:34:26 -0800181 .setSha256Signing()
182 .setDigestAlgorithm(DigestAlgorithm::SHA256)
183 .setSignatureInfo(SignatureInfo());
184
185 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(info), "id:/localhost/identity/digest-sha256");
186}
187
Nick Gordon2a6d45a2017-01-25 14:10:44 -0600188BOOST_AUTO_TEST_CASE(OperatorEquals)
189{
190 // Check name equality
191 SigningInfo info1("id:/my-id");
192 SigningInfo info2("id:/my-id");
193 BOOST_CHECK_EQUAL(info1, info2);
194 // Change name, check inequality
195 info2 = SigningInfo("id:/not-same-id");
196 BOOST_CHECK_NE(info1, info2);
197
198 // Check name, digest algo equality
199 info1 = SigningInfo("id:/my-id");
200 info2 = SigningInfo("id:/my-id");
201 info1.setDigestAlgorithm(DigestAlgorithm::SHA256);
202 info2.setDigestAlgorithm(DigestAlgorithm::SHA256);
203 BOOST_CHECK_EQUAL(info1, info2);
204 // Change digest algo, check inequality
205 info2.setDigestAlgorithm(DigestAlgorithm::NONE);
206 BOOST_CHECK_NE(info1, info2);
207
208 // Check name, digest algo, signature info equality
209 info1 = SigningInfo("id:/my-id");
210 info2 = SigningInfo("id:/my-id");
211 info1.setDigestAlgorithm(DigestAlgorithm::SHA256);
212 info2.setDigestAlgorithm(DigestAlgorithm::SHA256);
Davide Pesavento7e6f6f82017-10-31 18:05:28 -0400213 SignatureInfo sigInfo1(tlv::DigestSha256);
Nick Gordon2a6d45a2017-01-25 14:10:44 -0600214 info1.setSignatureInfo(sigInfo1);
215 info2.setSignatureInfo(sigInfo1);
216 BOOST_CHECK_EQUAL(info1, info2);
217 // Change signature info, check inequality
Davide Pesavento7e6f6f82017-10-31 18:05:28 -0400218 SignatureInfo sigInfo2(tlv::SignatureSha256WithRsa);
Nick Gordon2a6d45a2017-01-25 14:10:44 -0600219 info2.setSignatureInfo(sigInfo2);
220 BOOST_CHECK_NE(info1, info2);
221}
222
223BOOST_AUTO_TEST_CASE(OperatorEqualsDifferentTypes)
224{
225 SigningInfo info1("key:/my-id/KEY/1");
226 SigningInfo info2("key:/my-id/KEY/1");
227 // Check equality for key type
228 BOOST_CHECK_EQUAL(info1, info2);
229 info2 = SigningInfo("id:/my-id");
230 // Change signature type, check inequality
231 BOOST_CHECK_NE(info1, info2);
232 info2 = SigningInfo("key:/not-same-id/KEY/1");
233 // Change key name, check inequality
234 BOOST_CHECK_NE(info1, info2);
235
236 info1 = SigningInfo("cert:/my-id/KEY/1/self/%FD01");
237 info2 = SigningInfo("cert:/my-id/KEY/1/self/%FD01");
238 // Check equality for cert type
239 BOOST_CHECK_EQUAL(info1, info2);
240 info2 = SigningInfo("cert:/not-my-id/KEY/1/other/%FD01");
241 // Change cert name, check inequality
242 BOOST_CHECK_NE(info1, info2);
243 info2 = SigningInfo("id:/my-id");
244 // Change signature type, check inequality
245 BOOST_CHECK_NE(info1, info2);
246
247 info1 = SigningInfo(SigningInfo::SIGNER_TYPE_NULL);
248 info2 = SigningInfo(SigningInfo::SIGNER_TYPE_NULL);
249 // Check equality for null type
250 BOOST_CHECK_EQUAL(info1, info2);
251 info2 = SigningInfo("id:/my-id");
252 // Change signature type, check inequality
253 BOOST_CHECK_NE(info1, info2);
254
255 info1 = SigningInfo(SigningInfo::SIGNER_TYPE_SHA256);
256 info2 = SigningInfo(SigningInfo::SIGNER_TYPE_SHA256);
257 // Check equality for SHA256 digest type
258 BOOST_CHECK_EQUAL(info1, info2);
259 info2 = SigningInfo("id:/my-id");
260 // Change signature type, check inequality
261 BOOST_CHECK_NE(info1, info2);
262}
263
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100264BOOST_AUTO_TEST_SUITE_END() // TestSigningInfo
265BOOST_AUTO_TEST_SUITE_END() // Security
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700266
267} // namespace tests
268} // namespace security
269} // namespace ndn