tools: Add security tools; Add waf build options for tools.
Change-Id: I058510acc1a0361fed43d94c7a0b545416a9efb4
diff --git a/tools/ndnsec-dump-certificate.cpp b/tools/ndnsec-dump-certificate.cpp
new file mode 100644
index 0000000..d3936ec
--- /dev/null
+++ b/tools/ndnsec-dump-certificate.cpp
@@ -0,0 +1,220 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2013, Regents of the University of California
+ * Yingdi Yu
+ *
+ * BSD license, See the LICENSE file for more information
+ *
+ * Author: Yingdi Yu <yingdi@cs.ucla.edu>
+ */
+
+#include <iostream>
+#include <fstream>
+
+#include <boost/program_options/options_description.hpp>
+#include <boost/program_options/variables_map.hpp>
+#include <boost/program_options/parsers.hpp>
+#include <boost/date_time/posix_time/posix_time.hpp>
+#include <boost/asio.hpp>
+
+#include <cryptopp/base64.h>
+#include <cryptopp/files.h>
+
+#include "security/key-chain.hpp"
+
+using namespace ndn;
+namespace po = boost::program_options;
+
+shared_ptr<IdentityCertificate>
+getIdentityCertificate(const std::string& fileName)
+{
+ std::istream* ifs;
+ if(fileName == "-")
+ ifs = &std::cin;
+ else
+ ifs = new std::ifstream(fileName.c_str());
+
+ std::string decoded;
+ CryptoPP::FileSource ss2(*ifs, true,
+ new CryptoPP::Base64Decoder(new CryptoPP::StringSink(decoded)));
+
+ ptr_lib::shared_ptr<IdentityCertificate> identityCertificate = ptr_lib::make_shared<IdentityCertificate>();
+ identityCertificate->wireDecode(Block(decoded.c_str(), decoded.size()));
+
+ return identityCertificate;
+}
+
+int main(int argc, char** argv)
+{
+ std::string name;
+ bool isKeyName = false;
+ bool isIdentityName = false;
+ bool isCertName = true;
+ bool isFileName = false;
+ bool isPretty = false;
+ bool isStdOut = true;
+ bool isRepoOut = false;
+ std::string repoHost = "127.0.0.1";
+ std::string repoPort = "7376";
+ bool isDnsOut = false;
+
+ po::options_description desc("General Usage\n ndn-dump-certificate [-h] [-p] [-d] [-r [-H repo-host] [-P repor-port] ] [-i|k|f] certName\nGeneral options");
+ desc.add_options()
+ ("help,h", "produce help message")
+ ("pretty,p", "optional, if specified, display certificate in human readable format")
+ ("identity,i", "optional, if specified, name is identity name (e.g. /ndn/edu/ucla/alice), otherwise certificate name")
+ ("key,k", "optional, if specified, name is key name (e.g. /ndn/edu/ucla/alice/KSK-123456789), otherwise certificate name")
+ ("file,f", "optional, if specified, name is file name, - for stdin")
+ ("repo-output,r", "optional, if specified, certificate is dumped (published) to repo")
+ ("repo-host,H", po::value<std::string>(&repoHost)->default_value("localhost"), "optional, the repo host if repo-output is specified")
+ ("repo-port,P", po::value<std::string>(&repoPort)->default_value("7376"), "optional, the repo port if repo-output is specified")
+ ("dns-output,d", "optional, if specified, certificate is dumped (published) to DNS")
+ ("name,n", po::value<std::string>(&name), "certificate name, for example, /ndn/edu/ucla/KEY/cs/alice/ksk-1234567890/ID-CERT/%FD%FF%FF%FF%FF%FF%FF%FF")
+ ;
+
+ po::positional_options_description p;
+ p.add("name", 1);
+
+ po::variables_map vm;
+ po::store(po::command_line_parser(argc, argv).options(desc).positional(p).run(), vm);
+ po::notify(vm);
+
+ if (vm.count("help"))
+ {
+ std::cerr << desc << std::endl;
+ return 1;
+ }
+
+ if (0 == vm.count("name"))
+ {
+ std::cerr << "identity_name must be specified" << std::endl;
+ std::cerr << desc << std::endl;
+ return 1;
+ }
+
+ if (vm.count("key"))
+ {
+ isCertName = false;
+ isKeyName = true;
+ }
+ else if (vm.count("identity"))
+ {
+ isCertName = false;
+ isIdentityName = true;
+ }
+ else if (vm.count("file"))
+ {
+ isCertName = false;
+ isFileName = true;
+ }
+
+ if (vm.count("pretty"))
+ isPretty = true;
+
+ if (vm.count("repo-output"))
+ {
+ isRepoOut = true;
+ isStdOut = false;
+ }
+ else if(vm.count("dns-output"))
+ {
+ isDnsOut = true;
+ isStdOut = false;
+ std::cerr << "Error: DNS output is not supported yet!" << std::endl;
+ return 1;
+ }
+
+ if (isPretty && !isStdOut)
+ {
+ std::cerr << "Error: pretty option can only be specified when other output option is specified" << std::endl;
+ return 1;
+ }
+
+ KeyChain keyChain;
+ ptr_lib::shared_ptr<IdentityCertificate> certificate;
+
+ try{
+ if(isIdentityName || isKeyName || isCertName)
+ {
+ if(isIdentityName)
+ {
+ Name certName = keyChain.getDefaultCertificateNameForIdentity(name);
+ certificate = keyChain.getCertificate(certName);
+ }
+ else if(isKeyName)
+ {
+ Name certName = keyChain.getDefaultCertificateNameForKey(name);
+ certificate = keyChain.getCertificate(certName);
+ }
+ else
+ certificate = keyChain.getCertificate(name);
+
+ if(NULL == certificate)
+ {
+ std::cerr << "No certificate found!" << std::endl;
+ return 1;
+ }
+ }
+ else
+ {
+ certificate = getIdentityCertificate(name);
+ if(NULL == certificate)
+ {
+ std::cerr << "No certificate read!" << std::endl;
+ return 1;
+ }
+ }
+
+ if(isPretty)
+ {
+ std::cout << *certificate << std::endl;
+ // cout << "Certificate name: " << std::endl;
+ // cout << " " << certificate->getName() << std::endl;
+ // cout << "Validity: " << std::endl;
+ // cout << " NotBefore: " << boost::posix_time::to_simple_string(certificate->getNotBefore()) << std::endl;
+ // cout << " NotAfter: " << boost::posix_time::to_simple_string(certificate->getNotAfter()) << std::endl;
+ // cout << "Subject Description: " << std::endl;
+ // const vector<CertificateSubjectDescription>& SubDescriptionList = certificate->getSubjectDescriptionList();
+ // vector<CertificateSubjectDescription>::const_iterator it = SubDescriptionList.begin();
+ // for(; it != SubDescriptionList.end(); it++)
+ // cout << " " << it->getOidStr() << ": " << it->getValue() << std::endl;
+ // cout << "Public key bits: " << std::endl;
+ // const Blob& keyBlob = certificate->getPublicKeygetKeyBlob();
+ // std::string encoded;
+ // CryptoPP::StringSource ss(reinterpret_cast<const unsigned char *>(keyBlob.buf()), keyBlob.size(), true,
+ // new CryptoPP::Base64Encoder(new CryptoPP::StringSink(encoded), true, 64));
+ // cout << encoded;
+ }
+ else
+ {
+ if(isStdOut)
+ {
+ CryptoPP::StringSource ss(certificate->wireEncode().wire(), certificate->wireEncode().size(),
+ true,
+ new CryptoPP::Base64Encoder(new CryptoPP::FileSink(std::cout), true, 64));
+ return 0;
+ }
+ if(isRepoOut)
+ {
+ using namespace boost::asio::ip;
+ tcp::iostream request_stream;
+#if (BOOST_VERSION >= 104700)
+ request_stream.expires_from_now(boost::posix_time::milliseconds(3000));
+#endif
+ request_stream.connect(repoHost,repoPort);
+ if(!request_stream)
+ {
+ std::cerr << "fail to open the stream!" << std::endl;
+ return 1;
+ }
+ request_stream.write(reinterpret_cast<const char*>(certificate->wireEncode().wire()), certificate->wireEncode().size());
+ return 0;
+ }
+ }
+ }
+ catch(std::exception & e){
+ std::cerr << e.what() << std::endl;
+ return 1;
+ }
+ return 0;
+}