security: Canonize PIB and TPM scheme names

Change-Id: I5e81b177a1047e1ede38c9f424ae34e78f7cf001
Refs: #2391
diff --git a/src/security/key-chain.cpp b/src/security/key-chain.cpp
index 4c7d08f..3a1cbda 100644
--- a/src/security/key-chain.cpp
+++ b/src/security/key-chain.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2013-2014 Regents of the University of California.
+ * Copyright (c) 2013-2015 Regents of the University of California.
  *
  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
  *
@@ -62,35 +62,52 @@
 
 NDN_CXX_KEYCHAIN_REGISTER_TPM(SecTpmFile, "tpm-file", "file");
 
-static std::map<std::string, KeyChain::PibCreateFunc>&
+template<class T>
+struct Factory
+{
+  Factory(const std::string& canonicalName, const T& create)
+    : canonicalName(canonicalName)
+    , create(create)
+  {
+  }
+
+  std::string canonicalName;
+  T create;
+};
+typedef Factory<KeyChain::PibCreateFunc> PibFactory;
+typedef Factory<KeyChain::TpmCreateFunc> TpmFactory;
+
+static std::map<std::string, PibFactory>&
 getPibFactories()
 {
-  static std::map<std::string, KeyChain::PibCreateFunc> pibFactories;
+  static std::map<std::string, PibFactory> pibFactories;
   return pibFactories;
 }
 
-static std::map<std::string, KeyChain::TpmCreateFunc>&
+static std::map<std::string, TpmFactory>&
 getTpmFactories()
 {
-  static std::map<std::string, KeyChain::TpmCreateFunc> tpmFactories;
+  static std::map<std::string, TpmFactory> tpmFactories;
   return tpmFactories;
 }
 
 void
-KeyChain::registerPibImpl(std::initializer_list<std::string> schemes,
+KeyChain::registerPibImpl(const std::string& canonicalName,
+                          std::initializer_list<std::string> aliases,
                           KeyChain::PibCreateFunc createFunc)
 {
-  for (const std::string& scheme : schemes) {
-    getPibFactories()[scheme] = createFunc;
+  for (const std::string& alias : aliases) {
+    getPibFactories().insert(make_pair(alias, PibFactory(canonicalName, createFunc)));
   }
 }
 
 void
-KeyChain::registerTpmImpl(std::initializer_list<std::string> schemes,
+KeyChain::registerTpmImpl(const std::string& canonicalName,
+                          std::initializer_list<std::string> aliases,
                           KeyChain::TpmCreateFunc createFunc)
 {
-  for (const std::string& scheme : schemes) {
-    getTpmFactories()[scheme] = createFunc;
+  for (const std::string& alias : aliases) {
+    getTpmFactories().insert(make_pair(alias, TpmFactory(canonicalName, createFunc)));
   }
 }
 
@@ -157,6 +174,7 @@
   if (pibFactory == getPibFactories().end()) {
     throw Error("PIB scheme '" + pibScheme + "' is not supported");
   }
+  pibScheme = pibFactory->second.canonicalName;
 
   if (tpmScheme.empty()) {
     tpmScheme = DEFAULT_TPM_SCHEME;
@@ -165,9 +183,10 @@
   if (tpmFactory == getTpmFactories().end()) {
     throw Error("TPM scheme '" + tpmScheme + "' is not supported");
   }
+  tpmScheme = tpmFactory->second.canonicalName;
 
   // Create PIB
-  m_pib = pibFactory->second(pibLocation);
+  m_pib = pibFactory->second.create(pibLocation);
 
   std::string actualTpmLocator = tpmScheme + ":" + tpmLocation;
 
@@ -187,7 +206,7 @@
   // wrong one or if the PIB was shared by more than one TPMs before.  This is due to the
   // old PIB does not have TPM info, new pib should not have this problem.
 
-  m_tpm = tpmFactory->second(tpmLocation);
+  m_tpm = tpmFactory->second.create(tpmLocation);
   m_pib->setTpmLocator(actualTpmLocator);
 }
 
diff --git a/src/security/key-chain.hpp b/src/security/key-chain.hpp
index cc45dc3..bdf3514 100644
--- a/src/security/key-chain.hpp
+++ b/src/security/key-chain.hpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2013-2014 Regents of the University of California.
+ * Copyright (c) 2013-2015 Regents of the University of California.
  *
  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
  *
@@ -72,19 +72,21 @@
 
   /**
    * @brief Register a new PIB
-   * @param schemes List of scheme with which this PIB will be associated
+   * @param aliases List of schemes with which this PIB will be associated.
+   *        The first alias in the list is considered a canonical name of the PIB instance.
    */
   template<class PibType>
   static void
-  registerPib(std::initializer_list<std::string> schemes);
+  registerPib(std::initializer_list<std::string> aliases);
 
   /**
    * @brief Register a new TPM
-   * @param schemes List of scheme with which this TPM will be associated
+   * @param aliases List of schemes with which this TPM will be associated
+   *        The first alias in the list is considered a canonical name of the TPM instance.
    */
   template<class TpmType>
   static void
-  registerTpm(std::initializer_list<std::string> schemes);
+  registerTpm(std::initializer_list<std::string> aliases);
 
   /**
    * @brief Constructor to create KeyChain with default PIB and TPM
@@ -743,10 +745,12 @@
                     const Name& keyName, DigestAlgorithm digestAlgorithm);
 
   static void
-  registerPibImpl(std::initializer_list<std::string> schemes, PibCreateFunc createFunc);
+  registerPibImpl(const std::string& canonicalName,
+                  std::initializer_list<std::string> aliases, PibCreateFunc createFunc);
 
   static void
-  registerTpmImpl(std::initializer_list<std::string> schemes, TpmCreateFunc createFunc);
+  registerTpmImpl(const std::string& canonicalName,
+                  std::initializer_list<std::string> aliases, TpmCreateFunc createFunc);
 
 public:
   static const Name DEFAULT_PREFIX;
@@ -819,18 +823,18 @@
 
 template<class PibType>
 inline void
-KeyChain::registerPib(std::initializer_list<std::string> schemes)
+KeyChain::registerPib(std::initializer_list<std::string> aliases)
 {
-  registerPibImpl(schemes, [] (const std::string& locator) {
+  registerPibImpl(*aliases.begin(), aliases, [] (const std::string& locator) {
       return unique_ptr<SecPublicInfo>(new PibType(locator));
     });
 }
 
 template<class TpmType>
 inline void
-KeyChain::registerTpm(std::initializer_list<std::string> schemes)
+KeyChain::registerTpm(std::initializer_list<std::string> aliases)
 {
-  registerTpmImpl(schemes, [] (const std::string& locator) {
+  registerTpmImpl(*aliases.begin(), aliases, [] (const std::string& locator) {
       return unique_ptr<SecTpm>(new TpmType(locator));
     });
 }
@@ -841,7 +845,7 @@
  * This macro should be placed once in the implementation file of the
  * SecPib type within the namespace where the type is declared.
  */
-#define NDN_CXX_KEYCHAIN_REGISTER_PIB(PibType, ...)  \
+#define NDN_CXX_KEYCHAIN_REGISTER_PIB(PibType, ...)     \
 static class NdnCxxAuto ## PibType ## PibRegistrationClass    \
 {                                                             \
 public:                                                       \
@@ -857,7 +861,7 @@
  * This macro should be placed once in the implementation file of the
  * SecTpm type within the namespace where the type is declared.
  */
-#define NDN_CXX_KEYCHAIN_REGISTER_TPM(TpmType, ...)  \
+#define NDN_CXX_KEYCHAIN_REGISTER_TPM(TpmType, ...)     \
 static class NdnCxxAuto ## TpmType ## TpmRegistrationClass    \
 {                                                             \
 public:                                                       \