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/reader_monitor.h
CHANGED
|
@@ -7,8 +7,11 @@
|
|
|
7
7
|
#include <vector>
|
|
8
8
|
#include <string>
|
|
9
9
|
#include <mutex>
|
|
10
|
+
#include <condition_variable>
|
|
10
11
|
#include <unordered_map>
|
|
12
|
+
#include <cstddef>
|
|
11
13
|
#include "platform/pcsc.h"
|
|
14
|
+
#include "reader_state_utils.h"
|
|
12
15
|
|
|
13
16
|
/**
|
|
14
17
|
* ReaderMonitor - Native PC/SC event monitoring using ThreadSafeFunction
|
|
@@ -27,8 +30,6 @@ public:
|
|
|
27
30
|
~ReaderMonitor();
|
|
28
31
|
|
|
29
32
|
private:
|
|
30
|
-
static Napi::FunctionReference constructor;
|
|
31
|
-
|
|
32
33
|
// PC/SC context
|
|
33
34
|
SCARDCONTEXT context_;
|
|
34
35
|
bool contextValid_;
|
|
@@ -38,11 +39,31 @@ private:
|
|
|
38
39
|
std::atomic<bool> running_;
|
|
39
40
|
std::mutex mutex_;
|
|
40
41
|
|
|
42
|
+
// One event on its way from the monitor thread to the JS callback
|
|
43
|
+
struct EventData;
|
|
44
|
+
|
|
45
|
+
// Called by Node for every queued event: delivers it to the JS
|
|
46
|
+
// callback, then frees it. Node also calls it with no env to drain the
|
|
47
|
+
// queue when the environment is torn down first, so the event is freed
|
|
48
|
+
// either way.
|
|
49
|
+
static void DeliverEvent(Napi::Env env, Napi::Function callback,
|
|
50
|
+
std::nullptr_t* context, EventData* data);
|
|
51
|
+
using EventTsfn =
|
|
52
|
+
Napi::TypedThreadSafeFunction<std::nullptr_t, EventData, DeliverEvent>;
|
|
53
|
+
|
|
41
54
|
// Thread-safe function for emitting events. tsfnActive_ tracks whether
|
|
42
55
|
// tsfn_ holds a live handle that still needs Release().
|
|
43
|
-
|
|
56
|
+
EventTsfn tsfn_;
|
|
44
57
|
bool tsfnActive_;
|
|
45
58
|
|
|
59
|
+
// Shutdown() wakes the monitor thread from the pause between retries
|
|
60
|
+
// on the error path, so stop() needn't wait it out
|
|
61
|
+
std::mutex pauseMutex_;
|
|
62
|
+
std::condition_variable pauseCv_;
|
|
63
|
+
|
|
64
|
+
// OnEnvCleanup is registered with Node from start() until Shutdown()
|
|
65
|
+
bool cleanupHookAdded_;
|
|
66
|
+
|
|
46
67
|
// Current known reader states (Issue #111: keyed by reader name for reliable lookup)
|
|
47
68
|
struct ReaderInfo {
|
|
48
69
|
DWORD lastState;
|
|
@@ -51,6 +72,10 @@ private:
|
|
|
51
72
|
std::vector<uint8_t> atr;
|
|
52
73
|
};
|
|
53
74
|
std::unordered_map<std::string, ReaderInfo> readerStates_;
|
|
75
|
+
// A recovery's resync couldn't read the readers afresh, so the cards
|
|
76
|
+
// whose handles died in the outage are still to be reported (by the
|
|
77
|
+
// next resync that can)
|
|
78
|
+
bool outagePending_;
|
|
54
79
|
|
|
55
80
|
// JavaScript methods
|
|
56
81
|
Napi::Value Start(const Napi::CallbackInfo& info);
|
|
@@ -58,13 +83,19 @@ private:
|
|
|
58
83
|
Napi::Value GetIsRunning(const Napi::CallbackInfo& info);
|
|
59
84
|
|
|
60
85
|
// Internal methods
|
|
86
|
+
void Shutdown();
|
|
87
|
+
static void OnEnvCleanup(void* arg);
|
|
61
88
|
void MonitorLoop(std::shared_ptr<std::atomic<bool>> sessionActive);
|
|
62
|
-
|
|
63
|
-
void ResyncReaderList();
|
|
64
|
-
|
|
89
|
+
bool UpdateReaderList();
|
|
90
|
+
void ResyncReaderList(bool acrossOutage = false);
|
|
91
|
+
LONG RecoverContext();
|
|
92
|
+
void DetachAllReaders();
|
|
93
|
+
void PauseBeforeRetry(const std::shared_ptr<std::atomic<bool>>& sessionActive);
|
|
65
94
|
// code: the PC/SC error code, for "error" events only
|
|
66
95
|
void EmitEvent(const std::string& eventType, const std::string& readerName,
|
|
67
96
|
DWORD state, const std::vector<uint8_t>& atr, DWORD code = 0);
|
|
68
97
|
void ApplyCardStateChange(ReaderInfo& info, const std::string& readerName,
|
|
69
98
|
const SCARD_READERSTATE& readerState);
|
|
99
|
+
void EmitCardEvent(smartcard::CardEvent event, const std::string& readerName,
|
|
100
|
+
DWORD state, const std::vector<uint8_t>& atr);
|
|
70
101
|
};
|
package/src/reader_state_utils.h
CHANGED
|
@@ -4,21 +4,61 @@
|
|
|
4
4
|
|
|
5
5
|
namespace smartcard {
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
// Replaced: a card was present at both readings, but it was taken out and a
|
|
8
|
+
// card (maybe the same one) put back in between them
|
|
9
|
+
enum class CardEvent { None, Inserted, Removed, Replaced };
|
|
10
|
+
|
|
11
|
+
// pcsc-lite counts card insertions and removals per reader in bits 16-31 of
|
|
12
|
+
// dwEventState ("can be used to detect a card removal/insertion between two
|
|
13
|
+
// calls to SCardGetStatusChange()", winscard_clnt.c)
|
|
14
|
+
inline uint64_t CardEventCount(uint64_t state) {
|
|
15
|
+
return (state >> 16) & 0xFFFF;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// Did pcsc-lite count card events between two readings? The counter only
|
|
19
|
+
// goes up (wrapping at 16 bits), but starts again at 0 when pcscd re-creates
|
|
20
|
+
// the reader - plugged back in, or pcscd restarted - without counting a card
|
|
21
|
+
// that is already in it. So only a forward move counts: a restarted counter
|
|
22
|
+
// reads as a large backward jump, and is not mistaken for a swap.
|
|
23
|
+
inline bool CardEventsCountedBetween(uint64_t oldState, uint64_t newState) {
|
|
24
|
+
uint64_t forward = (CardEventCount(newState) - CardEventCount(oldState)) & 0xFFFF;
|
|
25
|
+
return forward != 0 && forward < 0x8000;
|
|
26
|
+
}
|
|
8
27
|
|
|
9
28
|
// presentMask is the platform's SCARD_STATE_PRESENT bit, passed in by the
|
|
10
29
|
// caller. This header deliberately includes no PC/SC headers so the logic is
|
|
11
30
|
// testable in isolation - receiving the mask instead of mirroring its value
|
|
12
31
|
// means there is no hand-typed copy of the constant to drift out of sync
|
|
13
32
|
// (the bug class behind the original 0x10/0x20 inversion).
|
|
33
|
+
//
|
|
34
|
+
// countsEvents: the platform keeps the card event counter above
|
|
35
|
+
// (SMARTCARD_PCSC_COUNTS_CARD_EVENTS). Without it, a card swapped between
|
|
36
|
+
// two readings is indistinguishable from the same card staying put.
|
|
14
37
|
inline CardEvent DetectCardStateChange(uint64_t oldState, uint64_t newState,
|
|
15
|
-
uint64_t presentMask) {
|
|
38
|
+
uint64_t presentMask, bool countsEvents) {
|
|
16
39
|
bool wasPresent = (oldState & presentMask) != 0;
|
|
17
40
|
bool isPresent = (newState & presentMask) != 0;
|
|
18
41
|
|
|
19
42
|
if (!wasPresent && isPresent) return CardEvent::Inserted;
|
|
20
43
|
if (wasPresent && !isPresent) return CardEvent::Removed;
|
|
44
|
+
if (wasPresent && countsEvents && CardEventsCountedBetween(oldState, newState)) {
|
|
45
|
+
return CardEvent::Replaced;
|
|
46
|
+
}
|
|
21
47
|
return CardEvent::None;
|
|
22
48
|
}
|
|
23
49
|
|
|
50
|
+
// The same, comparing a reading from before a PC/SC service outage with
|
|
51
|
+
// one from after it. Every card handle opened before the outage died with
|
|
52
|
+
// it, so a card present at both readings is reported as Replaced: JS is
|
|
53
|
+
// told the old card is gone and connects to it again. That also covers a
|
|
54
|
+
// card swapped during the outage, which presence and pcscd's restarted
|
|
55
|
+
// counters can't show.
|
|
56
|
+
inline CardEvent DetectCardStateChangeAcrossOutage(uint64_t oldState, uint64_t newState,
|
|
57
|
+
uint64_t presentMask) {
|
|
58
|
+
bool wasPresent = (oldState & presentMask) != 0;
|
|
59
|
+
bool isPresent = (newState & presentMask) != 0;
|
|
60
|
+
if (wasPresent && isPresent) return CardEvent::Replaced;
|
|
61
|
+
return DetectCardStateChange(oldState, newState, presentMask, false);
|
|
62
|
+
}
|
|
63
|
+
|
|
24
64
|
} // namespace smartcard
|