Spaces:
Running
Running
File size: 3,060 Bytes
5cee033 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
//========================================================================
//
// CryptoSignBackend.cc
//
// This file is licensed under the GPLv2 or later
//
// Copyright 2023 g10 Code GmbH, Author: Sune Stolborg Vuorela <[email protected]>
//========================================================================
#include "CryptoSignBackend.h"
#include "config.h"
#ifdef ENABLE_GPGME
# include "GPGMECryptoSignBackend.h"
#endif
#ifdef ENABLE_NSS3
# include "NSSCryptoSignBackend.h"
#endif
namespace CryptoSign {
void Factory::setPreferredBackend(CryptoSign::Backend::Type backend)
{
preferredBackend = backend;
}
static std::string_view toStringView(const char *str)
{
if (str) {
return std::string_view(str);
}
return {};
}
std::optional<CryptoSign::Backend::Type> Factory::typeFromString(std::string_view string)
{
if (string.empty()) {
return std::nullopt;
}
if ("GPG" == string) {
return Backend::Type::GPGME;
}
if ("NSS" == string) {
return Backend::Type::NSS3;
}
return std::nullopt;
}
std::optional<CryptoSign::Backend::Type> Factory::getActive()
{
if (preferredBackend) {
return *preferredBackend;
}
static auto backendFromEnvironment = typeFromString(toStringView(getenv("POPPLER_SIGNATURE_BACKEND")));
if (backendFromEnvironment) {
return *backendFromEnvironment;
}
static auto backendFromCompiledDefault = typeFromString(toStringView(DEFAULT_SIGNATURE_BACKEND));
if (backendFromCompiledDefault) {
return *backendFromCompiledDefault;
}
return std::nullopt;
}
static std::vector<Backend::Type> createAvailableBackends()
{
std::vector<Backend::Type> backends;
#ifdef ENABLE_NSS3
backends.push_back(Backend::Type::NSS3);
#endif
#ifdef ENABLE_GPGME
if (GpgSignatureBackend::hasSufficientVersion()) {
backends.push_back(Backend::Type::GPGME);
}
#endif
return backends;
}
std::vector<Backend::Type> Factory::getAvailable()
{
static std::vector<Backend::Type> availableBackends = createAvailableBackends();
return availableBackends;
}
std::unique_ptr<Backend> Factory::createActive()
{
auto active = getActive();
if (active) {
return create(active.value());
}
return nullptr;
}
std::unique_ptr<CryptoSign::Backend> CryptoSign::Factory::create(Backend::Type backend)
{
switch (backend) {
case Backend::Type::NSS3:
#ifdef ENABLE_NSS3
return std::make_unique<NSSCryptoSignBackend>();
#else
return nullptr;
#endif
case Backend::Type::GPGME: {
#ifdef ENABLE_GPGME
return std::make_unique<GpgSignatureBackend>();
#else
return nullptr;
#endif
}
}
return nullptr;
}
/// backend specific settings
// Android build wants some methods out of line in the interfaces
Backend::~Backend() = default;
SigningInterface::~SigningInterface() = default;
VerificationInterface::~VerificationInterface() = default;
std::optional<Backend::Type> Factory::preferredBackend = std::nullopt;
} // namespace Signature;
|