Botan  2.1.0
Crypto and TLS for C++11
p11_session.h
Go to the documentation of this file.
1 /*
2 * PKCS#11 Session
3 * (C) 2016 Daniel Neus, Sirrix AG
4 * (C) 2016 Philipp Weber, Sirrix AG
5 *
6 * Botan is released under the Simplified BSD License (see license.txt)
7 */
8 
9 #ifndef BOTAN_P11_SESSION_H__
10 #define BOTAN_P11_SESSION_H__
11 
12 #include <botan/p11.h>
13 #include <botan/p11_slot.h>
14 
15 #include <utility>
16 
17 namespace Botan {
18 namespace PKCS11 {
19 class Module;
20 
21 /// Represents a PKCS#11 session
22 class BOTAN_DLL Session final
23  {
24  public:
25  /**
26  * @param slot the slot to use
27  * @param read_only true if the session should be read only, false to create a read-write session
28  */
29  Session(Slot& slot, bool read_only);
30 
31  /**
32  * @param slot the slot to use
33  * @param flags the flags to use for the session. Remark: Flag::SerialSession is mandatory
34  * @param callback_data application-defined pointer to be passed to the notification callback
35  * @param notify_callback address of the notification callback function
36  */
37  Session(Slot& slot, Flags flags, VoidPtr callback_data, Notify notify_callback);
38 
39  /// Takes ownership of a session
40  Session(Slot& slot, SessionHandle handle);
41 
42 /* Microsoft Visual Studio <= 2013 does not support default generated move special member functions.
43  Everything else we target should support it */
44 #if !defined( _MSC_VER ) || ( _MSC_VER >= 1900 )
45  Session(Session&& other) = default;
46  Session& operator=(Session&& other) = default;
47 #endif
48 
49  // Dtor calls C_CloseSession() and eventually C_Logout. A copy could close the session while the origin still exists
50  Session(const Session& other) = delete;
51  Session& operator=(const Session& other) = delete;
52 
53  /// Logout user and close the session on destruction
55 
56  /// @return a reference to the slot
57  inline const Slot& slot() const
58  {
59  return m_slot;
60  }
61 
62  /// @return the session handle of this session
63  inline SessionHandle handle() const
64  {
65  return m_handle;
66  }
67 
68  /// @return a reference to the used module
69  inline Module& module() const
70  {
71  return m_slot.module();
72  }
73 
74  /// @return the released session handle
75  SessionHandle release();
76 
77  /**
78  * Login to this session
79  * @param userType the user type to use for the login
80  * @param pin the PIN of the user
81  */
82  void login(UserType userType, const secure_string& pin);
83 
84  /// Logout from this session
85  void logoff();
86 
87  /// @return information about this session
88  SessionInfo get_info() const;
89 
90  /// Calls `C_SetPIN` to change the PIN using the old PIN (requires a logged in session)
91  void set_pin(const secure_string& old_pin, const secure_string& new_pin) const;
92 
93  /// Calls `C_InitPIN` to change or initialize the PIN using the SO_PIN (requires a logged in session)
94  void init_pin(const secure_string& new_pin);
95 
96  private:
97  const Slot& m_slot;
99  bool m_logged_in;
100  };
101 
102 }
103 }
104 
105 #endif
CK_NOTIFY Notify
Definition: p11.h:842
Module & module() const
Definition: p11_session.h:69
void set_pin(Slot &slot, const secure_string &so_pin, const secure_string &pin)
Definition: p11.cpp:66
Flags flags(Flag flags)
Definition: p11.h:858
CK_VOID_PTR VoidPtr
Definition: p11.h:826
HCRYPTPROV m_handle
Definition: es_capi.cpp:45
#define BOTAN_NOEXCEPT
Definition: compiler.h:116
SessionHandle handle() const
Definition: p11_session.h:63
CK_SESSION_HANDLE SessionHandle
Definition: p11.h:843
Definition: alg_id.cpp:13
CK_FLAGS Flags
Definition: p11.h:832
secure_vector< uint8_t > secure_string
Definition: p11.h:61
Represents a PKCS#11 session.
Definition: p11_session.h:22
Represents a PKCS#11 Slot, i.e., a card reader.
Definition: p11_slot.h:23
const Slot & slot() const
Definition: p11_session.h:57