summaryrefslogtreecommitdiff
path: root/src/windows/csp/State.h
blob: c1535d4a9848df8ee70bbb31669464917651b136 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/** BEGIN COPYRIGHT BLOCK
* This Program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation; version 2 of the License.
*
* This Program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
* Place, Suite 330, Boston, MA 02111-1307 USA.
*
* Copyright (C) 2003-2004 Identity Alliance

* All rights reserved.
* END COPYRIGHT BLOCK **/

/*****************************************************************
/
/ File   :   State.h
/ Date   :   December 3, 2002
/ Purpose:   Crypto API CSP->PKCS#11 Module
/ License:   Copyright (C) 2003-2004 Identity Alliance
/
******************************************************************/

#ifndef __INCLUDE_STATE_H__
#define __INCLUDE_STATE_H__

#include "csp.h"

namespace MCSP {

// Global state; only one instance of this
class State
{
private:
   HANDLE lock_;
   bool init_;
   bool logging_;
   std::string logFilename_;
   CK_SLOT_ID slot_;
   bool keyGenHack_;
   std::set<Session*> sessions_;
   std::set<Key*> keys_;
   std::string pkcs11dllname_;

public:
   CK_FUNCTION_LIST_PTR p11;

public:
   State();
   ~State();

   bool init() const
      { return init_; }

   void init(bool init)
      { init_ = init; }

   bool logging() const
      { return logging_; }

   void logging(bool logging)
      { logging_ = logging; }

   std::string logFilename() const
      { return logFilename_; }

   void logFilename(std::string logFilename)
      { logFilename_ = logFilename; }

   CK_SLOT_ID slot() const
      { return slot_; }

   void slot(CK_SLOT_ID slot)
      { slot_ = slot; }

   bool keyGenHack() const
      { return keyGenHack_; }

   void keyGenHack(bool keyGenHack)
      { keyGenHack_ = keyGenHack; }

   void addSession(Session* session)
      { lock(); sessions_.insert(session); unlock(); }

   void removeSession(Session* session);
   bool sessionExists(Session* session);

   Session* checkValidSession(HCRYPTPROV hProv);

   void addKey(Key* key)
      { lock(); keys_.insert(key); unlock(); }

   void removeKey(Key* key)
      { lock(); keys_.erase(key); unlock(); }

   bool keyExists(Key* key);
   Key* checkValidKey(HCRYPTKEY hKey);
   bool shutdown();

   void lock()
      { ::WaitForSingleObject(lock_, INFINITE); }

   void unlock()
      { ::ReleaseMutex(lock_); }

   bool initP11(const BinStr& reader_name, DWORD dwFlags);
};

} // namespace MCSP
#endif // __INCLUDE_STATE_H__