security: Add CommandInterestSigner

v2::KeyChain::sign(Interest) does not add timestamp and nonce
components, required by CommandInterest specification
(https://redmine.named-data.net/projects/ndn-cxx/wiki/CommandInterest).
Whenever command interests are needed, CommandInterestSigner should be
used.

Change-Id: I3d573e6dd1bc686e42b0564add54173b7edf40f2
Refs: #3912
diff --git a/src/security/command-interest-signer.cpp b/src/security/command-interest-signer.cpp
new file mode 100644
index 0000000..1dfe1ce
--- /dev/null
+++ b/src/security/command-interest-signer.cpp
@@ -0,0 +1,59 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2013-2017 Regents of the University of California.
+ *
+ * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
+ *
+ * ndn-cxx library is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later version.
+ *
+ * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
+ *
+ * You should have received copies of the GNU General Public License and GNU Lesser
+ * General Public License along with ndn-cxx, e.g., in COPYING.md file.  If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
+ */
+
+#include "command-interest-signer.hpp"
+#include "../util/random.hpp"
+
+namespace ndn {
+namespace security {
+
+Name
+CommandInterestPreparer::prepareCommandInterestName(Name name)
+{
+  time::milliseconds timestamp = time::toUnixTimestamp(time::system_clock::now());
+  if (timestamp <= m_lastUsedTimestamp) {
+    timestamp = m_lastUsedTimestamp + time::milliseconds(1);
+  }
+  m_lastUsedTimestamp = timestamp;
+
+  name
+    .append(name::Component::fromNumber(timestamp.count()))
+    .append(name::Component::fromNumber(random::generateWord64())) // nonce
+    ;
+
+  return name;
+}
+
+CommandInterestSigner::CommandInterestSigner(v2::KeyChain& keyChain)
+  : m_keyChain(keyChain)
+{
+}
+
+Interest
+CommandInterestSigner::makeCommandInterest(const Name& name, const SigningInfo& params)
+{
+  Interest commandInterest(prepareCommandInterestName(name));
+  m_keyChain.sign(commandInterest, params);
+  return commandInterest;
+}
+
+} // namespace security
+} // namespace ndn
diff --git a/src/security/command-interest-signer.hpp b/src/security/command-interest-signer.hpp
new file mode 100644
index 0000000..7033b3f
--- /dev/null
+++ b/src/security/command-interest-signer.hpp
@@ -0,0 +1,93 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2013-2017 Regents of the University of California.
+ *
+ * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
+ *
+ * ndn-cxx library is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later version.
+ *
+ * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
+ *
+ * You should have received copies of the GNU General Public License and GNU Lesser
+ * General Public License along with ndn-cxx, e.g., in COPYING.md file.  If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
+ */
+
+#ifndef NDN_SECURITY_COMMAND_INTEREST_SIGNER_HPP
+#define NDN_SECURITY_COMMAND_INTEREST_SIGNER_HPP
+
+#include "v2/key-chain.hpp"
+
+namespace ndn {
+namespace security {
+
+/**
+ * @brief Helper class to prepare command interest name
+ *
+ * The preparer adds timestamp and nonce name components to the supplied name.
+ *
+ * This class is primarily designed to be used as part of CommandInterestSigner, but can also
+ * be using in an application that defines custom signing methods not support by the KeyChain
+ * (such as HMAC-SHA1).
+ *
+ * @sa https://redmine.named-data.net/projects/ndn-cxx/wiki/CommandInterest
+ */
+class CommandInterestPreparer : noncopyable
+{
+public:
+  /**
+   * @brief Prepare name of the CommandInterest
+   *
+   * This method appends the timestamp and nonce name components to the supplied name.
+   */
+  Name
+  prepareCommandInterestName(Name name);
+
+private:
+  time::milliseconds m_lastUsedTimestamp;
+};
+
+/**
+ * @brief Helper class to create command interests
+ *
+ * The signer adds timestamp and nonce name components to the supplied name, creates an
+ * Interest, and signs it with the KeyChain.
+ *
+ * @sa https://redmine.named-data.net/projects/ndn-cxx/wiki/CommandInterest
+ */
+class CommandInterestSigner : private CommandInterestPreparer
+{
+public:
+  explicit
+  CommandInterestSigner(v2::KeyChain& keyChain);
+
+  /**
+   * @brief Create CommandInterest
+   *
+   * This method appends the timestamp and nonce name components to the supplied name, create
+   * an Interest object and signs it with the keychain.
+   *
+   * Note that signature of the command interest covers only Name of the interest.  Therefore,
+   * other fields in the returned interest can be changed without breaking validity of the
+   * signature, because s
+   *
+   * @sa https://redmine.named-data.net/projects/ndn-cxx/wiki/CommandInterest
+   */
+  Interest
+  makeCommandInterest(const Name& name, const SigningInfo& params = v2::KeyChain::getDefaultSigningInfo());
+
+private:
+  v2::KeyChain& m_keyChain;
+};
+
+} // namespace security
+} // namespace ndn
+
+
+#endif // NDN_SECURITY_COMMAND_INTEREST_SIGNER_HPP
diff --git a/tests/unit-tests/security/command-interest-signer.t.cpp b/tests/unit-tests/security/command-interest-signer.t.cpp
new file mode 100644
index 0000000..fcfcaa2
--- /dev/null
+++ b/tests/unit-tests/security/command-interest-signer.t.cpp
@@ -0,0 +1,71 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2013-2017 Regents of the University of California.
+ *
+ * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
+ *
+ * ndn-cxx library is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later version.
+ *
+ * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
+ *
+ * You should have received copies of the GNU General Public License and GNU Lesser
+ * General Public License along with ndn-cxx, e.g., in COPYING.md file.  If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
+ */
+
+#include "security/command-interest-signer.hpp"
+#include "security/signing-helpers.hpp"
+
+#include "boost-test.hpp"
+#include "../identity-management-time-fixture.hpp"
+
+namespace ndn {
+namespace security {
+namespace tests {
+
+using namespace ndn::tests;
+
+BOOST_AUTO_TEST_SUITE(Security)
+BOOST_FIXTURE_TEST_SUITE(TestCommandInterestSigner, IdentityManagementV2TimeFixture)
+
+BOOST_AUTO_TEST_CASE(Basic)
+{
+  addIdentity("/test");
+
+  // @TODO replace signed_interest::* with command_interest::*
+
+  CommandInterestSigner signer(m_keyChain);
+  Interest i1 = signer.makeCommandInterest("/hello/world");
+  BOOST_CHECK_EQUAL(i1.getName().size(), 6);
+  BOOST_CHECK_EQUAL(i1.getName().at(signed_interest::POS_SIG_VALUE).blockFromValue().type(), tlv::SignatureValue);
+  BOOST_CHECK_EQUAL(i1.getName().at(signed_interest::POS_SIG_INFO).blockFromValue().type(), tlv::SignatureInfo);
+
+  time::milliseconds timestamp = toUnixTimestamp(time::system_clock::now());
+  BOOST_CHECK_EQUAL(i1.getName().at(signed_interest::POS_TIMESTAMP).toNumber(), timestamp.count());
+
+  Interest i2 = signer.makeCommandInterest("/hello/world/!", signingByIdentity("/test"));
+  BOOST_CHECK_EQUAL(i2.getName().size(), 7);
+  BOOST_CHECK_EQUAL(i2.getName().at(signed_interest::POS_SIG_VALUE).blockFromValue().type(), tlv::SignatureValue);
+  BOOST_CHECK_EQUAL(i2.getName().at(signed_interest::POS_SIG_INFO).blockFromValue().type(), tlv::SignatureInfo);
+  BOOST_CHECK_GT(i2.getName().at(signed_interest::POS_TIMESTAMP), i1.getName().at(signed_interest::POS_TIMESTAMP));
+  BOOST_CHECK_NE(i2.getName().at(signed_interest::POS_RANDOM_VAL),
+                 i1.getName().at(signed_interest::POS_RANDOM_VAL)); // this sometimes can fail
+
+  advanceClocks(time::seconds(100));
+
+  i2 = signer.makeCommandInterest("/hello/world/!");
+  BOOST_CHECK_GT(i2.getName().at(signed_interest::POS_TIMESTAMP), i1.getName().at(signed_interest::POS_TIMESTAMP));
+}
+
+BOOST_AUTO_TEST_SUITE_END() // TestCommandInterestSigner
+BOOST_AUTO_TEST_SUITE_END() // Security
+
+} // namespace tests
+} // namespace security
+} // namespace ndn