forked from LudovicRousseau/PCSC-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PCSCv2part10.c
123 lines (102 loc) · 2.75 KB
/
PCSCv2part10.c
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
/*
PCSCv2part10.c: helper functions for PC/SC v2 part 10 services
Copyright (C) 2012 Ludovic Rousseau
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 Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdio.h>
#include <arpa/inet.h>
#ifdef __APPLE__
#include <PCSC/winscard.h>
#include <PCSC/wintypes.h>
#else
#include <winscard.h>
#endif
#include "PCSCv2part10.h"
int PCSCv2Part10_find_TLV_property_by_tag_from_buffer(
unsigned char *buffer, int length, int property, int * value_int)
{
unsigned char *p;
int found = 0, len;
int value = -1;
int ret = -1; /* not found by default */
p = buffer;
while (p-buffer < length)
{
if (*p++ == property)
{
found = 1;
break;
}
/* go to next tag */
len = *p++;
p += len;
}
if (found)
{
len = *p++;
ret = 0;
switch(len)
{
case 1:
value = *p;
break;
case 2:
value = *p + (*(p+1)<<8);
break;
case 4:
value = *p + (*(p+1)<<8) + (*(p+2)<<16) + (*(p+3)<<24);
break;
default:
/* wrong length for an integer */
ret = -2;
}
}
if (value_int)
*value_int = value;
return ret;
} /* PCSCv2Part10_find_TLV_property_by_tag_from_buffer */
int PCSCv2Part10_find_TLV_property_by_tag_from_hcard(SCARDHANDLE hCard,
int property, int * value)
{
unsigned char buffer[MAX_BUFFER_SIZE];
LONG rv;
DWORD length;
unsigned int i;
PCSC_TLV_STRUCTURE *pcsc_tlv;
DWORD properties_in_tlv_ioctl;
int found;
rv = SCardControl(hCard, CM_IOCTL_GET_FEATURE_REQUEST, NULL, 0,
buffer, sizeof buffer, &length);
if (rv != SCARD_S_SUCCESS)
return -1;
/* get the number of elements instead of the complete size */
length /= sizeof(PCSC_TLV_STRUCTURE);
pcsc_tlv = (PCSC_TLV_STRUCTURE *)buffer;
found = 0;
for (i = 0; i < length; i++)
{
if (FEATURE_GET_TLV_PROPERTIES == pcsc_tlv[i].tag)
{
properties_in_tlv_ioctl = ntohl(pcsc_tlv[i].value);
found = 1;
}
}
if (! found)
return -3;
rv= SCardControl(hCard, properties_in_tlv_ioctl, NULL, 0,
buffer, sizeof buffer, &length);
if (rv != SCARD_S_SUCCESS)
return -1;
return PCSCv2Part10_find_TLV_property_by_tag_from_buffer(buffer,
length, property, value);
}