tools: Fix ndnsec-cert-gen to correctly infer prefix before KEY

This commit also includes a few help message corrections in cert-dump,
cert-gen, and key-gen.

Change-Id: I67977e62c144c35e0e17fa1228c53b9d3cff201e
Refs: #2052
diff --git a/tools/ndnsec-cert-dump.hpp b/tools/ndnsec-cert-dump.hpp
index a8ade74..95b1320 100644
--- a/tools/ndnsec-cert-dump.hpp
+++ b/tools/ndnsec-cert-dump.hpp
@@ -40,22 +40,32 @@
   bool isPretty = false;
   bool isStdOut = true;
   bool isRepoOut = false;
-  std::string repoHost = "127.0.0.1";
-  std::string repoPort = "7376";
+  std::string repoHost;
+  std::string repoPort;
   // bool isDnsOut = false;
 
-  po::options_description description("General Usage\n  ndnsec cert-dump [-h] [-p] [-d] [-r [-H repo-host] [-P repor-port] ] [-i|k|f] name\nGeneral options");
+  po::options_description description("General Usage\n"
+                                      "  ndnsec cert-dump [-h] [-p] [-d] [-r [-H repo-host] "
+                                         "[-P repo-port] ] [-i|k|f] name\n"
+                                      "General options");
   description.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")
+    ("help,h",     "produce help message")
+    ("pretty,p",   "display certificate in human readable format")
+    ("identity,i", "treat the name parameter as identity name (e.g., /ndn/edu/ucla/alice")
+    ("key,k",      "treat the name parameter as key name "
+                   "(e.g., /ndn/edu/ucla/alice/ksk-123456789)")
+    ("file,f",     "treat the name parameter as file name with base64 encoded certificate, "
+                   "- for stdin")
+    ("repo-output,r", "publish the certificate to the repo-ng")
+    ("repo-host,H", po::value<std::string>(&repoHost)->default_value("localhost"),
+                   "the repo host if repo-output is specified")
+    ("repo-port,P", po::value<std::string>(&repoPort)->default_value("7376"),
+                   "the repo port if repo-output is specified")
+    // ("dns-output,d", "published the certificate to NDNS")
+    ("name,n", po::value<std::string>(&name),
+                   "unless overridden with --identity or --key parameter, the 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;
@@ -122,7 +132,8 @@
 
   if (isPretty && !isStdOut)
     {
-      std::cerr << "Error: pretty option can only be specified when other output option is specified" << std::endl;
+      std::cerr << "Error: pretty option can only be specified when other "
+                << "output option is specified" << std::endl;
       return 1;
     }
 
diff --git a/tools/ndnsec-cert-gen.hpp b/tools/ndnsec-cert-gen.hpp
index 71b2035..0126646 100644
--- a/tools/ndnsec-cert-gen.hpp
+++ b/tools/ndnsec-cert-gen.hpp
@@ -40,10 +40,11 @@
   std::string notAfterStr;
   std::string subjectName;
   std::string requestFile("-");
-  std::string signId;
+  Name signId;
   std::string subjectInfo;
-  std::string certPrefix;
-  bool hasSignId = false;
+  Name certPrefix;
+
+  KeyChain keyChain;
 
   po::options_description description(
     "General Usage\n"
@@ -53,22 +54,22 @@
 
   description.add_options()
     ("help,h", "produce help message")
-    ("not-before,S",     po::value<std::string>(&notBeforeStr),
-                         "certificate starting date, YYYYMMDDhhmmss")
-    ("not-after,E",      po::value<std::string>(&notAfterStr),
-                         "certificate ending date, YYYYMMDDhhmmss")
-    ("subject-name,N",   po::value<std::string>(&subjectName),
-                         "subject name")
-    ("subject-info,I",   po::value<std::string>(&subjectInfo),
-                         "subject info, pairs of OID and string description: "
-                         "\"2.5.4.10 'University of California, Los Angeles'\"")
-    ("sign-id,s",        po::value<std::string>(&signId),
-                         "signing Identity, system default identity if not specified")
-    ("cert-prefix,p", po::value<std::string>(&certPrefix),
-                         "cert prefix, which is the part of certificate name before "
-                         "KEY component")
-    ("request,r",        po::value<std::string>(&requestFile),
-                         "request file name, - for stdin")
+    ("not-before,S",   po::value<std::string>(&notBeforeStr),
+                       "certificate starting date, YYYYMMDDhhmmss (default: now)")
+    ("not-after,E",    po::value<std::string>(&notAfterStr),
+                       "certificate ending date, YYYYMMDDhhmmss (default: now + 365 days)")
+    ("subject-name,N", po::value<std::string>(&subjectName),
+                       "subject name")
+    ("subject-info,I", po::value<std::string>(&subjectInfo),
+                       "subject info, pairs of OID and string description: "
+                       "\"2.5.4.10 'University of California, Los Angeles'\"")
+    ("sign-id,s",      po::value<Name>(&signId)->default_value(keyChain.getDefaultIdentity()),
+                       "signing identity")
+    ("cert-prefix,p",  po::value<Name>(&certPrefix)->default_value(KeyChain::DEFAULT_PREFIX),
+                       "cert prefix, which is the part of certificate name before "
+                       "KEY component")
+    ("request,r",      po::value<std::string>(&requestFile)->default_value("-"),
+                       "request file name, - for stdin")
     ;
 
   po::positional_options_description p;
@@ -89,18 +90,15 @@
 
   if (vm.count("help") != 0)
     {
-      std::cerr << description << std::endl;
+      std::cout << description << std::endl;
       return 0;
     }
 
-  if (vm.count("sign-id") != 0)
-    {
-      hasSignId = true;
-    }
-
   if (vm.count("subject-name") == 0)
     {
-      std::cerr << "subject_name must be specified" << std::endl;
+      std::cerr << "ERROR: subject name must be specified" << std::endl
+                << std::endl
+                << description << std::endl;
       return 1;
     }
 
@@ -155,14 +153,18 @@
 
       if (notAfter < notBefore)
         {
-          std::cerr << "not-before is later than not-after" << std::endl;
+          std::cerr << "ERROR: not-before cannot be later than not-after" << std::endl
+                    << std::endl
+                    << description << std::endl;
           return 1;
         }
     }
 
   if (vm.count("request") == 0)
     {
-      std::cerr << "request file must be specified" << std::endl;
+      std::cerr << "ERROR: request file must be specified" << std::endl
+                << std::endl
+                << description << std::endl;
       return 1;
     }
 
@@ -175,31 +177,22 @@
       return 1;
     }
 
-  KeyChain keyChain;
-
   Name keyName = selfSignedCertificate->getPublicKeyName();
-  Name signIdName;
-  Name prefix(certPrefix);
-
-  if (!hasSignId)
-    signIdName = keyChain.getDefaultIdentity();
-  else
-    signIdName = Name(signId);
 
   shared_ptr<IdentityCertificate> certificate =
     keyChain.prepareUnsignedIdentityCertificate(keyName, selfSignedCertificate->getPublicKeyInfo(),
-                                                signIdName, notBefore, notAfter,
-                                                subjectDescription, prefix);
+                                                signId, notBefore, notAfter,
+                                                subjectDescription, certPrefix);
 
   if (!static_cast<bool>(certificate))
     {
-      std::cerr << "ERROR: key name is not formated correctly or does not match certificate name."
+      std::cerr << "ERROR: key name is not formated correctly or does not match certificate name"
                 << std::endl;
       return 1;
     }
 
-  keyChain.createIdentity(signIdName);
-  Name signingCertificateName = keyChain.getDefaultCertificateNameForIdentity(signIdName);
+  keyChain.createIdentity(signId);
+  Name signingCertificateName = keyChain.getDefaultCertificateNameForIdentity(signId);
   keyChain.sign(*certificate, signingCertificateName);
 
   Block wire = certificate->wireEncode();
diff --git a/tools/ndnsec-key-gen.hpp b/tools/ndnsec-key-gen.hpp
index cbd500a..b74ccc2 100644
--- a/tools/ndnsec-key-gen.hpp
+++ b/tools/ndnsec-key-gen.hpp
@@ -38,13 +38,20 @@
   int keySize = 2048;
   std::string outputFilename;
 
-  po::options_description description("General Usage\n  ndnsec key-gen [-h] [-n] identity\nGeneral options");
+  po::options_description description("General Usage\n"
+                                      "  ndnsec key-gen [-h] [-n] identity\n"
+                                      "General options");
   description.add_options()
     ("help,h", "produce help message")
-    ("identity,i", po::value<std::string>(&identityName), "identity name, for example, /ndn/ucla.edu/alice")
-    ("not_default,n", "optional, if not specified, the target identity will be set as the default identity of the system")
-    // ("type,t", po::value<char>(&keyType)->default_value('r'), "optional, key type, r for RSA key (default)")
-    // ("size,s", po::value<int>(&keySize)->default_value(2048), "optional, key size, 2048 (default)")
+    ("identity,i", po::value<std::string>(&identityName),
+     "identity name, for example, /ndn/edu/ucla/alice")
+    ("not_default,n",
+     "optional, if not specified, the target identity will be set as "
+     "the default identity of the system")
+    // ("type,t", po::value<char>(&keyType)->default_value('r'),
+    // "optional, key type, r for RSA key (default)")
+    // ("size,s", po::value<int>(&keySize)->default_value(2048),
+    // "optional, key size, 2048 (default)")
     ;
 
   po::positional_options_description p;