security: Correct code style for ndnsec and add command-line exception handling

Change-Id: I68de24c3fb2af5decd57b790ccbfc908dc0c8332
diff --git a/tools/ndnsec-cert-gen.hpp b/tools/ndnsec-cert-gen.hpp
index 363dd57..197cc6b 100644
--- a/tools/ndnsec-cert-gen.hpp
+++ b/tools/ndnsec-cert-gen.hpp
@@ -10,7 +10,7 @@
 
 #include "ndnsec-util.hpp"
 
-int 
+int
 ndnsec_cert_gen(int argc, char** argv)
 {
   using boost::tokenizer;
@@ -21,23 +21,23 @@
 
   std::string notBeforeStr;
   std::string notAfterStr;
-  std::string sName;
-  std::string reqFile;
+  std::string subjectName;
+  std::string requestFile("-");
   std::string signId;
-  std::string subInfo;
+  std::string subjectInfo;
   bool isSelfSigned = false;
-  bool nack = false;
+  bool isNack = false;
 
-  po::options_description desc("General Usage\n  ndnsec cert-gen [-h] [-S date] [-E date] [-N subject-name] [-I subject-info] [-s sign-id] request\nGeneral options");
-  desc.add_options()
+  po::options_description description("General Usage\n  ndnsec cert-gen [-h] [-S date] [-E date] [-N subject-name] [-I subject-info] [-s sign-id] request\nGeneral options");
+  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>(&sName), "subject name")
-    ("subject-info,I", po::value<std::string>(&subInfo), "subject info, pairs of OID and string description: \"2.5.4.10 'University of California, Los Angeles'\"")
+    ("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'\"")
     ("nack", "Generate revocation certificate (NACK)")
     ("sign-id,s", po::value<std::string>(&signId), "signing Identity, self-signed if not specified")
-    ("request,r", po::value<std::string>(&reqFile), "request file name, - for stdin")
+    ("request,r", po::value<std::string>(&requestFile), "request file name, - for stdin")
     ;
 
   po::positional_options_description p;
@@ -46,100 +46,96 @@
   po::variables_map vm;
   try
     {
-      po::store(po::command_line_parser(argc, argv).options(desc).positional(p).run(), vm);
+      po::store(po::command_line_parser(argc, argv).options(description).positional(p).run(),
+                vm);
       po::notify(vm);
     }
-  catch (std::exception &e)
+  catch (const std::exception& e)
     {
       std::cerr << "ERROR: " << e.what() << std::endl;
       return 1;
     }
 
-  if (vm.count("help"))
+  if (vm.count("help") != 0)
     {
-      std::cerr << desc << std::endl;
+      std::cerr << description << std::endl;
       return 0;
     }
 
-  if (0 == vm.count("sign-id"))
-    {     
+  if (vm.count("sign-id") == 0)
+    {
       isSelfSigned = true;
     }
 
-  if (vm.count("nack"))
+  if (vm.count("nack") != 0)
     {
-      nack = true;
+      isNack = true;
     }
 
   std::vector<CertificateSubjectDescription> otherSubDescrypt;
-  tokenizer<escaped_list_separator<char> > subInfoItems(subInfo, escaped_list_separator<char> ("\\", " \t", "'\""));
+  tokenizer<escaped_list_separator<char> > subjectInfoItems
+    (subjectInfo, escaped_list_separator<char> ("\\", " \t", "'\""));
 
-  tokenizer<escaped_list_separator<char> >::iterator it = subInfoItems.begin();
-  try
+  tokenizer<escaped_list_separator<char> >::iterator it =
+    subjectInfoItems.begin();
+
+  while (it != subjectInfoItems.end())
     {
-      while (it != subInfoItems.end())
+      std::string oid = *it;
+
+      it++;
+      if (it == subjectInfoItems.end())
         {
-          std::string oid = *it;
-
-          it++;
-          if (it == subInfoItems.end ())
-            {
-              std::cerr << "ERROR: unmatched info for oid [" << oid << "]" << std::endl;
-              return 1;
-            }
-
-          std::string value = *it;
-
-          otherSubDescrypt.push_back (CertificateSubjectDescription(oid, value));
-
-          it++;
+          std::cerr << "ERROR: unmatched info for oid [" << oid << "]" << std::endl;
+          return 1;
         }
-    }
-  catch (std::exception &e)
-    {
-      std::cerr << "error in parsing subject info" << std::endl;
-      return 1;
+
+      std::string value = *it;
+
+      otherSubDescrypt.push_back(CertificateSubjectDescription(oid, value));
+
+      it++;
     }
 
   time::system_clock::TimePoint notBefore;
   time::system_clock::TimePoint notAfter;
-  try{
-    if (0 == vm.count("not-before"))
-      {
-        notBefore = time::system_clock::now();
-      }
-    else
-      {
-        notBefore = time::fromIsoString(notBeforeStr.substr(0, 8) + "T" + notBeforeStr.substr(8, 6));
-      }
 
+  if (vm.count("not-before") == 0)
+    {
+      notBefore = time::system_clock::now();
+    }
+  else
+    {
+      notBefore = time::fromIsoString(notBeforeStr.substr(0, 8) + "T" +
+                                      notBeforeStr.substr(8, 6));
+    }
 
-    if (0 == vm.count("not-after"))
-      {
-        notAfter = notBefore + time::days(365);
-      }
-    else
-      {
-        notAfter = time::fromIsoString(notAfterStr.substr(0, 8) + "T" + notAfterStr.substr(8, 6));
-        if(notAfter < notBefore)
-          {
-            std::cerr << "not-before is later than not-after" << std::endl;
-            return 1;
-          }
-      }
-  }catch(std::exception & e){
-    std::cerr << "Error in converting validity timestamp!" << std::endl;
-    return 1;
-  }
+  if (vm.count("not-after") == 0)
+    {
+      notAfter = notBefore + time::days(365);
+    }
+  else
+    {
+      notAfter = time::fromIsoString(notAfterStr.substr(0, 8) + "T" +
+                                     notAfterStr.substr(8, 6));
 
-  if (0 == vm.count("request"))
+      if (notAfter < notBefore)
+        {
+          std::cerr << "not-before is later than not-after" << std::endl;
+          return 1;
+        }
+    }
+
+  if (vm.count("request") == 0)
     {
       std::cerr << "request file must be specified" << std::endl;
       return 1;
     }
 
-  shared_ptr<IdentityCertificate> selfSignedCertificate = getIdentityCertificate(reqFile);
-  if(!static_cast<bool>(selfSignedCertificate))
+  shared_ptr<IdentityCertificate> selfSignedCertificate
+    = getIdentityCertificate(requestFile);
+
+  if (!static_cast<bool>(selfSignedCertificate))
     {
       std::cerr << "ERROR: input error" << std::endl;
       return 1;
@@ -149,7 +145,7 @@
   Name signIdName;
   Name certName;
 
-  if(isSelfSigned)
+  if (isSelfSigned)
     {
       certName = keyName.getPrefix(keyName.size()-1);
       certName.append("KEY").append(keyName.get(-1)).append("ID-CERT").appendVersion();
@@ -157,72 +153,64 @@
   else
     {
       signIdName = Name(signId);
-  
+
+      int count = 0;
       Name::const_iterator i = keyName.begin();
       Name::const_iterator j = signIdName.begin();
-      int count = 0;
-      for(; i != keyName.end() && j != signIdName.end(); i++, j++, count++)
+      for (; i != keyName.end() && j != signIdName.end(); i++, j++)
         {
-          if(*i != *j)
+          if (*i != *j)
             break;
+
+          count++;
         }
-      
-      if(j != signIdName.end() || i == keyName.end())
+
+      if (j != signIdName.end() || i == keyName.end())
         {
           std::cerr << "wrong signing identity!" << std::endl;
           return 1;
         }
 
       certName = keyName.getSubName(0, count);
-      certName.append("KEY").append(keyName.getSubName(count, keyName.size()-count));
-      certName.append("ID-CERT").appendVersion ();
+      certName.append("KEY")
+        .append(keyName.getSubName(count, keyName.size() - count))
+        .append("ID-CERT")
+        .appendVersion ();
     }
 
   Block wire;
 
-  if (!nack)
+  if (!isNack)
     {
-      if (0 == vm.count("subject-name"))
+      if (vm.count("subject-name") == 0)
         {
           std::cerr << "subject_name must be specified" << std::endl;
           return 1;
         }
 
-      try
-        {
-          CertificateSubjectDescription subDescryptName("2.5.4.41", sName);
-          IdentityCertificate certificate;
-          certificate.setName(certName);
-          certificate.setNotBefore(notBefore);
-          certificate.setNotAfter(notAfter);
-          certificate.setPublicKeyInfo(selfSignedCertificate->getPublicKeyInfo());
-          certificate.addSubjectDescription(subDescryptName);
-          for (size_t i = 0; i < otherSubDescrypt.size(); i++)
-            certificate.addSubjectDescription(otherSubDescrypt[i]);
-          certificate.encode();
+      CertificateSubjectDescription subDescryptName("2.5.4.41", subjectName);
+      IdentityCertificate certificate;
+      certificate.setName(certName);
+      certificate.setNotBefore(notBefore);
+      certificate.setNotAfter(notAfter);
+      certificate.setPublicKeyInfo(selfSignedCertificate->getPublicKeyInfo());
+      certificate.addSubjectDescription(subDescryptName);
+      for (size_t i = 0; i < otherSubDescrypt.size(); i++)
+        certificate.addSubjectDescription(otherSubDescrypt[i]);
+      certificate.encode();
 
-          KeyChain keyChain;
+      KeyChain keyChain;
 
-          if(isSelfSigned)
-            keyChain.selfSign(certificate);
-          else
-            {
-              Name signingCertificateName = keyChain.getDefaultCertificateNameForIdentity(Name(signId));
-              
-              keyChain.sign(certificate, signingCertificateName);
-            }
-          wire = certificate.wireEncode();
-        }
-      catch(SecPublicInfo::Error& e)
+      if (isSelfSigned)
+        keyChain.selfSign(certificate);
+      else
         {
-          std::cerr << "ERROR: " << e.what() << std::endl;
-          return 1;
+          Name signingCertificateName =
+            keyChain.getDefaultCertificateNameForIdentity(Name(signId));
+
+          keyChain.sign(certificate, signingCertificateName);
         }
-      catch(SecTpm::Error& e)
-        {
-          std::cerr << "ERROR: " << e.what() << std::endl;
-          return 1;
-        }
+      wire = certificate.wireEncode();
     }
   else
     {
@@ -230,25 +218,13 @@
       // revocationCert.setContent(void*, 0); // empty content
       revocationCert.setName(certName);
 
-      try
-        {
-          KeyChain keyChain;
-          
-          Name signingCertificateName = keyChain.getDefaultCertificateNameForIdentity(Name(signId));
-          
-          keyChain.sign (revocationCert, signingCertificateName);
-          wire = revocationCert.wireEncode();
-        }
-      catch(SecPublicInfo::Error& e)
-        {
-          std::cerr << "ERROR: " << e.what() << std::endl;
-          return 1;
-        }
-      catch(SecTpm::Error& e)
-        {
-          std::cerr << "ERROR: " << e.what() << std::endl;
-          return 1;
-        }
+      KeyChain keyChain;
+
+      Name signingCertificateName =
+        keyChain.getDefaultCertificateNameForIdentity(signId);
+
+      keyChain.sign(revocationCert, signingCertificateName);
+      wire = revocationCert.wireEncode();
     }
 
   try
@@ -257,7 +233,7 @@
       StringSource ss(wire.wire(), wire.size(), true,
                       new Base64Encoder(new FileSink(std::cout), true, 64));
     }
-  catch(CryptoPP::Exception& e)
+  catch (const CryptoPP::Exception& e)
     {
       std::cerr << "ERROR: " << e.what() << std::endl;
       return 1;