nfets 0.0.134 → 0.0.136

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.
@@ -0,0 +1,81 @@
1
+ #pragma once
2
+
3
+ #include <node_api.h>
4
+
5
+ #include <windows.h>
6
+ #include <wincrypt.h>
7
+
8
+ #include <stdexcept>
9
+ #include <string>
10
+ #include <vector>
11
+
12
+ inline std::vector<BYTE> ReadCertificateDer(napi_env env, napi_value value)
13
+ {
14
+ if (value == nullptr)
15
+ return {};
16
+
17
+ napi_valuetype type;
18
+ if (napi_typeof(env, value, &type) != napi_ok)
19
+ return {};
20
+
21
+ if (type == napi_undefined || type == napi_null)
22
+ return {};
23
+
24
+ bool is_buffer = false;
25
+ if (napi_is_buffer(env, value, &is_buffer) != napi_ok || !is_buffer)
26
+ throw std::runtime_error("Certificate DER must be a Buffer");
27
+
28
+ void *data = nullptr;
29
+ size_t length = 0;
30
+ if (napi_get_buffer_info(env, value, &data, &length) != napi_ok ||
31
+ data == nullptr || length == 0)
32
+ return {};
33
+
34
+ const auto *bytes = static_cast<const BYTE *>(data);
35
+ return std::vector<BYTE>(bytes, bytes + length);
36
+ }
37
+
38
+ inline PCCERT_CONTEXT FindCertificateInStore(
39
+ HCERTSTORE store,
40
+ const std::wstring &subject,
41
+ const std::vector<BYTE> &der)
42
+ {
43
+ if (!der.empty())
44
+ {
45
+ PCCERT_CONTEXT needle = CertCreateCertificateContext(
46
+ X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
47
+ der.data(),
48
+ static_cast<DWORD>(der.size()));
49
+
50
+ if (!needle)
51
+ throw std::runtime_error("Failed to parse certificate DER");
52
+
53
+ PCCERT_CONTEXT found = CertFindCertificateInStore(
54
+ store,
55
+ X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
56
+ 0,
57
+ CERT_FIND_EXISTING,
58
+ needle,
59
+ NULL);
60
+
61
+ CertFreeCertificateContext(needle);
62
+
63
+ if (!found)
64
+ throw std::runtime_error("Certificate not found in store");
65
+
66
+ return found;
67
+ }
68
+
69
+ PCCERT_CONTEXT found = CertFindCertificateInStore(
70
+ store,
71
+ X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
72
+ 0,
73
+ CERT_FIND_SUBJECT_STR_W,
74
+ (LPVOID)subject.c_str(),
75
+ NULL);
76
+
77
+ if (!found)
78
+ throw std::runtime_error("Certificate not found in store");
79
+
80
+ return found;
81
+ }
@@ -1,5 +1,7 @@
1
1
  #include <node_api.h>
2
2
 
3
+ #include "find-certificate-in-store.h"
4
+
3
5
  #include <windows.h>
4
6
  #include <wincrypt.h>
5
7
  #include <ncrypt.h>
@@ -24,14 +26,14 @@ std::wstring utf8_to_wstring(const std::string &str)
24
26
 
25
27
  napi_value SignDataWithCertificate(napi_env env, napi_callback_info info)
26
28
  {
27
- size_t argc = 3;
28
- napi_value args[3];
29
+ size_t argc = 4;
30
+ napi_value args[4] = {};
29
31
  napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
30
32
 
31
33
  if (argc < 3)
32
34
  {
33
35
  napi_value error_msg;
34
- napi_create_string_utf8(env, "Expected: subject (string), data (Buffer), algorithm (string)", NAPI_AUTO_LENGTH, &error_msg);
36
+ napi_create_string_utf8(env, "Expected: subject (string), data (Buffer), algorithm (string), certificateDer (Buffer, optional)", NAPI_AUTO_LENGTH, &error_msg);
35
37
  napi_value error;
36
38
  napi_create_type_error(env, nullptr, error_msg, &error);
37
39
  napi_throw(env, error);
@@ -46,7 +48,7 @@ napi_value SignDataWithCertificate(napi_env env, napi_callback_info info)
46
48
  if (arg0_type != napi_string || arg1_type != napi_object || arg2_type != napi_string)
47
49
  {
48
50
  napi_value error_msg;
49
- napi_create_string_utf8(env, "Expected: subject (string), data (Buffer), algorithm (string)", NAPI_AUTO_LENGTH, &error_msg);
51
+ napi_create_string_utf8(env, "Expected: subject (string), data (Buffer), algorithm (string), certificateDer (Buffer, optional)", NAPI_AUTO_LENGTH, &error_msg);
50
52
  napi_value error;
51
53
  napi_create_type_error(env, nullptr, error_msg, &error);
52
54
  napi_throw(env, error);
@@ -102,18 +104,10 @@ napi_value SignDataWithCertificate(napi_env env, napi_callback_info info)
102
104
  throw std::runtime_error("Failed to open certificate store");
103
105
  }
104
106
 
105
- pCertContext = CertFindCertificateInStore(
106
- hStore,
107
- X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
108
- 0,
109
- CERT_FIND_SUBJECT_STR_W,
110
- (LPVOID)subject.c_str(),
111
- NULL);
107
+ const std::vector<BYTE> certificateDer =
108
+ argc >= 4 ? ReadCertificateDer(env, args[3]) : std::vector<BYTE>();
112
109
 
113
- if (!pCertContext)
114
- {
115
- throw std::runtime_error("Certificate not found in store");
116
- }
110
+ pCertContext = FindCertificateInStore(hStore, subject, certificateDer);
117
111
 
118
112
  ALG_ID hashAlg = 0;
119
113
  if (algorithm_str == "sha1")
@@ -1,5 +1,7 @@
1
1
  #include <node_api.h>
2
2
 
3
+ #include "find-certificate-in-store.h"
4
+
3
5
  #include <windows.h>
4
6
  #include <winhttp.h>
5
7
  #include <wincrypt.h>
@@ -85,6 +87,7 @@ napi_value MakeRequest(napi_env env, napi_callback_info info)
85
87
  std::wstring headers;
86
88
  std::string body;
87
89
  int timeout = 30000;
90
+ napi_value certificate_der_val = nullptr;
88
91
 
89
92
  napi_value headers_key;
90
93
  napi_create_string_utf8(env, "headers", NAPI_AUTO_LENGTH, &headers_key);
@@ -137,6 +140,10 @@ napi_value MakeRequest(napi_env env, napi_callback_info info)
137
140
  }
138
141
  }
139
142
 
143
+ napi_value certificate_der_key;
144
+ napi_create_string_utf8(env, "certificateDer", NAPI_AUTO_LENGTH, &certificate_der_key);
145
+ napi_get_property(env, optionsObj, certificate_der_key, &certificate_der_val);
146
+
140
147
  HCERTSTORE hStore = NULL;
141
148
  PCCERT_CONTEXT pCertContext = NULL;
142
149
  HINTERNET hSession = NULL;
@@ -152,18 +159,10 @@ napi_value MakeRequest(napi_env env, napi_callback_info info)
152
159
  throw std::runtime_error("Failed to open certificate store");
153
160
  }
154
161
 
155
- pCertContext = CertFindCertificateInStore(
156
- hStore,
157
- X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
158
- 0,
159
- CERT_FIND_SUBJECT_STR_W,
160
- (LPVOID)subject.c_str(),
161
- NULL);
162
+ const std::vector<BYTE> certificateDer =
163
+ ReadCertificateDer(env, certificate_der_val);
162
164
 
163
- if (!pCertContext)
164
- {
165
- throw std::runtime_error("Certificate not found in store");
166
- }
165
+ pCertContext = FindCertificateInStore(hStore, subject, certificateDer);
167
166
 
168
167
  URL_COMPONENTS urlComp;
169
168
  ZeroMemory(&urlComp, sizeof(urlComp));
Binary file