-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathagent.js
More file actions
413 lines (375 loc) · 9.23 KB
/
agent.js
File metadata and controls
413 lines (375 loc) · 9.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
const daemon = require("./lib/frame/daemon.js");
const client = require("./lib/frame/client.js");
const proxy = require("./lib/frame/proxy.js");
const http = require("./lib/http");
const fs = require("fs");
const encoding = require("encoding");
const gost89 = require("gost89");
const jk = require("jkurwa");
const algos = gost89.compat.algos;
const Certificate = jk.models.Certificate;
const Priv = jk.models.Priv;
const Box = jk.Box;
const io = {
stdout: process.stdout,
readFileSync: fs.readFileSync,
writeFileSync: fs.writeFileSync,
};
function error(...all) {
if (io.stderr) {
all.forEach((path) => io.stderr.write(path));
} else {
console.error(...all);
}
}
class ReadFileError extends Error {}
function readFile(filename) {
return new Promise((resolve, reject) => {
fs.readFile(filename, (err, data) => {
if (err) {
error("read file", err.toString());
reject(new ReadFileError());
} else {
resolve(data);
}
});
});
}
async function upload(filename, data, uploadUrl) {
const headers = {
'Content-Type': 'application/json',
'Accept': 'application/json',
};
const uploadData = JSON.stringify([{
fname: filename,
contentBase64: data.toString('base64'),
}]);
const ret = await http.query_promise('POST', uploadUrl, headers, uploadData);
console.log(ret.toString());
}
async function output(filename, data, isWin, uploadUrl) {
if (typeof filename === "string" && filename !== "-") {
io.writeFileSync(filename, data);
} else {
io.stdout.write(isWin ? encoding.convert(data, "utf-8", "cp1251") : data);
}
}
function dateStr(d) {
d = d || new Date();
return d
.toISOString()
.replace(/[\-T:Z.]/g, "")
.slice(0, 14);
}
function key_param_parse(key) {
let pw;
if (key.indexOf(":") !== -1) {
pw = key.substr(key.indexOf(":") + 1);
key = key.substr(0, key.indexOf(":"));
}
return {
path: key,
pw: pw,
};
}
function tsp_arg(value) {
if (value === true) {
return "content";
}
return value;
}
function listOf(value) {
if (!value) {
return [];
}
if (Array.isArray(value)) {
return value;
}
return [value];
}
async function get_local_box(key, cert, ca) {
const box = new Box({ algo: algos(), query: http.query });
const keyInfo = listOf(key).map(key_param_parse);
for (let { path, pw } of keyInfo) {
let buf = await readFile(path);
box.load({ keyBuffers: [buf], password: pw });
}
for (let path of listOf(cert)) {
let buf = await readFile(path);
box.load({ certPem: buf });
}
if (ca) {
let buf = await readFile(ca);
box.loadCAs(buf);
}
return box;
}
async function do_sc(
shouldSign,
shouldCrypt,
box,
inputF,
outputF,
uploadUrl,
certRecF,
edrpou,
email,
filename,
tax,
detached,
role,
tsp,
ocsp,
includeChain,
encode_win,
time
) {
let content = await readFile(inputF);
let cert_rcrypt;
if (shouldCrypt) {
let buf = await readFile(certRecF || shouldCrypt);
cert_rcrypt = Certificate.from_asn1(buf).as_pem();
shouldCrypt = true;
}
if (!box.keys[0].cert) {
error('No certificate loaded for key 0, use --cert filename or --cert-fetch url');
return;
}
const ipn_ext = box.keys[0].cert.extension.ipn;
const subject = box.keys[0].cert.subject;
let headers;
if (email && tax) {
if (filename === undefined) {
filename = inputF.replace(/\\/g, "/").split("/");
filename = filename[filename.length - 1];
}
headers = {
CERTYPE: "UA1",
RCV_NAME: encoding.convert(subject.organizationName, "cp1251"),
PRG_TYPE: "TRANSPORT GATE",
PRG_VER: "1.0.0",
SND_DATE: dateStr(),
FILENAME: filename || inputF,
EDRPOU: edrpou || ipn_ext.EDRPOU,
};
if (email) {
headers.RCV_EMAIL = email;
}
if (encode_win) {
headers.ENCODING = "WIN";
content = encoding.convert(content, "cp1251");
}
}
const pipe = [];
if (shouldSign === true) {
pipe.push({
op: "sign",
tax: Boolean(tax),
detached: Boolean(detached),
role: role,
tsp: tsp,
ocsp: ocsp,
includeChain: includeChain,
time: time,
});
}
if (shouldCrypt === true) {
pipe.push({
op: "encrypt",
forCert: cert_rcrypt,
addCert: true,
tax: Boolean(tax),
role: role,
});
pipe.push({
op: "sign",
tax: Boolean(tax),
detached: Boolean(detached),
role: role,
tsp: tsp,
ocsp: ocsp,
includeChain: includeChain,
time: time,
});
}
const tb = await box.pipe(content, pipe, headers);
if (tb.error) {
error("Error occured inside the pipeline.");
return false;
}
if(uploadUrl) {
await upload(filename, tb, uploadUrl);
} else {
await output(outputF, tb, null, uploadUrl);
}
return true;
}
async function do_parse(inputF, outputF, box, tsp, ocsp) {
let content, content2;
if (typeof inputF === "string") {
content = await readFile(inputF);
} else if (inputF.length === 2) {
content = await readFile(inputF[0]);
content2 = await readFile(inputF[1]);
}
const textinfo = await box.unwrap(content, content2, { tsp, ocsp });
const rpipe = textinfo.pipe || [];
let isWin = false;
let isErr = false;
rpipe.forEach(function (step) {
const x = step.cert;
const tr = (step.transport ? step.headers : {}) || {};
if (step.error) {
isErr = true;
error("Error occured during unwrap: " + step.error);
return;
}
if (tr.ENCODING === "WIN") {
isWin = true;
Object.keys(tr).forEach((key) => {
tr[key] = encoding.convert(tr[key], "utf8", "cp1251").toString();
});
}
if (tr.SUBJECT) {
error("Subject:", tr.SUBJECT);
}
if (tr.FILENAME) {
error("Filename:", tr.FILENAME);
}
if (tr.EDRPOU) {
error("Sent-By-EDRPOU:", tr.EDRPOU);
}
if (step.signed) {
error("Signed-By:", x.subject.commonName || x.subject.organizationName);
if (x.extension.ipn && x.extension.ipn.EDRPOU) {
error("Signed-By-EDRPOU:", x.extension.ipn.EDRPOU);
}
}
if (step.signed && !step.cert.verified) {
error("Signer-Authentity:", "Not-Verified");
error("Signer-Authentity-Reason:", "No CA list supplied");
}
if (step.ocsp) {
for (let ocsp of step.ocsp) {
error(
"OCSP-Check:",
ocsp.statusOk ? "OK" : ocsp.requestOk ? "Fail" : "Unknown",
ocsp.isOcspStamp ? "Stamp" : "Online"
);
if (ocsp.hasOwnProperty("time")) {
error("OCSP-Check-Time:", ocsp.time);
}
}
}
if (step.contentTime) {
error("Content-Time-TSP:", step.contentTime / 1000);
}
if (step.tokenTime) {
error("Signature-Time-TSP:", step.tokenTime / 1000);
}
if (step.signingTime) {
error("Signature-Time:", step.signingTime / 1000);
}
if (step.enc) {
error("Encrypted");
}
});
if (isErr === false && outputF !== null) {
await output(outputF, textinfo.content, isWin);
}
return true;
}
async function unprotect(key, outputF) {
key = key_param_parse(key);
const buf = fs.readFileSync(key.path);
const store = Priv.from_protected(buf, key.pw, algos());
for(let key of store.keys) {
await output(outputF, key.as_pem());
}
return true;
}
async function main(argv, setIo) {
let ret = false;
setIo && Object.assign(io, setIo);
jk.Curve.only_known = argv.only_known;
if (argv.unprotect) {
return await unprotect(argv.key, argv.output);
}
let box;
if (argv.connect) {
box = await client.remoteBox(argv);
} else {
box = await get_local_box(argv.key, argv.cert, argv.ca_path);
}
let certFetch = argv['cert-fetch'];
if (certFetch) {
let urls = [];
if (typeof certFetch === 'string') {
urls = [certFetch];
} else if (Array.isArray(certFetch)) {
urls = certFetch;
}
await box.findCertsCmp(urls);
}
if (argv.sign || argv.crypt) {
if (argv.crypt === true && !argv.recipient_cert) {
return error(
"Please specify recipient certificate for encryption mode: --crypt filename.cert"
);
}
ret = await do_sc(
argv.sign,
argv.crypt,
box,
argv.input,
argv.output,
argv.upload_url,
argv.recipient_cert,
argv.edrpou,
argv.email,
argv.filename,
argv.tax,
argv.detached,
argv.role,
tsp_arg(argv.tsp),
argv.ocsp,
argv.include_chain,
argv.encode_win,
argv.time && Number(argv.time)
);
}
if (argv.decrypt || argv.verify) {
ret = await do_parse(
argv.input || argv.decrypt || argv.verify,
argv.verify ? null : argv.output,
box,
tsp_arg(argv.tsp),
argv.ocsp
);
}
if (argv.agent && !argv.connect) {
return daemon.start({
box,
silent: argv.silent,
bind: argv.bind,
tcp: argv.tcp,
ca: argv.ca_path,
key: argv.connect_key,
keep_alive: argv.keep_alive,
});
}
if (argv.proxy) {
return proxy.start({
host: argv.proxy === true ? '::' : argv.proxy,
silent: argv.silent,
bind: argv.bind,
key: argv.connect_key,
ports: argv.ports,
});
}
if (box && box.sock) {
box.sock.destroy();
}
return ret;
}
module.exports = { main, ReadFileError };