security: Reorganizing source code to prepare for support of two version of NDN certificates

This commit also removes unused ndn_digestSha256 function and deprecates
crypto::sha256 in favor of crypto::computeSha256Digest in util/crypto.hpp.

Change-Id: I24ee50ff073a96b868633bdf2cfade412d3605f3
Refs: #3098
diff --git a/tools/ndnsec/cert-revoke.hpp b/tools/ndnsec/cert-revoke.hpp
index c2b20c8..6aa8d02 100644
--- a/tools/ndnsec/cert-revoke.hpp
+++ b/tools/ndnsec/cert-revoke.hpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2013-2015 Regents of the University of California.
+ * Copyright (c) 2013-2016 Regents of the University of California.
  *
  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
  *
@@ -30,6 +30,7 @@
 ndnsec_cert_revoke(int argc, char** argv)
 {
   using namespace ndn;
+  using namespace ndn::security;
   namespace po = boost::program_options;
 
   KeyChain keyChain;
@@ -56,136 +57,118 @@
   p.add("request", 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;
-      return 1;
-    }
+  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;
+    return 1;
+  }
 
-  if (vm.count("help") != 0)
-    {
-      std::cerr << description << std::endl;
-      return 0;
-    }
+  if (vm.count("help") != 0) {
+    std::cerr << description << std::endl;
+    return 0;
+  }
 
   hasSignId = (vm.count("sign-id") != 0);
 
-  if (vm.count("request") == 0)
-    {
-      std::cerr << "request file must be specified" << std::endl;
-      return 1;
-    }
+  if (vm.count("request") == 0) {
+    std::cerr << "request file must be specified" << std::endl;
+    return 1;
+  }
 
-  shared_ptr<IdentityCertificate> revokedCertificate
-    = getIdentityCertificate(requestFile);
+  shared_ptr<v1::IdentityCertificate> revokedCertificate = getIdentityCertificate(requestFile);
 
-  if (!static_cast<bool>(revokedCertificate))
-    {
-      std::cerr << "ERROR: input error" << std::endl;
-      return 1;
-    }
+  if (!static_cast<bool>(revokedCertificate)) {
+    std::cerr << "ERROR: input error" << std::endl;
+    return 1;
+  }
 
   Block wire;
 
-  try
-    {
-      Name keyName;
+  try {
+    Name keyName;
 
-      if (hasSignId) {
-        keyName = keyChain.getDefaultKeyNameForIdentity(signId);
-      }
-      else {
-        const Signature& signature = revokedCertificate->getSignature();
-        if (!signature.hasKeyLocator() ||
-            signature.getKeyLocator().getType() != KeyLocator::KeyLocator_Name)
-          {
-            std::cerr << "ERROR: Invalid certificate to revoke" << std::endl;
-            return 1;
-          }
-
-        keyName = IdentityCertificate::certificateNameToPublicKeyName(
-                    signature.getKeyLocator().getName());
-      }
-
-      Name certName;
-      if (certPrefix == KeyChain::DEFAULT_PREFIX) {
-        certName = revokedCertificate->getName().getPrefix(-1);
-      }
-      else {
-        Name revokedKeyName = revokedCertificate->getPublicKeyName();
-
-        if (certPrefix.isPrefixOf(revokedKeyName) && certPrefix != revokedKeyName) {
-          certName.append(certPrefix)
-            .append("KEY")
-            .append(revokedKeyName.getSubName(certPrefix.size()))
-            .append("ID-CERT");
-        }
-        else {
-          std::cerr << "ERROR: certificate prefix does not match the revoked certificate"
-                    << std::endl;
-          return 1;
-        }
-      }
-      certName
-        .appendVersion()
-        .append("REVOKED");
-
-      Data revocationCert;
-      revocationCert.setName(certName);
-
-      if (keyChain.doesPublicKeyExist(keyName))
+    if (hasSignId) {
+      keyName = keyChain.getDefaultKeyNameForIdentity(signId);
+    }
+    else {
+      const Signature& signature = revokedCertificate->getSignature();
+      if (!signature.hasKeyLocator() ||
+          signature.getKeyLocator().getType() != KeyLocator::KeyLocator_Name)
         {
-          Name signingCertificateName = keyChain.getDefaultCertificateNameForKey(keyName);
-          keyChain.sign(revocationCert,
-                        security::SigningInfo(security::SigningInfo::SIGNER_TYPE_CERT,
-                                              signingCertificateName));
-        }
-      else
-        {
-          std::cerr << "ERROR: Cannot find the signing key!" << std::endl;
+          std::cerr << "ERROR: Invalid certificate to revoke" << std::endl;
           return 1;
         }
 
-      wire = revocationCert.wireEncode();
+      keyName = v1::IdentityCertificate::certificateNameToPublicKeyName(
+                  signature.getKeyLocator().getName());
     }
-  catch (Signature::Error& e)
-    {
-      std::cerr << "ERROR: No valid signature!" << std::endl;
-      return 1;
+
+    Name certName;
+    if (certPrefix == KeyChain::DEFAULT_PREFIX) {
+      certName = revokedCertificate->getName().getPrefix(-1);
     }
-  catch (KeyLocator::Error& e)
-    {
-      std::cerr << "ERROR: No valid KeyLocator!" << std::endl;
-      return 1;
+    else {
+      Name revokedKeyName = revokedCertificate->getPublicKeyName();
+
+      if (certPrefix.isPrefixOf(revokedKeyName) && certPrefix != revokedKeyName) {
+        certName.append(certPrefix)
+          .append("KEY")
+          .append(revokedKeyName.getSubName(certPrefix.size()))
+          .append("ID-CERT");
+      }
+      else {
+        std::cerr << "ERROR: certificate prefix does not match the revoked certificate"
+                  << std::endl;
+        return 1;
+      }
     }
-  catch (IdentityCertificate::Error& e)
-    {
-      std::cerr << "ERROR: Cannot determine the signing key!" << std::endl;
-      return 1;
+    certName
+      .appendVersion()
+      .append("REVOKED");
+
+    Data revocationCert;
+    revocationCert.setName(certName);
+
+    if (keyChain.doesPublicKeyExist(keyName)) {
+      Name signingCertificateName = keyChain.getDefaultCertificateNameForKey(keyName);
+      keyChain.sign(revocationCert,
+                    SigningInfo(SigningInfo::SIGNER_TYPE_CERT, signingCertificateName));
     }
-  catch (SecPublicInfo::Error& e)
-    {
-      std::cerr << "ERROR: Incomplete or corrupted PIB (" << e.what() << ")" << std::endl;
+    else {
+      std::cerr << "ERROR: Cannot find the signing key!" << std::endl;
       return 1;
     }
 
-  try
-    {
-      using namespace CryptoPP;
-      StringSource ss(wire.wire(), wire.size(), true,
-                      new Base64Encoder(new FileSink(std::cout), true, 64));
+    wire = revocationCert.wireEncode();
+  }
+  catch (const Signature::Error& e) {
+    std::cerr << "ERROR: No valid signature!" << std::endl;
+    return 1;
+  }
+  catch (const KeyLocator::Error& e) {
+    std::cerr << "ERROR: No valid KeyLocator!" << std::endl;
+    return 1;
+  }
+  catch (const v1::IdentityCertificate::Error& e) {
+    std::cerr << "ERROR: Cannot determine the signing key!" << std::endl;
+    return 1;
+  }
+  catch (const SecPublicInfo::Error& e) {
+    std::cerr << "ERROR: Incomplete or corrupted PIB (" << e.what() << ")" << std::endl;
+    return 1;
+  }
+
+  try {
+    transform::bufferSource(wire.wire(), wire.size()) >> transform::base64Encode(true) >> transform::streamSink(std::cout);
     }
-  catch (const CryptoPP::Exception& e)
-    {
-      std::cerr << "ERROR: " << e.what() << std::endl;
-      return 1;
-    }
+  catch (const transform::Error& e) {
+    std::cerr << "ERROR: " << e.what() << std::endl;
+    return 1;
+  }
 
   return 0;
 }