security: Add new TPM framework
The TPM framework is separated into two parts:
1) The front end class Tpm provides the interface to KeyChain. The public
interface of Tpm is read-only.
2) The back end classes represent concrete implementations, such as
tpm::BackEndFile and tpm::BackEndOsx which may also provide
implementation-specific management interfaces.
New TPM supports different key id type when generating new key. The
default type is changed to 64-bit random number.
Change-Id: I41154c2ded4b65fb0bef2f4a0d2c5b77843be05d
Refs: #2948
diff --git a/src/security/tpm/back-end.hpp b/src/security/tpm/back-end.hpp
new file mode 100644
index 0000000..749b53d
--- /dev/null
+++ b/src/security/tpm/back-end.hpp
@@ -0,0 +1,182 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2013-2016 Regents of the University of California.
+ *
+ * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
+ *
+ * ndn-cxx library is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later version.
+ *
+ * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
+ *
+ * You should have received copies of the GNU General Public License and GNU Lesser
+ * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
+ */
+
+#ifndef NDN_SECURITY_TPM_BACK_END_HPP
+#define NDN_SECURITY_TPM_BACK_END_HPP
+
+#include "../../common.hpp"
+#include "../../name.hpp"
+#include "../../encoding/buffer.hpp"
+#include "../key-params.hpp"
+
+namespace ndn {
+namespace security {
+namespace tpm {
+
+class KeyHandle;
+
+/**
+ * @brief Abstraction of Tpm back-end.
+ *
+ * This class provides KeyHandle to the front-end and other TPM management operations.
+ */
+class BackEnd : noncopyable
+{
+public:
+ class Error : public std::runtime_error
+ {
+ public:
+ explicit
+ Error(const std::string& what)
+ : std::runtime_error(what)
+ {
+ }
+ };
+
+public:
+ virtual
+ ~BackEnd();
+
+public: // key management
+ /**
+ * @return True if a key with name @p keyName exists in TPM.
+ */
+ bool
+ hasKey(const Name& keyName) const;
+
+ /**
+ * @return The handle of a key with name @p keyName, or nullptr if the key does not exist.
+ *
+ * Calling getKeyHandle multiple times with the same keyName will return different KeyHandle
+ * objects that all refer to the same key.
+ */
+ unique_ptr<KeyHandle>
+ getKeyHandle(const Name& keyName) const;
+
+ /**
+ * @brief Create key for @p identity according to @p params.
+ *
+ * The key name is set in the returned KeyHandle.
+ *
+ * @return The handle of the created key.
+ * @throws Tpm::Error if @p params is invalid
+ * @throws Error if the key cannot be created.
+ */
+ unique_ptr<KeyHandle>
+ createKey(const Name& identity, const KeyParams& params);
+
+ /**
+ * @brief Delete a key with name @p keyName.
+ *
+ * Continuing to use existing KeyHandles on a deleted key results in undefined behavior.
+ *
+ * @throws Error if the deletion fails.
+ */
+ void
+ deleteKey(const Name& keyName);
+
+ /**
+ * @return A private key with name @p keyName in encrypted PKCS #8 format using password @p pw
+ * @throws Error if the key does not exist
+ * @throws Error if the key cannot be exported, e.g., insufficient privilege
+ */
+ ConstBufferPtr
+ exportKey(const Name& keyName, const char* pw, size_t pwLen);
+
+ /**
+ * @brief Import a private key in encrypted PKCS #8 format
+ *
+ * @param keyName The name of imported private key
+ * @param pkcs8 Pointer to the key in encrypted PKCS #8 format
+ * @param pkcs8Len The size of the key in encrypted PKCS #8 format
+ * @param pw The password to decrypt the private key
+ * @param pwLen The length of the password
+ * @throws Error if import fails.
+ */
+ void
+ importKey(const Name& keyName, const uint8_t* pkcs8, size_t pkcs8Len, const char* pw, size_t pwLen);
+
+protected: // static helper method
+ /**
+ * @brief Set the key name in @p keyHandle according to @p identity and @p params
+ */
+ static void
+ setKeyName(KeyHandle& keyHandle, const Name& identity, const KeyParams& params);
+
+private: // pure virtual methods
+ /**
+ * @return True if a key with name @p keyName exists in TPM.
+ */
+ virtual bool
+ doHasKey(const Name& keyName) const = 0;
+
+ /**
+ * @return The handle of a key with name @p keyName, or nullptr if the key does not exist
+ */
+ virtual unique_ptr<KeyHandle>
+ doGetKeyHandle(const Name& keyName) const = 0;
+
+ /**
+ * @brief Create key for @p identityName according to @p params.
+ *
+ * The created key is named as: /<identityName>/[keyId]/KEY
+ * The key name is set in the returned KeyHandle.
+ *
+ * @return The handle of the created key.
+ * @throws Error when key cannot be created.
+ */
+ virtual unique_ptr<KeyHandle>
+ doCreateKey(const Name& identity, const KeyParams& params) = 0;
+
+ /**
+ * @brief Delete a key with name @p keyName.
+ *
+ * @throws Error if the deletion fails.
+ */
+ virtual void
+ doDeleteKey(const Name& keyName) = 0;
+
+ /**
+ * @return A private key with name @p keyName in encrypted PKCS #8 format using password @p pw
+ * @throws Error if the key cannot be exported, e.g., insufficient privilege
+ */
+ virtual ConstBufferPtr
+ doExportKey(const Name& keyName, const char* pw, size_t pwLen) = 0;
+
+ /**
+ * @brief Import a private key in encrypted PKCS #8 format using @p password
+ *
+ * @param keyName The name of imported private key
+ * @param pkcs8 Pointer to the key in PKCS #8 format
+ * @param pkcs8Len The size of the key in PKCS #8 format
+ * @param pw The password to decrypt the private key
+ * @param pwLen The length of the password
+ * @throws Error if import fails.
+ */
+ virtual void
+ doImportKey(const Name& keyName, const uint8_t* pkcs8, size_t pkcs8Len, const char* pw, size_t pwLen) = 0;
+};
+
+} // namespace tpm
+} // namespace security
+} // namespace ndn
+
+#endif // NDN_SECURITY_TPM_BACK_END_HPP