Basic command-line tools to export KEK and KDKs

Change-Id: I0975871b6639653e60a2f7ccefaf435147828f33
diff --git a/tools/ndn-nac/add-member.cpp b/tools/ndn-nac/add-member.cpp
new file mode 100644
index 0000000..cc8455c
--- /dev/null
+++ b/tools/ndn-nac/add-member.cpp
@@ -0,0 +1,107 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2014-2018, Regents of the University of California
+ *
+ * NAC 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.
+ *
+ * NAC 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 NAC library authors and contributors.
+ */
+
+#include "ndn-nac.hpp"
+#include "access-manager.hpp"
+
+namespace ndn {
+namespace nac {
+
+int
+nac_add_member(int argc, char** argv)
+{
+  namespace po = boost::program_options;
+
+  Name identityName;
+  Name datasetName;
+  std::string output;
+  std::string member;
+
+  po::options_description description("General Usage\n"
+                                      "  ndn-nac add-member [-h] [-o output] [-d dataset] [-i] identity [-m] memberCert\n"
+                                      "General options");
+  description.add_options()
+    ("help,h", "Produce help message")
+    ("output,o", po::value<std::string>(&output), "(Optional) output file, stdout if not specified")
+    ("identity,i", po::value<Name>(&identityName), "Data owner's namespace identity")
+    ("dataset,d", po::value<Name>(&datasetName), "Name of dataset to control")
+    ("member,m", po::value<std::string>(&member), "File with member's certificate, stdin if -")
+    ;
+
+  po::positional_options_description p;
+  p.add("identity", 1);
+  p.add("member", 1);
+
+  po::variables_map vm;
+  try {
+    po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(), vm);
+    po::notify(vm);
+  }
+  catch (const std::exception& e) {
+    std::cerr << "ERROR: " << e.what() << std::endl;
+    std::cerr << description << std::endl;
+    return 1;
+  }
+
+  if (vm.count("help") != 0) {
+    std::cerr << description << std::endl;
+    return 0;
+  }
+
+  if (vm.count("identity") == 0) {
+    std::cerr << "ERROR: identity must be specified" << std::endl;
+    std::cerr << description << std::endl;
+    return 1;
+  }
+
+  if (vm.count("member") == 0) {
+    std::cerr << "ERROR: member must be specified" << std::endl;
+    std::cerr << description << std::endl;
+    return 1;
+  }
+
+  if (vm.count("output") == 0)
+    output = "-";
+
+  try {
+    security::v2::KeyChain keyChain;
+    security::Identity id = keyChain.getPib().getIdentity(identityName);
+
+    auto cert = loadCertificate(member);
+
+    util::DummyClientFace face(keyChain); // to avoid any real IO
+    AccessManager manager(id, datasetName, keyChain, face);
+
+    auto kdk = manager.addMember(cert);
+
+    if (output == "-")
+      io::save(kdk, std::cout);
+    else
+      io::save(kdk, output);
+
+    return 0;
+  }
+  catch (const std::runtime_error& e) {
+    std::cerr << "ERROR: " << e.what() << std::endl;
+    return 1;
+  }
+}
+
+} // namespace nac
+} // namespace ndn
diff --git a/tools/ndn-nac/dump-kek.cpp b/tools/ndn-nac/dump-kek.cpp
new file mode 100644
index 0000000..5d3c35e
--- /dev/null
+++ b/tools/ndn-nac/dump-kek.cpp
@@ -0,0 +1,99 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2014-2018, Regents of the University of California
+ *
+ * NAC 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.
+ *
+ * NAC 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 NAC library authors and contributors.
+ */
+
+#include "ndn-nac.hpp"
+#include "access-manager.hpp"
+
+namespace ndn {
+namespace nac {
+
+int
+nac_dump_kek(int argc, char** argv)
+{
+  namespace po = boost::program_options;
+
+  Name identityName;
+  Name datasetName;
+  std::string output;
+
+  po::options_description description("General Usage\n"
+                                      "  ndn-nac dump-kek [-h] [-o output] [-d dataset] [-i] identity \n"
+                                      "General options");
+  description.add_options()
+    ("help,h", "Produce help message")
+    ("output,o", po::value<std::string>(&output), "(Optional) output file, stdout if not specified")
+    ("identity,i", po::value<Name>(&identityName), "Data owner's namespace identity")
+    ("dataset,d", po::value<Name>(&datasetName), "Name of dataset to control")
+    ;
+
+  po::positional_options_description p;
+  p.add("identity", 1);
+
+  po::variables_map vm;
+  try {
+    po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(), vm);
+    po::notify(vm);
+  }
+  catch (const std::exception& e) {
+    std::cerr << "ERROR: " << e.what() << std::endl;
+    std::cerr << description << std::endl;
+    return 1;
+  }
+
+  if (vm.count("help") != 0) {
+    std::cerr << description << std::endl;
+    return 0;
+  }
+
+  if (vm.count("identity") == 0) {
+    std::cerr << "ERROR: identity must be specified" << std::endl;
+    std::cerr << description << std::endl;
+    return 1;
+  }
+
+  if (vm.count("output") == 0)
+    output = "-";
+
+  try {
+    security::v2::KeyChain keyChain;
+    security::Identity id = keyChain.getPib().getIdentity(identityName);
+
+    util::DummyClientFace face(keyChain); // to avoid any real IO
+    AccessManager manager(id, datasetName, keyChain, face);
+
+    if (manager.size() != 1) {
+      std::cerr << "ERROR: Incorrect state of AccessManager instance (expect 1 KDK)" << std::endl;
+      return 2;
+    }
+
+    if (output == "-")
+      io::save(*manager.begin(), std::cout);
+    else
+      io::save(*manager.begin(), output);
+
+    return 0;
+  }
+  catch (const std::runtime_error& e) {
+    std::cerr << "ERROR: " << e.what() << std::endl;
+    return 1;
+  }
+}
+
+} // namespace nac
+} // namespace ndn
diff --git a/tools/ndn-nac/main.cpp b/tools/ndn-nac/main.cpp
new file mode 100644
index 0000000..0ebe9ed
--- /dev/null
+++ b/tools/ndn-nac/main.cpp
@@ -0,0 +1,76 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2014-2018, Regents of the University of California
+ *
+ * NAC 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.
+ *
+ * NAC 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 NAC library authors and contributors.
+ */
+
+#include "ndn-nac.hpp"
+#include "version.hpp"
+
+#include <boost/exception/get_error_info.hpp>
+#include <ndn-cxx/util/logger.hpp>
+
+NDN_LOG_INIT(nac.Cmd);
+
+std::string nac_helper = R"STR(
+  help         Show all commands
+  version      Show version and exit
+  dump-kek     Dump KEK
+  add-member   Create KDK for the member
+)STR";
+
+int
+main(int argc, char** argv)
+{
+  if (argc < 2) {
+    std::cerr << nac_helper << std::endl;
+    return 1;
+  }
+
+  using namespace ndn::nac;
+
+  std::string command(argv[1]);
+  try {
+    if (command == "help")              { std::cout << nac_helper << std::endl; }
+    else if (command == "version")      { std::cout << NDN_NAC_VERSION_BUILD_STRING << std::endl; }
+    else if (command == "dump-kek")     { return nac_dump_kek(argc - 1, argv + 1); }
+    else if (command == "add-member")   { return nac_add_member(argc - 1, argv + 1); }
+    else {
+      std::cerr << nac_helper << std::endl;
+      return 1;
+    }
+  }
+  catch (const std::exception& e) {
+
+    std::cerr << "ERROR: " << e.what();
+
+    std::ostringstream extendedError;
+    const char* const* file = boost::get_error_info<boost::throw_file>(e);
+    const int* line = boost::get_error_info<boost::throw_line>(e);
+    const char* const* func = boost::get_error_info<boost::throw_function>(e);
+    if (file && line) {
+      extendedError << " [from " << *file << ":" << *line;
+      if (func) {
+        extendedError << " in " << *func;
+      }
+      extendedError << "]";
+    }
+    NDN_LOG_ERROR(e.what() << extendedError.str());
+    return 1;
+  }
+
+  return 0;
+}
diff --git a/tools/ndn-nac/ndn-nac.hpp b/tools/ndn-nac/ndn-nac.hpp
new file mode 100644
index 0000000..3f70bbd
--- /dev/null
+++ b/tools/ndn-nac/ndn-nac.hpp
@@ -0,0 +1,65 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2014-2018, Regents of the University of California
+ *
+ * NAC 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.
+ *
+ * NAC 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 NAC library authors and contributors.
+ */
+
+#ifndef NAC_TOOLS_NDN_NAC_NDN_NAC_HPP
+#define NAC_TOOLS_NDN_NAC_NDN_NAC_HPP
+
+#include "common.hpp"
+
+#include <fstream>
+#include <iostream>
+#include <string>
+
+#include <boost/asio.hpp>
+#include <boost/exception/all.hpp>
+#include <boost/program_options/options_description.hpp>
+#include <boost/program_options/parsers.hpp>
+#include <boost/program_options/variables_map.hpp>
+
+#include <ndn-cxx/util/dummy-client-face.hpp>
+#include <ndn-cxx/util/io.hpp>
+
+namespace ndn {
+namespace nac {
+
+int
+nac_dump_kek(int argc, char** argv);
+
+int
+nac_add_member(int argc, char** argv);
+
+inline security::v2::Certificate
+loadCertificate(const std::string& fileName)
+{
+  shared_ptr<security::v2::Certificate> cert;
+  if (fileName == "-")
+    cert = io::load<security::v2::Certificate>(std::cin);
+  else
+    cert = io::load<security::v2::Certificate>(fileName);
+
+  if (cert == nullptr) {
+    BOOST_THROW_EXCEPTION(std::runtime_error("Cannot load certificate from " + fileName));
+  }
+  return *cert;
+}
+
+} // namespace nac
+} // namespace ndn
+
+#endif // NAC_TOOLS_NDN_NAC_NDN_NAC_HPP