forked from tsdgeos/poppler_mirror
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCryptoSignBackend.h
129 lines (111 loc) · 4.46 KB
/
CryptoSignBackend.h
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
118
119
120
121
122
123
124
125
126
127
128
129
//========================================================================
//
// CryptoSignBackend.h
//
// This file is licensed under the GPLv2 or later
//
// Copyright 2023, 2024 g10 Code GmbH, Author: Sune Stolborg Vuorela <[email protected]>
//========================================================================
#ifndef SIGNATUREBACKEND_H
#define SIGNATUREBACKEND_H
#include <vector>
#include <memory>
#include <chrono>
#include <variant>
#include <functional>
#include <optional>
#include "HashAlgorithm.h"
#include "CertificateInfo.h"
#include "SignatureInfo.h"
#include "poppler_private_export.h"
namespace CryptoSign {
enum class SignatureType
{
adbe_pkcs7_sha1,
adbe_pkcs7_detached,
ETSI_CAdES_detached,
unknown_signature_type,
unsigned_signature_field
};
SignatureType signatureTypeFromString(std::string_view data);
std::string toStdString(SignatureType type);
// experiments seems to say that this is a bit above
// what we have seen in the wild, and much larger than
// what we have managed to get nss and gpgme to create.
static const int maxSupportedSignatureSize = 10000;
enum class SigningError
{
GenericError /** Unclassified error*/,
InternalError /** Some sort of internal error. This is likely coming from an actual bug in the code*/,
WriteFailed /**Some sort of IO error, missing write permissions or ...*/,
UserCancelled /**User cancelled the action*/,
KeyMissing, /**The key/certificate not specified*/
};
// Classes to help manage signature backends
class VerificationInterface
{
public:
virtual void addData(unsigned char *data_block, int data_len) = 0;
virtual SignatureValidationStatus validateSignature() = 0;
virtual std::chrono::system_clock::time_point getSigningTime() const = 0;
virtual std::string getSignerName() const = 0;
virtual std::string getSignerSubjectDN() const = 0;
virtual HashAlgorithm getHashAlgorithm() const = 0;
// Blocking if doneCallback to validateCertificateAsync has not yet been called
virtual CertificateValidationStatus validateCertificateResult() = 0;
virtual void validateCertificateAsync(std::chrono::system_clock::time_point validation_time, bool ocspRevocationCheck, bool useAIACertFetch, const std::function<void()> &doneCallback) = 0;
virtual std::unique_ptr<X509CertificateInfo> getCertificateInfo() const = 0;
virtual ~VerificationInterface();
VerificationInterface() = default;
VerificationInterface(const VerificationInterface &other) = delete;
VerificationInterface &operator=(const VerificationInterface &other) = delete;
};
class SigningInterface
{
public:
virtual void addData(unsigned char *data_block, int data_len) = 0;
virtual SignatureType signatureType() const = 0;
virtual std::unique_ptr<X509CertificateInfo> getCertificateInfo() const = 0;
virtual std::variant<std::vector<unsigned char>, SigningError> signDetached(const std::string &password) = 0;
virtual ~SigningInterface();
SigningInterface() = default;
SigningInterface(const SigningInterface &other) = delete;
SigningInterface &operator=(const SigningInterface &other) = delete;
};
class Backend
{
public:
enum class Type
{
NSS3,
GPGME
};
virtual std::unique_ptr<VerificationInterface> createVerificationHandler(std::vector<unsigned char> &&pkcs7, SignatureType type) = 0;
virtual std::unique_ptr<SigningInterface> createSigningHandler(const std::string &certID, HashAlgorithm digestAlgTag) = 0;
virtual std::vector<std::unique_ptr<X509CertificateInfo>> getAvailableSigningCertificates() = 0;
virtual ~Backend();
Backend() = default;
Backend(const Backend &other) = delete;
Backend &operator=(const Backend &other) = delete;
};
class POPPLER_PRIVATE_EXPORT Factory
{
public:
// Sets the user preferred backend
static void setPreferredBackend(Backend::Type backend);
// Gets the current active backend
// prioritized from 1) setPreferredBackend,
// 2) POPPLER_SIGNATURE_BACKEND
// 3) Compiled in default
static std::optional<Backend::Type> getActive();
static std::vector<Backend::Type> getAvailable();
static std::unique_ptr<Backend> createActive();
static std::unique_ptr<Backend> create(Backend::Type);
static std::optional<Backend::Type> typeFromString(std::string_view string);
Factory() = delete;
/// backend specific settings
private:
static std::optional<Backend::Type> preferredBackend;
};
}
#endif // SIGNATUREBACKEND_H