forked from arnaudmorin/libgtop11dotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Device.hpp
219 lines (121 loc) · 13.6 KB
/
Device.hpp
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*
* PKCS#11 library for .Net smart cards
* Copyright (C) 2007-2009 Gemalto <[email protected]>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#ifndef __GEMALTO_READER__
#define __GEMALTO_READER__
#include <string>
#include <boost/shared_ptr.hpp>
#include "MiniDriver.hpp"
#include "SmartCardReader.hpp"
#include "MiniDriverException.hpp"
#include "Timer.hpp"
//#include "PCSC.h"
/* This class is a facade exporting all smart card & reader features
*/
class Device {
public:
static bool s_bEnableCache;
Device( const SCARD_READERSTATE&, const unsigned char& );
virtual ~Device( );
unsigned char getDeviceID( void ) { return m_ucDeviceID; }
void clear( void );
inline void saveCache( void ) { if( Device::s_bEnableCache && m_MiniDriver.get( ) ) { m_MiniDriver->saveCache( ); } }
// Smart card reader operations
inline const std::string& getReaderName( void ) { if( m_SmartCardReader.get( ) ) return m_SmartCardReader->getReaderName( ); else throw MiniDriverException( SCARD_E_NO_SMARTCARD ); }
inline bool isSmartCardPresent( void ) { /*if( !m_MiniDriver.get( ) ) { return false; }*/ return ( ( m_DeviceState.dwCurrentState & SCARD_STATE_PRESENT ) ? true : false ); }
inline bool isSmartCardMute( void ) { return ( ( m_DeviceState.dwCurrentState & SCARD_STATE_MUTE ) ? true : false ); }
inline bool isSmartCardRecognized( void ) { if ( m_MiniDriver.get( ) ) return true; else return false;}
inline const SCARD_READERSTATE& getReaderState( void ) { return m_DeviceState; }
void set( const SCARD_READERSTATE& );
void put( SCARD_READERSTATE& );
void update( const SCARD_READERSTATE& );
void addMiniDriver( void );
void removeMiniDriver( void );
unsigned long getHandle( void );
inline bool isVerifyPinSecured( void ) { return m_SmartCardReader->isVerifyPinSecured( ); }
inline void verifyPinSecured( const unsigned char& a_ucRole ) { m_SmartCardReader->verifyPinSecured( a_ucRole ); }
inline bool isV2Plus( void ) { if( m_MiniDriver.get( ) ) { return m_MiniDriver->isV2Plus( ); } else throw MiniDriverException( SCARD_E_NO_SMARTCARD ); }
inline void beginTransaction( void ) { if( m_MiniDriver.get( ) ) { m_MiniDriver->beginTransaction( ); } }
inline void endTransaction( void ) { if( m_MiniDriver.get( ) ) { m_MiniDriver->endTransaction( ); } }
// Smart card operations
void hasChanged( MiniDriverCardCacheFile::ChangeType& a_Pins, MiniDriverCardCacheFile::ChangeType& a_Containers, MiniDriverCardCacheFile::ChangeType& a_Files );
Marshaller::u1Array* getCardProperty( const unsigned char& a_ucProperty, const unsigned char& a_ucFlags );
void setCardProperty( const unsigned char& a_ucProperty, Marshaller::u1Array* a_Data, const unsigned char& a_ucFlags );
inline const Marshaller::u1Array* getSerialNumber( void ) { if( m_MiniDriver.get( ) ) return m_MiniDriver->getSerialNumber( ); else throw MiniDriverException( SCARD_E_NO_SMARTCARD ); }
boost::shared_ptr< Marshaller::u1Array > privateKeyDecrypt( const unsigned char& a_ucContainerIndex, const unsigned char& a_ucKeySpec, Marshaller::u1Array* a_pDataToDecrypt ) { if( m_MiniDriver.get( ) ) return m_MiniDriver->privateKeyDecrypt( a_ucContainerIndex, a_ucKeySpec, a_pDataToDecrypt ); else throw MiniDriverException( SCARD_E_NO_SMARTCARD ); }
inline const CardModuleService* getCardModule( void ) { if( m_MiniDriver.get( ) ) return m_MiniDriver->getCardModule( ); else throw MiniDriverException( SCARD_E_NO_SMARTCARD ); }
inline void forceGarbageCollection( void ) { try { if( m_MiniDriver.get( ) ) { m_MiniDriver->forceGarbageCollection( ); } } catch( ... ) { } }
inline bool isReadOnly( void ) { if( m_MiniDriver.get( ) ) return m_MiniDriver->isReadOnly( ); else throw MiniDriverException( SCARD_E_NO_SMARTCARD ); }
inline bool isPinInitialized( void ) { if( m_MiniDriver.get( ) ) return m_MiniDriver->isPinInitialized( ); else throw MiniDriverException( SCARD_E_NO_SMARTCARD ); }
// PIN operations
inline unsigned char getPinMaxPinLength( void ) { if( m_MiniDriver.get( ) ) return m_MiniDriver->getPinMaxPinLength( ); else throw MiniDriverException( SCARD_E_NO_SMARTCARD ); }
inline unsigned char getPinMinPinLength( void ) { if( m_MiniDriver.get( ) ) return m_MiniDriver->getPinMinPinLength( ); else throw MiniDriverException( SCARD_E_NO_SMARTCARD ); }
inline bool isSSO( void ) { if( m_MiniDriver.get( ) ) return m_MiniDriver->isSSO( ); else throw MiniDriverException( SCARD_E_NO_SMARTCARD ); }
inline bool isNoPin( void ) { if( m_MiniDriver.get( ) ) return m_MiniDriver->isNoPin( ); else throw MiniDriverException( SCARD_E_NO_SMARTCARD ); }
bool isAuthenticated( void );
inline bool isExternalPin( void ) {if( m_MiniDriver.get( ) ) return m_MiniDriver->isExternalPin( ); else throw MiniDriverException( SCARD_E_NO_SMARTCARD ); }
inline bool isModePinOnly( void ) { if( m_MiniDriver.get( ) ) return m_MiniDriver->isModePinOnly( ); else throw MiniDriverException( SCARD_E_NO_SMARTCARD ); }
inline bool isModeNotPinOnly( void ) { if( m_MiniDriver.get( ) ) return m_MiniDriver->isModeNotPinOnly( ); else throw MiniDriverException( SCARD_E_NO_SMARTCARD ); }
inline bool isModePinOrBiometry( void ) { if( m_MiniDriver.get( ) ) return m_MiniDriver->isModePinOrBiometry( ); else throw MiniDriverException( SCARD_E_NO_SMARTCARD ); }
inline void changePin( Marshaller::u1Array* a_pOldPIN, Marshaller::u1Array* a_pNewPIN ) { if( m_MiniDriver.get( ) ) m_MiniDriver->changePin( a_pOldPIN, a_pNewPIN ); else throw MiniDriverException( SCARD_E_NO_SMARTCARD ); }
inline void unblockPin( Marshaller::u1Array* a_PinSo, Marshaller::u1Array* a_PinUser ) { if( m_MiniDriver.get( ) ) m_MiniDriver->unblockPin( a_PinSo, a_PinUser ); else throw MiniDriverException( SCARD_E_NO_SMARTCARD ); }
void verifyPin( Marshaller::u1Array* a_Pin );
void logOut( void );
inline int getTriesRemaining( void ) { if( m_MiniDriver.get( ) ) return m_MiniDriver->getTriesRemaining( ); else throw MiniDriverException( SCARD_E_NO_SMARTCARD ); }
inline void administratorLogin( Marshaller::u1Array* a_pAdministratorKey ) { if( m_MiniDriver.get( ) ) m_MiniDriver->administratorLogin( a_pAdministratorKey ); else throw MiniDriverException( SCARD_E_NO_SMARTCARD ); }
inline void administratorLogout( void ) { if( m_MiniDriver.get( ) ) m_MiniDriver->administratorLogout( ); else throw MiniDriverException( SCARD_E_NO_SMARTCARD ); }
inline void administratorChangeKey( Marshaller::u1Array* a_OldKey, Marshaller::u1Array* a_NewKey ) { if( m_MiniDriver.get( ) ) m_MiniDriver->administratorChangeKey( a_OldKey, a_NewKey ); else throw MiniDriverException( SCARD_E_NO_SMARTCARD ); }
inline unsigned char administratorGetTriesRemaining( void ) { if( m_MiniDriver.get( ) ) return m_MiniDriver->administratorGetTriesRemaining( ); else throw MiniDriverException( SCARD_E_NO_SMARTCARD ); }
inline bool administratorIsAuthenticated( void ) { if( m_MiniDriver.get( ) ) return m_MiniDriver->administratorIsAuthenticated( ); else throw MiniDriverException( SCARD_E_NO_SMARTCARD ); }
// Files operations
inline void createDirectory( const std::string& a_stDirectoryParent, const std::string& a_stDirectory ) { if( m_MiniDriver.get( ) ) return m_MiniDriver->createDirectory( a_stDirectoryParent, a_stDirectory ); else throw MiniDriverException( SCARD_E_NO_SMARTCARD ); }
inline void createFile( const std::string& a_stDirectory, const std::string& a_stFile, const bool& a_bIsReadProtected ) { if( m_MiniDriver.get( ) ) m_MiniDriver->createFile( a_stDirectory, a_stFile, a_bIsReadProtected ); else throw MiniDriverException( SCARD_E_NO_SMARTCARD ); }
inline void deleteFile( const std::string& a_stDirectory, const std::string& a_stFile ) { if( m_MiniDriver.get( ) ) m_MiniDriver->deleteFile( a_stDirectory, a_stFile ); else throw MiniDriverException( SCARD_E_NO_SMARTCARD ); }
inline void readCertificate( const std::string& a_stPath, boost::shared_ptr< Marshaller::u1Array >& a_pCertificateValue ) { if( m_MiniDriver.get( ) ) return m_MiniDriver->readCertificate( a_stPath, a_pCertificateValue ); else throw MiniDriverException( SCARD_E_NO_SMARTCARD ); }
inline MiniDriverFiles::FILES_NAME& enumFiles( const std::string& a_DirectoryPath ) { if( m_MiniDriver.get( ) ) return m_MiniDriver->enumFiles( a_DirectoryPath ); else throw MiniDriverException( SCARD_E_NO_SMARTCARD ); }
inline Marshaller::u1Array* readFile( const std::string& a_stDirectory, const std::string& a_stFile ) { if( m_MiniDriver.get( ) ) return m_MiniDriver->readFile( a_stDirectory, a_stFile ); else throw MiniDriverException( SCARD_E_NO_SMARTCARD ); }
inline void writeFile( const std::string& a_stDirectory, const std::string& a_stFile, Marshaller::u1Array* a_FileData, const bool& a_bAddToCache = true ) { if( m_MiniDriver.get( ) ) m_MiniDriver->writeFile( a_stDirectory, a_stFile, a_FileData, a_bAddToCache ); else throw MiniDriverException( SCARD_E_NO_SMARTCARD ); }
inline void createCertificate( unsigned char& a_ucContainerIndex, unsigned char& a_ucKeySpec, std::string& a_stCertificateName, Marshaller::u1Array* a_pValue, Marshaller::u1Array* a_pModulus, const bool& a_bSmartCardLogon ) { if( m_MiniDriver.get( ) ) m_MiniDriver->createCertificate( a_ucContainerIndex, a_ucKeySpec, a_stCertificateName, a_pValue, a_pModulus, a_bSmartCardLogon ); else throw MiniDriverException( SCARD_E_NO_SMARTCARD ); }
inline void createCertificateRoot( std::string& a_stCertificateName, Marshaller::u1Array* a_pValue ) { if( m_MiniDriver.get( ) ) m_MiniDriver->createCertificateRoot( a_stCertificateName, a_pValue ); else throw MiniDriverException( SCARD_E_NO_SMARTCARD ); }
void deletePrivateKey( const unsigned char& a_ucContainerIndex );
inline void deleteFileStructure( void ) { if( m_MiniDriver.get( ) ) m_MiniDriver->deleteFileStructure( ); else throw MiniDriverException( SCARD_E_NO_SMARTCARD ); }
inline void certificateDelete( unsigned char& a_ucContainerIndex ) { if( m_MiniDriver.get( ) ) m_MiniDriver->certificateDelete( a_ucContainerIndex ); else throw MiniDriverException( SCARD_E_NO_SMARTCARD ); }
inline void cacheDisable( const std::string& a_stFileName ) { if( m_MiniDriver.get( ) ) m_MiniDriver->cacheDisable( a_stFileName ); }
inline void renameFile( const std::string& a_stOldFileDirectory, const std::string& a_stOldFileName, const std::string& a_stNewFileDirectory, const std::string& a_stNewFileName ) { if( m_MiniDriver.get( ) ) m_MiniDriver->renameFile( a_stOldFileDirectory, a_stOldFileName, a_stNewFileDirectory, a_stNewFileName ); }
// Container operations
inline void containerCreate( unsigned char& a_ucContainerIndex, const bool& a_bKeyImport, unsigned char& a_ucKeySpec, Marshaller::u1Array* a_pPublicKeyModulus, const int& a_KeySize, Marshaller::u1Array* a_pKeyValue ) { if( m_MiniDriver.get( ) ) m_MiniDriver->containerCreate( a_ucContainerIndex, a_bKeyImport, a_ucKeySpec, a_pPublicKeyModulus, a_KeySize, a_pKeyValue ); else throw MiniDriverException( SCARD_E_NO_SMARTCARD ); }
inline void containerDelete( const unsigned char& a_ucContainerIndex ) { if( m_MiniDriver.get( ) ) m_MiniDriver->containerDelete( a_ucContainerIndex ); else throw MiniDriverException( SCARD_E_NO_SMARTCARD ); }
inline const MiniDriverContainer& containerGet( const unsigned char& a_ucContainerIndex ) { if( m_MiniDriver.get( ) ) return m_MiniDriver->containerGet( a_ucContainerIndex ); else throw MiniDriverException( SCARD_E_NO_SMARTCARD ); }
inline unsigned char containerCount( void ) { if( m_MiniDriver.get( ) ) return m_MiniDriver->containerCount( ); else throw MiniDriverException( SCARD_E_NO_SMARTCARD ); }
inline bool containerGetMatching( unsigned char& a_ucContainerIndex, unsigned char& a_ucKeySpec, std::string& a_stFileName, const Marshaller::u1Array* a_pPublicKeyModulus ) { if( m_MiniDriver.get( ) ) return m_MiniDriver->containerGetMatching( a_ucContainerIndex, a_ucKeySpec, a_stFileName, a_pPublicKeyModulus ); else throw MiniDriverException( SCARD_E_NO_SMARTCARD ); }
inline bool containerIsImportedExchangeKey( const unsigned char& a_ucContainerIndex ) { if( m_MiniDriver.get( ) ) return m_MiniDriver->containerIsImportedExchangeKey( a_ucContainerIndex ); else throw MiniDriverException( SCARD_E_NO_SMARTCARD ); }
inline bool containerIsImportedSignatureKey( const unsigned char& a_ucContainerIndex ) { if( m_MiniDriver.get( ) ) return m_MiniDriver->containerIsImportedSignatureKey( a_ucContainerIndex ); else throw MiniDriverException( SCARD_E_NO_SMARTCARD ); }
inline unsigned char containerGetFree( void ) { if( m_MiniDriver.get( ) ) return m_MiniDriver->containerGetFree( ); else throw MiniDriverException( SCARD_E_NO_SMARTCARD ); }
private:
// Reader
boost::shared_ptr< SmartCardReader > m_SmartCardReader;
SCARD_READERSTATE m_DeviceState;
boost::shared_ptr< MiniDriver > m_MiniDriver;
//boost::shared_ptr< PCSC > m_PCSC;
unsigned char m_ucDeviceID;
Timer m_TimerLastChange;
Timer m_TimerLastAuth;
bool m_bIsLastAuth;
};
#endif // __GEMALTO_READER__