1 |
/* OpenPGP Smart Card Mini Driver |
2 |
Copyright (C) 2009 Vincent Le Toux |
3 |
|
4 |
This library is Free software; you can redistribute it and/or |
5 |
modify it under the terms of the GNU Lesser General Public |
6 |
License version 2.1 as published by the Free Software Foundation. |
7 |
|
8 |
This library is distributed in the hope that it will be useful, |
9 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
10 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
11 |
Lesser General Public License for more details. |
12 |
|
13 |
You should have received a copy of the GNU Lesser General Public |
14 |
License along with this library; if not, write to the Free Software |
15 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
16 |
*/ |
17 |
|
18 |
#include <windows.h> |
19 |
|
20 |
|
21 |
|
22 |
DWORD getTlvSize(__in PBYTE pbPointer, __in PDWORD pdwOffset) |
23 |
{ |
24 |
DWORD dwSize; |
25 |
switch(*pbPointer) |
26 |
{ |
27 |
case 0x81: |
28 |
*pdwOffset+=2; |
29 |
dwSize = pbPointer[1]; |
30 |
break; |
31 |
case 0x82: |
32 |
*pdwOffset+=3; |
33 |
dwSize = pbPointer[1] * 0x100 + pbPointer[2]; |
34 |
break; |
35 |
default: |
36 |
dwSize = *pbPointer; |
37 |
*pdwOffset+=1; |
38 |
break; |
39 |
} |
40 |
return dwSize; |
41 |
} |
42 |
|
43 |
/** used to parse tlv data returned when reading the public certificate */ |
44 |
BOOL find_tlv(__in PBYTE pbData, __in DWORD dwTlvSearched, __in DWORD dwTotalSize, __out PBYTE *pbDataOut, __out_opt PDWORD pdwSize) |
45 |
{ |
46 |
DWORD dwOffset = 0, dwTlv ; |
47 |
DWORD dwSize; |
48 |
BOOL bFound = FALSE; |
49 |
while (dwOffset < dwTotalSize) |
50 |
{ |
51 |
// check the tlv |
52 |
// if it begins with 0x5F => tlv of 2 bytes. |
53 |
// else 1 byte |
54 |
dwTlv = 0; |
55 |
if (pbData[dwOffset] == 0x5F) |
56 |
{ |
57 |
dwTlv = pbData[dwOffset] * 0x100; |
58 |
dwOffset++; |
59 |
} |
60 |
dwTlv += pbData[dwOffset]; |
61 |
dwOffset++; |
62 |
|
63 |
|
64 |
if (dwTlv == dwTlvSearched) |
65 |
{ |
66 |
// size sequence |
67 |
dwSize = getTlvSize(pbData + dwOffset,&dwOffset); |
68 |
if (pdwSize) |
69 |
{ |
70 |
*pdwSize = dwSize; |
71 |
} |
72 |
*pbDataOut = pbData + dwOffset; |
73 |
return TRUE; |
74 |
} |
75 |
else |
76 |
{ |
77 |
dwSize = getTlvSize(pbData + dwOffset,&dwOffset); |
78 |
if (dwTlv != 0x73) |
79 |
{ |
80 |
dwOffset += dwSize; |
81 |
} |
82 |
} |
83 |
} |
84 |
return FALSE; |
85 |
} |