blob: 04351160be1ec1d905745a2518b5b7d644610f47 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Yingdi Yu4270f202014-01-28 14:19:16 -08002/**
Alexander Afanasyevc169a812014-05-20 20:37:29 -04003 * Copyright (c) 2013-2014 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * 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.
Yingdi Yu4270f202014-01-28 14:19:16 -080020 */
21
Yingdi Yu4270f202014-01-28 14:19:16 -080022#include "security/key-chain.hpp"
Yingdi Yu6ac97982014-01-30 14:49:21 -080023#include "security/validator.hpp"
Yingdi Yu3ed09d02014-10-13 16:24:08 -070024#include "identity-management-fixture.hpp"
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070025#include "boost-test.hpp"
26
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -080027namespace ndn {
Yingdi Yu4270f202014-01-28 14:19:16 -080028
Yingdi Yu3ed09d02014-10-13 16:24:08 -070029BOOST_FIXTURE_TEST_SUITE(SecurityTestSignedInterest, security::IdentityManagementFixture)
Yingdi Yu4270f202014-01-28 14:19:16 -080030
Yingdi Yu0f5fb692014-06-10 12:07:28 -070031BOOST_AUTO_TEST_CASE(SignVerifyInterest)
Yingdi Yu4270f202014-01-28 14:19:16 -080032{
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070033 Name identityName("/TestSignedInterest/SignVerify");
34 identityName.appendVersion();
Yingdi Yu3ed09d02014-10-13 16:24:08 -070035 BOOST_REQUIRE(addIdentity(identityName, RsaKeyParams()));
36 Name certificateName = m_keyChain.getDefaultCertificateNameForIdentity(identityName);
Yingdi Yu4270f202014-01-28 14:19:16 -080037
Yingdi Yu17bc3012014-02-10 17:37:12 -080038 Interest interest("/TestSignedInterest/SignVerify/Interest1");
Yingdi Yu3ed09d02014-10-13 16:24:08 -070039 BOOST_CHECK_NO_THROW(m_keyChain.signByIdentity(interest, identityName));
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070040
Yingdi Yu0f5fb692014-06-10 12:07:28 -070041 usleep(100000);
42
43 Interest interest11("/TestSignedInterest/SignVerify/Interest1");
Yingdi Yu3ed09d02014-10-13 16:24:08 -070044 BOOST_CHECK_NO_THROW(m_keyChain.signByIdentity(interest11, identityName));
Yingdi Yu0f5fb692014-06-10 12:07:28 -070045
46 time::system_clock::TimePoint timestamp1 =
47 time::fromUnixTimestamp(
48 time::milliseconds(interest.getName().get(signed_interest::POS_TIMESTAMP).toNumber()));
49
50 time::system_clock::TimePoint timestamp2 =
51 time::fromUnixTimestamp(
52 time::milliseconds(interest11.getName().get(signed_interest::POS_TIMESTAMP).toNumber()));
53
54 BOOST_CHECK_LT(time::milliseconds(100), (timestamp2 - timestamp1));
55
56 uint64_t nonce1 = interest.getName().get(signed_interest::POS_RANDOM_VAL).toNumber();
57 uint64_t nonce2 = interest11.getName().get(signed_interest::POS_RANDOM_VAL).toNumber();
58 BOOST_CHECK_NE(nonce1, nonce2);
59
Yingdi Yu4270f202014-01-28 14:19:16 -080060 Block interestBlock(interest.wireEncode().wire(), interest.wireEncode().size());
61
62 Interest interest2;
63 interest2.wireDecode(interestBlock);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070064
Yingdi Yu2e57a582014-02-20 23:34:43 -080065 shared_ptr<PublicKey> publicKey;
Yingdi Yu3ed09d02014-10-13 16:24:08 -070066 BOOST_REQUIRE_NO_THROW(publicKey = m_keyChain.getPublicKeyFromTpm(
67 m_keyChain.getDefaultKeyNameForIdentity(identityName)));
Yingdi Yu6ac97982014-01-30 14:49:21 -080068 bool result = Validator::verifySignature(interest2, *publicKey);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070069
Yingdi Yu17bc3012014-02-10 17:37:12 -080070 BOOST_CHECK_EQUAL(result, true);
Yingdi Yu4270f202014-01-28 14:19:16 -080071}
72
73BOOST_AUTO_TEST_SUITE_END()
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -080074
75} // namespace ndn