Commit 2de41856a0fe2181c0adb7ac1d7cf06037113775

Authored by Jay Berkenbilt
1 parent 34c0a880

QPDFCryptoProvider: initial implementation

include/qpdf/QPDFCryptoImpl.hh 0 → 100644
  1 +// Copyright (c) 2005-2019 Jay Berkenbilt
  2 +//
  3 +// This file is part of qpdf.
  4 +//
  5 +// Licensed under the Apache License, Version 2.0 (the "License");
  6 +// you may not use this file except in compliance with the License.
  7 +// 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
  12 +// distributed under the License is distributed on an "AS IS" BASIS,
  13 +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14 +// See the License for the specific language governing permissions and
  15 +// limitations under the License.
  16 +//
  17 +// Versions of qpdf prior to version 7 were released under the terms
  18 +// of version 2.0 of the Artistic License. At your option, you may
  19 +// continue to consider qpdf to be licensed under those terms. Please
  20 +// see the manual for additional information.
  21 +
  22 +#ifndef QPDFCRYPTOIMPL_HH
  23 +#define QPDFCRYPTOIMPL_HH
  24 +
  25 +#include <qpdf/DLL.h>
  26 +
  27 +// This class is part of qpdf's pluggable crypto provider support.
  28 +// Most users won't need to know or care about this class, but you can
  29 +// use it if you want to supply your own crypto implementation. To do
  30 +// so, provide an implementation of QPDFCryptoImpl, ensure that you
  31 +// register it by calling QPDFCryptoProvider::registerImpl, and make
  32 +// it the default by calling QPDFCryptoProvider::setDefaultProvider.
  33 +
  34 +class QPDF_DLL_CLASS QPDFCryptoImpl
  35 +{
  36 + public:
  37 + QPDF_DLL
  38 + QPDFCryptoImpl() = default;
  39 +
  40 + QPDF_DLL
  41 + virtual ~QPDFCryptoImpl() = default;
  42 +};
  43 +
  44 +#endif // QPDFCRYPTOIMPL_HH
... ...
include/qpdf/QPDFCryptoProvider.hh 0 → 100644
  1 +// Copyright (c) 2005-2019 Jay Berkenbilt
  2 +//
  3 +// This file is part of qpdf.
  4 +//
  5 +// Licensed under the Apache License, Version 2.0 (the "License");
  6 +// you may not use this file except in compliance with the License.
  7 +// 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
  12 +// distributed under the License is distributed on an "AS IS" BASIS,
  13 +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14 +// See the License for the specific language governing permissions and
  15 +// limitations under the License.
  16 +//
  17 +// Versions of qpdf prior to version 7 were released under the terms
  18 +// of version 2.0 of the Artistic License. At your option, you may
  19 +// continue to consider qpdf to be licensed under those terms. Please
  20 +// see the manual for additional information.
  21 +
  22 +#ifndef QPDFCRYPTOPROVIDER_HH
  23 +#define QPDFCRYPTOPROVIDER_HH
  24 +
  25 +#include <qpdf/DLL.h>
  26 +#include <qpdf/QPDFCryptoImpl.hh>
  27 +#include <string>
  28 +#include <map>
  29 +#include <memory>
  30 +#include <functional>
  31 +
  32 +// This class is part of qpdf's pluggable crypto provider support.
  33 +// Most users won't need to know or care about this class, but you can
  34 +// use it if you want to supply your own crypto implementation. See
  35 +// also comments in QPDFCryptoImpl.hh.
  36 +
  37 +class QPDFCryptoProvider
  38 +{
  39 + public:
  40 +
  41 + // Methods for getting and registering crypto implementations.
  42 + // These methods are not thread-safe.
  43 +
  44 + // Return an instance of a crypto provider using the default
  45 + // implementation.
  46 + QPDF_DLL
  47 + static std::shared_ptr<QPDFCryptoImpl> getImpl();
  48 +
  49 + // Return an instance of the crypto provider registered using the
  50 + // given name.
  51 + QPDF_DLL
  52 + static std::shared_ptr<QPDFCryptoImpl>
  53 + getImpl(std::string const& name);
  54 +
  55 + // Register the given type (T) as a crypto implementation. T must
  56 + // be derived from QPDFCryptoImpl and must have a constructor that
  57 + // takes no arguments.
  58 + template<typename T>
  59 + QPDF_DLL
  60 + static void registerImpl(std::string const& name);
  61 +
  62 + // Set the crypto provider registered with the given name as the
  63 + // default crypto implementation.
  64 + QPDF_DLL
  65 + static void setDefaultProvider(std::string const& name);
  66 +
  67 + private:
  68 + QPDFCryptoProvider();
  69 + ~QPDFCryptoProvider() = default;
  70 + QPDFCryptoProvider(QPDFCryptoProvider const&) = delete;
  71 + QPDFCryptoProvider& operator=(QPDFCryptoProvider const&) = delete;
  72 +
  73 + static QPDFCryptoProvider& getInstance();
  74 +
  75 + std::shared_ptr<QPDFCryptoImpl>
  76 + getImpl_internal(std::string const& name) const;
  77 + template<typename T>
  78 + void registerImpl_internal(std::string const& name);
  79 + void setDefaultProvider_internal(std::string const& name);
  80 +
  81 + class Members
  82 + {
  83 + friend class QPDFCryptoProvider;
  84 +
  85 + public:
  86 + Members() = default;
  87 + QPDF_DLL
  88 + ~Members() = default;
  89 +
  90 + private:
  91 + Members(Members const&) = delete;
  92 + Members& operator=(Members const&) = delete;
  93 +
  94 + typedef std::function<std::shared_ptr<QPDFCryptoImpl> ()> provider_fn;
  95 + std::string default_provider;
  96 + std::map<std::string, provider_fn> providers;
  97 + };
  98 +
  99 + std::shared_ptr<Members> m;
  100 +};
  101 +
  102 +#endif // QPDFCRYPTOPROVIDER_HH
... ...
libqpdf/QPDFCryptoProvider.cc 0 → 100644
  1 +#include <qpdf/QPDFCryptoProvider.hh>
  2 +#include <stdexcept>
  3 +
  4 +#include <qpdf/QPDFCrypto_native.hh>
  5 +
  6 +std::shared_ptr<QPDFCryptoImpl>
  7 +QPDFCryptoProvider::getImpl()
  8 +{
  9 + QPDFCryptoProvider& p = getInstance();
  10 + if (p.m->default_provider.empty())
  11 + {
  12 + throw std::logic_error(
  13 + "QPDFCryptoProvider::getImpl called with no default provider.");
  14 + }
  15 + return p.getImpl_internal(p.m->default_provider);
  16 +}
  17 +
  18 +std::shared_ptr<QPDFCryptoImpl>
  19 +QPDFCryptoProvider::getImpl(std::string const& name)
  20 +{
  21 + return getInstance().getImpl_internal(name);
  22 +}
  23 +
  24 +template<typename T>
  25 +void
  26 +QPDFCryptoProvider::registerImpl(std::string const& name)
  27 +{
  28 + getInstance().registerImpl_internal<T>(name);
  29 +}
  30 +
  31 +void
  32 +QPDFCryptoProvider::setDefaultProvider(std::string const& name)
  33 +{
  34 + getInstance().setDefaultProvider_internal(name);
  35 +}
  36 +
  37 +QPDFCryptoProvider::QPDFCryptoProvider() :
  38 + m(std::make_shared<Members>())
  39 +{
  40 + registerImpl_internal<QPDFCrypto_native>("native");
  41 + setDefaultProvider_internal("native");
  42 +}
  43 +
  44 +QPDFCryptoProvider&
  45 +QPDFCryptoProvider::getInstance()
  46 +{
  47 + static QPDFCryptoProvider instance;
  48 + return instance;
  49 +}
  50 +
  51 +std::shared_ptr<QPDFCryptoImpl>
  52 +QPDFCryptoProvider::getImpl_internal(std::string const& name) const
  53 +{
  54 + auto iter = this->m->providers.find(name);
  55 + if (iter == this->m->providers.end())
  56 + {
  57 + throw std::logic_error(
  58 + "QPDFCryptoProvider requested unknown implementation \"" +
  59 + name + "\"");
  60 + }
  61 + return this->m->providers[name]();
  62 +}
  63 +
  64 +template<typename T>
  65 +void
  66 +QPDFCryptoProvider::registerImpl_internal(std::string const& name)
  67 +{
  68 + this->m->providers[name] = std::make_shared<T>;
  69 +
  70 +}
  71 +
  72 +void
  73 +QPDFCryptoProvider::setDefaultProvider_internal(std::string const& name)
  74 +{
  75 + this->m->default_provider = name;
  76 +}
... ...
libqpdf/QPDFCrypto_native.cc 0 → 100644
  1 +#include <qpdf/QPDFCrypto_native.hh>
... ...
libqpdf/build.mk
... ... @@ -39,6 +39,8 @@ SRCS_libqpdf = \
39 39 libqpdf/QPDF.cc \
40 40 libqpdf/QPDFAcroFormDocumentHelper.cc \
41 41 libqpdf/QPDFAnnotationObjectHelper.cc \
  42 + libqpdf/QPDFCryptoProvider.cc \
  43 + libqpdf/QPDFCrypto_native.cc \
42 44 libqpdf/QPDFExc.cc \
43 45 libqpdf/QPDFFormFieldObjectHelper.cc \
44 46 libqpdf/QPDFMatrix.cc \
... ...
libqpdf/qpdf/QPDFCrypto_native.hh 0 → 100644
  1 +#ifndef QPDFCRYPTO_NATIVE_HH
  2 +#define QPDFCRYPTO_NATIVE_HH
  3 +
  4 +#include <qpdf/DLL.h>
  5 +#include <qpdf/QPDFCryptoImpl.hh>
  6 +
  7 +class QPDFCrypto_native: public QPDFCryptoImpl
  8 +{
  9 + public:
  10 + QPDFCrypto_native() = default;
  11 +
  12 + QPDF_DLL
  13 + virtual ~QPDFCrypto_native() = default;
  14 +};
  15 +
  16 +#endif // QPDFCRYPTO_NATIVE_HH
... ...