2019-11-04 01:36:53 +00:00
|
|
|
#include <qpdf/QPDFCryptoProvider.hh>
|
2019-11-05 03:13:13 +00:00
|
|
|
#include <qpdf/qpdf-config.h>
|
2019-11-06 02:14:37 +00:00
|
|
|
#include <qpdf/QUtil.hh>
|
2019-11-04 01:36:53 +00:00
|
|
|
#include <stdexcept>
|
|
|
|
|
2019-11-05 03:13:13 +00:00
|
|
|
#ifdef USE_CRYPTO_NATIVE
|
|
|
|
# include <qpdf/QPDFCrypto_native.hh>
|
|
|
|
#endif
|
2019-11-05 19:32:28 +00:00
|
|
|
#ifdef USE_CRYPTO_GNUTLS
|
|
|
|
# include <qpdf/QPDFCrypto_gnutls.hh>
|
|
|
|
#endif
|
2020-04-06 03:19:56 +00:00
|
|
|
#ifdef USE_CRYPTO_OPENSSL
|
|
|
|
# include <qpdf/QPDFCrypto_openssl.hh>
|
|
|
|
#endif
|
2019-11-04 01:36:53 +00:00
|
|
|
|
|
|
|
std::shared_ptr<QPDFCryptoImpl>
|
|
|
|
QPDFCryptoProvider::getImpl()
|
|
|
|
{
|
|
|
|
QPDFCryptoProvider& p = getInstance();
|
|
|
|
if (p.m->default_provider.empty())
|
|
|
|
{
|
|
|
|
throw std::logic_error(
|
|
|
|
"QPDFCryptoProvider::getImpl called with no default provider.");
|
|
|
|
}
|
|
|
|
return p.getImpl_internal(p.m->default_provider);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<QPDFCryptoImpl>
|
|
|
|
QPDFCryptoProvider::getImpl(std::string const& name)
|
|
|
|
{
|
|
|
|
return getInstance().getImpl_internal(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
void
|
|
|
|
QPDFCryptoProvider::registerImpl(std::string const& name)
|
|
|
|
{
|
|
|
|
getInstance().registerImpl_internal<T>(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QPDFCryptoProvider::setDefaultProvider(std::string const& name)
|
|
|
|
{
|
|
|
|
getInstance().setDefaultProvider_internal(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
QPDFCryptoProvider::QPDFCryptoProvider() :
|
|
|
|
m(std::make_shared<Members>())
|
|
|
|
{
|
2019-11-05 03:13:13 +00:00
|
|
|
#ifdef USE_CRYPTO_NATIVE
|
2019-11-04 01:36:53 +00:00
|
|
|
registerImpl_internal<QPDFCrypto_native>("native");
|
2019-11-05 19:32:28 +00:00
|
|
|
#endif
|
|
|
|
#ifdef USE_CRYPTO_GNUTLS
|
|
|
|
registerImpl_internal<QPDFCrypto_gnutls>("gnutls");
|
2020-04-06 03:19:56 +00:00
|
|
|
#endif
|
|
|
|
#ifdef USE_CRYPTO_OPENSSL
|
|
|
|
registerImpl_internal<QPDFCrypto_openssl>("openssl");
|
2019-11-05 03:13:13 +00:00
|
|
|
#endif
|
2019-11-06 02:14:37 +00:00
|
|
|
std::string default_crypto;
|
|
|
|
if (! QUtil::get_env("QPDF_CRYPTO_PROVIDER", &default_crypto))
|
|
|
|
{
|
|
|
|
default_crypto = DEFAULT_CRYPTO;
|
|
|
|
}
|
|
|
|
setDefaultProvider_internal(default_crypto);
|
2019-11-04 01:36:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QPDFCryptoProvider&
|
|
|
|
QPDFCryptoProvider::getInstance()
|
|
|
|
{
|
|
|
|
static QPDFCryptoProvider instance;
|
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<QPDFCryptoImpl>
|
|
|
|
QPDFCryptoProvider::getImpl_internal(std::string const& name) const
|
|
|
|
{
|
|
|
|
auto iter = this->m->providers.find(name);
|
|
|
|
if (iter == this->m->providers.end())
|
|
|
|
{
|
|
|
|
throw std::logic_error(
|
|
|
|
"QPDFCryptoProvider requested unknown implementation \"" +
|
|
|
|
name + "\"");
|
|
|
|
}
|
|
|
|
return this->m->providers[name]();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
void
|
|
|
|
QPDFCryptoProvider::registerImpl_internal(std::string const& name)
|
|
|
|
{
|
|
|
|
this->m->providers[name] = std::make_shared<T>;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QPDFCryptoProvider::setDefaultProvider_internal(std::string const& name)
|
|
|
|
{
|
2019-11-06 02:14:37 +00:00
|
|
|
if (! this->m->providers.count(name))
|
|
|
|
{
|
|
|
|
throw std::logic_error(
|
|
|
|
"QPDFCryptoProvider: request to set default"
|
|
|
|
" provider to unknown implementation \"" +
|
|
|
|
name + "\"");
|
|
|
|
}
|
2019-11-04 01:36:53 +00:00
|
|
|
this->m->default_provider = name;
|
|
|
|
}
|
2019-11-06 02:14:37 +00:00
|
|
|
|
|
|
|
std::set<std::string>
|
|
|
|
QPDFCryptoProvider::getRegisteredImpls()
|
|
|
|
{
|
|
|
|
std::set<std::string> result;
|
|
|
|
QPDFCryptoProvider& p = getInstance();
|
|
|
|
for (auto iter = p.m->providers.begin(); iter != p.m->providers.end();
|
|
|
|
++iter)
|
|
|
|
{
|
|
|
|
result.insert((*iter).first);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
|
|
|
QPDFCryptoProvider::getDefaultProvider()
|
|
|
|
{
|
|
|
|
return getInstance().m->default_provider;
|
|
|
|
}
|