forked from arnaudmorin/libgtop11dotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MiniDriverContainer.cpp
302 lines (198 loc) · 10.6 KB
/
MiniDriverContainer.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
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*
* 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 "MiniDriverContainer.hpp"
#include <boost/foreach.hpp>
#include <memory>
#include "Log.hpp"
const unsigned char g_ucPublicKeyExponentLen = 4;
const unsigned char g_ucPublicKeyModulusLen = 4;
#define CONTAINER_MAP_RECORD_GUID_SIZE 40 * sizeof( WCHAR )
/*
*/
MiniDriverContainer::MiniDriverContainer( ) {
clear( );
}
/*
*/
void MiniDriverContainer::clear( void ) {
memset( &m_ContainerMapRecord, 0, sizeof( CONTAINER_MAP_RECORD ) );
m_bIsSmartCardLogon = false;
m_ucSignatureContainerType = 0;
m_ucExchangeContainerType = 0;
m_PinIdentifier = MiniDriverAuthentication::PIN_USER;
}
/*
*/
void MiniDriverContainer::setContainerMapRecord( CONTAINER_MAP_RECORD* a_pContainerMapRecord ) {
Log::begin( "MiniDriverContainer::setContainerMapRecord" );
if( a_pContainerMapRecord->wSigKeySizeBits || a_pContainerMapRecord->wKeyExchangeKeySizeBits ) {
m_ContainerMapRecord.bFlags = a_pContainerMapRecord->bFlags;
m_ContainerMapRecord.wKeyExchangeKeySizeBits = a_pContainerMapRecord->wKeyExchangeKeySizeBits;
m_ContainerMapRecord.wSigKeySizeBits = a_pContainerMapRecord->wSigKeySizeBits;
if( a_pContainerMapRecord->wszGuid ) {
memcpy( m_ContainerMapRecord.wszGuid, a_pContainerMapRecord->wszGuid, CONTAINER_MAP_RECORD_GUID_SIZE );
} else {
memset( m_ContainerMapRecord.wszGuid, 0, CONTAINER_MAP_RECORD_GUID_SIZE );
}
}
//print( );
Log::end( "MiniDriverContainer::setContainerMapRecord" );
}
/*
*/
void MiniDriverContainer::setContainerInformation( const boost::shared_ptr< Marshaller::u1Array >& a_pContainerInformation ) {
Log::begin( "MiniDriverContainer::setContainerInformation" );
std::string s;
Log::toString( a_pContainerInformation->GetBuffer( ), a_pContainerInformation->GetLength( ), s );
Log::log( "ContainerInformation <%s>", s.c_str( ) );
// The container information is a byte array blob containing the public key(s) in the selected container.
// The blob is formatted as follows: Blob = [Signature_Pub_Key] | [Exchange_Pub_Key]
// Signature_Pub_Key and Exchange_Pub_Key are optional depending on which key exists in the container and it’s a sequence of 3 TLV formatted as follows:
//T_Key_Type = 0x03
//L_Key_Type = 0x01
//V_Key_Type = 0x01 for Exchange_Pub_Key or 0x02 for Signature_Pub_Key
//T_Key_Pub_Exp = 0x01
//L_Key_Pub_Exp = 0x04
//V_Key_Pub_Exp = Value of Public key Exponent on 4 bytes.
//T_Key_Modulus = 0x02
//L_Key_Modulus = Key_Size_Bytes >> 4 (1 byte !)
//V_Key_Modulus = Value of Public key Modulus on Key_Size_Bytes bytes.
// Get the first public key type
unsigned int iOffset = 2;
unsigned char ucFirstPublicKeyType = a_pContainerInformation->ReadU1At( iOffset );
// Read the first public key exponent value
iOffset += 2;
unsigned int uiFirstPublicKeyExponentLength = a_pContainerInformation->ReadU1At( iOffset );
// Read the first public key exponent value
iOffset += 1;
Marshaller::u1Array* pFirstPublicKeyExponent = new Marshaller::u1Array( g_ucPublicKeyExponentLen );
// The exponent must be a 4 bytes buffer.
if( uiFirstPublicKeyExponentLength < g_ucPublicKeyExponentLen ) {
// Add zero at the head of the buffer
memset( pFirstPublicKeyExponent->GetBuffer( ), 0, g_ucPublicKeyExponentLen );
int iPaddingLength = g_ucPublicKeyExponentLen - uiFirstPublicKeyExponentLength;
memcpy( pFirstPublicKeyExponent->GetBuffer( ) + iPaddingLength, a_pContainerInformation->GetBuffer( ) + iOffset, uiFirstPublicKeyExponentLength );
} else {
memcpy( pFirstPublicKeyExponent->GetBuffer( ), a_pContainerInformation->GetBuffer( ) + iOffset, g_ucPublicKeyExponentLen );
}
// Read the first public key modulus len.
// Keep in mind that the signature public key modulus len is stored as a 4 rigth-shifted byte (>>4) to pass the modulus length on 1 byte ofr values 64 to 256 (512 to 2048bits)
iOffset += uiFirstPublicKeyExponentLength + 1;
int ucPublicKeyModulusLen = a_pContainerInformation->ReadU1At( iOffset ) << 4;
// Read the first public key modulus value
iOffset += 1;
Marshaller::u1Array* pFirstPublicKeyModulus = new Marshaller::u1Array( ucPublicKeyModulusLen );
memcpy( pFirstPublicKeyModulus->GetBuffer( ), a_pContainerInformation->GetBuffer( ) + iOffset, ucPublicKeyModulusLen );
if( KEYSPEC_EXCHANGE == ucFirstPublicKeyType ) {
m_pExchangePublicKeyExponent.reset( pFirstPublicKeyExponent );
m_pExchangePublicKeyModulus.reset( pFirstPublicKeyModulus );
} else {
m_pSignaturePublicKeyExponent.reset( pFirstPublicKeyExponent );
m_pSignaturePublicKeyModulus.reset( pFirstPublicKeyModulus );
}
// Check if the second key information is present into the container information
iOffset += ucPublicKeyModulusLen + 1;
if( iOffset < a_pContainerInformation->GetLength( ) ) {
// Read the second public key type
iOffset += 2;
unsigned char ucSecondPublicKeyType = a_pContainerInformation->ReadU1At( iOffset );
// Read the second public key exponent value
iOffset += 2;
unsigned int uiSecondPublicKeyExponentLength = a_pContainerInformation->ReadU1At( iOffset );
// The exponent must be a 4 bytes buffer.
Marshaller::u1Array* pSecondPublicKeyExponent = new Marshaller::u1Array( g_ucPublicKeyExponentLen );
if( uiSecondPublicKeyExponentLength < g_ucPublicKeyExponentLen ) {
// Add zero at the head of the buffer
memset( pSecondPublicKeyExponent->GetBuffer( ), 0, g_ucPublicKeyExponentLen );
int iPaddingLength = g_ucPublicKeyExponentLen - uiSecondPublicKeyExponentLength;
memcpy( pSecondPublicKeyExponent->GetBuffer( ) + iPaddingLength, a_pContainerInformation->GetBuffer( ) + iOffset, uiSecondPublicKeyExponentLength );
} else {
memcpy( pSecondPublicKeyExponent->GetBuffer( ), a_pContainerInformation->GetBuffer( ) + iOffset, g_ucPublicKeyExponentLen );
}
// Read the second public key modulus len.
// Keep in mind that the signature public key modulus len is stored as a 4 rigth-shifted byte (>>4) to pass the modulus length on 1 byte ofr values 64 to 256 (512 to 2048bits)
iOffset += uiSecondPublicKeyExponentLength + 1;
ucPublicKeyModulusLen = a_pContainerInformation->ReadU1At( iOffset ) << 4;
// Read the second public key modulus value
++iOffset;
Marshaller::u1Array* pSecondPublicKeyModulus = new Marshaller::u1Array( ucPublicKeyModulusLen );
memcpy( pSecondPublicKeyModulus->GetBuffer( ), a_pContainerInformation->GetBuffer( ) + iOffset, ucPublicKeyModulusLen );
if( KEYSPEC_EXCHANGE == ucSecondPublicKeyType ) {
m_pExchangePublicKeyExponent.reset( pSecondPublicKeyExponent );
m_pExchangePublicKeyModulus.reset( pSecondPublicKeyModulus );
} else {
m_pSignaturePublicKeyExponent.reset( pSecondPublicKeyExponent );
m_pSignaturePublicKeyModulus.reset( pSecondPublicKeyModulus );
}
}
//print( );
Log::end( "MiniDriverContainer::setContainerInformation" );
}
/*
*/
void MiniDriverContainer::print( void ) {
if( !Log::s_bEnableLog ) {
return;
}
Log::log( "MiniDriverContainer - ===" );
Log::log( "MiniDriverContainer - [SmartCard Logon <%d>]", m_bIsSmartCardLogon );
Log::log( "MiniDriverContainer - Flag <%#02x>", m_ContainerMapRecord.bFlags );
Log::log( "MiniDriverContainer - wKeyExchangeKeySizeBits <%#02x>", m_ContainerMapRecord.wKeyExchangeKeySizeBits );
Log::log( "MiniDriverContainer - wSigKeySizeBits <%#02x>", m_ContainerMapRecord.wSigKeySizeBits );
std::string s;
Log::toString( (const unsigned char*)m_ContainerMapRecord.wszGuid, (size_t)sizeof( m_ContainerMapRecord.wszGuid ), s );
Log::log( "MiniDriverContainer - wszGuid <%s>", s.c_str( ) );
s = "";
if( m_pSignaturePublicKeyExponent ) {
Log::toString( m_pSignaturePublicKeyExponent->GetBuffer( ), m_pSignaturePublicKeyExponent->GetLength( ), s );
Log::log( "MiniDriverContainer - SignaturePublicKeyExponent <%s>", s.c_str( ) );
} else {
Log::log( "MiniDriverContainer - SignaturePublicKeyExponent <0>" );
}
if( m_pSignaturePublicKeyModulus ) {
Log::toString( m_pSignaturePublicKeyModulus->GetBuffer( ), m_pSignaturePublicKeyModulus->GetLength( ), s );
Log::log( "MiniDriverContainer - SignaturePublicKeyModulus <%s>", s.c_str( ) );
} else {
Log::log( "MiniDriverContainer - SignaturePublicKeyModulus <0>" );
}
if( m_pExchangePublicKeyExponent ) {
Log::toString( m_pExchangePublicKeyExponent->GetBuffer( ), m_pExchangePublicKeyExponent->GetLength( ), s );
Log::log( "MiniDriverContainer - ExchangePublicKeyExponent <%s>", s.c_str( ) );
} else {
Log::log( "MiniDriverContainer - ExchangePublicKeyExponent <0>" );
}
if( m_pExchangePublicKeyModulus ) {
Log::toString( m_pExchangePublicKeyModulus->GetBuffer( ), m_pExchangePublicKeyModulus->GetLength( ), s );
Log::log( "MiniDriverContainer - ExchangePublicKeyModulus <%s>", s.c_str( ) );
} else {
Log::log( "MiniDriverContainer - ExchangePublicKeyModulus <0>" );
}
}
void MiniDriverContainer::setGUID( const std::string& a_stGUID ) {
memset( m_ContainerMapRecord.wszGuid, 0, sizeof( m_ContainerMapRecord.wszGuid ) );
size_t length = ( a_stGUID.size( ) > 39 ) ? 39 : a_stGUID.size( );
for( size_t i = 0 ; i < length; ++i ) {
m_ContainerMapRecord.wszGuid[ i ] = (WCHAR)a_stGUID[ i ];
}
//for( size_t i = 0 ; i < length; ++i ) {
// // Convert to wchar, little endian.
// m_ContainerMapRecord.wszGuid[ 2*i ] = a_stGUID[ i ];
//}
}