1//========================================================================
2//
3// CryptoSignBackend.h
4//
5// This file is licensed under the GPLv2 or later
6//
7// Copyright 2023, 2024 g10 Code GmbH, Author: Sune Stolborg Vuorela <sune@vuorela.dk>
8//========================================================================
9
10#ifndef SIGNATUREBACKEND_H
11#define SIGNATUREBACKEND_H
12
13#include <vector>
14#include <memory>
15#include <chrono>
16#include <optional>
17#include <functional>
18#include "HashAlgorithm.h"
19#include "CertificateInfo.h"
20#include "SignatureInfo.h"
21#include "goo/GooString.h"
22#include "poppler_private_export.h"
23
24namespace CryptoSign {
25
26// experiments seems to say that this is a bit above
27// what we have seen in the wild, and much larger than
28// what we have managed to get nss and gpgme to create.
29static const int maxSupportedSignatureSize = 10000;
30
31// Classes to help manage signature backends
32
33class VerificationInterface
34{
35public:
36 virtual void addData(unsigned char *data_block, int data_len) = 0;
37 virtual SignatureValidationStatus validateSignature() = 0;
38 virtual std::chrono::system_clock::time_point getSigningTime() const = 0;
39 virtual std::string getSignerName() const = 0;
40 virtual std::string getSignerSubjectDN() const = 0;
41 virtual HashAlgorithm getHashAlgorithm() const = 0;
42
43 // Blocking if doneCallback to validateCertificateAsync has not yet been called
44 virtual CertificateValidationStatus validateCertificateResult() = 0;
45 virtual void validateCertificateAsync(std::chrono::system_clock::time_point validation_time, bool ocspRevocationCheck, bool useAIACertFetch, const std::function<void()> &doneCallback) = 0;
46 virtual std::unique_ptr<X509CertificateInfo> getCertificateInfo() const = 0;
47 virtual ~VerificationInterface();
48 VerificationInterface() = default;
49 VerificationInterface(const VerificationInterface &other) = delete;
50 VerificationInterface &operator=(const VerificationInterface &other) = delete;
51};
52
53class SigningInterface
54{
55public:
56 virtual void addData(unsigned char *data_block, int data_len) = 0;
57 virtual std::unique_ptr<X509CertificateInfo> getCertificateInfo() const = 0;
58 virtual std::optional<GooString> signDetached(const std::string &password) = 0;
59 virtual ~SigningInterface();
60 SigningInterface() = default;
61 SigningInterface(const SigningInterface &other) = delete;
62 SigningInterface &operator=(const SigningInterface &other) = delete;
63};
64
65class Backend
66{
67public:
68 enum class Type
69 {
70 NSS3,
71 GPGME
72 };
73 virtual std::unique_ptr<VerificationInterface> createVerificationHandler(std::vector<unsigned char> &&pkcs7) = 0;
74 virtual std::unique_ptr<SigningInterface> createSigningHandler(const std::string &certID, HashAlgorithm digestAlgTag) = 0;
75 virtual std::vector<std::unique_ptr<X509CertificateInfo>> getAvailableSigningCertificates() = 0;
76 virtual ~Backend();
77 Backend() = default;
78 Backend(const Backend &other) = delete;
79 Backend &operator=(const Backend &other) = delete;
80};
81
82class POPPLER_PRIVATE_EXPORT Factory
83{
84public:
85 // Sets the user preferred backend
86 static void setPreferredBackend(Backend::Type backend);
87 // Gets the current active backend
88 // prioritized from 1) setPreferredBackend,
89 // 2) POPPLER_SIGNATURE_BACKEND
90 // 3) Compiled in default
91 static std::optional<Backend::Type> getActive();
92 static std::vector<Backend::Type> getAvailable();
93 static std::unique_ptr<Backend> createActive();
94 static std::unique_ptr<Backend> create(Backend::Type);
95 static std::optional<Backend::Type> typeFromString(std::string_view string);
96 Factory() = delete;
97 /// backend specific settings
98
99private:
100 static std::optional<Backend::Type> preferredBackend;
101};
102
103}
104
105#endif // SIGNATUREBACKEND_H
106

source code of poppler/poppler/CryptoSignBackend.h