security: Bug fixing

1. correct the error message for non-existing public key.
2. rename private key export/import methods to avoid confusion.

Refs: #1527

Change-Id: I3ea8378378a66e1fbec7f0d4f210057c8f9b969b
diff --git a/src/security/key-chain.hpp b/src/security/key-chain.hpp
index 1a76d1a..5143a4a 100644
--- a/src/security/key-chain.hpp
+++ b/src/security/key-chain.hpp
@@ -511,14 +511,14 @@
 
     Name keyName = Info::getDefaultKeyNameForIdentity(identity);
 
-    ConstBufferPtr pkcs8;
+    ConstBufferPtr pkcs5;
     try
       {
-        pkcs8 = Tpm::exportPrivateKeyPkcs8FromTpm(keyName, passwordStr);
+        pkcs5 = Tpm::exportPrivateKeyPkcs5FromTpm(keyName, passwordStr);
       }
     catch (TpmError& e)
       {
-        throw InfoError("Fail to export PKCS8 of private key");
+        throw InfoError("Fail to export PKCS5 of private key");
       }
 
     shared_ptr<IdentityCertificate> cert;
@@ -533,7 +533,7 @@
       }
 
     shared_ptr<SecuredBag> secureBag = make_shared<SecuredBag>(boost::cref(*cert),
-                                                               boost::cref(pkcs8));
+                                                               boost::cref(pkcs5));
 
     return secureBag;
   }
@@ -555,7 +555,7 @@
     Info::addIdentity(identity);
 
     // Add key
-    Tpm::importPrivateKeyPkcs8IntoTpm(keyName,
+    Tpm::importPrivateKeyPkcs5IntoTpm(keyName,
                                       securedBag.getKey()->buf(),
                                       securedBag.getKey()->size(),
                                       passwordStr);
diff --git a/src/security/sec-tpm-file.cpp b/src/security/sec-tpm-file.cpp
index ba3cca8..94ed510 100644
--- a/src/security/sec-tpm-file.cpp
+++ b/src/security/sec-tpm-file.cpp
@@ -149,7 +149,7 @@
   string keyURI = keyName.toUri();
 
   if (!doesKeyExistInTpm(keyName, KEY_CLASS_PUBLIC))
-    throw Error("Public Key already exist");
+    throw Error("Public Key does not exist");
 
   ostringstream os;
   try
@@ -169,7 +169,7 @@
 }
 
 ConstBufferPtr
-SecTpmFile::exportPrivateKeyPkcs1FromTpm(const Name& keyName)
+SecTpmFile::exportPrivateKeyPkcs8FromTpm(const Name& keyName)
 {
   OBufferStream privateKeyOs;
   CryptoPP::FileSource(m_impl->nameTransform(keyName.toUri(), ".pri").string().c_str(), true,
@@ -179,7 +179,7 @@
 }
 
 bool
-SecTpmFile::importPrivateKeyPkcs1IntoTpm(const Name& keyName, const uint8_t* buf, size_t size)
+SecTpmFile::importPrivateKeyPkcs8IntoTpm(const Name& keyName, const uint8_t* buf, size_t size)
 {
   try
     {
diff --git a/src/security/sec-tpm-file.hpp b/src/security/sec-tpm-file.hpp
index 8cc20df..4dbbf79 100644
--- a/src/security/sec-tpm-file.hpp
+++ b/src/security/sec-tpm-file.hpp
@@ -107,10 +107,10 @@
    * From TrustedPlatformModule *
    ******************************/
   virtual ConstBufferPtr
-  exportPrivateKeyPkcs1FromTpm(const Name& keyName);
+  exportPrivateKeyPkcs8FromTpm(const Name& keyName);
 
   virtual bool
-  importPrivateKeyPkcs1IntoTpm(const Name& keyName, const uint8_t* buf, size_t size);
+  importPrivateKeyPkcs8IntoTpm(const Name& keyName, const uint8_t* buf, size_t size);
 
   virtual bool
   importPublicKeyPkcs1IntoTpm(const Name& keyName, const uint8_t* buf, size_t size);
diff --git a/src/security/sec-tpm-osx.cpp b/src/security/sec-tpm-osx.cpp
index fd21439..84be0e2 100644
--- a/src/security/sec-tpm-osx.cpp
+++ b/src/security/sec-tpm-osx.cpp
@@ -376,7 +376,7 @@
 }
 
 ConstBufferPtr
-SecTpmOsx::exportPrivateKeyPkcs1FromTpmInternal(const Name& keyName, bool needRetry)
+SecTpmOsx::exportPrivateKeyPkcs8FromTpmInternal(const Name& keyName, bool needRetry)
 {
   using namespace CryptoPP;
 
@@ -393,7 +393,7 @@
       if (res == errSecAuthFailed && !needRetry)
         {
           if (unlockTpm(0, 0, false))
-            return exportPrivateKeyPkcs1FromTpmInternal(keyName, true);
+            return exportPrivateKeyPkcs8FromTpmInternal(keyName, true);
           else
             return shared_ptr<Buffer>();
         }
@@ -438,7 +438,7 @@
 #endif // __GNUC__
 
 bool
-SecTpmOsx::importPrivateKeyPkcs1IntoTpmInternal(const Name& keyName,
+SecTpmOsx::importPrivateKeyPkcs8IntoTpmInternal(const Name& keyName,
                                                 const uint8_t* buf, size_t size,
                                                 bool needRetry)
 {
@@ -506,7 +506,7 @@
       if (res == errSecAuthFailed && !needRetry)
         {
           if (unlockTpm(0, 0, false))
-            return importPrivateKeyPkcs1IntoTpmInternal(keyName, buf, size, true);
+            return importPrivateKeyPkcs8IntoTpmInternal(keyName, buf, size, true);
           else
             return false;
         }
diff --git a/src/security/sec-tpm-osx.hpp b/src/security/sec-tpm-osx.hpp
index 1a5bd1f..d4363c2 100644
--- a/src/security/sec-tpm-osx.hpp
+++ b/src/security/sec-tpm-osx.hpp
@@ -99,15 +99,15 @@
    * From TrustedPlatformModule *
    ******************************/
   virtual ConstBufferPtr
-  exportPrivateKeyPkcs1FromTpm(const Name& keyName)
+  exportPrivateKeyPkcs8FromTpm(const Name& keyName)
   {
-    return exportPrivateKeyPkcs1FromTpmInternal(keyName, false);
+    return exportPrivateKeyPkcs8FromTpmInternal(keyName, false);
   }
 
   virtual bool
-  importPrivateKeyPkcs1IntoTpm(const Name& keyName, const uint8_t* buf, size_t size)
+  importPrivateKeyPkcs8IntoTpm(const Name& keyName, const uint8_t* buf, size_t size)
   {
-    return importPrivateKeyPkcs1IntoTpmInternal(keyName, buf, size, false);
+    return importPrivateKeyPkcs8IntoTpmInternal(keyName, buf, size, false);
   }
 
   virtual bool
@@ -123,10 +123,10 @@
   deleteKeyPairInTpmInternal(const Name& keyName, bool needRetry);
 
   ConstBufferPtr
-  exportPrivateKeyPkcs1FromTpmInternal(const Name& keyName, bool needRetry);
+  exportPrivateKeyPkcs8FromTpmInternal(const Name& keyName, bool needRetry);
 
   bool
-  importPrivateKeyPkcs1IntoTpmInternal(const Name& keyName,
+  importPrivateKeyPkcs8IntoTpmInternal(const Name& keyName,
                                        const uint8_t* buf, size_t size,
                                        bool needRetry);
 
diff --git a/src/security/sec-tpm.cpp b/src/security/sec-tpm.cpp
index d2e4af5..253ab02 100644
--- a/src/security/sec-tpm.cpp
+++ b/src/security/sec-tpm.cpp
@@ -14,7 +14,7 @@
 namespace ndn {
 
 ConstBufferPtr
-SecTpm::exportPrivateKeyPkcs8FromTpm(const Name& keyName, const string& passwordStr)
+SecTpm::exportPrivateKeyPkcs5FromTpm(const Name& keyName, const string& passwordStr)
 {
   using namespace CryptoPP;
 
@@ -47,14 +47,14 @@
   CBC_Mode< DES_EDE3 >::Encryption e;
   e.SetKeyWithIV(derived, derivedLen, iv);
 
-  ConstBufferPtr pkcs1PrivateKey = exportPrivateKeyPkcs1FromTpm(keyName);
-  if (!static_cast<bool>(pkcs1PrivateKey))
+  ConstBufferPtr pkcs8PrivateKey = exportPrivateKeyPkcs8FromTpm(keyName);
+  if (!static_cast<bool>(pkcs8PrivateKey))
     throw Error("Cannot export the private key, #1");
 
   OBufferStream encryptedOs;
   try
     {
-      StringSource stringSource(pkcs1PrivateKey->buf(), pkcs1PrivateKey->size(), true,
+      StringSource stringSource(pkcs8PrivateKey->buf(), pkcs8PrivateKey->size(), true,
                                 new StreamTransformationFilter(e, new FileSink(encryptedOs)));
     }
   catch (CryptoPP::Exception& e)
@@ -136,7 +136,7 @@
 }
 
 bool
-SecTpm::importPrivateKeyPkcs8IntoTpm(const Name& keyName,
+SecTpm::importPrivateKeyPkcs5IntoTpm(const Name& keyName,
                                      const uint8_t* buf, size_t size,
                                      const string& passwordStr)
 {
@@ -249,7 +249,7 @@
       return false;
     }
 
-  if (!importPrivateKeyPkcs1IntoTpm(keyName,
+  if (!importPrivateKeyPkcs8IntoTpm(keyName,
                                     privateKeyOs.buf()->buf(), privateKeyOs.buf()->size()))
     return false;
 
diff --git a/src/security/sec-tpm.hpp b/src/security/sec-tpm.hpp
index 14b5756..395cec9 100644
--- a/src/security/sec-tpm.hpp
+++ b/src/security/sec-tpm.hpp
@@ -208,7 +208,7 @@
   addAppToACL(const Name& keyName, KeyClass keyClass, const std::string& appPath, AclType acl) = 0;
 
   /**
-   * @brief Export a private key in PKCS#8 format.
+   * @brief Export a private key in PKCS#5 format.
    *
    * @param keyName The private key name.
    * @param password The password to encrypt the private key.
@@ -216,10 +216,10 @@
    * @throws SecTpm::Error if private key cannot be exported.
    */
   ConstBufferPtr
-  exportPrivateKeyPkcs8FromTpm(const Name& keyName, const std::string& password);
+  exportPrivateKeyPkcs5FromTpm(const Name& keyName, const std::string& password);
 
   /**
-   * @brief Import a private key in PKCS#8 format.
+   * @brief Import a private key in PKCS#5 format.
    *
    * Also recover the public key and installed it in TPM.
    *
@@ -229,29 +229,29 @@
    * @return False if import fails.
    */
   bool
-  importPrivateKeyPkcs8IntoTpm(const Name& keyName,
+  importPrivateKeyPkcs5IntoTpm(const Name& keyName,
                                const uint8_t* buf, size_t size,
                                const std::string& password);
 
 protected:
   /**
-   * @brief Export a private key in PKCS#1 format.
+   * @brief Export a private key in PKCS#8 format.
    *
    * @param keyName The private key name.
-   * @return The private key info (in PKCS#1 format) if exist, otherwise a NULL pointer.
+   * @return The private key info (in PKCS#8 format) if exist, otherwise a NULL pointer.
    */
   virtual ConstBufferPtr
-  exportPrivateKeyPkcs1FromTpm(const Name& keyName) = 0;
+  exportPrivateKeyPkcs8FromTpm(const Name& keyName) = 0;
 
   /**
-   * @brief Import a private key in PKCS#1 format.
+   * @brief Import a private key in PKCS#8 format.
    *
    * @param keyName The private key name.
    * @param key The encoded private key info.
    * @return False if import fails.
    */
   virtual bool
-  importPrivateKeyPkcs1IntoTpm(const Name& keyName, const uint8_t* buf, size_t size) = 0;
+  importPrivateKeyPkcs8IntoTpm(const Name& keyName, const uint8_t* buf, size_t size) = 0;
 
   /**
    * @brief Import a public key in PKCS#1 format.