smartcard 0.2.0-mswin32
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +14 -0
- data/LICENSE +22 -0
- data/Manifest +19 -0
- data/README +22 -0
- data/ext/smartcard_pcsc/extconf.rb +53 -0
- data/ext/smartcard_pcsc/pcsc.h +46 -0
- data/ext/smartcard_pcsc/pcsc_card.c +475 -0
- data/ext/smartcard_pcsc/pcsc_constants.c +193 -0
- data/ext/smartcard_pcsc/pcsc_context.c +286 -0
- data/ext/smartcard_pcsc/pcsc_io_request.c +87 -0
- data/ext/smartcard_pcsc/pcsc_main.c +10 -0
- data/ext/smartcard_pcsc/pcsc_multi_strings.c +82 -0
- data/ext/smartcard_pcsc/pcsc_namespace.c +18 -0
- data/ext/smartcard_pcsc/pcsc_reader_states.c +326 -0
- data/ext/smartcard_pcsc/pcsc_surrogate_reader.h +207 -0
- data/ext/smartcard_pcsc/pcsc_surrogate_wintypes.h +63 -0
- data/lib/smartcard.rb +1 -0
- data/lib/smartcard/pcsc.so +0 -0
- data/smartcard.gemspec +53 -0
- data/test/test_all.rb +0 -0
- data/tests/ts_pcsc_ext.rb +135 -0
- metadata +67 -0
data/CHANGELOG
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
v0.2.0 Added automatic builds
|
2
|
+
Rakefile for auto builds using echoe
|
3
|
+
extconf.rb: hack to fix Windows makefiles
|
4
|
+
|
5
|
+
v0.1.2 Added Windows compatibility
|
6
|
+
*.c: restructured code so VC2005 likes it
|
7
|
+
(variable declarations before function body)
|
8
|
+
docs: license, readme, and what the gem is
|
9
|
+
|
10
|
+
v0.1.1 Added support for Ubuntu 7.10
|
11
|
+
ext/smartcard_pcsc/extconf.rb: better header
|
12
|
+
detection
|
13
|
+
|
14
|
+
v0.1.0 Initial release for OSX Leopard
|
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
The MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2007 Victor Costan
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
22
|
+
|
data/Manifest
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
CHANGELOG
|
2
|
+
ext/smartcard_pcsc/extconf.rb
|
3
|
+
ext/smartcard_pcsc/pcsc.h
|
4
|
+
ext/smartcard_pcsc/pcsc_card.c
|
5
|
+
ext/smartcard_pcsc/pcsc_constants.c
|
6
|
+
ext/smartcard_pcsc/pcsc_context.c
|
7
|
+
ext/smartcard_pcsc/pcsc_io_request.c
|
8
|
+
ext/smartcard_pcsc/pcsc_main.c
|
9
|
+
ext/smartcard_pcsc/pcsc_multi_strings.c
|
10
|
+
ext/smartcard_pcsc/pcsc_namespace.c
|
11
|
+
ext/smartcard_pcsc/pcsc_reader_states.c
|
12
|
+
ext/smartcard_pcsc/pcsc_surrogate_reader.h
|
13
|
+
ext/smartcard_pcsc/pcsc_surrogate_wintypes.h
|
14
|
+
lib/smartcard.rb
|
15
|
+
LICENSE
|
16
|
+
README
|
17
|
+
test/test_all.rb
|
18
|
+
tests/ts_pcsc_ext.rb
|
19
|
+
Manifest
|
data/README
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
SUMMARY
|
2
|
+
|
3
|
+
'smartcard' aims to become the de-facto support library for smart-card development on ruby.
|
4
|
+
At the moment, 'smartcard' offers a PC/SC binding that is working on ruby 1.8, under
|
5
|
+
linux, osx, and windows. Future plans include a high level abstraction for the PC/SC binding
|
6
|
+
(and any other bindings people need), and an interface for Java cards.
|
7
|
+
|
8
|
+
LICENSE
|
9
|
+
|
10
|
+
'smartcard' is released under the MIT license. This means you're free to do whatever you want
|
11
|
+
with it. For the legalese version, please see the 'LICENSE' file.
|
12
|
+
|
13
|
+
DOCUMENTATION
|
14
|
+
|
15
|
+
The API is fully documented with RDoc. If you installed the 'smartcard' gem, you should be
|
16
|
+
able to use ri to see the documentation. If you're using the SVN version, you can use rdoc to
|
17
|
+
build the HTML and RI documentation yourself.
|
18
|
+
|
19
|
+
ACKNOWLEDGEMENTS
|
20
|
+
|
21
|
+
'smartcard' was developed for Victor Costan while working as a Research Assistant for MIT.
|
22
|
+
The work was sponsored by a grant from Quanta Computer Inc (www.quanta.com.tw)
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'mkmf'
|
2
|
+
|
3
|
+
$CFLAGS ||= ''
|
4
|
+
$LDFLAGS ||= ''
|
5
|
+
|
6
|
+
if RUBY_PLATFORM =~ /darwin/
|
7
|
+
$LDFLAGS += ' -framework PCSC'
|
8
|
+
elsif RUBY_PLATFORM =~ /win/
|
9
|
+
have_library('winscard')
|
10
|
+
else
|
11
|
+
# pcsc is retarded and uses stuff like '#include <wintypes.h>'
|
12
|
+
$CFLAGS += ' -I /usr/include/PCSC -I /usr/local/include/pcsc'
|
13
|
+
have_library('pcsclite')
|
14
|
+
end
|
15
|
+
|
16
|
+
pcsc_headers = []
|
17
|
+
['wintypes.h', 'reader.h', 'winscard.h', 'pcsclite.h'].each do |header|
|
18
|
+
['', 'PCSC/', './pcsc_surrogate_'].each do |path_prefix|
|
19
|
+
if have_header(path_prefix + header)
|
20
|
+
pcsc_headers.push((path_prefix[0,1] == '.') ? "\"#{path_prefix + header}\"" : "<#{path_prefix + header}>")
|
21
|
+
break
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
pcsc_defines = []
|
27
|
+
|
28
|
+
File.open('pcsc_autogen.h', 'w') do |f|
|
29
|
+
pcsc_defines.each { |d| f.write "\#define #{d}\n" }
|
30
|
+
pcsc_headers.each { |h| f.write "\#include #{h}\n" }
|
31
|
+
end
|
32
|
+
|
33
|
+
create_makefile('smartcard/pcsc')
|
34
|
+
|
35
|
+
def win32_hack(mf_name)
|
36
|
+
# evil, evil, evil -- hack the makefile to embed the manifest in the extension dll
|
37
|
+
make_contents = File.open(mf_name, 'r') { |f| f.read }
|
38
|
+
make_rules = make_contents.split(/(\n|\r)(\n|\r)+/)
|
39
|
+
new_make_rules = make_rules.map do |rule|
|
40
|
+
if rule =~ /^\$\(DLLIB\)\:/
|
41
|
+
rule + "\n\tmt.exe -manifest $(@).manifest -outputresource:$(@);2"
|
42
|
+
else
|
43
|
+
rule
|
44
|
+
end
|
45
|
+
end
|
46
|
+
File.open(mf_name, 'w') { |f| f.write new_make_rules.join("\n\n")}
|
47
|
+
end
|
48
|
+
|
49
|
+
case RUBY_PLATFORM
|
50
|
+
when /darwin/
|
51
|
+
when /win/
|
52
|
+
win32_hack 'Makefile'
|
53
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
/* Ruby extension API. */
|
2
|
+
#include <ruby.h>
|
3
|
+
/* Generated by 'extconf.rb' to point to the PC/SC header. */
|
4
|
+
#include "pcsc_autogen.h"
|
5
|
+
|
6
|
+
/* Entrypoint into the Ruby extension. */
|
7
|
+
void Init_pcsc();
|
8
|
+
|
9
|
+
/* Namespace structure. */
|
10
|
+
extern VALUE mSmartcard; /* Smartcard module / namespace */
|
11
|
+
extern VALUE mPcsc; /* Smartcard::PCSC module / namespace */
|
12
|
+
|
13
|
+
/* Class Smartcard::PCSC::ReaderStates */
|
14
|
+
extern VALUE cPcscReaderStates;
|
15
|
+
void Init_PCSC_ReaderStates();
|
16
|
+
int _PCSC_ReaderStates_lowlevel_get(VALUE rbReaderStates, SCARD_READERSTATE **reader_states, size_t *reader_states_count);
|
17
|
+
|
18
|
+
/* Class Smartcard::PCSC::IoRequest */
|
19
|
+
extern VALUE cPcscIoRequest;
|
20
|
+
void Init_PCSC_IoRequest();
|
21
|
+
int _PCSC_IoRequest_lowlevel_get(VALUE rbIoRequest, SCARD_IO_REQUEST **io_request);
|
22
|
+
|
23
|
+
/* Class Smartcard::PCSC::Context */
|
24
|
+
extern VALUE cPcscContext;
|
25
|
+
void Init_PCSC_Context();
|
26
|
+
int _PCSC_Context_lowlevel_get(VALUE rbContext, SCARDCONTEXT *pcsc_context);
|
27
|
+
|
28
|
+
/* Class Smartcard::PCSC::Card */
|
29
|
+
extern VALUE cPcscCard;
|
30
|
+
void Init_PCSC_Card();
|
31
|
+
int _PCSC_Card_lowlevel_get(VALUE rbCard, SCARDHANDLE *card_handle);
|
32
|
+
|
33
|
+
/* Constants in Smartcard::PCSC */
|
34
|
+
void Init_PCSC_Consts();
|
35
|
+
|
36
|
+
/* Multi-string (win32 abomination) tools. */
|
37
|
+
VALUE PCSC_Internal_multistring_to_ruby_array(char *mstr, size_t mstr_len);
|
38
|
+
int PCSC_Internal_ruby_strings_to_multistring(VALUE rbStrings, char **strings);
|
39
|
+
|
40
|
+
/* Messing up with constants that aren't uniformly defined. */
|
41
|
+
#if !defined(MAX_ATR_SIZE)
|
42
|
+
#define MAX_ATR_SIZE 32
|
43
|
+
#endif
|
44
|
+
#if !defined(SCARD_PROTOCOL_ANY)
|
45
|
+
#define SCARD_PROTOCOL_ANY (SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1)
|
46
|
+
#endif
|
@@ -0,0 +1,475 @@
|
|
1
|
+
#include "pcsc.h"
|
2
|
+
|
3
|
+
VALUE cPcscCard;
|
4
|
+
|
5
|
+
/* Wraps a SCARDHANDLE, tracking whether it was released or not, together with the last error that occured on it. */
|
6
|
+
struct SCardHandleEx {
|
7
|
+
SCARDHANDLE card_handle;
|
8
|
+
DWORD pcsc_error;
|
9
|
+
int released;
|
10
|
+
};
|
11
|
+
|
12
|
+
/* Custom free for Smartcard::PCSC::Card. Releases the card handle via a disconnect if that was not already done. */
|
13
|
+
static void PCSC_Card_free(struct SCardHandleEx *_card) {
|
14
|
+
if(_card != NULL) {
|
15
|
+
if(!_card->released)
|
16
|
+
SCardDisconnect(_card->card_handle, SCARD_LEAVE_CARD);
|
17
|
+
xfree(_card);
|
18
|
+
}
|
19
|
+
}
|
20
|
+
|
21
|
+
/* Custom allocation for Smartcard::PCSC::Card. Wraps a SCardHandleEx. */
|
22
|
+
static VALUE PCSC_Card_alloc(VALUE klass) {
|
23
|
+
struct SCardHandleEx *card;
|
24
|
+
|
25
|
+
VALUE rbCard = Data_Make_Struct(klass, struct SCardHandleEx, NULL, PCSC_Card_free, card);
|
26
|
+
card->pcsc_error = SCARD_S_SUCCESS;
|
27
|
+
card->released = 1;
|
28
|
+
return rbCard;
|
29
|
+
}
|
30
|
+
|
31
|
+
/* :Document-method: new
|
32
|
+
* call-seq:
|
33
|
+
* new(context, reader_name, share_mode, preferred_protocols) --> card
|
34
|
+
*
|
35
|
+
* Establishes a connection to the card in the reader whose friendly name is +reader_name+.
|
36
|
+
* The first connection will power up and perform a reset on the card.
|
37
|
+
* Wraps _SCardConnect_ in PC/SC.
|
38
|
+
*
|
39
|
+
* +context+:: the Smartcard::PCSC::Context to use to connect to the PC/SC resource manager
|
40
|
+
* +reader_name+:: friendly name of the reader to connect to; get using Smartcard::PCSC::Context#list_readers
|
41
|
+
* +share_mode+:: whether a shared or exclusive lock will be requested on the reader; use one of the Smartcard::PCSC::SHARE_ constants
|
42
|
+
* +preferred_protocols+:: desired protocol; use one of the Smartcard::PCSC::PROTOCOL_ constants
|
43
|
+
*/
|
44
|
+
static VALUE PCSC_Card_initialize(VALUE self, VALUE rbContext, VALUE rbReaderName, VALUE rbShareMode, VALUE rbPreferredProtocols) {
|
45
|
+
struct SCardHandleEx *card;
|
46
|
+
SCARDCONTEXT context;
|
47
|
+
VALUE rbFinalReaderName;
|
48
|
+
DWORD share_mode, preferred_protocols, active_protocol;
|
49
|
+
|
50
|
+
Data_Get_Struct(self, struct SCardHandleEx, card);
|
51
|
+
|
52
|
+
if(_PCSC_Context_lowlevel_get(rbContext, &context) == 0) {
|
53
|
+
rb_raise(rb_eArgError, "first argument is not a Context instance");
|
54
|
+
return self;
|
55
|
+
}
|
56
|
+
|
57
|
+
rbFinalReaderName = rb_check_string_type(rbReaderName);
|
58
|
+
if(NIL_P(rbFinalReaderName)) {
|
59
|
+
rb_raise(rb_eArgError, "second argument (should be reader name) does not convert to a String");
|
60
|
+
return self;
|
61
|
+
}
|
62
|
+
|
63
|
+
share_mode = NUM2UINT(rbShareMode);
|
64
|
+
preferred_protocols = NUM2UINT(rbPreferredProtocols);
|
65
|
+
|
66
|
+
card->pcsc_error = SCardConnect(context, RSTRING(rbFinalReaderName)->ptr, share_mode, preferred_protocols, &card->card_handle, &active_protocol);
|
67
|
+
if(card->pcsc_error != SCARD_S_SUCCESS)
|
68
|
+
rb_raise(rb_eRuntimeError, "SCardConnect: %s", pcsc_stringify_error(card->pcsc_error));
|
69
|
+
else
|
70
|
+
card->released = 0;
|
71
|
+
return self;
|
72
|
+
}
|
73
|
+
|
74
|
+
/* :Document-method: reconnect
|
75
|
+
* call-seq:
|
76
|
+
* card.reconnect(share_mode, preferred_protocols, initialization) --> self
|
77
|
+
*
|
78
|
+
* Reestablishes a connection to a reader that was previously connected to using Card#new.
|
79
|
+
* Wraps _SCardReconnect_ in PC/SC.
|
80
|
+
*
|
81
|
+
* +share_mode+:: whether a shared or exclusive lock will be requested on the reader; use one of the Smartcard::PCSC::SHARE_ constants
|
82
|
+
* +preferred_protocols+:: desired protocol; use one of the Smartcard::PCSC::PROTOCOL_ constants
|
83
|
+
* +initialization+:: action to be taken on the card inside the reader; use one of the Smartcard::PCSC::INITIALIZE_ constants
|
84
|
+
*/
|
85
|
+
static VALUE PCSC_Card_reconnect(VALUE self, VALUE rbShareMode, VALUE rbPreferredProtocols, VALUE rbInitialization) {
|
86
|
+
struct SCardHandleEx *card;
|
87
|
+
DWORD share_mode, preferred_protocols, initialization, active_protocol;
|
88
|
+
|
89
|
+
Data_Get_Struct(self, struct SCardHandleEx, card);
|
90
|
+
if(card == NULL) return self;
|
91
|
+
|
92
|
+
share_mode = NUM2UINT(rbShareMode);
|
93
|
+
preferred_protocols = NUM2UINT(rbPreferredProtocols);
|
94
|
+
initialization = NUM2UINT(rbInitialization);
|
95
|
+
|
96
|
+
card->pcsc_error = SCardReconnect(card->card_handle, share_mode, preferred_protocols, initialization, &active_protocol);
|
97
|
+
if(card->pcsc_error != SCARD_S_SUCCESS)
|
98
|
+
rb_raise(rb_eRuntimeError, "SCardReconnect: %s", pcsc_stringify_error(card->pcsc_error));
|
99
|
+
|
100
|
+
return self;
|
101
|
+
}
|
102
|
+
|
103
|
+
/* :Document-method: disconnect
|
104
|
+
* call-seq:
|
105
|
+
* context.disconnect(disposition) --> self
|
106
|
+
*
|
107
|
+
* Terminates the connection made using Card#new. The Card object is invalid afterwards.
|
108
|
+
* Wraps _SCardDisconnect_ in PC/SC.
|
109
|
+
*
|
110
|
+
* +disposition+:: action to be taken on the card inside the reader; use one of the Smartcard::PCSC::DISPOSITION_ constants
|
111
|
+
*/
|
112
|
+
static VALUE PCSC_Card_disconnect(VALUE self, VALUE rbDisposition) {
|
113
|
+
struct SCardHandleEx *card;
|
114
|
+
DWORD disposition;
|
115
|
+
|
116
|
+
Data_Get_Struct(self, struct SCardHandleEx, card);
|
117
|
+
if(card == NULL) return self;
|
118
|
+
|
119
|
+
disposition = NUM2UINT(rbDisposition);
|
120
|
+
if(!card->released) {
|
121
|
+
card->pcsc_error = SCardDisconnect(card->card_handle, disposition);
|
122
|
+
card->released = 1;
|
123
|
+
if(card->pcsc_error != SCARD_S_SUCCESS)
|
124
|
+
rb_raise(rb_eRuntimeError, "SCardDisconnect: %s", pcsc_stringify_error(card->pcsc_error));
|
125
|
+
}
|
126
|
+
return self;
|
127
|
+
}
|
128
|
+
|
129
|
+
/* :Document-method: begin_transaction
|
130
|
+
* call-seq:
|
131
|
+
* card.begin_transaction() --> self
|
132
|
+
*
|
133
|
+
* Establishes a temporary exclusive access mode for doing a series of commands or transaction.
|
134
|
+
* Wraps _SCardBeginTransaction_ in PC/SC.
|
135
|
+
*/
|
136
|
+
static VALUE PCSC_Card_begin_transaction(VALUE self) {
|
137
|
+
struct SCardHandleEx *card;
|
138
|
+
|
139
|
+
Data_Get_Struct(self, struct SCardHandleEx, card);
|
140
|
+
if(card == NULL) return self;
|
141
|
+
|
142
|
+
card->pcsc_error = SCardBeginTransaction(card->card_handle);
|
143
|
+
if(card->pcsc_error != SCARD_S_SUCCESS)
|
144
|
+
rb_raise(rb_eRuntimeError, "SCardBeginTransaction: %s", pcsc_stringify_error(card->pcsc_error));
|
145
|
+
return self;
|
146
|
+
}
|
147
|
+
|
148
|
+
/* :Document-method: end_transaction
|
149
|
+
* call-seq:
|
150
|
+
* context.end_transaction(disposition) --> attribute_value
|
151
|
+
*
|
152
|
+
* Ends a previously begun transaction. The calling application must be the owner of the previously begun transaction or an error will occur.
|
153
|
+
* Wraps _SCardEndTransaction_ in PC/SC.
|
154
|
+
*
|
155
|
+
* +disposition+:: action to be taken on the card inside the reader; use one of the Smartcard::PCSC::DISPOSITION_ constants
|
156
|
+
*/
|
157
|
+
static VALUE PCSC_Card_end_transaction(VALUE self, VALUE rbDisposition) {
|
158
|
+
struct SCardHandleEx *card;
|
159
|
+
DWORD disposition;
|
160
|
+
|
161
|
+
Data_Get_Struct(self, struct SCardHandleEx, card);
|
162
|
+
if(card == NULL) return self;
|
163
|
+
|
164
|
+
disposition = NUM2UINT(rbDisposition);
|
165
|
+
card->pcsc_error = SCardEndTransaction(card->card_handle, disposition);
|
166
|
+
if(card->pcsc_error != SCARD_S_SUCCESS)
|
167
|
+
rb_raise(rb_eRuntimeError, "SCardEndTransaction: %s", pcsc_stringify_error(card->pcsc_error));
|
168
|
+
return self;
|
169
|
+
}
|
170
|
+
|
171
|
+
/* :Document-method: get_attribute
|
172
|
+
* call-seq:
|
173
|
+
* card.get_attribute(attribute_id) --> attribute_value
|
174
|
+
*
|
175
|
+
* Reads the value of an attribute from the interface driver.
|
176
|
+
* Remember that the IFD may not implement some of the attributes specified in Smartcard::PCSC, and it may implement some attributes that
|
177
|
+
* are not included in Smartcard::PCSC.
|
178
|
+
* Wraps _SCardGetAttrib_ in PC/SC.
|
179
|
+
*
|
180
|
+
* The returned value has the bytes in the attribute, wrapped in a string. (don't complain, it's a low-level API)
|
181
|
+
*
|
182
|
+
* +attribute_id+:: identifies the attribute to be read; use one of the Smartcard::PCSC::ATTR_ constants
|
183
|
+
*/
|
184
|
+
static VALUE PCSC_Card_get_attribute(VALUE self, VALUE rbAttributeId) {
|
185
|
+
struct SCardHandleEx *card;
|
186
|
+
char *attribute_buffer;
|
187
|
+
DWORD attribute_id, attribute_length;
|
188
|
+
VALUE rbAttribute;
|
189
|
+
|
190
|
+
Data_Get_Struct(self, struct SCardHandleEx, card);
|
191
|
+
if(card == NULL) return Qnil;
|
192
|
+
|
193
|
+
attribute_id = NUM2UINT(rbAttributeId);
|
194
|
+
attribute_length;
|
195
|
+
card->pcsc_error = SCardGetAttrib(card->card_handle, attribute_id, NULL, &attribute_length);
|
196
|
+
if(card->pcsc_error == SCARD_S_SUCCESS) {
|
197
|
+
attribute_buffer = ALLOC_N(char, attribute_length);
|
198
|
+
if(attribute_buffer != NULL) {
|
199
|
+
card->pcsc_error = SCardGetAttrib(card->card_handle, attribute_id, (LPSTR)attribute_buffer, &attribute_length);
|
200
|
+
if(card->pcsc_error == SCARD_S_SUCCESS) {
|
201
|
+
rbAttribute = rb_str_new(attribute_buffer, attribute_length);
|
202
|
+
xfree(attribute_buffer);
|
203
|
+
return rbAttribute;
|
204
|
+
}
|
205
|
+
}
|
206
|
+
}
|
207
|
+
if(card->pcsc_error != SCARD_S_SUCCESS)
|
208
|
+
rb_raise(rb_eRuntimeError, "SCardGetAttrib: %s", pcsc_stringify_error(card->pcsc_error));
|
209
|
+
return Qnil;
|
210
|
+
}
|
211
|
+
|
212
|
+
/* :Document-method: set_attribute
|
213
|
+
* call-seq:
|
214
|
+
* context.set_attribute(attribute_id, attribute_value) --> self
|
215
|
+
*
|
216
|
+
* Sets the value of an attribute in the interface driver.
|
217
|
+
* Remember that the IFD may not implement some of the attributes specified in Smartcard::PCSC, and it may implement some attributes that
|
218
|
+
* are not included in Smartcard::PCSC.
|
219
|
+
* Wraps _SCardSetAttrib_ in PC/SC.
|
220
|
+
*
|
221
|
+
* +attribute_id+:: identifies the attribute to be set; use one of the Smartcard::PCSC::ATTR_ constants
|
222
|
+
* +attribute_value+:: the value to be assigned to the attribute; wrap the bytes in a string-like object (low-level API, remember?)
|
223
|
+
*/
|
224
|
+
static VALUE PCSC_Card_set_attribute(VALUE self, VALUE rbAttributeId, VALUE rbAttributeValue) {
|
225
|
+
struct SCardHandleEx *card;
|
226
|
+
DWORD attribute_id;
|
227
|
+
VALUE rbFinalAttributeValue;
|
228
|
+
|
229
|
+
Data_Get_Struct(self, struct SCardHandleEx, card);
|
230
|
+
if(card == NULL) return self;
|
231
|
+
|
232
|
+
attribute_id = NUM2UINT(rbAttributeId);
|
233
|
+
|
234
|
+
rbFinalAttributeValue = rb_check_string_type(rbAttributeValue);
|
235
|
+
if(NIL_P(rbFinalAttributeValue)) {
|
236
|
+
rb_raise(rb_eArgError, "second argument (attribute buffer) does not convert to a String");
|
237
|
+
return self;
|
238
|
+
}
|
239
|
+
|
240
|
+
card->pcsc_error = SCardSetAttrib(card->card_handle, attribute_id, (LPSTR)RSTRING(rbFinalAttributeValue)->ptr, RSTRING(rbFinalAttributeValue)->len);
|
241
|
+
if(card->pcsc_error != SCARD_S_SUCCESS)
|
242
|
+
rb_raise(rb_eRuntimeError, "SCardSetAttrib: %s", pcsc_stringify_error(card->pcsc_error));
|
243
|
+
return self;
|
244
|
+
}
|
245
|
+
|
246
|
+
/* :Document-method: transmit
|
247
|
+
* call-seq:
|
248
|
+
* card.transmit(send_data, send_io_request, recv_io_request) --> recv_data
|
249
|
+
*
|
250
|
+
* Sends an APDU to the smart card, and returns the card's response to the APDU.
|
251
|
+
* Wraps _SCardTransmit_ in PC/SC.
|
252
|
+
*
|
253
|
+
* The bytes in the card's response are returned wrapped in a string. (don't complain, it's a low-level API)
|
254
|
+
*
|
255
|
+
* +send_data+:: the APDU to be send to the card; wrap the bytes in a string-like object (low-level API, remember?)
|
256
|
+
* +send_io_request+:: Smartcard::PCSC::IoRequest instance indicating the send protocol; you can use one of the Smartcard::PCSC::PCI_ constants
|
257
|
+
* +recv_io_request+:: Smartcard::PCSC::IoRequest instance receving information about the recv protocol; you can use the result of Smartcard::PCSC::IoRequest#new
|
258
|
+
*/
|
259
|
+
static VALUE PCSC_Card_transmit(VALUE self, VALUE rbSendData, VALUE rbSendIoRequest, VALUE rbRecvIoRequest) {
|
260
|
+
struct SCardHandleEx *card;
|
261
|
+
VALUE rbFinalSendData, rbRecvData;
|
262
|
+
SCARD_IO_REQUEST *send_io_request, *recv_io_request;
|
263
|
+
char *recv_buffer;
|
264
|
+
DWORD recv_length;
|
265
|
+
|
266
|
+
Data_Get_Struct(self, struct SCardHandleEx, card);
|
267
|
+
if(card == NULL) return Qnil;
|
268
|
+
|
269
|
+
rbFinalSendData = rb_check_string_type(rbSendData);
|
270
|
+
if(NIL_P(rbFinalSendData)) {
|
271
|
+
rb_raise(rb_eArgError, "first argument (send buffer) does not convert to a String");
|
272
|
+
return Qnil;
|
273
|
+
}
|
274
|
+
|
275
|
+
if(_PCSC_IoRequest_lowlevel_get(rbSendIoRequest, &send_io_request) == 0) {
|
276
|
+
rb_raise(rb_eArgError, "second argument (send io request) is not an IoRequest instance");
|
277
|
+
return Qnil;
|
278
|
+
}
|
279
|
+
if(_PCSC_IoRequest_lowlevel_get(rbRecvIoRequest, &recv_io_request) == 0) {
|
280
|
+
rb_raise(rb_eArgError, "second argument (recv io request) is not an IoRequest instance");
|
281
|
+
return Qnil;
|
282
|
+
}
|
283
|
+
|
284
|
+
#if defined(PCSCLITE_MAX_MESSAGE_SIZE)
|
285
|
+
recv_length = PCSCLITE_MAX_MESSAGE_SIZE;
|
286
|
+
#elif defined(MAX_BUFFER_SIZE_EXTENDED)
|
287
|
+
recv_length = MAX_BUFFER_SIZE_EXTENDED;
|
288
|
+
#else
|
289
|
+
recv_length = 65536;
|
290
|
+
#endif
|
291
|
+
recv_buffer = ALLOC_N(char, recv_length);
|
292
|
+
if(recv_buffer == NULL) return Qnil;
|
293
|
+
|
294
|
+
card->pcsc_error = SCardTransmit(card->card_handle, send_io_request,
|
295
|
+
(LPSTR)RSTRING(rbFinalSendData)->ptr, RSTRING(rbFinalSendData)->len,
|
296
|
+
recv_io_request, (LPSTR)recv_buffer, &recv_length);
|
297
|
+
if(card->pcsc_error != SCARD_S_SUCCESS) {
|
298
|
+
xfree(recv_buffer);
|
299
|
+
rb_raise(rb_eRuntimeError, "SCardTransmit: %s", pcsc_stringify_error(card->pcsc_error));
|
300
|
+
return Qnil;
|
301
|
+
}
|
302
|
+
|
303
|
+
rbRecvData = rb_str_new(recv_buffer, recv_length);
|
304
|
+
xfree(recv_buffer);
|
305
|
+
return rbRecvData;
|
306
|
+
}
|
307
|
+
|
308
|
+
/* :Document-method: control
|
309
|
+
* call-seq:
|
310
|
+
* card.control(control_code, send_data, max_recv_bytes) --> recv_data
|
311
|
+
*
|
312
|
+
* Sends a command directly to the interface driver to be processed by the reader.
|
313
|
+
* Useful for creating client side reader drivers for functions like PIN pads, biometrics, or other smart card reader
|
314
|
+
* extensions that are not normally handled by PC/SC.
|
315
|
+
* Wraps _SCardControl_ in PC/SC.
|
316
|
+
*
|
317
|
+
* The bytes in the response are returned wrapped in a string. (don't complain, it's a low-level API)
|
318
|
+
*
|
319
|
+
* +control_code+:: control code for the operation; it's an integer, and it's IFD-specific
|
320
|
+
* +send_data+:: the data bytes to be send to the driver; wrap the bytes in a string-like object (low-level API, remember?)
|
321
|
+
* +max_recv_bytes+:: the maximum number of bytes that can be received
|
322
|
+
*
|
323
|
+
* In general, I tried to avoid having you specify receive buffer sizes. This is the only case where that is impossible to achieve,
|
324
|
+
* because there is no well-known maximum buffer size, and the _SCardControl_ call is not guaranteed to be idempotent, so it's not OK to
|
325
|
+
* re-issue it until the buffer size works out.
|
326
|
+
*/
|
327
|
+
static VALUE PCSC_Card_control(VALUE self, VALUE rbControlCode, VALUE rbSendData, VALUE rbMaxRecvBytes) {
|
328
|
+
struct SCardHandleEx *card;
|
329
|
+
VALUE rbFinalSendData, rbRecvData;
|
330
|
+
char *recv_buffer;
|
331
|
+
DWORD control_code, recv_length;
|
332
|
+
|
333
|
+
Data_Get_Struct(self, struct SCardHandleEx, card);
|
334
|
+
if(card == NULL) return Qnil;
|
335
|
+
|
336
|
+
rbFinalSendData = rb_check_string_type(rbSendData);
|
337
|
+
if(NIL_P(rbFinalSendData)) {
|
338
|
+
rb_raise(rb_eArgError, "second argument (send buffer) does not convert to a String");
|
339
|
+
return Qnil;
|
340
|
+
}
|
341
|
+
|
342
|
+
control_code = NUM2UINT(rbControlCode);
|
343
|
+
recv_length = NUM2UINT(rbMaxRecvBytes);
|
344
|
+
recv_buffer = ALLOC_N(char, recv_length);
|
345
|
+
if(recv_buffer == NULL) return Qnil;
|
346
|
+
|
347
|
+
card->pcsc_error = SCardControl(card->card_handle, control_code,
|
348
|
+
(LPSTR)RSTRING(rbFinalSendData)->ptr, RSTRING(rbFinalSendData)->len,
|
349
|
+
recv_buffer, recv_length, &recv_length);
|
350
|
+
if(card->pcsc_error != SCARD_S_SUCCESS) {
|
351
|
+
xfree(recv_buffer);
|
352
|
+
rb_raise(rb_eRuntimeError, "SCardControl: %s", pcsc_stringify_error(card->pcsc_error));
|
353
|
+
return Qnil;
|
354
|
+
}
|
355
|
+
|
356
|
+
rbRecvData = rb_str_new(recv_buffer, recv_length);
|
357
|
+
xfree(recv_buffer);
|
358
|
+
return rbRecvData;
|
359
|
+
}
|
360
|
+
|
361
|
+
static VALUE _rbStateKey, _rbProtocolKey, _rbAtrKey, _rbReaderNamesKey;
|
362
|
+
|
363
|
+
/* :Document-method: status
|
364
|
+
* call-seq:
|
365
|
+
* card.status() --> card_status
|
366
|
+
*
|
367
|
+
* Retrieves the current status of the smartcard, and packages it up in a nice hash for you.
|
368
|
+
* Wraps _SCardStatus_ in PC/SC.
|
369
|
+
*
|
370
|
+
* The response hash contains the following keys:
|
371
|
+
* <tt>:state</tt> :: reader/card status; bitfield, with bits defined as Smartcard::PCSC::STATUS_ constants
|
372
|
+
* <tt>:protocol</tt> :: the protocol established with the card; check against Smartcard::PCSC::PROTOCOL_ constants
|
373
|
+
* <tt>:atr</tt> :: the card's ATR bytes, wrapped in a string
|
374
|
+
* <tt>:reader_names</tt> :: array of strings containing all the names of the reader containing the smartcard
|
375
|
+
*/
|
376
|
+
static VALUE PCSC_Card_status(VALUE self) {
|
377
|
+
struct SCardHandleEx *card;
|
378
|
+
char *atr_buffer, *reader_names_buffer;
|
379
|
+
DWORD atr_length, reader_names_length, state, protocol;
|
380
|
+
VALUE rbStateVal, rbProtocolVal, rbAtrVal, rbReaderNamesVal, rbReturnHash;
|
381
|
+
|
382
|
+
Data_Get_Struct(self, struct SCardHandleEx, card);
|
383
|
+
if(card == NULL) return Qnil;
|
384
|
+
|
385
|
+
atr_length = MAX_ATR_SIZE;
|
386
|
+
atr_buffer = ALLOC_N(char, atr_length);
|
387
|
+
reader_names_length = 4096;
|
388
|
+
reader_names_buffer = ALLOC_N(char, reader_names_length);
|
389
|
+
if(atr_buffer == NULL || reader_names_buffer == NULL) {
|
390
|
+
if(reader_names_buffer != NULL) xfree(reader_names_buffer);
|
391
|
+
if(atr_buffer != NULL) xfree(atr_buffer);
|
392
|
+
return Qnil;
|
393
|
+
}
|
394
|
+
|
395
|
+
card->pcsc_error = SCardStatus(card->card_handle, reader_names_buffer, &reader_names_length, &state, &protocol, (LPSTR)atr_buffer, &atr_length);
|
396
|
+
if(card->pcsc_error != SCARD_S_SUCCESS) {
|
397
|
+
xfree(reader_names_buffer); xfree(atr_buffer);
|
398
|
+
rb_raise(rb_eRuntimeError, "SCardStatus: %s", pcsc_stringify_error(card->pcsc_error));
|
399
|
+
return Qnil;
|
400
|
+
}
|
401
|
+
|
402
|
+
rbStateVal = UINT2NUM(state);
|
403
|
+
rbProtocolVal = UINT2NUM(protocol);
|
404
|
+
rbAtrVal = rb_str_new(atr_buffer, atr_length);
|
405
|
+
rbReaderNamesVal = PCSC_Internal_multistring_to_ruby_array(reader_names_buffer, reader_names_length);
|
406
|
+
|
407
|
+
rbReturnHash = rb_hash_new();
|
408
|
+
rb_hash_aset(rbReturnHash, _rbStateKey, rbStateVal);
|
409
|
+
rb_hash_aset(rbReturnHash, _rbProtocolKey, rbProtocolVal);
|
410
|
+
rb_hash_aset(rbReturnHash, _rbAtrKey, rbAtrVal);
|
411
|
+
rb_hash_aset(rbReturnHash, _rbReaderNamesKey, rbReaderNamesVal);
|
412
|
+
return rbReturnHash;
|
413
|
+
}
|
414
|
+
|
415
|
+
/* :Document-method: last_error
|
416
|
+
* call-seq:
|
417
|
+
* card.last_error() --> last_error
|
418
|
+
*
|
419
|
+
* The error code returned by the last PC/SC call. Useful for recovering from exceptions.
|
420
|
+
*
|
421
|
+
* The returned code is a number, and should be one of the Smartcard::PCSC::SCARD_ constants.
|
422
|
+
* The code indicating correct operation is Smartcard::PCSC::SCARD_S_SUCCESS.
|
423
|
+
*/
|
424
|
+
static VALUE PCSC_Card_last_error(VALUE self) {
|
425
|
+
struct SCardHandleEx *card;
|
426
|
+
|
427
|
+
Data_Get_Struct(self, struct SCardHandleEx, card);
|
428
|
+
if(card == NULL) return Qnil;
|
429
|
+
|
430
|
+
return UINT2NUM(card->pcsc_error);
|
431
|
+
}
|
432
|
+
|
433
|
+
#ifdef MAKE_RDOC_HAPPY
|
434
|
+
mSmartcard = rb_define_module("Smartcard");
|
435
|
+
mPcsc = rb_define_module_under(mSmartcard, "PCSC");
|
436
|
+
#endif
|
437
|
+
|
438
|
+
/* :Document-class: Smartcard::PCSC::Card
|
439
|
+
* Connects a smart-card in a PC/SC reader to the Ruby world.
|
440
|
+
* Wraps a _SCARDHANDLE_ structure.
|
441
|
+
*/
|
442
|
+
void Init_PCSC_Card() {
|
443
|
+
ID state_id, protocol_id, atr_id, reader_names_id;
|
444
|
+
|
445
|
+
state_id = rb_intern("status"); _rbStateKey = ID2SYM(state_id);
|
446
|
+
protocol_id = rb_intern("protocol"); _rbProtocolKey = ID2SYM(protocol_id);
|
447
|
+
atr_id = rb_intern("atr"); _rbAtrKey = ID2SYM(atr_id);
|
448
|
+
reader_names_id = rb_intern("reader_names"); _rbReaderNamesKey = ID2SYM(reader_names_id);
|
449
|
+
|
450
|
+
cPcscCard = rb_define_class_under(mPcsc, "Card", rb_cObject);
|
451
|
+
rb_define_alloc_func(cPcscCard, PCSC_Card_alloc);
|
452
|
+
rb_define_method(cPcscCard, "initialize", PCSC_Card_initialize, 4);
|
453
|
+
rb_define_method(cPcscCard, "disconnect", PCSC_Card_disconnect, 1);
|
454
|
+
rb_define_method(cPcscCard, "reconnect", PCSC_Card_reconnect, 3);
|
455
|
+
rb_define_method(cPcscCard, "begin_transaction", PCSC_Card_begin_transaction, 0);
|
456
|
+
rb_define_method(cPcscCard, "end_transaction", PCSC_Card_end_transaction, 1);
|
457
|
+
rb_define_method(cPcscCard, "get_attribute", PCSC_Card_get_attribute, 1);
|
458
|
+
rb_define_method(cPcscCard, "set_attribute", PCSC_Card_set_attribute, 2);
|
459
|
+
rb_define_method(cPcscCard, "transmit", PCSC_Card_transmit, 3);
|
460
|
+
rb_define_method(cPcscCard, "control", PCSC_Card_control, 3);
|
461
|
+
rb_define_method(cPcscCard, "status", PCSC_Card_status, 0);
|
462
|
+
rb_define_method(cPcscCard, "last_error", PCSC_Card_last_error, 0);
|
463
|
+
}
|
464
|
+
|
465
|
+
/* Retrieves the SCARDHANDLE wrapped into a Smartcard::PCSC::Card instance. */
|
466
|
+
int _PCSC_Card_lowlevel_get(VALUE rbCard, SCARDHANDLE *card_handle) {
|
467
|
+
struct SCardHandleEx *card;
|
468
|
+
|
469
|
+
if(TYPE(rbCard) != T_DATA || RDATA(rbCard)->dfree != (void (*)(void *))PCSC_Card_free)
|
470
|
+
return 0;
|
471
|
+
|
472
|
+
Data_Get_Struct(rbCard, struct SCardHandleEx, card);
|
473
|
+
*card_handle = card->card_handle;
|
474
|
+
return 1;
|
475
|
+
}
|