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