blob: 90bd1b93195dd0f61ec397f0ef18827fb7dd1b2f [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Yingdi Yu9a335352014-01-31 11:57:46 -08002/**
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -08003 * Copyright (c) 2013-2017 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * 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.
Yingdi Yu9a335352014-01-31 11:57:46 -080020 */
21
Yingdi Yu9a335352014-01-31 11:57:46 -080022#include "security/validator-null.hpp"
Junxiao Shi198c3812016-08-12 19:24:18 +000023
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070024#include "boost-test.hpp"
Junxiao Shi198c3812016-08-12 19:24:18 +000025#include "identity-management-fixture.hpp"
26#include "../make-interest-data.hpp"
Yingdi Yu9a335352014-01-31 11:57:46 -080027
Yingdi Yu9a335352014-01-31 11:57:46 -080028namespace ndn {
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070029namespace security {
Spyridon Mastorakis429634f2015-02-19 17:35:33 -080030namespace tests {
Yingdi Yu9a335352014-01-31 11:57:46 -080031
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070032using namespace ndn::tests;
33
Junxiao Shi198c3812016-08-12 19:24:18 +000034BOOST_AUTO_TEST_SUITE(Security)
35BOOST_FIXTURE_TEST_SUITE(TestValidator, IdentityManagementFixture)
Yingdi Yu9a335352014-01-31 11:57:46 -080036
37void
38onValidated(const shared_ptr<const Data>& data)
Alexander Afanasyev24b75c82014-05-31 15:59:31 +030039{
40 BOOST_CHECK(true);
41}
Yingdi Yu9a335352014-01-31 11:57:46 -080042
43void
Junxiao Shi198c3812016-08-12 19:24:18 +000044onValidationFailed(const shared_ptr<const Data>& data, const std::string& failureInfo)
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070045{
46 BOOST_CHECK(false);
Yingdi Yu40587c02014-02-21 16:40:48 -080047}
Yingdi Yu9a335352014-01-31 11:57:46 -080048
Yingdi Yuf56c68f2014-04-24 21:50:13 -070049BOOST_AUTO_TEST_CASE(Null)
Yingdi Yu9a335352014-01-31 11:57:46 -080050{
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070051 Name identity("/TestValidator/Null");
52 identity.appendVersion();
Yingdi Yu3ed09d02014-10-13 16:24:08 -070053 BOOST_REQUIRE(addIdentity(identity, RsaKeyParams()));
Yingdi Yu9a335352014-01-31 11:57:46 -080054
55 Name dataName = identity;
56 dataName.append("1");
57 shared_ptr<Data> data = make_shared<Data>(dataName);
58
Yingdi Yu1b0311c2015-06-10 14:58:47 -070059 BOOST_CHECK_NO_THROW(m_keyChain.sign(*data,
60 security::SigningInfo(security::SigningInfo::SIGNER_TYPE_ID,
61 identity)));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070062
Yingdi Yu9a335352014-01-31 11:57:46 -080063 ValidatorNull validator;
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080064
65 // data must be a shared pointer
66 validator.validate(*data,
Yingdi Yu96e64062014-04-15 19:57:33 -070067 bind(&onValidated, _1),
68 bind(&onValidationFailed, _1, _2));
Yingdi Yu9a335352014-01-31 11:57:46 -080069}
70
Yingdi Yuc8f883c2014-06-20 23:25:22 -070071const uint8_t ecdsaSigInfo[] = {
720x16, 0x1b, // SignatureInfo
73 0x1b, 0x01, // SignatureType
74 0x03,
75 0x1c, 0x16, // KeyLocator
76 0x07, 0x14, // Name
77 0x08, 0x04,
78 0x74, 0x65, 0x73, 0x74,
79 0x08, 0x03,
80 0x6b, 0x65, 0x79,
81 0x08, 0x07,
82 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72
83};
84
85const uint8_t ecdsaSigValue[] = {
860x17, 0x40, // SignatureValue
87 0x2f, 0xd6, 0xf1, 0x6e, 0x80, 0x6f, 0x10, 0xbe, 0xb1, 0x6f, 0x3e, 0x31, 0xec,
88 0xe3, 0xb9, 0xea, 0x83, 0x30, 0x40, 0x03, 0xfc, 0xa0, 0x13, 0xd9, 0xb3, 0xc6,
89 0x25, 0x16, 0x2d, 0xa6, 0x58, 0x41, 0x69, 0x62, 0x56, 0xd8, 0xb3, 0x6a, 0x38,
90 0x76, 0x56, 0xea, 0x61, 0xb2, 0x32, 0x70, 0x1c, 0xb6, 0x4d, 0x10, 0x1d, 0xdc,
91 0x92, 0x8e, 0x52, 0xa5, 0x8a, 0x1d, 0xd9, 0x96, 0x5e, 0xc0, 0x62, 0x0b
92};
93
94BOOST_AUTO_TEST_CASE(RsaSignatureVerification)
95{
Yingdi Yuc8f883c2014-06-20 23:25:22 -070096 Name identity("/TestValidator/RsaSignatureVerification");
Yingdi Yu3ed09d02014-10-13 16:24:08 -070097 BOOST_REQUIRE(addIdentity(identity, RsaKeyParams()));
98 Name keyName = m_keyChain.getDefaultKeyNameForIdentity(identity);
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070099 shared_ptr<v1::PublicKey> publicKey = m_keyChain.getPublicKey(keyName);
Yingdi Yuc8f883c2014-06-20 23:25:22 -0700100
101 Name identity2("/TestValidator/RsaSignatureVerification/id2");
Yingdi Yu3ed09d02014-10-13 16:24:08 -0700102 BOOST_REQUIRE(addIdentity(identity2, RsaKeyParams()));
103 Name keyName2 = m_keyChain.getDefaultKeyNameForIdentity(identity2);
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700104 shared_ptr<v1::PublicKey> publicKey2 = m_keyChain.getPublicKey(keyName2);
Yingdi Yuc8f883c2014-06-20 23:25:22 -0700105
106 Data data("/TestData/1");
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700107 BOOST_CHECK_NO_THROW(m_keyChain.sign(data,
108 security::SigningInfo(security::SigningInfo::SIGNER_TYPE_ID,
109 identity)));
Yingdi Yuc8f883c2014-06-20 23:25:22 -0700110 BOOST_CHECK_EQUAL(Validator::verifySignature(data, *publicKey), true);
111 BOOST_CHECK_EQUAL(Validator::verifySignature(data, *publicKey2), false);
112
113 Interest interest("/TestInterest/1");
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700114 BOOST_CHECK_NO_THROW(m_keyChain.sign(interest,
115 security::SigningInfo(security::SigningInfo::SIGNER_TYPE_ID,
116 identity)));
Yingdi Yuc8f883c2014-06-20 23:25:22 -0700117 BOOST_CHECK_EQUAL(Validator::verifySignature(interest, *publicKey), true);
118 BOOST_CHECK_EQUAL(Validator::verifySignature(interest, *publicKey2), false);
119
120 Data wrongData("/TestData/2");
121 Block ecdsaSigInfoBlock(ecdsaSigInfo, sizeof(ecdsaSigInfo));
122 Block ecdsaSigValueBlock(ecdsaSigValue, sizeof(ecdsaSigValue));
123 Signature ecdsaSig(ecdsaSigInfoBlock, ecdsaSigValueBlock);
124 wrongData.setSignature(ecdsaSig);
125 BOOST_CHECK_EQUAL(Validator::verifySignature(wrongData, *publicKey), false);
Yingdi Yuc8f883c2014-06-20 23:25:22 -0700126}
127
128const uint8_t rsaSigInfo[] = {
1290x16, 0x1b, // SignatureInfo
130 0x1b, 0x01, // SignatureType
131 0x01,
132 0x1c, 0x16, // KeyLocator
133 0x07, 0x14, // Name
134 0x08, 0x04,
135 0x74, 0x65, 0x73, 0x74,
136 0x08, 0x03,
137 0x6b, 0x65, 0x79,
138 0x08, 0x07,
139 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72
140};
141
142const uint8_t rsaSigValue[] = {
1430x17, 0x80, // SignatureValue
144 0x2f, 0xd6, 0xf1, 0x6e, 0x80, 0x6f, 0x10, 0xbe, 0xb1, 0x6f, 0x3e, 0x31, 0xec,
145 0xe3, 0xb9, 0xea, 0x83, 0x30, 0x40, 0x03, 0xfc, 0xa0, 0x13, 0xd9, 0xb3, 0xc6,
146 0x25, 0x16, 0x2d, 0xa6, 0x58, 0x41, 0x69, 0x62, 0x56, 0xd8, 0xb3, 0x6a, 0x38,
147 0x76, 0x56, 0xea, 0x61, 0xb2, 0x32, 0x70, 0x1c, 0xb6, 0x4d, 0x10, 0x1d, 0xdc,
148 0x92, 0x8e, 0x52, 0xa5, 0x8a, 0x1d, 0xd9, 0x96, 0x5e, 0xc0, 0x62, 0x0b, 0xcf,
149 0x3a, 0x9d, 0x7f, 0xca, 0xbe, 0xa1, 0x41, 0x71, 0x85, 0x7a, 0x8b, 0x5d, 0xa9,
150 0x64, 0xd6, 0x66, 0xb4, 0xe9, 0x8d, 0x0c, 0x28, 0x43, 0xee, 0xa6, 0x64, 0xe8,
151 0x55, 0xf6, 0x1c, 0x19, 0x0b, 0xef, 0x99, 0x25, 0x1e, 0xdc, 0x78, 0xb3, 0xa7,
152 0xaa, 0x0d, 0x14, 0x58, 0x30, 0xe5, 0x37, 0x6a, 0x6d, 0xdb, 0x56, 0xac, 0xa3,
153 0xfc, 0x90, 0x7a, 0xb8, 0x66, 0x9c, 0x0e, 0xf6, 0xb7, 0x64, 0xd1
154};
155
156
157BOOST_AUTO_TEST_CASE(EcdsaSignatureVerification)
158{
Yingdi Yuc8f883c2014-06-20 23:25:22 -0700159 Name identity("/TestValidator/EcdsaSignatureVerification");
Yingdi Yu3ed09d02014-10-13 16:24:08 -0700160 BOOST_REQUIRE(addIdentity(identity, EcdsaKeyParams()));
161 Name keyName = m_keyChain.getDefaultKeyNameForIdentity(identity);
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700162 shared_ptr<v1::PublicKey> publicKey = m_keyChain.getPublicKey(keyName);
Yingdi Yuc8f883c2014-06-20 23:25:22 -0700163
164 Name identity2("/TestValidator/EcdsaSignatureVerification/id2");
Yingdi Yu3ed09d02014-10-13 16:24:08 -0700165 BOOST_REQUIRE(addIdentity(identity2, EcdsaKeyParams()));
166 Name keyName2 = m_keyChain.getDefaultKeyNameForIdentity(identity2);
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700167 shared_ptr<v1::PublicKey> publicKey2 = m_keyChain.getPublicKey(keyName2);
Yingdi Yuc8f883c2014-06-20 23:25:22 -0700168
169
170 Data data("/TestData/1");
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700171 BOOST_CHECK_NO_THROW(m_keyChain.sign(data,
172 security::SigningInfo(security::SigningInfo::SIGNER_TYPE_ID,
173 identity)));
Yingdi Yuc8f883c2014-06-20 23:25:22 -0700174 BOOST_CHECK_EQUAL(Validator::verifySignature(data, *publicKey), true);
175 BOOST_CHECK_EQUAL(Validator::verifySignature(data, *publicKey2), false);
176
177 Interest interest("/TestInterest/1");
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700178 BOOST_CHECK_NO_THROW(m_keyChain.sign(interest,
179 security::SigningInfo(security::SigningInfo::SIGNER_TYPE_ID,
180 identity)));
Yingdi Yuc8f883c2014-06-20 23:25:22 -0700181 BOOST_CHECK_EQUAL(Validator::verifySignature(interest, *publicKey), true);
182 BOOST_CHECK_EQUAL(Validator::verifySignature(interest, *publicKey2), false);
183
184 Data wrongData("/TestData/2");
185 Block rsaSigInfoBlock(rsaSigInfo, sizeof(rsaSigInfo));
186 Block rsaSigValueBlock(rsaSigValue, sizeof(rsaSigValue));
187 Signature rsaSig(rsaSigInfoBlock, rsaSigValueBlock);
188 wrongData.setSignature(rsaSig);
189 BOOST_CHECK_EQUAL(Validator::verifySignature(wrongData, *publicKey), false);
Yingdi Yuc8f883c2014-06-20 23:25:22 -0700190}
191
Yingdi Yu5ec0ee32014-06-24 16:26:09 -0700192BOOST_AUTO_TEST_CASE(EcdsaSignatureVerification2)
193{
Yingdi Yu5ec0ee32014-06-24 16:26:09 -0700194 Name ecdsaIdentity("/SecurityTestValidator/EcdsaSignatureVerification2/ecdsa");
Yingdi Yu3ed09d02014-10-13 16:24:08 -0700195 BOOST_REQUIRE(addIdentity(ecdsaIdentity, EcdsaKeyParams()));
196 Name ecdsaCertName = m_keyChain.getDefaultCertificateNameForIdentity(ecdsaIdentity);
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700197 shared_ptr<v1::IdentityCertificate> ecdsaCert = m_keyChain.getCertificate(ecdsaCertName);
Yingdi Yu5ec0ee32014-06-24 16:26:09 -0700198
199 Name rsaIdentity("/SecurityTestValidator/EcdsaSignatureVerification2/rsa");
Yingdi Yu3ed09d02014-10-13 16:24:08 -0700200 BOOST_REQUIRE(addIdentity(rsaIdentity, RsaKeyParams()));
201 Name rsaCertName = m_keyChain.getDefaultCertificateNameForIdentity(rsaIdentity);
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700202 shared_ptr<v1::IdentityCertificate> rsaCert = m_keyChain.getCertificate(rsaCertName);
Yingdi Yu5ec0ee32014-06-24 16:26:09 -0700203
204 Name packetName("/Test/Packet/Name");
205
206 shared_ptr<Data> testDataRsa = make_shared<Data>(packetName);
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700207 m_keyChain.sign(*testDataRsa,
208 security::SigningInfo(security::SigningInfo::SIGNER_TYPE_ID,
209 rsaIdentity));
Yingdi Yu5ec0ee32014-06-24 16:26:09 -0700210 shared_ptr<Data> testDataEcdsa = make_shared<Data>(packetName);
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700211 m_keyChain.sign(*testDataEcdsa,
212 security::SigningInfo(security::SigningInfo::SIGNER_TYPE_ID,
213 ecdsaIdentity));
Yingdi Yu5ec0ee32014-06-24 16:26:09 -0700214 shared_ptr<Interest> testInterestRsa = make_shared<Interest>(packetName);
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700215 m_keyChain.sign(*testInterestRsa,
216 security::SigningInfo(security::SigningInfo::SIGNER_TYPE_ID,
217 rsaIdentity));
Yingdi Yu5ec0ee32014-06-24 16:26:09 -0700218 shared_ptr<Interest> testInterestEcdsa = make_shared<Interest>(packetName);
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700219 m_keyChain.sign(*testInterestEcdsa,
220 security::SigningInfo(security::SigningInfo::SIGNER_TYPE_ID,
221 ecdsaIdentity));
Yingdi Yu5ec0ee32014-06-24 16:26:09 -0700222
223 BOOST_CHECK(Validator::verifySignature(*ecdsaCert, ecdsaCert->getPublicKeyInfo()));
224 BOOST_CHECK_EQUAL(Validator::verifySignature(*ecdsaCert, rsaCert->getPublicKeyInfo()), false);
225 BOOST_CHECK_EQUAL(Validator::verifySignature(*rsaCert, ecdsaCert->getPublicKeyInfo()), false);
226 BOOST_CHECK(Validator::verifySignature(*rsaCert, rsaCert->getPublicKeyInfo()));
227
228 BOOST_CHECK(Validator::verifySignature(*testDataEcdsa, ecdsaCert->getPublicKeyInfo()));
229 BOOST_CHECK_EQUAL(Validator::verifySignature(*testDataEcdsa, rsaCert->getPublicKeyInfo()), false);
230 BOOST_CHECK_EQUAL(Validator::verifySignature(*testDataRsa, ecdsaCert->getPublicKeyInfo()), false);
231 BOOST_CHECK(Validator::verifySignature(*testDataRsa, rsaCert->getPublicKeyInfo()));
232
233 BOOST_CHECK(Validator::verifySignature(*testInterestEcdsa, ecdsaCert->getPublicKeyInfo()));
234 BOOST_CHECK_EQUAL(Validator::verifySignature(*testInterestEcdsa, rsaCert->getPublicKeyInfo()),
235 false);
236 BOOST_CHECK_EQUAL(Validator::verifySignature(*testInterestRsa, ecdsaCert->getPublicKeyInfo()),
237 false);
238 BOOST_CHECK(Validator::verifySignature(*testInterestRsa, rsaCert->getPublicKeyInfo()));
Yingdi Yu5ec0ee32014-06-24 16:26:09 -0700239}
240
Junxiao Shi198c3812016-08-12 19:24:18 +0000241BOOST_AUTO_TEST_CASE(MalformedInterestSigInfo)
242{
243 auto interest = make_shared<Interest>("/prefix");
244 m_keyChain.sign(*interest);
245
246 setNameComponent(*interest, signed_interest::POS_SIG_INFO, "not-SignatureInfo");
247
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700248 v1::PublicKey pubkey = m_keyChain.getDefaultCertificate()->getPublicKeyInfo();
Junxiao Shi198c3812016-08-12 19:24:18 +0000249 BOOST_CHECK_EQUAL(Validator::verifySignature(*interest, pubkey), false);
250}
251
252BOOST_AUTO_TEST_CASE(MalformedInterestSigValue)
253{
254 auto interest = make_shared<Interest>("/prefix");
255 m_keyChain.sign(*interest);
256
257 setNameComponent(*interest, signed_interest::POS_SIG_VALUE, "bad-signature-bits");
258
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700259 v1::PublicKey pubkey = m_keyChain.getDefaultCertificate()->getPublicKeyInfo();
Junxiao Shi198c3812016-08-12 19:24:18 +0000260 BOOST_CHECK_EQUAL(Validator::verifySignature(*interest, pubkey), false);
261}
262
263BOOST_AUTO_TEST_SUITE_END() // TestValidator
264BOOST_AUTO_TEST_SUITE_END() // Security
Yingdi Yu9a335352014-01-31 11:57:46 -0800265
Spyridon Mastorakis429634f2015-02-19 17:35:33 -0800266} // namespace tests
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700267} // namespace security
Yingdi Yu9a335352014-01-31 11:57:46 -0800268} // namespace ndn