Yingdi Yu | ebfa4cb | 2014-06-17 15:28:53 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | 4c9a3d5 | 2017-01-03 17:45:19 -0800 | [diff] [blame^] | 3 | * Copyright (c) 2013-2017 Regents of the University of California. |
Yingdi Yu | ebfa4cb | 2014-06-17 15:28:53 -0700 | [diff] [blame] | 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-ecdsa.hpp" |
Yingdi Yu | 6ab6781 | 2014-11-27 15:00:34 -0800 | [diff] [blame] | 23 | #include "security/validator.hpp" |
| 24 | #include "util/scheduler.hpp" |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 25 | |
Yingdi Yu | ebfa4cb | 2014-06-17 15:28:53 -0700 | [diff] [blame] | 26 | #include "boost-test.hpp" |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 27 | #include "../identity-management-time-fixture.hpp" |
Yingdi Yu | ebfa4cb | 2014-06-17 15:28:53 -0700 | [diff] [blame] | 28 | |
| 29 | namespace ndn { |
Yingdi Yu | 6ab6781 | 2014-11-27 15:00:34 -0800 | [diff] [blame] | 30 | namespace tests { |
Yingdi Yu | ebfa4cb | 2014-06-17 15:28:53 -0700 | [diff] [blame] | 31 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 32 | class SignatureSha256EcdsaTimeFixture : public IdentityManagementTimeFixture |
Yingdi Yu | 6ab6781 | 2014-11-27 15:00:34 -0800 | [diff] [blame] | 33 | { |
| 34 | public: |
| 35 | SignatureSha256EcdsaTimeFixture() |
| 36 | : scheduler(io) |
| 37 | { |
| 38 | } |
| 39 | |
| 40 | public: |
| 41 | Scheduler scheduler; |
| 42 | }; |
| 43 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 44 | BOOST_AUTO_TEST_SUITE(Security) |
| 45 | BOOST_FIXTURE_TEST_SUITE(TestSignatureSha256WithEcdsa, SignatureSha256EcdsaTimeFixture) |
Yingdi Yu | ebfa4cb | 2014-06-17 15:28:53 -0700 | [diff] [blame] | 46 | |
| 47 | const uint8_t sigInfo[] = { |
| 48 | 0x16, 0x1b, // SignatureInfo |
| 49 | 0x1b, 0x01, // SignatureType |
| 50 | 0x03, |
| 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 | |
| 61 | const uint8_t sigValue[] = { |
| 62 | 0x17, 0x40, // 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 |
| 68 | }; |
| 69 | |
| 70 | |
| 71 | BOOST_AUTO_TEST_CASE(Decoding) |
| 72 | { |
| 73 | Block sigInfoBlock(sigInfo, sizeof(sigInfo)); |
| 74 | Block sigValueBlock(sigValue, sizeof(sigValue)); |
| 75 | |
| 76 | Signature sig(sigInfoBlock, sigValueBlock); |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 77 | BOOST_CHECK_NO_THROW(SignatureSha256WithEcdsa(sig)); |
| 78 | BOOST_CHECK_NO_THROW(sig.getKeyLocator()); |
Yingdi Yu | ebfa4cb | 2014-06-17 15:28:53 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | BOOST_AUTO_TEST_CASE(Encoding) |
| 82 | { |
Yingdi Yu | ebfa4cb | 2014-06-17 15:28:53 -0700 | [diff] [blame] | 83 | Name name("/test/key/locator"); |
| 84 | KeyLocator keyLocator(name); |
| 85 | |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 86 | SignatureSha256WithEcdsa sig(keyLocator); |
| 87 | |
| 88 | BOOST_CHECK_NO_THROW(sig.getKeyLocator()); |
Yingdi Yu | ebfa4cb | 2014-06-17 15:28:53 -0700 | [diff] [blame] | 89 | |
| 90 | const Block& encodeSigInfoBlock = sig.getInfo(); |
| 91 | |
| 92 | Block sigInfoBlock(sigInfo, sizeof(sigInfo)); |
| 93 | |
| 94 | BOOST_CHECK_EQUAL_COLLECTIONS(sigInfoBlock.wire(), |
| 95 | sigInfoBlock.wire() + sigInfoBlock.size(), |
| 96 | encodeSigInfoBlock.wire(), |
| 97 | encodeSigInfoBlock.wire() + encodeSigInfoBlock.size()); |
Alexander Afanasyev | 1c6976d | 2014-07-13 11:40:50 -0700 | [diff] [blame] | 98 | |
| 99 | sig.setKeyLocator(Name("/test/another/key/locator")); |
| 100 | |
| 101 | const Block& encodeSigInfoBlock2 = sig.getInfo(); |
| 102 | BOOST_CHECK(sigInfoBlock != encodeSigInfoBlock2); |
Yingdi Yu | ebfa4cb | 2014-06-17 15:28:53 -0700 | [diff] [blame] | 103 | } |
| 104 | |
Yingdi Yu | 6ab6781 | 2014-11-27 15:00:34 -0800 | [diff] [blame] | 105 | BOOST_AUTO_TEST_CASE(DataSignature) |
| 106 | { |
| 107 | Name identityName("/SecurityTestSignatureSha256WithEcdsa/DataSignature"); |
| 108 | BOOST_REQUIRE(addIdentity(identityName, EcdsaKeyParams())); |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 109 | shared_ptr<security::v1::PublicKey> publicKey; |
Yingdi Yu | 6ab6781 | 2014-11-27 15:00:34 -0800 | [diff] [blame] | 110 | BOOST_REQUIRE_NO_THROW(publicKey = m_keyChain.getPublicKeyFromTpm( |
| 111 | m_keyChain.getDefaultKeyNameForIdentity(identityName))); |
| 112 | |
| 113 | Data testData("/SecurityTestSignatureSha256WithEcdsa/DataSignature/Data1"); |
| 114 | char content[5] = "1234"; |
| 115 | testData.setContent(reinterpret_cast<uint8_t*>(content), 5); |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 116 | BOOST_CHECK_NO_THROW(m_keyChain.sign(testData, |
| 117 | security::SigningInfo(security::SigningInfo::SIGNER_TYPE_ID, |
| 118 | identityName))); |
Yingdi Yu | 6ab6781 | 2014-11-27 15:00:34 -0800 | [diff] [blame] | 119 | Block dataBlock(testData.wireEncode().wire(), testData.wireEncode().size()); |
| 120 | |
| 121 | Data testData2; |
| 122 | testData2.wireDecode(dataBlock); |
| 123 | BOOST_CHECK(Validator::verifySignature(testData2, *publicKey)); |
| 124 | } |
| 125 | |
Yingdi Yu | 6ab6781 | 2014-11-27 15:00:34 -0800 | [diff] [blame] | 126 | BOOST_AUTO_TEST_CASE(InterestSignature) |
| 127 | { |
| 128 | Name identityName("/SecurityTestSignatureSha256WithEcdsa/InterestSignature"); |
| 129 | BOOST_REQUIRE(addIdentity(identityName, EcdsaKeyParams())); |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 130 | shared_ptr<security::v1::PublicKey> publicKey; |
Yingdi Yu | 6ab6781 | 2014-11-27 15:00:34 -0800 | [diff] [blame] | 131 | BOOST_REQUIRE_NO_THROW(publicKey = m_keyChain.getPublicKeyFromTpm( |
| 132 | m_keyChain.getDefaultKeyNameForIdentity(identityName))); |
| 133 | |
| 134 | |
| 135 | Interest interest("/SecurityTestSignatureSha256WithEcdsa/InterestSignature/Interest1"); |
| 136 | Interest interest11("/SecurityTestSignatureSha256WithEcdsa/InterestSignature/Interest1"); |
| 137 | |
| 138 | scheduler.scheduleEvent(time::milliseconds(100), [&] { |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 139 | BOOST_CHECK_NO_THROW(m_keyChain.sign(interest, |
| 140 | security::SigningInfo(security::SigningInfo::SIGNER_TYPE_ID, |
| 141 | identityName))); |
Yingdi Yu | 6ab6781 | 2014-11-27 15:00:34 -0800 | [diff] [blame] | 142 | }); |
| 143 | |
| 144 | advanceClocks(time::milliseconds(100)); |
| 145 | scheduler.scheduleEvent(time::milliseconds(100), [&] { |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 146 | BOOST_CHECK_NO_THROW(m_keyChain.sign(interest11, |
| 147 | security::SigningInfo(security::SigningInfo::SIGNER_TYPE_ID, |
| 148 | identityName))); |
Yingdi Yu | 6ab6781 | 2014-11-27 15:00:34 -0800 | [diff] [blame] | 149 | }); |
| 150 | |
| 151 | advanceClocks(time::milliseconds(100)); |
| 152 | |
| 153 | time::system_clock::TimePoint timestamp1 = |
| 154 | time::fromUnixTimestamp( |
| 155 | time::milliseconds(interest.getName().get(signed_interest::POS_TIMESTAMP).toNumber())); |
| 156 | |
| 157 | time::system_clock::TimePoint timestamp2 = |
| 158 | time::fromUnixTimestamp( |
| 159 | time::milliseconds(interest11.getName().get(signed_interest::POS_TIMESTAMP).toNumber())); |
| 160 | |
| 161 | BOOST_CHECK_EQUAL(time::milliseconds(100), (timestamp2 - timestamp1)); |
| 162 | |
| 163 | uint64_t nonce1 = interest.getName().get(signed_interest::POS_RANDOM_VAL).toNumber(); |
| 164 | uint64_t nonce2 = interest11.getName().get(signed_interest::POS_RANDOM_VAL).toNumber(); |
| 165 | BOOST_WARN_NE(nonce1, nonce2); |
| 166 | |
| 167 | Block interestBlock(interest.wireEncode().wire(), interest.wireEncode().size()); |
| 168 | |
| 169 | Interest interest2; |
| 170 | interest2.wireDecode(interestBlock); |
| 171 | BOOST_CHECK(Validator::verifySignature(interest2, *publicKey)); |
| 172 | } |
| 173 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 174 | BOOST_AUTO_TEST_SUITE_END() // TestSignatureSha256WithEcdsa |
| 175 | BOOST_AUTO_TEST_SUITE_END() // Security |
Yingdi Yu | ebfa4cb | 2014-06-17 15:28:53 -0700 | [diff] [blame] | 176 | |
Yingdi Yu | 6ab6781 | 2014-11-27 15:00:34 -0800 | [diff] [blame] | 177 | } // namespace tests |
Yingdi Yu | ebfa4cb | 2014-06-17 15:28:53 -0700 | [diff] [blame] | 178 | } // namespace ndn |