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