Coverage Report

Created: 2025-08-26 07:08

/src/qpdf/include/qpdf/QPDFCryptoProvider.hh
Line
Count
Source
1
// Copyright (c) 2005-2021 Jay Berkenbilt
2
// Copyright (c) 2022-2025 Jay Berkenbilt and Manfred Holger
3
//
4
// This file is part of qpdf.
5
//
6
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
7
// in compliance with the License. You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing, software distributed under the License
12
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
13
// or implied. See the License for the specific language governing permissions and limitations under
14
// the License.
15
//
16
// Versions of qpdf prior to version 7 were released under the terms of version 2.0 of the Artistic
17
// License. At your option, you may continue to consider qpdf to be licensed under those terms.
18
// Please see the manual for additional information.
19
20
#ifndef QPDFCRYPTOPROVIDER_HH
21
#define QPDFCRYPTOPROVIDER_HH
22
23
#include <qpdf/DLL.h>
24
#include <qpdf/QPDFCryptoImpl.hh>
25
#include <functional>
26
#include <map>
27
#include <memory>
28
#include <set>
29
#include <string>
30
31
// This class is part of qpdf's pluggable crypto provider support. Most users won't need to know or
32
// care about this class, but you can use it if you want to supply your own crypto implementation.
33
// See also comments in QPDFCryptoImpl.hh.
34
class QPDFCryptoProvider
35
{
36
  public:
37
    // Methods for getting and registering crypto implementations. These methods are not
38
    // thread-safe.
39
40
    // Return an instance of a crypto provider using the default implementation.
41
    QPDF_DLL
42
    static std::shared_ptr<QPDFCryptoImpl> getImpl();
43
44
    // Return an instance of the crypto provider registered using the given name.
45
    QPDF_DLL
46
    static std::shared_ptr<QPDFCryptoImpl> getImpl(std::string const& name);
47
48
    typedef std::function<std::shared_ptr<QPDFCryptoImpl>()> provider_fn;
49
50
    // Register a crypto implementation with the given name. The provider function must return
51
    // a shared pointer to an instance of the implementation class, which must be derived from
52
    // QPDFCryptoImpl.
53
    QPDF_DLL static void registerImpl(std::string const& name, provider_fn f);
54
55
    // Register the given type (T) as a crypto implementation. T must be derived from QPDFCryptoImpl
56
    // and must have a constructor that takes no arguments.
57
    template <typename T>
58
    static void
59
    registerImpl(std::string const& name)
60
    {
61
        registerImpl(name, std::make_shared<T>);
62
    }
63
64
    // Set the crypto provider registered with the given name as the default crypto implementation.
65
    QPDF_DLL
66
    static void setDefaultProvider(std::string const& name);
67
68
    // Get the names of registered implementations
69
    QPDF_DLL
70
    static std::set<std::string> getRegisteredImpls();
71
72
    // Get the name of the default crypto provider
73
    QPDF_DLL
74
    static std::string getDefaultProvider();
75
76
  private:
77
    QPDFCryptoProvider();
78
1
    ~QPDFCryptoProvider() = default;
79
    QPDFCryptoProvider(QPDFCryptoProvider const&) = delete;
80
    QPDFCryptoProvider& operator=(QPDFCryptoProvider const&) = delete;
81
82
    static QPDFCryptoProvider& getInstance();
83
84
    std::shared_ptr<QPDFCryptoImpl> getImpl_internal(std::string const& name) const;
85
    void registerImpl_internal(std::string const& name, provider_fn f);
86
    void setDefaultProvider_internal(std::string const& name);
87
88
    class Members
89
    {
90
        friend class QPDFCryptoProvider;
91
92
      public:
93
1
        Members() = default;
94
1
        ~Members() = default;
95
96
      private:
97
        Members(Members const&) = delete;
98
        Members& operator=(Members const&) = delete;
99
100
        std::string default_provider;
101
        std::map<std::string, provider_fn> providers;
102
    };
103
104
    std::shared_ptr<Members> m;
105
};
106
107
#endif // QPDFCRYPTOPROVIDER_HH