smartcard 3.7.2 → 3.8.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/src/pcsc_errors.h CHANGED
@@ -40,6 +40,41 @@ inline const char* GetPCSCErrorString(LONG code) {
40
40
  if (ucode == SCARD_W_UNRESPONSIVE_CARD) return "Card is unresponsive";
41
41
  if (ucode == SCARD_W_UNSUPPORTED_CARD) return "Card is not supported";
42
42
 
43
+ // Extended mappings - numeric literals because macro availability differs
44
+ // between winscard.h and pcsc-lite while the code values are shared
45
+ // (MS SCARD error space). Note: 0x8010001F is SCARD_E_UNEXPECTED on
46
+ // Windows but SCARD_E_UNSUPPORTED_FEATURE on pcsc-lite.
47
+ if (ucode == 0x80100007) return "Waited too long"; // SCARD_F_WAITED_TOO_LONG
48
+ if (ucode == 0x80100014) return "Unknown internal error"; // SCARD_F_UNKNOWN_ERROR
49
+ if (ucode == 0x80100018) return "Service shutting down"; // SCARD_P_SHUTDOWN
50
+ if (ucode == 0x8010001A) return "Reader unsupported"; // SCARD_E_READER_UNSUPPORTED
51
+ if (ucode == 0x8010001B) return "Duplicate reader name"; // SCARD_E_DUPLICATE_READER
52
+ if (ucode == 0x8010001C) return "Card unsupported"; // SCARD_E_CARD_UNSUPPORTED
53
+ if (ucode == 0x8010001F) return "Unexpected error / unsupported feature"; // SCARD_E_UNEXPECTED (win) / SCARD_E_UNSUPPORTED_FEATURE (pcsc-lite)
54
+ if (ucode == 0x80100020) return "ICC installation error"; // SCARD_E_ICC_INSTALLATION
55
+ if (ucode == 0x80100021) return "ICC create-order error"; // SCARD_E_ICC_CREATEORDER
56
+ if (ucode == 0x80100022) return "Unsupported feature"; // SCARD_E_UNSUPPORTED_FEATURE (win)
57
+ if (ucode == 0x80100023) return "Directory not found"; // SCARD_E_DIR_NOT_FOUND
58
+ if (ucode == 0x80100024) return "File not found"; // SCARD_E_FILE_NOT_FOUND
59
+ if (ucode == 0x80100025) return "No directory"; // SCARD_E_NO_DIR
60
+ if (ucode == 0x80100026) return "No file"; // SCARD_E_NO_FILE
61
+ if (ucode == 0x80100027) return "Access denied"; // SCARD_E_NO_ACCESS
62
+ if (ucode == 0x80100028) return "Too many writes"; // SCARD_E_WRITE_TOO_MANY
63
+ if (ucode == 0x80100029) return "Bad seek"; // SCARD_E_BAD_SEEK
64
+ if (ucode == 0x8010002A) return "Invalid CHV/PIN"; // SCARD_E_INVALID_CHV
65
+ if (ucode == 0x8010002B) return "Unknown resource manager"; // SCARD_E_UNKNOWN_RES_MNG
66
+ if (ucode == 0x8010002C) return "No such certificate"; // SCARD_E_NO_SUCH_CERTIFICATE
67
+ if (ucode == 0x8010002D) return "Certificate unavailable"; // SCARD_E_CERTIFICATE_UNAVAILABLE
68
+ if (ucode == 0x8010002F) return "Communication data lost"; // SCARD_E_COMM_DATA_LOST
69
+ if (ucode == 0x80100030) return "No key container"; // SCARD_E_NO_KEY_CONTAINER
70
+ if (ucode == 0x80100031) return "Server too busy"; // SCARD_E_SERVER_TOO_BUSY
71
+ if (ucode == 0x8010006A) return "Security violation"; // SCARD_W_SECURITY_VIOLATION
72
+ if (ucode == 0x8010006B) return "Wrong CHV/PIN"; // SCARD_W_WRONG_CHV
73
+ if (ucode == 0x8010006C) return "CHV/PIN blocked"; // SCARD_W_CHV_BLOCKED
74
+ if (ucode == 0x8010006D) return "End of file"; // SCARD_W_EOF
75
+ if (ucode == 0x8010006E) return "Cancelled by user"; // SCARD_W_CANCELLED_BY_USER
76
+ if (ucode == 0x8010006F) return "Card not authenticated"; // SCARD_W_CARD_NOT_AUTHENTICATED
77
+
43
78
  return "Unknown PC/SC error";
44
79
  }
45
80
 
@@ -0,0 +1,43 @@
1
+ #pragma once
2
+
3
+ #include <napi.h>
4
+ #include <string>
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
+ };
14
+
15
+ // Build a JS Error for a PC/SC failure. The code is exposed unsigned
16
+ // (e.g. 0x80100069) to match the constants in lib/errors.ts; message
17
+ // defaults to the description of the code.
18
+ inline Napi::Error NewPCSCError(Napi::Env env, LONG code, const char* message = nullptr) {
19
+ std::string text = message != nullptr ? message : GetPCSCErrorString(code);
20
+ Napi::Number ucode = Napi::Number::New(env, static_cast<double>(static_cast<DWORD>(code)));
21
+
22
+ AddonData* data = env.GetInstanceData<AddonData>();
23
+ if (data != nullptr && !data->errorFactory.IsEmpty()) {
24
+ try {
25
+ Napi::Value err = data->errorFactory.Call({Napi::String::New(env, text), ucode});
26
+ if (err.IsObject()) {
27
+ return Napi::Error(env, err);
28
+ }
29
+ } catch (const Napi::Error&) {
30
+ // Factory threw - fall back to a plain coded Error
31
+ }
32
+ }
33
+
34
+ Napi::Error err = Napi::Error::New(env, text);
35
+ err.Set("code", ucode);
36
+ return err;
37
+ }
38
+
39
+ // For a handle this library already knows is unusable (disconnected card,
40
+ // closed context): coded as the SCARD_E_INVALID_HANDLE PC/SC would return
41
+ inline Napi::Error NewInvalidHandleError(Napi::Env env, const char* message) {
42
+ return NewPCSCError(env, static_cast<LONG>(SCARD_E_INVALID_HANDLE), message);
43
+ }
@@ -1,6 +1,7 @@
1
1
  #include "pcsc_reader.h"
2
2
  #include "pcsc_card.h"
3
3
  #include "pcsc_errors.h"
4
+ #include "pcsc_napi_error.h"
4
5
  #include "async_workers.h"
5
6
 
6
7
  Napi::FunctionReference PCSCReader::constructor;
@@ -20,12 +21,12 @@ Napi::Object PCSCReader::Init(Napi::Env env, Napi::Object exports) {
20
21
  return exports;
21
22
  }
22
23
 
23
- Napi::Object PCSCReader::NewInstance(Napi::Env env, SCARDCONTEXT context,
24
+ Napi::Object PCSCReader::NewInstance(Napi::Env env, ContextHandlePtr context,
24
25
  const std::string& name, DWORD state,
25
26
  const std::vector<uint8_t>& atr) {
26
27
  Napi::Object obj = constructor.New({});
27
28
  PCSCReader* reader = Napi::ObjectWrap<PCSCReader>::Unwrap(obj);
28
- reader->context_ = context;
29
+ reader->context_ = std::move(context);
29
30
  reader->name_ = name;
30
31
  reader->state_ = state;
31
32
  reader->atr_ = atr;
@@ -34,7 +35,6 @@ Napi::Object PCSCReader::NewInstance(Napi::Env env, SCARDCONTEXT context,
34
35
 
35
36
  PCSCReader::PCSCReader(const Napi::CallbackInfo& info)
36
37
  : Napi::ObjectWrap<PCSCReader>(info),
37
- context_(0),
38
38
  state_(SCARD_STATE_UNAWARE) {
39
39
  // Properties set via NewInstance
40
40
  }
@@ -66,6 +66,13 @@ Napi::Value PCSCReader::GetAtrValue(const Napi::CallbackInfo& info) {
66
66
  Napi::Value PCSCReader::Connect(const Napi::CallbackInfo& info) {
67
67
  Napi::Env env = info.Env();
68
68
 
69
+ // The context may have been closed since listReaders(); connecting on
70
+ // its released handle would reach whatever PC/SC reassigned it to
71
+ if (!context_ || !context_->IsValid()) {
72
+ NewInvalidHandleError(env, "Context is not valid").ThrowAsJavaScriptException();
73
+ return env.Null();
74
+ }
75
+
69
76
  // Parse arguments: shareMode (optional), preferredProtocols (optional)
70
77
  DWORD shareMode = SCARD_SHARE_SHARED;
71
78
  DWORD preferredProtocols = SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1;
@@ -82,7 +89,8 @@ Napi::Value PCSCReader::Connect(const Napi::CallbackInfo& info) {
82
89
  Napi::Promise::Deferred deferred = Napi::Promise::Deferred::New(env);
83
90
 
84
91
  ConnectWorker* worker = new ConnectWorker(
85
- env, context_, name_, shareMode, preferredProtocols, deferred);
92
+ env, info.This().As<Napi::Object>(), context_, name_, shareMode,
93
+ preferredProtocols, deferred);
86
94
  worker->Queue();
87
95
 
88
96
  return deferred.Promise();
package/src/pcsc_reader.h CHANGED
@@ -4,11 +4,12 @@
4
4
  #include <string>
5
5
  #include <vector>
6
6
  #include "platform/pcsc.h"
7
+ #include "pcsc_context_handle.h"
7
8
 
8
9
  class PCSCReader : public Napi::ObjectWrap<PCSCReader> {
9
10
  public:
10
11
  static Napi::Object Init(Napi::Env env, Napi::Object exports);
11
- static Napi::Object NewInstance(Napi::Env env, SCARDCONTEXT context,
12
+ static Napi::Object NewInstance(Napi::Env env, ContextHandlePtr context,
12
13
  const std::string& name, DWORD state,
13
14
  const std::vector<uint8_t>& atr);
14
15
 
@@ -18,7 +19,7 @@ public:
18
19
  const std::string& GetName() const { return name_; }
19
20
  DWORD GetState() const { return state_; }
20
21
  const std::vector<uint8_t>& GetAtr() const { return atr_; }
21
- SCARDCONTEXT GetContext() const { return context_; }
22
+ SCARDCONTEXT GetContext() const { return context_ ? context_->Get() : 0; }
22
23
 
23
24
  // Update state (called from monitoring)
24
25
  void UpdateState(DWORD state, const std::vector<uint8_t>& atr);
@@ -27,7 +28,10 @@ private:
27
28
  static Napi::FunctionReference constructor;
28
29
 
29
30
  std::string name_;
30
- SCARDCONTEXT context_;
31
+ // Shared with the PCSCContext that created this reader, so the handle
32
+ // outlives the Context object; checked before use, since close() can
33
+ // still release it
34
+ ContextHandlePtr context_;
31
35
  DWORD state_;
32
36
  std::vector<uint8_t> atr_;
33
37