security: fix error handling in KeyChain::importSafeBag()
Tpm::importPrivateKey() now throws on error instead of
returning false, for consistency with similar functions.
Change-Id: Id07c2be3809e32d1779c0b5977232e4728528e3c
Refs: #4359
diff --git a/src/security/tpm/tpm.cpp b/src/security/tpm/tpm.cpp
index 0ed1062..d72440b 100644
--- a/src/security/tpm/tpm.cpp
+++ b/src/security/tpm/tpm.cpp
@@ -1,5 +1,5 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
+/*
* Copyright (c) 2013-2017 Regents of the University of California.
*
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
@@ -138,36 +138,27 @@
return m_backEnd->exportKey(keyName, pw, pwLen);
}
-bool
+void
Tpm::importPrivateKey(const Name& keyName, const uint8_t* pkcs8, size_t pkcs8Len,
const char* pw, size_t pwLen)
{
- try {
- m_backEnd->importKey(keyName, pkcs8, pkcs8Len, pw, pwLen);
- }
- catch (const BackEnd::Error&) {
- return false;
- }
- return true;
+ m_backEnd->importKey(keyName, pkcs8, pkcs8Len, pw, pwLen);
}
const KeyHandle*
Tpm::findKey(const Name& keyName) const
{
auto it = m_keys.find(keyName);
-
if (it != m_keys.end())
return it->second.get();
- unique_ptr<KeyHandle> handle = m_backEnd->getKeyHandle(keyName);
+ auto handle = m_backEnd->getKeyHandle(keyName);
+ if (handle == nullptr)
+ return nullptr;
- if (handle != nullptr) {
- KeyHandle* key = handle.get();
- m_keys[keyName] = std::move(handle);
- return key;
- }
-
- return nullptr;
+ const KeyHandle* key = handle.get();
+ m_keys[keyName] = std::move(handle);
+ return key;
}
} // namespace tpm
diff --git a/src/security/tpm/tpm.hpp b/src/security/tpm/tpm.hpp
index dde9d14..4ce57f2 100644
--- a/src/security/tpm/tpm.hpp
+++ b/src/security/tpm/tpm.hpp
@@ -1,5 +1,5 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
+/*
* Copyright (c) 2013-2017 Regents of the University of California.
*
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
@@ -22,10 +22,9 @@
#ifndef NDN_SECURITY_TPM_TPM_HPP
#define NDN_SECURITY_TPM_TPM_HPP
-#include "../security-common.hpp"
-#include "../../name.hpp"
-#include "../key-params.hpp"
#include "key-handle.hpp"
+#include "../key-params.hpp"
+#include "../../name.hpp"
#include <unordered_map>
@@ -180,7 +179,7 @@
* @param pw The password to encrypt the private key
* @param pwLen The length of the password
* @return The encoded private key wrapper.
- * @throw BackEnd::Error the key does not exist or it cannot be exported.
+ * @throw BackEnd::Error The key does not exist or it could not be exported.
*/
ConstBufferPtr
exportPrivateKey(const Name& keyName, const char* pw, size_t pwLen) const;
@@ -193,16 +192,16 @@
* @param pkcs8Len The length of the private key wrapper
* @param pw The password to encrypt the private key
* @param pwLen The length of the password
- * @return true if the operation is successful, false otherwise.
+ * @throw BackEnd::Error The key could not be imported.
*/
- bool
+ void
importPrivateKey(const Name& keyName, const uint8_t* pkcs8, size_t pkcs8Len,
const char* pw, size_t pwLen);
/**
* @brief Clear the key cache.
*
- * An empty cache can force Tpm to do key lookup in back-end.
+ * An empty cache can force Tpm to do key lookup in the back-end.
*/
void
clearKeyCache()