aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--include/qpdf/QPDFCryptoImpl.hh44
-rw-r--r--include/qpdf/QPDFCryptoProvider.hh102
-rw-r--r--libqpdf/QPDFCryptoProvider.cc76
-rw-r--r--libqpdf/QPDFCrypto_native.cc1
-rw-r--r--libqpdf/build.mk2
-rw-r--r--libqpdf/qpdf/QPDFCrypto_native.hh16
6 files changed, 241 insertions, 0 deletions
diff --git a/include/qpdf/QPDFCryptoImpl.hh b/include/qpdf/QPDFCryptoImpl.hh
new file mode 100644
index 00000000..4da299aa
--- /dev/null
+++ b/include/qpdf/QPDFCryptoImpl.hh
@@ -0,0 +1,44 @@
+// Copyright (c) 2005-2019 Jay Berkenbilt
+//
+// This file is part of qpdf.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// Versions of qpdf prior to version 7 were released under the terms
+// of version 2.0 of the Artistic License. At your option, you may
+// continue to consider qpdf to be licensed under those terms. Please
+// see the manual for additional information.
+
+#ifndef QPDFCRYPTOIMPL_HH
+#define QPDFCRYPTOIMPL_HH
+
+#include <qpdf/DLL.h>
+
+// This class is part of qpdf's pluggable crypto provider support.
+// Most users won't need to know or care about this class, but you can
+// use it if you want to supply your own crypto implementation. To do
+// so, provide an implementation of QPDFCryptoImpl, ensure that you
+// register it by calling QPDFCryptoProvider::registerImpl, and make
+// it the default by calling QPDFCryptoProvider::setDefaultProvider.
+
+class QPDF_DLL_CLASS QPDFCryptoImpl
+{
+ public:
+ QPDF_DLL
+ QPDFCryptoImpl() = default;
+
+ QPDF_DLL
+ virtual ~QPDFCryptoImpl() = default;
+};
+
+#endif // QPDFCRYPTOIMPL_HH
diff --git a/include/qpdf/QPDFCryptoProvider.hh b/include/qpdf/QPDFCryptoProvider.hh
new file mode 100644
index 00000000..4243fcfd
--- /dev/null
+++ b/include/qpdf/QPDFCryptoProvider.hh
@@ -0,0 +1,102 @@
+// Copyright (c) 2005-2019 Jay Berkenbilt
+//
+// This file is part of qpdf.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// Versions of qpdf prior to version 7 were released under the terms
+// of version 2.0 of the Artistic License. At your option, you may
+// continue to consider qpdf to be licensed under those terms. Please
+// see the manual for additional information.
+
+#ifndef QPDFCRYPTOPROVIDER_HH
+#define QPDFCRYPTOPROVIDER_HH
+
+#include <qpdf/DLL.h>
+#include <qpdf/QPDFCryptoImpl.hh>
+#include <string>
+#include <map>
+#include <memory>
+#include <functional>
+
+// This class is part of qpdf's pluggable crypto provider support.
+// Most users won't need to know or care about this class, but you can
+// use it if you want to supply your own crypto implementation. See
+// also comments in QPDFCryptoImpl.hh.
+
+class QPDFCryptoProvider
+{
+ public:
+
+ // Methods for getting and registering crypto implementations.
+ // These methods are not thread-safe.
+
+ // Return an instance of a crypto provider using the default
+ // implementation.
+ QPDF_DLL
+ static std::shared_ptr<QPDFCryptoImpl> getImpl();
+
+ // Return an instance of the crypto provider registered using the
+ // given name.
+ QPDF_DLL
+ static std::shared_ptr<QPDFCryptoImpl>
+ getImpl(std::string const& name);
+
+ // Register the given type (T) as a crypto implementation. T must
+ // be derived from QPDFCryptoImpl and must have a constructor that
+ // takes no arguments.
+ template<typename T>
+ QPDF_DLL
+ static void registerImpl(std::string const& name);
+
+ // Set the crypto provider registered with the given name as the
+ // default crypto implementation.
+ QPDF_DLL
+ static void setDefaultProvider(std::string const& name);
+
+ private:
+ QPDFCryptoProvider();
+ ~QPDFCryptoProvider() = default;
+ QPDFCryptoProvider(QPDFCryptoProvider const&) = delete;
+ QPDFCryptoProvider& operator=(QPDFCryptoProvider const&) = delete;
+
+ static QPDFCryptoProvider& getInstance();
+
+ std::shared_ptr<QPDFCryptoImpl>
+ getImpl_internal(std::string const& name) const;
+ template<typename T>
+ void registerImpl_internal(std::string const& name);
+ void setDefaultProvider_internal(std::string const& name);
+
+ class Members
+ {
+ friend class QPDFCryptoProvider;
+
+ public:
+ Members() = default;
+ QPDF_DLL
+ ~Members() = default;
+
+ private:
+ Members(Members const&) = delete;
+ Members& operator=(Members const&) = delete;
+
+ typedef std::function<std::shared_ptr<QPDFCryptoImpl> ()> provider_fn;
+ std::string default_provider;
+ std::map<std::string, provider_fn> providers;
+ };
+
+ std::shared_ptr<Members> m;
+};
+
+#endif // QPDFCRYPTOPROVIDER_HH
diff --git a/libqpdf/QPDFCryptoProvider.cc b/libqpdf/QPDFCryptoProvider.cc
new file mode 100644
index 00000000..8d795534
--- /dev/null
+++ b/libqpdf/QPDFCryptoProvider.cc
@@ -0,0 +1,76 @@
+#include <qpdf/QPDFCryptoProvider.hh>
+#include <stdexcept>
+
+#include <qpdf/QPDFCrypto_native.hh>
+
+std::shared_ptr<QPDFCryptoImpl>
+QPDFCryptoProvider::getImpl()
+{
+ QPDFCryptoProvider& p = getInstance();
+ if (p.m->default_provider.empty())
+ {
+ throw std::logic_error(
+ "QPDFCryptoProvider::getImpl called with no default provider.");
+ }
+ return p.getImpl_internal(p.m->default_provider);
+}
+
+std::shared_ptr<QPDFCryptoImpl>
+QPDFCryptoProvider::getImpl(std::string const& name)
+{
+ return getInstance().getImpl_internal(name);
+}
+
+template<typename T>
+void
+QPDFCryptoProvider::registerImpl(std::string const& name)
+{
+ getInstance().registerImpl_internal<T>(name);
+}
+
+void
+QPDFCryptoProvider::setDefaultProvider(std::string const& name)
+{
+ getInstance().setDefaultProvider_internal(name);
+}
+
+QPDFCryptoProvider::QPDFCryptoProvider() :
+ m(std::make_shared<Members>())
+{
+ registerImpl_internal<QPDFCrypto_native>("native");
+ setDefaultProvider_internal("native");
+}
+
+QPDFCryptoProvider&
+QPDFCryptoProvider::getInstance()
+{
+ static QPDFCryptoProvider instance;
+ return instance;
+}
+
+std::shared_ptr<QPDFCryptoImpl>
+QPDFCryptoProvider::getImpl_internal(std::string const& name) const
+{
+ auto iter = this->m->providers.find(name);
+ if (iter == this->m->providers.end())
+ {
+ throw std::logic_error(
+ "QPDFCryptoProvider requested unknown implementation \"" +
+ name + "\"");
+ }
+ return this->m->providers[name]();
+}
+
+template<typename T>
+void
+QPDFCryptoProvider::registerImpl_internal(std::string const& name)
+{
+ this->m->providers[name] = std::make_shared<T>;
+
+}
+
+void
+QPDFCryptoProvider::setDefaultProvider_internal(std::string const& name)
+{
+ this->m->default_provider = name;
+}
diff --git a/libqpdf/QPDFCrypto_native.cc b/libqpdf/QPDFCrypto_native.cc
new file mode 100644
index 00000000..ab9fd3a2
--- /dev/null
+++ b/libqpdf/QPDFCrypto_native.cc
@@ -0,0 +1 @@
+#include <qpdf/QPDFCrypto_native.hh>
diff --git a/libqpdf/build.mk b/libqpdf/build.mk
index 1da6c1a1..d2bcd8c5 100644
--- a/libqpdf/build.mk
+++ b/libqpdf/build.mk
@@ -39,6 +39,8 @@ SRCS_libqpdf = \
libqpdf/QPDF.cc \
libqpdf/QPDFAcroFormDocumentHelper.cc \
libqpdf/QPDFAnnotationObjectHelper.cc \
+ libqpdf/QPDFCryptoProvider.cc \
+ libqpdf/QPDFCrypto_native.cc \
libqpdf/QPDFExc.cc \
libqpdf/QPDFFormFieldObjectHelper.cc \
libqpdf/QPDFMatrix.cc \
diff --git a/libqpdf/qpdf/QPDFCrypto_native.hh b/libqpdf/qpdf/QPDFCrypto_native.hh
new file mode 100644
index 00000000..785d6c6f
--- /dev/null
+++ b/libqpdf/qpdf/QPDFCrypto_native.hh
@@ -0,0 +1,16 @@
+#ifndef QPDFCRYPTO_NATIVE_HH
+#define QPDFCRYPTO_NATIVE_HH
+
+#include <qpdf/DLL.h>
+#include <qpdf/QPDFCryptoImpl.hh>
+
+class QPDFCrypto_native: public QPDFCryptoImpl
+{
+ public:
+ QPDFCrypto_native() = default;
+
+ QPDF_DLL
+ virtual ~QPDFCrypto_native() = default;
+};
+
+#endif // QPDFCRYPTO_NATIVE_HH