Repository: randombit/botan
Pull request #2448 (2020-11-04 14:11:53)

Merge some of the P11 type headers (Slot/Module/Session) into p11_types.h
Also forward declare the DLL wrapper class rather than including it


Patch:
diff --git a/src/lib/prov/pkcs11/info.txt b/src/lib/prov/pkcs11/info.txt
index 730fd9ef6..0f8aab8e0 100644
--- a/src/lib/prov/pkcs11/info.txt
+++ b/src/lib/prov/pkcs11/info.txt
@@ -24,11 +24,13 @@ p11.h
 p11_ecc_key.h
 p11_ecdh.h
 p11_ecdsa.h
-p11_module.h
 p11_object.h
 p11_randomgenerator.h
 p11_rsa.h
-p11_session.h
-p11_slot.h
+p11_types.h
 p11_x509.h
+
+p11_module.h
+p11_slot.h
+p11_session.h
 </header:public>
diff --git a/src/lib/prov/pkcs11/p11.cpp b/src/lib/prov/pkcs11/p11.cpp
index ab4784112..334a75caa 100644
--- a/src/lib/prov/pkcs11/p11.cpp
+++ b/src/lib/prov/pkcs11/p11.cpp
@@ -7,7 +7,8 @@
 */
 
 #include <botan/p11.h>
-#include <botan/p11_session.h>
+#include <botan/p11_types.h>
+#include <botan/dyn_load.h>
 
 #include <cstdint>
 #include <string>
diff --git a/src/lib/prov/pkcs11/p11.h b/src/lib/prov/pkcs11/p11.h
index 46545b925..0fae1c2c5 100644
--- a/src/lib/prov/pkcs11/p11.h
+++ b/src/lib/prov/pkcs11/p11.h
@@ -11,7 +11,6 @@
 
 #include <botan/secmem.h>
 #include <botan/exceptn.h>
-#include <botan/dyn_load.h>
 
 #include <vector>
 #include <string>
@@ -56,6 +55,9 @@ static_assert(CRYPTOKI_VERSION_MAJOR == 2 && CRYPTOKI_VERSION_MINOR == 40,
               "The Botan PKCS#11 module was implemented against PKCS#11 v2.40. Please use the correct PKCS#11 headers.");
 
 namespace Botan {
+
+class Dynamically_Loaded_Library;
+
 namespace PKCS11 {
 
 using secure_string = secure_vector<uint8_t>;
diff --git a/src/lib/prov/pkcs11/p11_module.cpp b/src/lib/prov/pkcs11/p11_module.cpp
index 066fb676e..1c84d415c 100644
--- a/src/lib/prov/pkcs11/p11_module.cpp
+++ b/src/lib/prov/pkcs11/p11_module.cpp
@@ -6,12 +6,15 @@
 * Botan is released under the Simplified BSD License (see license.txt)
 */
 
-#include <botan/p11_module.h>
+#include <botan/p11_types.h>
+#include <botan/dyn_load.h>
 
 namespace Botan {
 
 namespace PKCS11 {
 
+Module::Module(Module&&) = default;
+
 Module::Module(const std::string& file_path, C_InitializeArgs init_args)
    : m_file_path(file_path)
    {
diff --git a/src/lib/prov/pkcs11/p11_module.h b/src/lib/prov/pkcs11/p11_module.h
index 40807fc39..0c8a6a6db 100644
--- a/src/lib/prov/pkcs11/p11_module.h
+++ b/src/lib/prov/pkcs11/p11_module.h
@@ -9,67 +9,7 @@
 #ifndef BOTAN_P11_MODULE_H_
 #define BOTAN_P11_MODULE_H_
 
-#include <string>
-#include <memory>
-
-#include <botan/p11.h>
-#include <botan/dyn_load.h>
-
-namespace Botan {
-namespace PKCS11 {
-
-/**
-* Loads the PKCS#11 shared library
-* Calls C_Initialize on load and C_Finalize on destruction
-*/
-class BOTAN_PUBLIC_API(2,0) Module final
-   {
-   public:
-      /**
-      * Loads the shared library and calls C_Initialize
-      * @param file_path the path to the PKCS#11 shared library
-      * @param init_args flags to use for `C_Initialize`
-      */
-      Module(const std::string& file_path, C_InitializeArgs init_args = { nullptr, nullptr, nullptr, nullptr, static_cast< CK_FLAGS >(Flag::OsLockingOk), nullptr });
-
-      Module(Module&& other) = default;
-      Module& operator=(Module&& other) = delete;
-
-      // Dtor calls C_Finalize(). A copy could be deleted while the origin still exists
-      // Furthermore std::unique_ptr member -> not copyable
-      Module(const Module& other) = delete;
-      Module& operator=(const Module& other) = delete;
-
-      /// Calls C_Finalize()
-      ~Module() noexcept;
-
-      /**
-      * Reloads the module and reinitializes it
-      * @param init_args flags to use for `C_Initialize`
-      */
-      void reload(C_InitializeArgs init_args = { nullptr, nullptr, nullptr, nullptr, static_cast< CK_FLAGS >(Flag::OsLockingOk), nullptr });
-
-      inline LowLevel* operator->() const
-         {
-         return m_low_level.get();
-         }
-
-      /// @return general information about Cryptoki
-      inline Info get_info() const
-         {
-         Info info;
-         m_low_level->C_GetInfo(&info);
-         return info;
-         }
-
-   private:
-      const std::string m_file_path;
-      FunctionListPtr m_func_list = nullptr;
-      std::unique_ptr<Dynamically_Loaded_Library> m_library = nullptr;
-      std::unique_ptr<LowLevel> m_low_level = nullptr;
-   };
-
-}
-}
+#include <botan/p11_types.h>
+BOTAN_DEPRECATED_HEADER(p11_module.h)
 
 #endif
diff --git a/src/lib/prov/pkcs11/p11_object.h b/src/lib/prov/pkcs11/p11_object.h
index 70d4b3810..a0da1b5ca 100644
--- a/src/lib/prov/pkcs11/p11_object.h
+++ b/src/lib/prov/pkcs11/p11_object.h
@@ -10,7 +10,7 @@
 #define BOTAN_P11_OBJECT_H_
 
 #include <botan/p11.h>
-#include <botan/p11_session.h>
+#include <botan/p11_types.h>
 #include <botan/secmem.h>
 
 #include <vector>
diff --git a/src/lib/prov/pkcs11/p11_randomgenerator.h b/src/lib/prov/pkcs11/p11_randomgenerator.h
index 82b7d9f48..339cb95a5 100644
--- a/src/lib/prov/pkcs11/p11_randomgenerator.h
+++ b/src/lib/prov/pkcs11/p11_randomgenerator.h
@@ -10,7 +10,7 @@
 #define BOTAN_P11_RNG_H_
 
 #include <botan/rng.h>
-#include <botan/p11_session.h>
+#include <botan/p11_types.h>
 #include <botan/entropy_src.h>
 
 #include <string>
diff --git a/src/lib/prov/pkcs11/p11_rsa.h b/src/lib/prov/pkcs11/p11_rsa.h
index f17d6c2ee..41d9bc134 100644
--- a/src/lib/prov/pkcs11/p11_rsa.h
+++ b/src/lib/prov/pkcs11/p11_rsa.h
@@ -9,7 +9,7 @@
 #ifndef BOTAN_P11_RSA_H_
 #define BOTAN_P11_RSA_H_
 
-#include <botan/p11_session.h>
+#include <botan/p11_types.h>
 #include <botan/p11_object.h>
 #include <botan/pk_keys.h>
 #include <botan/bigint.h>
diff --git a/src/lib/prov/pkcs11/p11_session.cpp b/src/lib/prov/pkcs11/p11_session.cpp
index b0120a2ac..3f5bfc001 100644
--- a/src/lib/prov/pkcs11/p11_session.cpp
+++ b/src/lib/prov/pkcs11/p11_session.cpp
@@ -6,7 +6,7 @@
 * Botan is released under the Simplified BSD License (see license.txt)
 */
 
-#include <botan/p11_session.h>
+#include <botan/p11_types.h>
 
 namespace Botan {
 namespace PKCS11 {
diff --git a/src/lib/prov/pkcs11/p11_session.h b/src/lib/prov/pkcs11/p11_session.h
index 3c305d6a8..39b7877cb 100644
--- a/src/lib/prov/pkcs11/p11_session.h
+++ b/src/lib/prov/pkcs11/p11_session.h
@@ -9,92 +9,7 @@
 #ifndef BOTAN_P11_SESSION_H_
 #define BOTAN_P11_SESSION_H_
 
-#include <botan/p11_slot.h>
-
-#include <utility>
-
-namespace Botan {
-namespace PKCS11 {
-class Module;
-
-/// Represents a PKCS#11 session
-class BOTAN_PUBLIC_API(2,0) Session final
-   {
-   public:
-      /**
-      * @param slot the slot to use
-      * @param read_only true if the session should be read only, false to create a read-write session
-      */
-      Session(Slot& slot, bool read_only);
-
-      /**
-      * @param slot the slot to use
-      * @param flags the flags to use for the session. Remark: Flag::SerialSession is mandatory
-      * @param callback_data application-defined pointer to be passed to the notification callback
-      * @param notify_callback address of the notification callback function
-      */
-      Session(Slot& slot, Flags flags, VoidPtr callback_data, Notify notify_callback);
-
-      /// Takes ownership of a session
-      Session(Slot& slot, SessionHandle handle);
-
-      Session(Session&& other) = default;
-      Session& operator=(Session&& other) = delete;
-
-      // Dtor calls C_CloseSession() and eventually C_Logout. A copy could close the session while the origin still exists
-      Session(const Session& other) = delete;
-      Session& operator=(const Session& other) = delete;
-
-      /// Logout user and close the session on destruction
-      ~Session() noexcept;
-
-      /// @return a reference to the slot
-      inline const Slot& slot() const
-         {
-         return m_slot;
-         }
-
-      /// @return the session handle of this session
-      inline SessionHandle handle() const
-         {
-         return m_handle;
-         }
-
-      /// @return a reference to the used module
-      inline Module& module() const
-         {
-         return m_slot.module();
-         }
-
-      /// @return the released session handle
-      SessionHandle release();
-
-      /**
-      * Login to this session
-      * @param userType the user type to use for the login
-      * @param pin the PIN of the user
-      */
-      void login(UserType userType, const secure_string& pin);
-
-      /// Logout from this session
-      void logoff();
-
-      /// @return information about this session
-      SessionInfo get_info() const;
-
-      /// Calls `C_SetPIN` to change the PIN using the old PIN (requires a logged in session)
-      void set_pin(const secure_string& old_pin, const secure_string& new_pin) const;
-
-      /// Calls `C_InitPIN` to change or initialize the PIN using the SO_PIN (requires a logged in session)
-      void init_pin(const secure_string& new_pin);
-
-   private:
-      const Slot& m_slot;
-      SessionHandle m_handle;
-      bool m_logged_in;
-   };
-
-}
-}
+#include <botan/p11_types.h>
+BOTAN_DEPRECATED_HEADER(p11_session.h)
 
 #endif
diff --git a/src/lib/prov/pkcs11/p11_slot.cpp b/src/lib/prov/pkcs11/p11_slot.cpp
index 95a0fad50..5c6ce91ad 100644
--- a/src/lib/prov/pkcs11/p11_slot.cpp
+++ b/src/lib/prov/pkcs11/p11_slot.cpp
@@ -6,7 +6,7 @@
 * Botan is released under the Simplified BSD License (see license.txt)
 */
 
-#include <botan/p11_slot.h>
+#include <botan/p11_types.h>
 
 namespace Botan {
 
diff --git a/src/lib/prov/pkcs11/p11_slot.h b/src/lib/prov/pkcs11/p11_slot.h
index 63da32a0e..0a33ed5f4 100644
--- a/src/lib/prov/pkcs11/p11_slot.h
+++ b/src/lib/prov/pkcs11/p11_slot.h
@@ -9,70 +9,7 @@
 #ifndef BOTAN_P11_SLOT_H_
 #define BOTAN_P11_SLOT_H_
 
-#include <string>
-#include <vector>
-#include <functional>
-
-#include <botan/p11_module.h>
-
-namespace Botan {
-namespace PKCS11 {
-
-/// Represents a PKCS#11 Slot, i.e., a card reader
-class BOTAN_PUBLIC_API(2,0) Slot final
-   {
-   public:
-      /**
-      * @param module the PKCS#11 module to use
-      * @param slot_id the slot id to use
-      */
-      Slot(Module& module, SlotId slot_id);
-
-      /// @return a reference to the module that is used
-      inline Module& module() const
-         {
-         return m_module;
-         }
-
-      /// @return the slot id
-      inline SlotId slot_id() const
-         {
-         return m_slot_id;
-         }
-
-      /**
-      * Get available slots
-      * @param module the module to use
-      * @param token_present true if only slots with attached tokens should be returned, false for all slots
-      * @return a list of available slots (calls C_GetSlotList)
-      */
-      static std::vector<SlotId> get_available_slots(Module& module, bool token_present);
-
-      /// @return information about the slot (`C_GetSlotInfo`)
-      SlotInfo get_slot_info() const;
-
-      /// Obtains a list of mechanism types supported by the slot (`C_GetMechanismList`)
-      std::vector<MechanismType> get_mechanism_list() const;
-
-      /// Obtains information about a particular mechanism possibly supported by a slot (`C_GetMechanismInfo`)
-      MechanismInfo get_mechanism_info(MechanismType mechanism_type) const;
-
-      /// Obtains information about a particular token in the system (`C_GetTokenInfo`)
-      TokenInfo get_token_info() const;
-
-      /**
-      * Calls `C_InitToken` to initialize the token
-      * @param label the label for the token (must not exceed 32 bytes according to PKCS#11)
-      * @param so_pin the PIN of the security officer
-      */
-      void initialize(const std::string& label, const secure_string& so_pin) const;
-
-   private:
-      const std::reference_wrapper<Module> m_module;
-      const SlotId m_slot_id;
-   };
-
-}
-}
+#include <botan/p11_types.h>
+BOTAN_DEPRECATED_HEADER(p11_slot.h)
 
 #endif
diff --git a/src/lib/prov/pkcs11/p11_types.h b/src/lib/prov/pkcs11/p11_types.h
new file mode 100644
index 000000000..bd445da3c
--- /dev/null
+++ b/src/lib/prov/pkcs11/p11_types.h
@@ -0,0 +1,209 @@
+/*
+* PKCS#11 Module/Slot/Session
+* (C) 2016 Daniel Neus, Sirrix AG
+* (C) 2016 Philipp Weber, Sirrix AG
+*
+* Botan is released under the Simplified BSD License (see license.txt)
+*/
+
+#ifndef BOTAN_P11_TYPES_H_
+#define BOTAN_P11_TYPES_H_
+
+#include <botan/p11.h>
+#include <string>
+#include <memory>
+#include <functional>
+#include <utility>
+
+namespace Botan {
+
+class Dynamically_Loaded_Library;
+
+namespace PKCS11 {
+
+/**
+* Loads the PKCS#11 shared library
+* Calls C_Initialize on load and C_Finalize on destruction
+*/
+class BOTAN_PUBLIC_API(2,0) Module final
+   {
+   public:
+      /**
+      * Loads the shared library and calls C_Initialize
+      * @param file_path the path to the PKCS#11 shared library
+      * @param init_args flags to use for `C_Initialize`
+      */
+      Module(const std::string& file_path, C_InitializeArgs init_args = { nullptr, nullptr, nullptr, nullptr, static_cast< CK_FLAGS >(Flag::OsLockingOk), nullptr });
+
+      Module(Module&& other);
+      Module& operator=(Module&& other) = delete;
+
+      // Dtor calls C_Finalize(). A copy could be deleted while the origin still exists
+      // Furthermore std::unique_ptr member -> not copyable
+      Module(const Module& other) = delete;
+      Module& operator=(const Module& other) = delete;
+
+      /// Calls C_Finalize()
+      ~Module() noexcept;
+
+      /**
+      * Reloads the module and reinitializes it
+      * @param init_args flags to use for `C_Initialize`
+      */
+      void reload(C_InitializeArgs init_args = { nullptr, nullptr, nullptr, nullptr, static_cast< CK_FLAGS >(Flag::OsLockingOk), nullptr });
+
+      inline LowLevel* operator->() const
+         {
+         return m_low_level.get();
+         }
+
+      /// @return general information about Cryptoki
+      inline Info get_info() const
+         {
+         Info info;
+         m_low_level->C_GetInfo(&info);
+         return info;
+         }
+
+   private:
+      const std::string m_file_path;
+      FunctionListPtr m_func_list = nullptr;
+      std::unique_ptr<Dynamically_Loaded_Library> m_library;
+      std::unique_ptr<LowLevel> m_low_level = nullptr;
+   };
+
+/// Represents a PKCS#11 Slot, i.e., a card reader
+class BOTAN_PUBLIC_API(2,0) Slot final
+   {
+   public:
+      /**
+      * @param module the PKCS#11 module to use
+      * @param slot_id the slot id to use
+      */
+      Slot(Module& module, SlotId slot_id);
+
+      /// @return a reference to the module that is used
+      inline Module& module() const
+         {
+         return m_module;
+         }
+
+      /// @return the slot id
+      inline SlotId slot_id() const
+         {
+         return m_slot_id;
+         }
+
+      /**
+      * Get available slots
+      * @param module the module to use
+      * @param token_present true if only slots with attached tokens should be returned, false for all slots
+      * @return a list of available slots (calls C_GetSlotList)
+      */
+      static std::vector<SlotId> get_available_slots(Module& module, bool token_present);
+
+      /// @return information about the slot (`C_GetSlotInfo`)
+      SlotInfo get_slot_info() const;
+
+      /// Obtains a list of mechanism types supported by the slot (`C_GetMechanismList`)
+      std::vector<MechanismType> get_mechanism_list() const;
+
+      /// Obtains information about a particular mechanism possibly supported by a slot (`C_GetMechanismInfo`)
+      MechanismInfo get_mechanism_info(MechanismType mechanism_type) const;
+
+      /// Obtains information about a particular token in the system (`C_GetTokenInfo`)
+      TokenInfo get_token_info() const;
+
+      /**
+      * Calls `C_InitToken` to initialize the token
+      * @param label the label for the token (must not exceed 32 bytes according to PKCS#11)
+      * @param so_pin the PIN of the security officer
+      */
+      void initialize(const std::string& label, const secure_string& so_pin) const;
+
+   private:
+      const std::reference_wrapper<Module> m_module;
+      const SlotId m_slot_id;
+   };
+
+/// Represents a PKCS#11 session
+class BOTAN_PUBLIC_API(2,0) Session final
+   {
+   public:
+      /**
+      * @param slot the slot to use
+      * @param read_only true if the session should be read only, false to create a read-write session
+      */
+      Session(Slot& slot, bool read_only);
+
+      /**
+      * @param slot the slot to use
+      * @param flags the flags to use for the session. Remark: Flag::SerialSession is mandatory
+      * @param callback_data application-defined pointer to be passed to the notification callback
+      * @param notify_callback address of the notification callback function
+      */
+      Session(Slot& slot, Flags flags, VoidPtr callback_data, Notify notify_callback);
+
+      /// Takes ownership of a session
+      Session(Slot& slot, SessionHandle handle);
+
+      Session(Session&& other) = default;
+      Session& operator=(Session&& other) = delete;
+
+      // Dtor calls C_CloseSession() and eventually C_Logout. A copy could close the session while the origin still exists
+      Session(const Session& other) = delete;
+      Session& operator=(const Session& other) = delete;
+
+      /// Logout user and close the session on destruction
+      ~Session() noexcept;
+
+      /// @return a reference to the slot
+      inline const Slot& slot() const
+         {
+         return m_slot;
+         }
+
+      /// @return the session handle of this session
+      inline SessionHandle handle() const
+         {
+         return m_handle;
+         }
+
+      /// @return a reference to the used module
+      inline Module& module() const
+         {
+         return m_slot.module();
+         }
+
+      /// @return the released session handle
+      SessionHandle release();
+
+      /**
+      * Login to this session
+      * @param userType the user type to use for the login
+      * @param pin the PIN of the user
+      */
+      void login(UserType userType, const secure_string& pin);
+
+      /// Logout from this session
+      void logoff();
+
+      /// @return information about this session
+      SessionInfo get_info() const;
+
+      /// Calls `C_SetPIN` to change the PIN using the old PIN (requires a logged in session)
+      void set_pin(const secure_string& old_pin, const secure_string& new_pin) const;
+
+      /// Calls `C_InitPIN` to change or initialize the PIN using the SO_PIN (requires a logged in session)
+      void init_pin(const secure_string& new_pin);
+
+   private:
+      const Slot& m_slot;
+      SessionHandle m_handle;
+      bool m_logged_in;
+   };
+
+}
+}
+
+#endif


Test patch:
diff --git a/src/tests/test_pkcs11_high_level.cpp b/src/tests/test_pkcs11_high_level.cpp
index 6a3367eec..84c2c62c3 100644
--- a/src/tests/test_pkcs11_high_level.cpp
+++ b/src/tests/test_pkcs11_high_level.cpp
@@ -17,9 +17,6 @@
 
 #if defined(BOTAN_HAS_PKCS11)
    #include <botan/p11.h>
-   #include <botan/p11_slot.h>
-   #include <botan/p11_session.h>
-   #include <botan/p11_module.h>
    #include <botan/p11_object.h>
    #include <botan/p11_randomgenerator.h>
 #endif