-
Notifications
You must be signed in to change notification settings - Fork 576
/
fix_proc.js
37 lines (35 loc) · 1.47 KB
/
fix_proc.js
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
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
// Copyright 2017 crypto-browserify. All rights reserved. MIT license.
// from https://github.com/crypto-browserify/parse-asn1/blob/fbd70dca8670d17955893e083ca69118908570be/fixProc.js
import evp from "../evp_bytes_to_key.ts";
import * as ciphers from "../browserify_aes/mod.js";
import { Buffer } from "../../../buffer.ts";
const findProc =
/Proc-Type: 4,ENCRYPTED[\n\r]+DEK-Info: AES-((?:128)|(?:192)|(?:256))-CBC,([0-9A-H]+)[\n\r]+([0-9A-z\n\r+/=]+)[\n\r]+/m;
const startRegex = /^-----BEGIN ((?:.*? KEY)|CERTIFICATE)-----/m;
const fullRegex =
/^-----BEGIN ((?:.*? KEY)|CERTIFICATE)-----([0-9A-z\n\r+/=]+)-----END \1-----$/m;
export default function (okey, password) {
const key = okey.toString();
const match = key.match(findProc);
let decrypted;
if (!match) {
const match2 = key.match(fullRegex);
decrypted = Buffer.from(match2[2].replace(/[\r\n]/g, ""), "base64");
} else {
const suite = "aes" + match[1];
const iv = Buffer.from(match[2], "hex");
const cipherText = Buffer.from(match[3].replace(/[\r\n]/g, ""), "base64");
const cipherKey = evp(password, iv.slice(0, 8), parseInt(match[1], 10)).key;
const out = [];
const cipher = ciphers.createDecipheriv(suite, cipherKey, iv);
out.push(cipher.update(cipherText));
out.push(cipher.final());
decrypted = Buffer.concat(out);
}
const tag = key.match(startRegex)[1];
return {
tag: tag,
data: decrypted,
};
}