blob: 494ea71e6f2d769efb8b8657fd39b2577c2e93fd [file] [log] [blame]
Yingdi Yuebfa4cb2014-06-17 15:28:53 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -08003 * Copyright (c) 2013-2017 Regents of the University of California.
Yingdi Yuebfa4cb2014-06-17 15:28:53 -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
22#include "security/signature-sha256-with-rsa.hpp"
Yingdi Yu6ab67812014-11-27 15:00:34 -080023#include "security/validator.hpp"
24#include "util/scheduler.hpp"
Davide Pesaventoeee3e822016-11-26 19:19:34 +010025
Yingdi Yuebfa4cb2014-06-17 15:28:53 -070026#include "boost-test.hpp"
Davide Pesaventoeee3e822016-11-26 19:19:34 +010027#include "../identity-management-time-fixture.hpp"
Yingdi Yuebfa4cb2014-06-17 15:28:53 -070028
29namespace ndn {
Yingdi Yu6ab67812014-11-27 15:00:34 -080030namespace tests {
Yingdi Yuebfa4cb2014-06-17 15:28:53 -070031
Davide Pesaventoeee3e822016-11-26 19:19:34 +010032class SignatureSha256RsaTimeFixture : public IdentityManagementTimeFixture
Yingdi Yu6ab67812014-11-27 15:00:34 -080033{
34public:
35 SignatureSha256RsaTimeFixture()
36 : scheduler(io)
37 {
38 }
39
40public:
41 Scheduler scheduler;
42};
43
Davide Pesaventoeee3e822016-11-26 19:19:34 +010044BOOST_AUTO_TEST_SUITE(Security)
45BOOST_FIXTURE_TEST_SUITE(TestSignatureSha256WithRsa, SignatureSha256RsaTimeFixture)
Yingdi Yuebfa4cb2014-06-17 15:28:53 -070046
47const uint8_t sigInfo[] = {
480x16, 0x1b, // SignatureInfo
49 0x1b, 0x01, // SignatureType
50 0x01,
51 0x1c, 0x16, // KeyLocator
52 0x07, 0x14, // Name
53 0x08, 0x04,
54 0x74, 0x65, 0x73, 0x74,
55 0x08, 0x03,
56 0x6b, 0x65, 0x79,
57 0x08, 0x07,
58 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72
59};
60
61const uint8_t sigValue[] = {
620x17, 0x80, // SignatureValue
63 0x2f, 0xd6, 0xf1, 0x6e, 0x80, 0x6f, 0x10, 0xbe, 0xb1, 0x6f, 0x3e, 0x31, 0xec,
64 0xe3, 0xb9, 0xea, 0x83, 0x30, 0x40, 0x03, 0xfc, 0xa0, 0x13, 0xd9, 0xb3, 0xc6,
65 0x25, 0x16, 0x2d, 0xa6, 0x58, 0x41, 0x69, 0x62, 0x56, 0xd8, 0xb3, 0x6a, 0x38,
66 0x76, 0x56, 0xea, 0x61, 0xb2, 0x32, 0x70, 0x1c, 0xb6, 0x4d, 0x10, 0x1d, 0xdc,
67 0x92, 0x8e, 0x52, 0xa5, 0x8a, 0x1d, 0xd9, 0x96, 0x5e, 0xc0, 0x62, 0x0b, 0xcf,
68 0x3a, 0x9d, 0x7f, 0xca, 0xbe, 0xa1, 0x41, 0x71, 0x85, 0x7a, 0x8b, 0x5d, 0xa9,
69 0x64, 0xd6, 0x66, 0xb4, 0xe9, 0x8d, 0x0c, 0x28, 0x43, 0xee, 0xa6, 0x64, 0xe8,
70 0x55, 0xf6, 0x1c, 0x19, 0x0b, 0xef, 0x99, 0x25, 0x1e, 0xdc, 0x78, 0xb3, 0xa7,
71 0xaa, 0x0d, 0x14, 0x58, 0x30, 0xe5, 0x37, 0x6a, 0x6d, 0xdb, 0x56, 0xac, 0xa3,
72 0xfc, 0x90, 0x7a, 0xb8, 0x66, 0x9c, 0x0e, 0xf6, 0xb7, 0x64, 0xd1
73};
74
75
76BOOST_AUTO_TEST_CASE(Decoding)
77{
78 Block sigInfoBlock(sigInfo, sizeof(sigInfo));
79 Block sigValueBlock(sigValue, sizeof(sigValue));
80
81 Signature sig(sigInfoBlock, sigValueBlock);
Yingdi Yu4a557052014-07-09 16:40:37 -070082 BOOST_CHECK_NO_THROW(SignatureSha256WithRsa(sig));
83 BOOST_CHECK_NO_THROW(sig.getKeyLocator());
Yingdi Yuebfa4cb2014-06-17 15:28:53 -070084}
85
86BOOST_AUTO_TEST_CASE(Encoding)
87{
Yingdi Yuebfa4cb2014-06-17 15:28:53 -070088 Name name("/test/key/locator");
89 KeyLocator keyLocator(name);
90
Yingdi Yu4a557052014-07-09 16:40:37 -070091 SignatureSha256WithRsa sig(keyLocator);
92
93 BOOST_CHECK_NO_THROW(sig.getKeyLocator());
Yingdi Yuebfa4cb2014-06-17 15:28:53 -070094
95 const Block& encodeSigInfoBlock = sig.getInfo();
96
97 Block sigInfoBlock(sigInfo, sizeof(sigInfo));
98
99 BOOST_CHECK_EQUAL_COLLECTIONS(sigInfoBlock.wire(),
100 sigInfoBlock.wire() + sigInfoBlock.size(),
101 encodeSigInfoBlock.wire(),
102 encodeSigInfoBlock.wire() + encodeSigInfoBlock.size());
Alexander Afanasyev1c6976d2014-07-13 11:40:50 -0700103
104 sig.setKeyLocator(Name("/test/another/key/locator"));
105
106 const Block& encodeSigInfoBlock2 = sig.getInfo();
107 BOOST_CHECK(sigInfoBlock != encodeSigInfoBlock2);
Yingdi Yuebfa4cb2014-06-17 15:28:53 -0700108}
109
Yingdi Yu6ab67812014-11-27 15:00:34 -0800110BOOST_AUTO_TEST_CASE(DataSignature)
111{
112 Name identityName("/SecurityTestSignatureSha256WithRsa/DataSignature");
113 BOOST_REQUIRE(addIdentity(identityName, RsaKeyParams()));
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700114 shared_ptr<security::v1::PublicKey> publicKey;
Yingdi Yu6ab67812014-11-27 15:00:34 -0800115 BOOST_REQUIRE_NO_THROW(publicKey = m_keyChain.getPublicKeyFromTpm(
116 m_keyChain.getDefaultKeyNameForIdentity(identityName)));
117
118 Data testData("/SecurityTestSignatureSha256WithRsa/DataSignature/Data1");
119 char content[5] = "1234";
120 testData.setContent(reinterpret_cast<uint8_t*>(content), 5);
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700121 BOOST_CHECK_NO_THROW(m_keyChain.sign(testData,
122 security::SigningInfo(security::SigningInfo::SIGNER_TYPE_ID,
123 identityName)));
Yingdi Yu6ab67812014-11-27 15:00:34 -0800124 Block dataBlock(testData.wireEncode().wire(), testData.wireEncode().size());
125
126 Data testData2;
127 testData2.wireDecode(dataBlock);
128 BOOST_CHECK(Validator::verifySignature(testData2, *publicKey));
129}
130
131BOOST_AUTO_TEST_CASE(InterestSignature)
132{
133 Name identityName("/SecurityTestSignatureSha256WithRsa/InterestSignature");
134 BOOST_REQUIRE(addIdentity(identityName, RsaKeyParams()));
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700135 shared_ptr<security::v1::PublicKey> publicKey;
Yingdi Yu6ab67812014-11-27 15:00:34 -0800136 BOOST_REQUIRE_NO_THROW(publicKey = m_keyChain.getPublicKeyFromTpm(
137 m_keyChain.getDefaultKeyNameForIdentity(identityName)));
138
139 Interest interest("/SecurityTestSignatureSha256WithRsa/InterestSignature/Interest1");
140 Interest interest11("/SecurityTestSignatureSha256WithRsa/InterestSignature/Interest1");
141
142 scheduler.scheduleEvent(time::milliseconds(100), [&] {
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700143 BOOST_CHECK_NO_THROW(m_keyChain.sign(interest,
144 security::SigningInfo(security::SigningInfo::SIGNER_TYPE_ID,
145 identityName)));
Yingdi Yu6ab67812014-11-27 15:00:34 -0800146 });
147
148 advanceClocks(time::milliseconds(100));
149 scheduler.scheduleEvent(time::milliseconds(100), [&] {
Yingdi Yu1b0311c2015-06-10 14:58:47 -0700150 BOOST_CHECK_NO_THROW(m_keyChain.sign(interest11,
151 security::SigningInfo(security::SigningInfo::SIGNER_TYPE_ID,
152 identityName)));
Yingdi Yu6ab67812014-11-27 15:00:34 -0800153 });
154
155 advanceClocks(time::milliseconds(100));
156
157 time::system_clock::TimePoint timestamp1 =
158 time::fromUnixTimestamp(
159 time::milliseconds(interest.getName().get(signed_interest::POS_TIMESTAMP).toNumber()));
160
161 time::system_clock::TimePoint timestamp2 =
162 time::fromUnixTimestamp(
163 time::milliseconds(interest11.getName().get(signed_interest::POS_TIMESTAMP).toNumber()));
164
165 BOOST_CHECK_EQUAL(time::milliseconds(100), (timestamp2 - timestamp1));
166
167 uint64_t nonce1 = interest.getName().get(signed_interest::POS_RANDOM_VAL).toNumber();
168 uint64_t nonce2 = interest11.getName().get(signed_interest::POS_RANDOM_VAL).toNumber();
169 BOOST_WARN_NE(nonce1, nonce2);
170
171 Block interestBlock(interest.wireEncode().wire(), interest.wireEncode().size());
172
173 Interest interest2;
174 interest2.wireDecode(interestBlock);
175 BOOST_CHECK(Validator::verifySignature(interest2, *publicKey));
176}
177
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100178BOOST_AUTO_TEST_SUITE_END() // TestSignatureSha256WithRsa
179BOOST_AUTO_TEST_SUITE_END() // Security
Yingdi Yuebfa4cb2014-06-17 15:28:53 -0700180
Yingdi Yu6ab67812014-11-27 15:00:34 -0800181} // namespace tests
Yingdi Yuebfa4cb2014-06-17 15:28:53 -0700182} // namespace ndn