forked from arnaudmorin/libgtop11dotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MiniDriverCardCacheFile.cpp
178 lines (129 loc) · 5.61 KB
/
MiniDriverCardCacheFile.cpp
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
/*
* 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
*
*/
#include "util.h"
#include "MiniDriver.hpp"
#include "MiniDriverCardCacheFile.hpp"
#include "Log.hpp"
#include "MiniDriverException.hpp"
const unsigned char MAX_RETRY = 2;
/*
*/
void MiniDriverCardCacheFile::write( void ) {
// Create a buffer to write the file oncard
std::auto_ptr< Marshaller::u1Array > f( new Marshaller::u1Array( 6 ) );
// Set the version flag
f->SetU1At( 0, m_ucVersion );
// Set the PIN freshness counter
f->SetU1At( 1, m_ucPinsFreshness );
// Set the container freshness counter
IntToLittleEndian< unsigned short >( m_wContainersFreshness, f->GetBuffer( ), 2 );
// Set the file freshness counter
IntToLittleEndian< unsigned short >( m_wFilesFreshness, f->GetBuffer( ), 4 );
// Write cache file back
std::string g_stPathCardCF( szCACHE_FILE );
m_pCardModuleService->writeFile( &g_stPathCardCF, f.get( ) );
}
/*
*/
void MiniDriverCardCacheFile::notifyChange( const ChangeType& a_change ) {
switch( a_change ) {
case PINS:
m_ucPinsFreshness++;
Log::log( "MiniDriverCardCacheFile::notifyChange - PINS" );
break;
case CONTAINERS:
m_wContainersFreshness++;
Log::log( "MiniDriverCardCacheFile::notifyChange - CONTAINERS" );
break;
case FILES:
m_wFilesFreshness++;
Log::log( "MiniDriverCardCacheFile::notifyChange - FILES" );
break;
case NONE:
default:
// No update
break;
};
write( );
}
/*
*/
void MiniDriverCardCacheFile::hasChanged( ChangeType& a_Pins, ChangeType& a_Containers, ChangeType& a_Files ) {
Log::begin( "MiniDriverCardCacheFile::hasChanged" );
Timer t;
t.start( );
a_Pins = NONE;
a_Containers = NONE;
a_Files = NONE;
Log::log( "MiniDriverCardCacheFile::hasChanged - Inner Version <%#02x>", m_ucVersion );
Log::log( "MiniDriverCardCacheFile::hasChanged - Inner PIN freshness counter <%#02x>", m_ucPinsFreshness );
Log::log( "MiniDriverCardCacheFile::hasChanged - Inner Containers freshness counter <%#04x>", m_wContainersFreshness );
Log::log( "MiniDriverCardCacheFile::hasChanged - Inner Files freshness counter <%#04x>", m_wFilesFreshness );
// Get the file from the smart card
std::string g_stPathCardCF( szCACHE_FILE );
Marshaller::u1Array* f = m_pCardModuleService->readFileWithoutMemoryCheck( &g_stPathCardCF );
if( f ) {
std::string s;
Log::toString( f->GetBuffer( ), f->GetLength( ), s );
Log::log( "MiniDriverCardCacheFile::hasChanged - cardcf <%s>", s.c_str( ) );
// Get the version
unsigned char ucVersion = f->ReadU1At( 0 );
Log::log( "MiniDriverCardCacheFile::hasChanged - Read Version <0x%#02x>", ucVersion );
if( ucVersion != m_ucVersion ) {
m_ucVersion = ucVersion;
}
// Get the PIN freshness counter
unsigned char bPinsFreshness = f->ReadU1At( 1 );
Log::log( "MiniDriverCardCacheFile::hasChanged - Read PIN freshness counter <%#02x>", bPinsFreshness );
if( m_ucPinsFreshness != bPinsFreshness ) {
Log::log( "MiniDriverCardCacheFile::hasChanged - $$$$$ PIN freshness counter changed $$$$$" );
m_ucPinsFreshness = bPinsFreshness;
a_Pins = PINS;
}
// Get the container freshness counter
unsigned short wContainersFreshness = LittleEndianToInt< unsigned short >( f->GetBuffer( ), 2 );
Log::log( "MiniDriverCardCacheFile::hasChanged - Read Containers freshness counter <%#02x>", wContainersFreshness );
if( m_wContainersFreshness != wContainersFreshness ) {
Log::log( "MiniDriverCardCacheFile::hasChanged - $$$$$ CONTAINER freshness counter changed $$$$$" );
m_wContainersFreshness = wContainersFreshness;
a_Containers = CONTAINERS;
}
// Get the file freshness counter
unsigned short wFilesFreshness = LittleEndianToInt< unsigned short >( f->GetBuffer( ), 4 );
Log::log( "MiniDriverCardCacheFile::hasChanged - Read Files freshness counter <%#02x>", wFilesFreshness );
if( m_wFilesFreshness != wFilesFreshness ) {
Log::log( "MiniDriverCardCacheFile::hasChanged - $$$$$ FILE freshness counter changed $$$$$" );
m_wFilesFreshness = wFilesFreshness;
a_Files = FILES;
}
}
t.stop( "MiniDriverCardCacheFile::read" );
Log::end( "MiniDriverCardCacheFile::read" );
}
/*
*/
void MiniDriverCardCacheFile::print( void ) {
Log::begin( "MiniDriverCardCacheFile::print" );
Log::log( "Version <%ld>", m_ucVersion );
Log::log( "m_ucPinsFreshness <%ld>", m_ucPinsFreshness );
Log::log( "m_wContainersFreshness <%ld>", m_wContainersFreshness );
Log::log( "m_wFilesFreshness <%ld>", m_wFilesFreshness );
Log::end( "MiniDriverCardCacheFile::print" );
}