blob: 7de3774116588c832fd43ce3aeba8436ce7e7a77 [file] [log] [blame]
Yingdi Yuebfa4cb2014-06-17 15:28:53 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento0f830802018-01-16 23:58:58 -05002/*
3 * Copyright (c) 2013-2018 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"
Alexander Afanasyev574aa862017-01-10 19:53:28 -080023#include "security/verification-helpers.hpp"
Yingdi Yu6ab67812014-11-27 15:00:34 -080024#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"
Junxiao Shib55e5d32018-07-18 13:32:00 -060028#include "make-interest-data.hpp"
Yingdi Yuebfa4cb2014-06-17 15:28:53 -070029
30namespace ndn {
Alexander Afanasyev70244f42017-01-04 12:47:12 -080031namespace security {
Yingdi Yu6ab67812014-11-27 15:00:34 -080032namespace tests {
Yingdi Yuebfa4cb2014-06-17 15:28:53 -070033
Alexander Afanasyev70244f42017-01-04 12:47:12 -080034using namespace ndn::tests;
35
Davide Pesaventoeee3e822016-11-26 19:19:34 +010036class SignatureSha256RsaTimeFixture : public IdentityManagementTimeFixture
Yingdi Yu6ab67812014-11-27 15:00:34 -080037{
38public:
39 SignatureSha256RsaTimeFixture()
40 : scheduler(io)
41 {
42 }
43
44public:
45 Scheduler scheduler;
46};
47
Davide Pesaventoeee3e822016-11-26 19:19:34 +010048BOOST_AUTO_TEST_SUITE(Security)
49BOOST_FIXTURE_TEST_SUITE(TestSignatureSha256WithRsa, SignatureSha256RsaTimeFixture)
Yingdi Yuebfa4cb2014-06-17 15:28:53 -070050
51const uint8_t sigInfo[] = {
Alexander Afanasyev70244f42017-01-04 12:47:12 -080052 0x16, 0x1b, // SignatureInfo
53 0x1b, 0x01, // SignatureType
54 0x01,
55 0x1c, 0x16, // KeyLocator
56 0x07, 0x14, // Name
57 0x08, 0x04,
58 0x74, 0x65, 0x73, 0x74,
59 0x08, 0x03,
60 0x6b, 0x65, 0x79,
61 0x08, 0x07,
62 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72
Yingdi Yuebfa4cb2014-06-17 15:28:53 -070063};
64
65const uint8_t sigValue[] = {
660x17, 0x80, // SignatureValue
67 0x2f, 0xd6, 0xf1, 0x6e, 0x80, 0x6f, 0x10, 0xbe, 0xb1, 0x6f, 0x3e, 0x31, 0xec,
68 0xe3, 0xb9, 0xea, 0x83, 0x30, 0x40, 0x03, 0xfc, 0xa0, 0x13, 0xd9, 0xb3, 0xc6,
69 0x25, 0x16, 0x2d, 0xa6, 0x58, 0x41, 0x69, 0x62, 0x56, 0xd8, 0xb3, 0x6a, 0x38,
70 0x76, 0x56, 0xea, 0x61, 0xb2, 0x32, 0x70, 0x1c, 0xb6, 0x4d, 0x10, 0x1d, 0xdc,
71 0x92, 0x8e, 0x52, 0xa5, 0x8a, 0x1d, 0xd9, 0x96, 0x5e, 0xc0, 0x62, 0x0b, 0xcf,
72 0x3a, 0x9d, 0x7f, 0xca, 0xbe, 0xa1, 0x41, 0x71, 0x85, 0x7a, 0x8b, 0x5d, 0xa9,
73 0x64, 0xd6, 0x66, 0xb4, 0xe9, 0x8d, 0x0c, 0x28, 0x43, 0xee, 0xa6, 0x64, 0xe8,
74 0x55, 0xf6, 0x1c, 0x19, 0x0b, 0xef, 0x99, 0x25, 0x1e, 0xdc, 0x78, 0xb3, 0xa7,
75 0xaa, 0x0d, 0x14, 0x58, 0x30, 0xe5, 0x37, 0x6a, 0x6d, 0xdb, 0x56, 0xac, 0xa3,
76 0xfc, 0x90, 0x7a, 0xb8, 0x66, 0x9c, 0x0e, 0xf6, 0xb7, 0x64, 0xd1
77};
78
79
80BOOST_AUTO_TEST_CASE(Decoding)
81{
82 Block sigInfoBlock(sigInfo, sizeof(sigInfo));
83 Block sigValueBlock(sigValue, sizeof(sigValue));
84
85 Signature sig(sigInfoBlock, sigValueBlock);
Davide Pesavento8a8c01b2018-03-11 00:07:52 -050086 BOOST_CHECK_NO_THROW(SignatureSha256WithRsa{sig});
Yingdi Yu4a557052014-07-09 16:40:37 -070087 BOOST_CHECK_NO_THROW(sig.getKeyLocator());
Yingdi Yuebfa4cb2014-06-17 15:28:53 -070088}
89
90BOOST_AUTO_TEST_CASE(Encoding)
91{
Yingdi Yuebfa4cb2014-06-17 15:28:53 -070092 Name name("/test/key/locator");
93 KeyLocator keyLocator(name);
94
Yingdi Yu4a557052014-07-09 16:40:37 -070095 SignatureSha256WithRsa sig(keyLocator);
96
97 BOOST_CHECK_NO_THROW(sig.getKeyLocator());
Yingdi Yuebfa4cb2014-06-17 15:28:53 -070098
99 const Block& encodeSigInfoBlock = sig.getInfo();
100
101 Block sigInfoBlock(sigInfo, sizeof(sigInfo));
102
103 BOOST_CHECK_EQUAL_COLLECTIONS(sigInfoBlock.wire(),
104 sigInfoBlock.wire() + sigInfoBlock.size(),
105 encodeSigInfoBlock.wire(),
106 encodeSigInfoBlock.wire() + encodeSigInfoBlock.size());
Alexander Afanasyev1c6976d2014-07-13 11:40:50 -0700107
108 sig.setKeyLocator(Name("/test/another/key/locator"));
109
110 const Block& encodeSigInfoBlock2 = sig.getInfo();
Junxiao Shi72c0c642018-04-20 15:41:09 +0000111 BOOST_CHECK_NE(sigInfoBlock, encodeSigInfoBlock2);
Yingdi Yuebfa4cb2014-06-17 15:28:53 -0700112}
113
Yingdi Yu6ab67812014-11-27 15:00:34 -0800114BOOST_AUTO_TEST_CASE(DataSignature)
115{
Alexander Afanasyev70244f42017-01-04 12:47:12 -0800116 Identity identity = addIdentity("/SecurityTestSignatureSha256WithRsa/DataSignature", RsaKeyParams());
Yingdi Yu6ab67812014-11-27 15:00:34 -0800117
118 Data testData("/SecurityTestSignatureSha256WithRsa/DataSignature/Data1");
119 char content[5] = "1234";
120 testData.setContent(reinterpret_cast<uint8_t*>(content), 5);
Alexander Afanasyev70244f42017-01-04 12:47:12 -0800121 BOOST_CHECK_NO_THROW(m_keyChain.sign(testData, security::SigningInfo(identity)));
Yingdi Yu6ab67812014-11-27 15:00:34 -0800122 Block dataBlock(testData.wireEncode().wire(), testData.wireEncode().size());
123
124 Data testData2;
125 testData2.wireDecode(dataBlock);
Alexander Afanasyev574aa862017-01-10 19:53:28 -0800126 BOOST_CHECK(verifySignature(testData2, identity.getDefaultKey()));
Yingdi Yu6ab67812014-11-27 15:00:34 -0800127}
128
129BOOST_AUTO_TEST_CASE(InterestSignature)
130{
Alexander Afanasyev70244f42017-01-04 12:47:12 -0800131 Identity identity = addIdentity("/SecurityTestSignatureSha256WithRsa/InterestSignature", RsaKeyParams());
Yingdi Yu6ab67812014-11-27 15:00:34 -0800132
Junxiao Shib55e5d32018-07-18 13:32:00 -0600133 auto interest = makeInterest("/SecurityTestSignatureSha256WithRsa/InterestSignature/Interest1");
134 auto interest11 = makeInterest("/SecurityTestSignatureSha256WithRsa/InterestSignature/Interest1");
Yingdi Yu6ab67812014-11-27 15:00:34 -0800135
Davide Pesavento0f830802018-01-16 23:58:58 -0500136 scheduler.scheduleEvent(100_ms, [&] {
Junxiao Shib55e5d32018-07-18 13:32:00 -0600137 m_keyChain.sign(*interest, security::SigningInfo(identity));
Yingdi Yu6ab67812014-11-27 15:00:34 -0800138 });
139
Davide Pesavento0f830802018-01-16 23:58:58 -0500140 advanceClocks(100_ms);
141 scheduler.scheduleEvent(100_ms, [&] {
Junxiao Shib55e5d32018-07-18 13:32:00 -0600142 m_keyChain.sign(*interest11, security::SigningInfo(identity));
Yingdi Yu6ab67812014-11-27 15:00:34 -0800143 });
144
Davide Pesavento0f830802018-01-16 23:58:58 -0500145 advanceClocks(100_ms);
Yingdi Yu6ab67812014-11-27 15:00:34 -0800146
Junxiao Shib55e5d32018-07-18 13:32:00 -0600147 Block interestBlock(interest->wireEncode().wire(), interest->wireEncode().size());
Yingdi Yu6ab67812014-11-27 15:00:34 -0800148
149 Interest interest2;
150 interest2.wireDecode(interestBlock);
Alexander Afanasyev574aa862017-01-10 19:53:28 -0800151 BOOST_CHECK(verifySignature(interest2, identity.getDefaultKey()));
Yingdi Yu6ab67812014-11-27 15:00:34 -0800152}
153
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100154BOOST_AUTO_TEST_SUITE_END() // TestSignatureSha256WithRsa
155BOOST_AUTO_TEST_SUITE_END() // Security
Yingdi Yuebfa4cb2014-06-17 15:28:53 -0700156
Yingdi Yu6ab67812014-11-27 15:00:34 -0800157} // namespace tests
Alexander Afanasyev70244f42017-01-04 12:47:12 -0800158} // namespace security
Yingdi Yuebfa4cb2014-06-17 15:28:53 -0700159} // namespace ndn