smartcard 3.8.0 → 3.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -0
- package/binding.gyp +5 -1
- package/dist/lib/devices.d.ts +14 -0
- package/dist/lib/devices.d.ts.map +1 -1
- package/dist/lib/devices.js +54 -23
- package/dist/lib/devices.js.map +1 -1
- package/dist/lib/types.d.ts +4 -2
- package/dist/lib/types.d.ts.map +1 -1
- package/package.json +2 -1
- package/src/addon.cpp +4 -1
- package/src/addon_data.h +23 -0
- package/src/async_workers.cpp +161 -16
- package/src/async_workers.h +88 -21
- package/src/pcsc_card.cpp +3 -5
- package/src/pcsc_card.h +0 -2
- package/src/pcsc_context.cpp +25 -30
- package/src/pcsc_context.h +7 -3
- package/src/pcsc_napi_error.h +1 -8
- package/src/pcsc_reader.cpp +3 -5
- package/src/pcsc_reader.h +0 -2
- package/src/platform/pcsc.h +22 -0
- package/src/reader_monitor.cpp +339 -162
- package/src/reader_monitor.h +37 -6
- package/src/reader_state_utils.h +42 -2
package/src/async_workers.h
CHANGED
|
@@ -3,39 +3,106 @@
|
|
|
3
3
|
#include <napi.h>
|
|
4
4
|
#include <vector>
|
|
5
5
|
#include <string>
|
|
6
|
+
#include <thread>
|
|
7
|
+
#include <mutex>
|
|
8
|
+
#include <condition_variable>
|
|
9
|
+
#include <memory>
|
|
10
|
+
#include <unordered_set>
|
|
6
11
|
#include "platform/pcsc.h"
|
|
7
12
|
#include "pcsc_context_handle.h"
|
|
8
13
|
|
|
9
|
-
// Every
|
|
10
|
-
// PC/SC handle (Card
|
|
11
|
-
// owner while Execute() is blocked in a PC/SC call: the finalizer
|
|
12
|
-
// or releases the handle out from under the worker, and PC/SC
|
|
13
|
-
// values, so a late call can even land on a different card.
|
|
14
|
-
//
|
|
15
|
-
|
|
16
|
-
//
|
|
17
|
-
|
|
14
|
+
// Every AsyncWorker below holds a Napi::ObjectReference to the JS object
|
|
15
|
+
// that owns its PC/SC handle (Card or Reader). Without it, GC can finalize
|
|
16
|
+
// the owner while Execute() is blocked in a PC/SC call: the finalizer
|
|
17
|
+
// disconnects or releases the handle out from under the worker, and PC/SC
|
|
18
|
+
// reuses handle values, so a late call can even land on a different card.
|
|
19
|
+
// The reference is released on the main thread once the result is
|
|
20
|
+
// delivered (OnOK/OnError). WaitForChangeWorker needs none: its context is
|
|
21
|
+
// its own.
|
|
22
|
+
|
|
23
|
+
// waitForChange(): SCardGetStatusChange on a thread of its own, not as a
|
|
24
|
+
// Napi::AsyncWorker on libuv's threadpool, because the wait can last
|
|
25
|
+
// forever (with no timeout, the default):
|
|
26
|
+
// - The threadpool is shared by the whole process, 4 threads by default.
|
|
27
|
+
// Four pending waits stopped every fs, dns, crypto and zlib call, and
|
|
28
|
+
// this addon's transmit() and connect(), until one of them returned.
|
|
29
|
+
// - Node tears an environment down (a worker exiting, or terminated) only
|
|
30
|
+
// once its threadpool work has finished, and runs cleanup hooks only
|
|
31
|
+
// after that, so nothing could cancel the wait: the worker never ended,
|
|
32
|
+
// and nor did the process, whose exit waits for its workers.
|
|
33
|
+
// Off the threadpool the wait doesn't hold up teardown, and OnEnvCleanup
|
|
34
|
+
// cancels it.
|
|
35
|
+
//
|
|
36
|
+
// Each wait also has a PC/SC context of its own, established and released
|
|
37
|
+
// on its thread. pcsc-lite holds a context's lock for as long as a wait on
|
|
38
|
+
// it lasts, and a call that needs the lock waits for it while holding the
|
|
39
|
+
// library's global lock: on the waiting Context, listReaders() and close()
|
|
40
|
+
// blocked the main thread until the wait ended (with no timeout, forever),
|
|
41
|
+
// and a second wait, or a connect(), blocked every PC/SC call in the
|
|
42
|
+
// process, cancel() included.
|
|
43
|
+
class WaitForChangeWorker {
|
|
18
44
|
public:
|
|
45
|
+
// The waits pending on one Context, for cancel() and close(). Main
|
|
46
|
+
// thread only; shared by the Context and its waits.
|
|
47
|
+
using PendingWaits = std::unordered_set<WaitForChangeWorker*>;
|
|
48
|
+
using PendingWaitsPtr = std::shared_ptr<PendingWaits>;
|
|
49
|
+
|
|
50
|
+
// Start the wait; returns the promise it settles
|
|
51
|
+
static Napi::Promise Start(Napi::Env env,
|
|
52
|
+
PendingWaitsPtr pendingWaits,
|
|
53
|
+
std::vector<std::string> readerNames,
|
|
54
|
+
std::vector<DWORD> currentStates,
|
|
55
|
+
DWORD timeout);
|
|
56
|
+
|
|
57
|
+
// End the wait, whether or not it has begun: one that hasn't returned
|
|
58
|
+
// yet resolves with null. Returns once it has returned, normally at
|
|
59
|
+
// once; gives up after about a second if SCardCancel can't end it.
|
|
60
|
+
// Main thread.
|
|
61
|
+
void Cancel();
|
|
62
|
+
|
|
63
|
+
private:
|
|
19
64
|
WaitForChangeWorker(Napi::Env env,
|
|
20
|
-
|
|
21
|
-
SCARDCONTEXT context,
|
|
65
|
+
PendingWaitsPtr pendingWaits,
|
|
22
66
|
std::vector<std::string> readerNames,
|
|
23
67
|
std::vector<DWORD> currentStates,
|
|
24
|
-
DWORD timeout
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
void Execute()
|
|
28
|
-
|
|
29
|
-
|
|
68
|
+
DWORD timeout);
|
|
69
|
+
|
|
70
|
+
// On thread_: the wait, then releasing tsfn_
|
|
71
|
+
void Execute();
|
|
72
|
+
// Set cancelled_, then cancel the wait until it has returned, trying
|
|
73
|
+
// at most maxAttempts times, 5 ms apart (0: no limit)
|
|
74
|
+
void CancelUntilReturned(int maxAttempts);
|
|
75
|
+
// The tsfn's finalizer, on the main thread: Node calls it once
|
|
76
|
+
// Execute() has released the tsfn, or at environment teardown
|
|
77
|
+
static void OnDone(Napi::Env env, void*, WaitForChangeWorker* self);
|
|
78
|
+
static void OnEnvCleanup(void* arg);
|
|
79
|
+
void Settle(Napi::Env env);
|
|
80
|
+
|
|
81
|
+
// Only ever released, never called: OnDone delivers the result
|
|
82
|
+
using DoneTsfn = Napi::TypedThreadSafeFunction<WaitForChangeWorker>;
|
|
30
83
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
SCARDCONTEXT context_;
|
|
84
|
+
Napi::Promise::Deferred deferred_;
|
|
85
|
+
PendingWaitsPtr pendingWaits_;
|
|
34
86
|
std::vector<std::string> readerNames_;
|
|
35
87
|
std::vector<SCARD_READERSTATE> states_;
|
|
36
88
|
DWORD timeout_;
|
|
37
89
|
LONG result_;
|
|
38
|
-
|
|
90
|
+
DoneTsfn tsfn_;
|
|
91
|
+
std::thread thread_;
|
|
92
|
+
// Guards the rest
|
|
93
|
+
std::mutex mutex_;
|
|
94
|
+
// This wait's own context, while thread_ holds it
|
|
95
|
+
SCARDCONTEXT context_;
|
|
96
|
+
bool contextValid_;
|
|
97
|
+
// Execute() is done with the context: the wait has returned
|
|
98
|
+
std::condition_variable waitedCv_;
|
|
99
|
+
bool waited_;
|
|
100
|
+
// Cancel() or OnEnvCleanup: end the wait, or don't begin it
|
|
101
|
+
bool cancelled_;
|
|
102
|
+
// OnEnvCleanup is registered with Node from Start() until OnDone
|
|
103
|
+
bool cleanupHookAdded_;
|
|
104
|
+
// OnEnvCleanup ran: the environment can't run JS any more
|
|
105
|
+
bool envTornDown_;
|
|
39
106
|
};
|
|
40
107
|
|
|
41
108
|
// Async worker for SCardTransmit
|
package/src/pcsc_card.cpp
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
#include "pcsc_card.h"
|
|
2
2
|
#include "pcsc_errors.h"
|
|
3
3
|
#include "pcsc_napi_error.h"
|
|
4
|
+
#include "addon_data.h"
|
|
4
5
|
#include "async_workers.h"
|
|
5
6
|
#include <cstring>
|
|
6
7
|
|
|
7
|
-
Napi::FunctionReference PCSCCard::constructor;
|
|
8
|
-
|
|
9
8
|
Napi::Object PCSCCard::Init(Napi::Env env, Napi::Object exports) {
|
|
10
9
|
Napi::Function func = DefineClass(env, "Card", {
|
|
11
10
|
InstanceAccessor("protocol", &PCSCCard::GetProtocolValue, nullptr),
|
|
@@ -18,8 +17,7 @@ Napi::Object PCSCCard::Init(Napi::Env env, Napi::Object exports) {
|
|
|
18
17
|
InstanceMethod("reconnect", &PCSCCard::Reconnect),
|
|
19
18
|
});
|
|
20
19
|
|
|
21
|
-
|
|
22
|
-
constructor.SuppressDestruct();
|
|
20
|
+
env.GetInstanceData<AddonData>()->cardConstructor = Napi::Persistent(func);
|
|
23
21
|
|
|
24
22
|
exports.Set("Card", func);
|
|
25
23
|
return exports;
|
|
@@ -28,7 +26,7 @@ Napi::Object PCSCCard::Init(Napi::Env env, Napi::Object exports) {
|
|
|
28
26
|
Napi::Object PCSCCard::NewInstance(Napi::Env env, ContextHandlePtr context,
|
|
29
27
|
SCARDHANDLE card, DWORD protocol,
|
|
30
28
|
const std::string& readerName) {
|
|
31
|
-
Napi::Object obj =
|
|
29
|
+
Napi::Object obj = env.GetInstanceData<AddonData>()->cardConstructor.New({});
|
|
32
30
|
PCSCCard* cardObj = Napi::ObjectWrap<PCSCCard>::Unwrap(obj);
|
|
33
31
|
cardObj->context_ = std::move(context);
|
|
34
32
|
cardObj->card_ = card;
|
package/src/pcsc_card.h
CHANGED
|
@@ -25,8 +25,6 @@ public:
|
|
|
25
25
|
bool IsConnected() const { return connected_ && context_ && context_->IsValid(); }
|
|
26
26
|
|
|
27
27
|
private:
|
|
28
|
-
static Napi::FunctionReference constructor;
|
|
29
|
-
|
|
30
28
|
// Keeps the context alive for as long as this card; destroyed after
|
|
31
29
|
// ~PCSCCard's body, so the card disconnects before the context goes
|
|
32
30
|
ContextHandlePtr context_;
|
package/src/pcsc_context.cpp
CHANGED
|
@@ -5,8 +5,6 @@
|
|
|
5
5
|
#include "async_workers.h"
|
|
6
6
|
#include <cstring>
|
|
7
7
|
|
|
8
|
-
Napi::FunctionReference PCSCContext::constructor;
|
|
9
|
-
|
|
10
8
|
Napi::Object PCSCContext::Init(Napi::Env env, Napi::Object exports) {
|
|
11
9
|
Napi::Function func = DefineClass(env, "Context", {
|
|
12
10
|
InstanceMethod("listReaders", &PCSCContext::ListReaders),
|
|
@@ -16,19 +14,13 @@ Napi::Object PCSCContext::Init(Napi::Env env, Napi::Object exports) {
|
|
|
16
14
|
InstanceAccessor("isValid", &PCSCContext::GetIsValid, nullptr),
|
|
17
15
|
});
|
|
18
16
|
|
|
19
|
-
constructor = Napi::Persistent(func);
|
|
20
|
-
constructor.SuppressDestruct();
|
|
21
|
-
|
|
22
17
|
exports.Set("Context", func);
|
|
23
18
|
return exports;
|
|
24
19
|
}
|
|
25
20
|
|
|
26
|
-
Napi::Object PCSCContext::NewInstance(Napi::Env env) {
|
|
27
|
-
return constructor.New({});
|
|
28
|
-
}
|
|
29
|
-
|
|
30
21
|
PCSCContext::PCSCContext(const Napi::CallbackInfo& info)
|
|
31
|
-
: Napi::ObjectWrap<PCSCContext>(info)
|
|
22
|
+
: Napi::ObjectWrap<PCSCContext>(info),
|
|
23
|
+
pendingWaits_(std::make_shared<WaitForChangeWorker::PendingWaits>()) {
|
|
32
24
|
|
|
33
25
|
Napi::Env env = info.Env();
|
|
34
26
|
|
|
@@ -178,37 +170,40 @@ Napi::Value PCSCContext::WaitForChange(const Napi::CallbackInfo& info) {
|
|
|
178
170
|
}
|
|
179
171
|
}
|
|
180
172
|
}
|
|
173
|
+
if (result != SCARD_S_SUCCESS &&
|
|
174
|
+
result != static_cast<LONG>(SCARD_E_NO_READERS_AVAILABLE)) {
|
|
175
|
+
// No list to wait on: a wait on no readers would return at
|
|
176
|
+
// once, as if it had timed out
|
|
177
|
+
Napi::Promise::Deferred deferred = Napi::Promise::Deferred::New(env);
|
|
178
|
+
deferred.Reject(NewPCSCError(env, result).Value());
|
|
179
|
+
return deferred.Promise();
|
|
180
|
+
}
|
|
181
181
|
}
|
|
182
182
|
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
WaitForChangeWorker* worker = new WaitForChangeWorker(
|
|
187
|
-
env, info.This().As<Napi::Object>(), context, readerNames, currentStates,
|
|
188
|
-
timeout, deferred);
|
|
189
|
-
worker->Queue();
|
|
190
|
-
|
|
191
|
-
return deferred.Promise();
|
|
183
|
+
return WaitForChangeWorker::Start(
|
|
184
|
+
env, pendingWaits_, std::move(readerNames), std::move(currentStates), timeout);
|
|
192
185
|
}
|
|
193
186
|
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
if (!IsValid()) {
|
|
198
|
-
return env.Undefined();
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
LONG result = SCardCancel(handle_->Get());
|
|
202
|
-
if (result != SCARD_S_SUCCESS && result != static_cast<LONG>(SCARD_E_INVALID_HANDLE)) {
|
|
203
|
-
NewPCSCError(env, result).ThrowAsJavaScriptException();
|
|
187
|
+
void PCSCContext::CancelPendingWaits() {
|
|
188
|
+
for (WaitForChangeWorker* wait : *pendingWaits_) {
|
|
189
|
+
wait->Cancel();
|
|
204
190
|
}
|
|
191
|
+
}
|
|
205
192
|
|
|
206
|
-
|
|
193
|
+
// Ends every waitForChange() pending on this context: one that hasn't
|
|
194
|
+
// returned yet resolves with null. A wait has a context of its own, so
|
|
195
|
+
// this works even once close() has released this one.
|
|
196
|
+
Napi::Value PCSCContext::Cancel(const Napi::CallbackInfo& info) {
|
|
197
|
+
CancelPendingWaits();
|
|
198
|
+
return info.Env().Undefined();
|
|
207
199
|
}
|
|
208
200
|
|
|
209
201
|
Napi::Value PCSCContext::Close(const Napi::CallbackInfo& info) {
|
|
210
202
|
Napi::Env env = info.Env();
|
|
211
203
|
|
|
204
|
+
// Pending waits end as if cancelled
|
|
205
|
+
CancelPendingWaits();
|
|
206
|
+
|
|
212
207
|
// Release now, even if readers or cards still hold the handle: they
|
|
213
208
|
// see it as invalid from here on and stop using it
|
|
214
209
|
if (handle_) {
|
package/src/pcsc_context.h
CHANGED
|
@@ -5,11 +5,11 @@
|
|
|
5
5
|
#include <vector>
|
|
6
6
|
#include "platform/pcsc.h"
|
|
7
7
|
#include "pcsc_context_handle.h"
|
|
8
|
+
#include "async_workers.h"
|
|
8
9
|
|
|
9
10
|
class PCSCContext : public Napi::ObjectWrap<PCSCContext> {
|
|
10
11
|
public:
|
|
11
12
|
static Napi::Object Init(Napi::Env env, Napi::Object exports);
|
|
12
|
-
static Napi::Object NewInstance(Napi::Env env);
|
|
13
13
|
|
|
14
14
|
PCSCContext(const Napi::CallbackInfo& info);
|
|
15
15
|
|
|
@@ -18,12 +18,16 @@ public:
|
|
|
18
18
|
bool IsValid() const { return handle_ && handle_->IsValid(); }
|
|
19
19
|
|
|
20
20
|
private:
|
|
21
|
-
static Napi::FunctionReference constructor;
|
|
22
|
-
|
|
23
21
|
// Shared with the Readers (and their Cards) created from this context;
|
|
24
22
|
// released when the last of them is finalized, or by close()
|
|
25
23
|
ContextHandlePtr handle_;
|
|
26
24
|
|
|
25
|
+
// waitForChange() calls still pending, each on a context of its own:
|
|
26
|
+
// cancel() and close() end them
|
|
27
|
+
WaitForChangeWorker::PendingWaitsPtr pendingWaits_;
|
|
28
|
+
|
|
29
|
+
void CancelPendingWaits();
|
|
30
|
+
|
|
27
31
|
// JavaScript-exposed methods
|
|
28
32
|
Napi::Value ListReaders(const Napi::CallbackInfo& info);
|
|
29
33
|
Napi::Value WaitForChange(const Napi::CallbackInfo& info);
|
package/src/pcsc_napi_error.h
CHANGED
|
@@ -3,14 +3,7 @@
|
|
|
3
3
|
#include <napi.h>
|
|
4
4
|
#include <string>
|
|
5
5
|
#include "pcsc_errors.h"
|
|
6
|
-
|
|
7
|
-
// Per-instance addon state (each worker_thread loads its own instance)
|
|
8
|
-
struct AddonData {
|
|
9
|
-
// (message, code) => Error, registered by lib/index.ts via
|
|
10
|
-
// setErrorFactory() so native failures arrive as the typed PCSCError
|
|
11
|
-
// classes from lib/errors.ts
|
|
12
|
-
Napi::FunctionReference errorFactory;
|
|
13
|
-
};
|
|
6
|
+
#include "addon_data.h"
|
|
14
7
|
|
|
15
8
|
// Build a JS Error for a PC/SC failure. The code is exposed unsigned
|
|
16
9
|
// (e.g. 0x80100069) to match the constants in lib/errors.ts; message
|
package/src/pcsc_reader.cpp
CHANGED
|
@@ -2,10 +2,9 @@
|
|
|
2
2
|
#include "pcsc_card.h"
|
|
3
3
|
#include "pcsc_errors.h"
|
|
4
4
|
#include "pcsc_napi_error.h"
|
|
5
|
+
#include "addon_data.h"
|
|
5
6
|
#include "async_workers.h"
|
|
6
7
|
|
|
7
|
-
Napi::FunctionReference PCSCReader::constructor;
|
|
8
|
-
|
|
9
8
|
Napi::Object PCSCReader::Init(Napi::Env env, Napi::Object exports) {
|
|
10
9
|
Napi::Function func = DefineClass(env, "Reader", {
|
|
11
10
|
InstanceAccessor("name", &PCSCReader::GetNameValue, nullptr),
|
|
@@ -14,8 +13,7 @@ Napi::Object PCSCReader::Init(Napi::Env env, Napi::Object exports) {
|
|
|
14
13
|
InstanceMethod("connect", &PCSCReader::Connect),
|
|
15
14
|
});
|
|
16
15
|
|
|
17
|
-
|
|
18
|
-
constructor.SuppressDestruct();
|
|
16
|
+
env.GetInstanceData<AddonData>()->readerConstructor = Napi::Persistent(func);
|
|
19
17
|
|
|
20
18
|
exports.Set("Reader", func);
|
|
21
19
|
return exports;
|
|
@@ -24,7 +22,7 @@ Napi::Object PCSCReader::Init(Napi::Env env, Napi::Object exports) {
|
|
|
24
22
|
Napi::Object PCSCReader::NewInstance(Napi::Env env, ContextHandlePtr context,
|
|
25
23
|
const std::string& name, DWORD state,
|
|
26
24
|
const std::vector<uint8_t>& atr) {
|
|
27
|
-
Napi::Object obj =
|
|
25
|
+
Napi::Object obj = env.GetInstanceData<AddonData>()->readerConstructor.New({});
|
|
28
26
|
PCSCReader* reader = Napi::ObjectWrap<PCSCReader>::Unwrap(obj);
|
|
29
27
|
reader->context_ = std::move(context);
|
|
30
28
|
reader->name_ = name;
|
package/src/pcsc_reader.h
CHANGED
|
@@ -25,8 +25,6 @@ public:
|
|
|
25
25
|
void UpdateState(DWORD state, const std::vector<uint8_t>& atr);
|
|
26
26
|
|
|
27
27
|
private:
|
|
28
|
-
static Napi::FunctionReference constructor;
|
|
29
|
-
|
|
30
28
|
std::string name_;
|
|
31
29
|
// Shared with the PCSCContext that created this reader, so the handle
|
|
32
30
|
// outlives the Context object; checked before use, since close() can
|
package/src/platform/pcsc.h
CHANGED
|
@@ -37,5 +37,27 @@
|
|
|
37
37
|
#define MAX_ATR_SIZE 33
|
|
38
38
|
#endif
|
|
39
39
|
|
|
40
|
+
// pcsc-lite keeps a per-reader card insertion/removal counter in the upper
|
|
41
|
+
// 16 bits of dwEventState. Windows and macOS don't document that word for
|
|
42
|
+
// reader entries (Windows uses it for the reader count on the PnP entry),
|
|
43
|
+
// so it is only relied on with pcsc-lite.
|
|
44
|
+
#if defined(_WIN32) || defined(__APPLE__)
|
|
45
|
+
#define SMARTCARD_PCSC_COUNTS_CARD_EVENTS false
|
|
46
|
+
#else
|
|
47
|
+
#define SMARTCARD_PCSC_COUNTS_CARD_EVENTS true
|
|
48
|
+
#endif
|
|
49
|
+
|
|
50
|
+
// pcsc-lite 2.3.0 and later put a count of reader events (readers added or
|
|
51
|
+
// removed) in the upper 16 bits of the "\\?PnP?\Notification" entry's
|
|
52
|
+
// dwEventState. Passed back as its dwCurrentState, the next call returns at
|
|
53
|
+
// once if readers changed in between. Older pcsc-lite leaves the word 0,
|
|
54
|
+
// and a 0 count turns the check off. Windows reports the reader count there
|
|
55
|
+
// instead, untested here; macOS doesn't support the PnP entry.
|
|
56
|
+
#if defined(_WIN32) || defined(__APPLE__)
|
|
57
|
+
#define SMARTCARD_PCSC_PNP_COUNTS_READER_EVENTS false
|
|
58
|
+
#else
|
|
59
|
+
#define SMARTCARD_PCSC_PNP_COUNTS_READER_EVENTS true
|
|
60
|
+
#endif
|
|
61
|
+
|
|
40
62
|
// Helper macro for unused parameters
|
|
41
63
|
#define PCSC_UNUSED(x) (void)(x)
|