blob: cf6b172992a60e550e632a461267e2b3714e4e1f [file] [log] [blame]
Yingdi Yuebfa4cb2014-06-17 15:28:53 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2014 Regents of the University of California.
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/signature-sha256-with-rsa.hpp"
Yingdi Yu6ab67812014-11-27 15:00:34 -080023#include "security/key-chain.hpp"
24#include "security/validator.hpp"
25#include "util/scheduler.hpp"
26#include "identity-management-fixture.hpp"
27#include "../unit-test-time-fixture.hpp"
Yingdi Yuebfa4cb2014-06-17 15:28:53 -070028#include "boost-test.hpp"
29
30namespace ndn {
Yingdi Yu6ab67812014-11-27 15:00:34 -080031namespace tests {
Yingdi Yuebfa4cb2014-06-17 15:28:53 -070032
Yingdi Yu6ab67812014-11-27 15:00:34 -080033class SignatureSha256RsaTimeFixture : public UnitTestTimeFixture
34 , public security::IdentityManagementFixture
35{
36public:
37 SignatureSha256RsaTimeFixture()
38 : scheduler(io)
39 {
40 }
41
42public:
43 Scheduler scheduler;
44};
45
46BOOST_FIXTURE_TEST_SUITE(SecurityTestSignatureSha256WithRsa, SignatureSha256RsaTimeFixture)
Yingdi Yuebfa4cb2014-06-17 15:28:53 -070047
48const uint8_t sigInfo[] = {
490x16, 0x1b, // SignatureInfo
50 0x1b, 0x01, // SignatureType
51 0x01,
52 0x1c, 0x16, // KeyLocator
53 0x07, 0x14, // Name
54 0x08, 0x04,
55 0x74, 0x65, 0x73, 0x74,
56 0x08, 0x03,
57 0x6b, 0x65, 0x79,
58 0x08, 0x07,
59 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72
60};
61
62const uint8_t sigValue[] = {
630x17, 0x80, // SignatureValue
64 0x2f, 0xd6, 0xf1, 0x6e, 0x80, 0x6f, 0x10, 0xbe, 0xb1, 0x6f, 0x3e, 0x31, 0xec,
65 0xe3, 0xb9, 0xea, 0x83, 0x30, 0x40, 0x03, 0xfc, 0xa0, 0x13, 0xd9, 0xb3, 0xc6,
66 0x25, 0x16, 0x2d, 0xa6, 0x58, 0x41, 0x69, 0x62, 0x56, 0xd8, 0xb3, 0x6a, 0x38,
67 0x76, 0x56, 0xea, 0x61, 0xb2, 0x32, 0x70, 0x1c, 0xb6, 0x4d, 0x10, 0x1d, 0xdc,
68 0x92, 0x8e, 0x52, 0xa5, 0x8a, 0x1d, 0xd9, 0x96, 0x5e, 0xc0, 0x62, 0x0b, 0xcf,
69 0x3a, 0x9d, 0x7f, 0xca, 0xbe, 0xa1, 0x41, 0x71, 0x85, 0x7a, 0x8b, 0x5d, 0xa9,
70 0x64, 0xd6, 0x66, 0xb4, 0xe9, 0x8d, 0x0c, 0x28, 0x43, 0xee, 0xa6, 0x64, 0xe8,
71 0x55, 0xf6, 0x1c, 0x19, 0x0b, 0xef, 0x99, 0x25, 0x1e, 0xdc, 0x78, 0xb3, 0xa7,
72 0xaa, 0x0d, 0x14, 0x58, 0x30, 0xe5, 0x37, 0x6a, 0x6d, 0xdb, 0x56, 0xac, 0xa3,
73 0xfc, 0x90, 0x7a, 0xb8, 0x66, 0x9c, 0x0e, 0xf6, 0xb7, 0x64, 0xd1
74};
75
76
77BOOST_AUTO_TEST_CASE(Decoding)
78{
79 Block sigInfoBlock(sigInfo, sizeof(sigInfo));
80 Block sigValueBlock(sigValue, sizeof(sigValue));
81
82 Signature sig(sigInfoBlock, sigValueBlock);
Yingdi Yu4a557052014-07-09 16:40:37 -070083 BOOST_CHECK_NO_THROW(SignatureSha256WithRsa(sig));
84 BOOST_CHECK_NO_THROW(sig.getKeyLocator());
Yingdi Yuebfa4cb2014-06-17 15:28:53 -070085}
86
87BOOST_AUTO_TEST_CASE(Encoding)
88{
Yingdi Yuebfa4cb2014-06-17 15:28:53 -070089 Name name("/test/key/locator");
90 KeyLocator keyLocator(name);
91
Yingdi Yu4a557052014-07-09 16:40:37 -070092 SignatureSha256WithRsa sig(keyLocator);
93
94 BOOST_CHECK_NO_THROW(sig.getKeyLocator());
Yingdi Yuebfa4cb2014-06-17 15:28:53 -070095
96 const Block& encodeSigInfoBlock = sig.getInfo();
97
98 Block sigInfoBlock(sigInfo, sizeof(sigInfo));
99
100 BOOST_CHECK_EQUAL_COLLECTIONS(sigInfoBlock.wire(),
101 sigInfoBlock.wire() + sigInfoBlock.size(),
102 encodeSigInfoBlock.wire(),
103 encodeSigInfoBlock.wire() + encodeSigInfoBlock.size());
Alexander Afanasyev1c6976d2014-07-13 11:40:50 -0700104
105 sig.setKeyLocator(Name("/test/another/key/locator"));
106
107 const Block& encodeSigInfoBlock2 = sig.getInfo();
108 BOOST_CHECK(sigInfoBlock != encodeSigInfoBlock2);
Yingdi Yuebfa4cb2014-06-17 15:28:53 -0700109}
110
Yingdi Yu6ab67812014-11-27 15:00:34 -0800111BOOST_AUTO_TEST_CASE(DataSignature)
112{
113 Name identityName("/SecurityTestSignatureSha256WithRsa/DataSignature");
114 BOOST_REQUIRE(addIdentity(identityName, RsaKeyParams()));
115 shared_ptr<PublicKey> publicKey;
116 BOOST_REQUIRE_NO_THROW(publicKey = m_keyChain.getPublicKeyFromTpm(
117 m_keyChain.getDefaultKeyNameForIdentity(identityName)));
118
119 Data testData("/SecurityTestSignatureSha256WithRsa/DataSignature/Data1");
120 char content[5] = "1234";
121 testData.setContent(reinterpret_cast<uint8_t*>(content), 5);
122 BOOST_CHECK_NO_THROW(m_keyChain.signByIdentity(testData, identityName));
123 Block dataBlock(testData.wireEncode().wire(), testData.wireEncode().size());
124
125 Data testData2;
126 testData2.wireDecode(dataBlock);
127 BOOST_CHECK(Validator::verifySignature(testData2, *publicKey));
128}
129
130BOOST_AUTO_TEST_CASE(InterestSignature)
131{
132 Name identityName("/SecurityTestSignatureSha256WithRsa/InterestSignature");
133 BOOST_REQUIRE(addIdentity(identityName, RsaKeyParams()));
134 shared_ptr<PublicKey> publicKey;
135 BOOST_REQUIRE_NO_THROW(publicKey = m_keyChain.getPublicKeyFromTpm(
136 m_keyChain.getDefaultKeyNameForIdentity(identityName)));
137
138 Interest interest("/SecurityTestSignatureSha256WithRsa/InterestSignature/Interest1");
139 Interest interest11("/SecurityTestSignatureSha256WithRsa/InterestSignature/Interest1");
140
141 scheduler.scheduleEvent(time::milliseconds(100), [&] {
142 BOOST_CHECK_NO_THROW(m_keyChain.signByIdentity(interest, identityName));
143 });
144
145 advanceClocks(time::milliseconds(100));
146 scheduler.scheduleEvent(time::milliseconds(100), [&] {
147 BOOST_CHECK_NO_THROW(m_keyChain.signByIdentity(interest11, identityName));
148 });
149
150 advanceClocks(time::milliseconds(100));
151
152 time::system_clock::TimePoint timestamp1 =
153 time::fromUnixTimestamp(
154 time::milliseconds(interest.getName().get(signed_interest::POS_TIMESTAMP).toNumber()));
155
156 time::system_clock::TimePoint timestamp2 =
157 time::fromUnixTimestamp(
158 time::milliseconds(interest11.getName().get(signed_interest::POS_TIMESTAMP).toNumber()));
159
160 BOOST_CHECK_EQUAL(time::milliseconds(100), (timestamp2 - timestamp1));
161
162 uint64_t nonce1 = interest.getName().get(signed_interest::POS_RANDOM_VAL).toNumber();
163 uint64_t nonce2 = interest11.getName().get(signed_interest::POS_RANDOM_VAL).toNumber();
164 BOOST_WARN_NE(nonce1, nonce2);
165
166 Block interestBlock(interest.wireEncode().wire(), interest.wireEncode().size());
167
168 Interest interest2;
169 interest2.wireDecode(interestBlock);
170 BOOST_CHECK(Validator::verifySignature(interest2, *publicKey));
171}
172
Yingdi Yuebfa4cb2014-06-17 15:28:53 -0700173BOOST_AUTO_TEST_SUITE_END()
174
Yingdi Yu6ab67812014-11-27 15:00:34 -0800175} // namespace tests
Yingdi Yuebfa4cb2014-06-17 15:28:53 -0700176} // namespace ndn