Bug #4359
closedKeyChain::importSafeBag does not properly check for error in Tpm::importPrivateKey
100%
Description
Tpm::importPrivateKey does not thrown an exception. Instead it catches BackEnd::Error and returns false for an error.
https://github.com/named-data/ndn-cxx/blob/794f687e16e9b836d3f3703d5ee0fc84e7895056/src/security/tpm/tpm.cpp#L145
try {
m_backEnd->importKey(keyName, pkcs8, pkcs8Len, pw, pwLen);
}
catch (const BackEnd::Error&) {
return false;
}
return true;
However, when KeyChain::importSafeBag calls importPrivateKey, it checks for an exception instead of checking for a return value of false:
https://github.com/named-data/ndn-cxx/blob/5d0b0106dfc7675f9048976d4dd4ea00e46e6c39/src/security/v2/key-chain.cpp#L387
try {
m_tpm->importPrivateKey(keyName,
safeBag.getEncryptedKeyBag().data(), safeBag.getEncryptedKeyBag().size(),
pw, pwLen);
}
catch (const std::runtime_error&) {
BOOST_THROW_EXCEPTION(Error("Fail to import private key `" + keyName.toUri() + "`"));
}
Therefore, if the TPM back end throws BackEnd::Error, it is ignored by KeyChain::importSafeBag. It should check for a return value of false when calling Tpm::importPrivateKey (or Tpm::importPrivateKey should throw the exception instead of returning false).