@certd/plugin-cert 1.38.2 → 1.38.4
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/CHANGELOG.md +8 -0
- package/dist/access/eab-access.js +1 -0
- package/dist/access/google-access.js +1 -0
- package/dist/access/index.js +1 -0
- package/dist/dns-provider/api.js +1 -0
- package/dist/dns-provider/base.js +1 -0
- package/dist/dns-provider/decorator.js +1 -3
- package/dist/dns-provider/domain-parser.js +1 -0
- package/dist/dns-provider/index.js +1 -0
- package/dist/dns-provider/registry.js +1 -0
- package/dist/libs/google.js +1 -0
- package/dist/plugin/cert-plugin/acme.js +1 -0
- package/dist/plugin/cert-plugin/base-convert.js +1 -0
- package/dist/plugin/cert-plugin/base.js +1 -0
- package/dist/plugin/cert-plugin/cert-reader.js +1 -0
- package/dist/plugin/cert-plugin/convert.js +1 -0
- package/dist/plugin/cert-plugin/custom/index.js +1 -0
- package/dist/plugin/cert-plugin/getter/aliyun.js +1 -0
- package/dist/plugin/cert-plugin/index.js +1 -0
- package/dist/plugin/cert-plugin/lego/dns.js +1 -0
- package/dist/plugin/cert-plugin/lego/index.js +1 -0
- package/dist/plugin/index.js +1 -0
- package/package.json +6 -6
- package/stats.html +6177 -0
- package/test/dist/cert-plugin.test.js +14 -0
- package/test/dist/test/cert-plugin.test.js +15 -0
- package/dist/cert-reader.d.ts +0 -51
- package/dist/cert-reader.js +0 -205
- package/dist/convert.d.ts +0 -26
- package/dist/convert.js +0 -123
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { expect } from "chai";
|
|
2
|
+
describe("test/cert-plugin.ts", () => {
|
|
3
|
+
it("should throw error when expires is null or undefined", () => {
|
|
4
|
+
expect(() => {
|
|
5
|
+
// @ts-ignore
|
|
6
|
+
instance.isWillExpire(undefined);
|
|
7
|
+
}).throw("过期时间不能为空");
|
|
8
|
+
expect(() => {
|
|
9
|
+
// @ts-ignore
|
|
10
|
+
instance.isWillExpire(null);
|
|
11
|
+
}).throw("过期时间不能为空");
|
|
12
|
+
});
|
|
13
|
+
});
|
|
14
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiY2VydC1wbHVnaW4udGVzdC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL2NlcnQtcGx1Z2luLnRlc3QudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsT0FBTyxFQUFFLE1BQU0sRUFBRSxNQUFNLE1BQU0sQ0FBQztBQUU5QixRQUFRLENBQUMscUJBQXFCLEVBQUUsR0FBRyxFQUFFO0lBQ25DLEVBQUUsQ0FBQyxzREFBc0QsRUFBRSxHQUFHLEVBQUU7UUFDOUQsTUFBTSxDQUFDLEdBQUcsRUFBRTtZQUNWLGFBQWE7WUFDYixRQUFRLENBQUMsWUFBWSxDQUFDLFNBQVMsQ0FBQyxDQUFDO1FBQ25DLENBQUMsQ0FBQyxDQUFDLEtBQUssQ0FBQyxVQUFVLENBQUMsQ0FBQztRQUVyQixNQUFNLENBQUMsR0FBRyxFQUFFO1lBQ1YsYUFBYTtZQUNiLFFBQVEsQ0FBQyxZQUFZLENBQUMsSUFBSSxDQUFDLENBQUM7UUFDOUIsQ0FBQyxDQUFDLENBQUMsS0FBSyxDQUFDLFVBQVUsQ0FBQyxDQUFDO0lBQ3ZCLENBQUMsQ0FBQyxDQUFDO0FBQ0wsQ0FBQyxDQUFDLENBQUMifQ==
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { expect } from "chai";
|
|
2
|
+
import { CertApplyPlugin } from "../src";
|
|
3
|
+
describe("test/cert-plugin.ts", () => {
|
|
4
|
+
it("should throw error when expires is null or undefined", () => {
|
|
5
|
+
expect(() => {
|
|
6
|
+
// @ts-ignore
|
|
7
|
+
new CertApplyPlugin().isWillExpire(undefined);
|
|
8
|
+
}).throw("过期时间不能为空");
|
|
9
|
+
expect(() => {
|
|
10
|
+
// @ts-ignore
|
|
11
|
+
new CertApplyPlugin().isWillExpire(null);
|
|
12
|
+
}).throw("过期时间不能为空");
|
|
13
|
+
});
|
|
14
|
+
});
|
|
15
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiY2VydC1wbHVnaW4udGVzdC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uL2NlcnQtcGx1Z2luLnRlc3QudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsT0FBTyxFQUFFLE1BQU0sRUFBRSxNQUFNLE1BQU0sQ0FBQztBQUM5QixPQUFPLEVBQUUsZUFBZSxFQUFFLE1BQU0sUUFBUSxDQUFDO0FBRXpDLFFBQVEsQ0FBQyxxQkFBcUIsRUFBRSxHQUFHLEVBQUU7SUFDbkMsRUFBRSxDQUFDLHNEQUFzRCxFQUFFLEdBQUcsRUFBRTtRQUM5RCxNQUFNLENBQUMsR0FBRyxFQUFFO1lBQ1YsYUFBYTtZQUNiLElBQUksZUFBZSxFQUFFLENBQUMsWUFBWSxDQUFDLFNBQVMsQ0FBQyxDQUFDO1FBQ2hELENBQUMsQ0FBQyxDQUFDLEtBQUssQ0FBQyxVQUFVLENBQUMsQ0FBQztRQUVyQixNQUFNLENBQUMsR0FBRyxFQUFFO1lBQ1YsYUFBYTtZQUNiLElBQUksZUFBZSxFQUFFLENBQUMsWUFBWSxDQUFDLElBQUksQ0FBQyxDQUFDO1FBQzNDLENBQUMsQ0FBQyxDQUFDLEtBQUssQ0FBQyxVQUFVLENBQUMsQ0FBQztJQUN2QixDQUFDLENBQUMsQ0FBQztBQUNMLENBQUMsQ0FBQyxDQUFDIn0=
|
package/dist/cert-reader.d.ts
DELETED
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
import { CertInfo } from "./acme.js";
|
|
2
|
-
import { CertificateInfo } from "@certd/acme-client";
|
|
3
|
-
import { ILogger } from "@certd/basic";
|
|
4
|
-
export type CertReaderHandleContext = {
|
|
5
|
-
reader: CertReader;
|
|
6
|
-
tmpCrtPath: string;
|
|
7
|
-
tmpKeyPath: string;
|
|
8
|
-
tmpOcPath?: string;
|
|
9
|
-
tmpPfxPath?: string;
|
|
10
|
-
tmpDerPath?: string;
|
|
11
|
-
tmpIcPath?: string;
|
|
12
|
-
tmpJksPath?: string;
|
|
13
|
-
tmpOnePath?: string;
|
|
14
|
-
tmpP7bPath?: string;
|
|
15
|
-
};
|
|
16
|
-
export type CertReaderHandle = (ctx: CertReaderHandleContext) => Promise<void>;
|
|
17
|
-
export type HandleOpts = {
|
|
18
|
-
logger: ILogger;
|
|
19
|
-
handle: CertReaderHandle;
|
|
20
|
-
};
|
|
21
|
-
export declare class CertReader {
|
|
22
|
-
cert: CertInfo;
|
|
23
|
-
detail: CertificateInfo;
|
|
24
|
-
effective: number;
|
|
25
|
-
expires: number;
|
|
26
|
-
constructor(certInfo: CertInfo);
|
|
27
|
-
getIc(): any;
|
|
28
|
-
getOc(): string;
|
|
29
|
-
toCertInfo(format?: string): CertInfo;
|
|
30
|
-
getCrtDetail(crt?: string): {
|
|
31
|
-
detail: CertificateInfo;
|
|
32
|
-
effective: Date;
|
|
33
|
-
expires: Date;
|
|
34
|
-
};
|
|
35
|
-
static readCertDetail(crt: string): {
|
|
36
|
-
detail: CertificateInfo;
|
|
37
|
-
effective: Date;
|
|
38
|
-
expires: Date;
|
|
39
|
-
};
|
|
40
|
-
getAllDomains(): any;
|
|
41
|
-
getAltNames(): string[];
|
|
42
|
-
static getMainDomain(crt: string): string;
|
|
43
|
-
getMainDomain(): string;
|
|
44
|
-
static getMainDomainFromDetail(detail: CertificateInfo): string;
|
|
45
|
-
saveToFile(type: "crt" | "key" | "pfx" | "der" | "oc" | "one" | "ic" | "jks" | "p7b", filepath?: string): string;
|
|
46
|
-
readCertFile(opts: HandleOpts): Promise<void>;
|
|
47
|
-
buildCertFileName(suffix: string, applyTime: any, prefix?: string): string;
|
|
48
|
-
buildCertName(prefix?: string): string;
|
|
49
|
-
static appendTimeSuffix(name?: string): string;
|
|
50
|
-
static buildCertName(cert: any): string;
|
|
51
|
-
}
|
package/dist/cert-reader.js
DELETED
|
@@ -1,205 +0,0 @@
|
|
|
1
|
-
import fs from "fs";
|
|
2
|
-
import os from "os";
|
|
3
|
-
import path from "path";
|
|
4
|
-
import { crypto } from "@certd/acme-client";
|
|
5
|
-
import dayjs from "dayjs";
|
|
6
|
-
import { uniq } from "lodash-es";
|
|
7
|
-
const formats = {
|
|
8
|
-
pem: ["crt", "key", "ic"],
|
|
9
|
-
one: ["one"],
|
|
10
|
-
pfx: ["pfx"],
|
|
11
|
-
der: ["der"],
|
|
12
|
-
jks: ["jks"],
|
|
13
|
-
p7b: ["p7b", "key"],
|
|
14
|
-
};
|
|
15
|
-
export class CertReader {
|
|
16
|
-
cert;
|
|
17
|
-
detail;
|
|
18
|
-
//毫秒时间戳
|
|
19
|
-
effective;
|
|
20
|
-
//毫秒时间戳
|
|
21
|
-
expires;
|
|
22
|
-
constructor(certInfo) {
|
|
23
|
-
this.cert = certInfo;
|
|
24
|
-
if (!certInfo.ic) {
|
|
25
|
-
this.cert.ic = this.getIc();
|
|
26
|
-
}
|
|
27
|
-
if (!certInfo.oc) {
|
|
28
|
-
this.cert.oc = this.getOc();
|
|
29
|
-
}
|
|
30
|
-
if (!certInfo.one) {
|
|
31
|
-
this.cert.one = this.cert.crt + "\n" + this.cert.key;
|
|
32
|
-
}
|
|
33
|
-
try {
|
|
34
|
-
const { detail, effective, expires } = this.getCrtDetail(this.cert.crt);
|
|
35
|
-
this.detail = detail;
|
|
36
|
-
this.effective = effective.getTime();
|
|
37
|
-
this.expires = expires.getTime();
|
|
38
|
-
}
|
|
39
|
-
catch (e) {
|
|
40
|
-
throw new Error("证书解析失败:" + e.message);
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
getIc() {
|
|
44
|
-
//中间证书ic, 就是crt的第一个 -----END CERTIFICATE----- 之后的内容
|
|
45
|
-
const endStr = "-----END CERTIFICATE-----";
|
|
46
|
-
const firstBlockEndIndex = this.cert.crt.indexOf(endStr);
|
|
47
|
-
const start = firstBlockEndIndex + endStr.length + 1;
|
|
48
|
-
if (this.cert.crt.length <= start) {
|
|
49
|
-
return "";
|
|
50
|
-
}
|
|
51
|
-
const ic = this.cert.crt.substring(start);
|
|
52
|
-
if (ic == null) {
|
|
53
|
-
return "";
|
|
54
|
-
}
|
|
55
|
-
return ic?.trim();
|
|
56
|
-
}
|
|
57
|
-
getOc() {
|
|
58
|
-
//原始证书 就是crt的第一个 -----END CERTIFICATE----- 之前的内容
|
|
59
|
-
const endStr = "-----END CERTIFICATE-----";
|
|
60
|
-
const arr = this.cert.crt.split(endStr);
|
|
61
|
-
return arr[0] + endStr;
|
|
62
|
-
}
|
|
63
|
-
toCertInfo(format) {
|
|
64
|
-
if (!format) {
|
|
65
|
-
return this.cert;
|
|
66
|
-
}
|
|
67
|
-
const formatArr = formats[format];
|
|
68
|
-
const res = {};
|
|
69
|
-
formatArr.forEach((key) => {
|
|
70
|
-
res[key] = this.cert[key];
|
|
71
|
-
});
|
|
72
|
-
return res;
|
|
73
|
-
}
|
|
74
|
-
getCrtDetail(crt = this.cert.crt) {
|
|
75
|
-
return CertReader.readCertDetail(crt);
|
|
76
|
-
}
|
|
77
|
-
static readCertDetail(crt) {
|
|
78
|
-
const detail = crypto.readCertificateInfo(crt.toString());
|
|
79
|
-
const effective = detail.notBefore;
|
|
80
|
-
const expires = detail.notAfter;
|
|
81
|
-
return { detail, effective, expires };
|
|
82
|
-
}
|
|
83
|
-
getAllDomains() {
|
|
84
|
-
const { detail } = this.getCrtDetail();
|
|
85
|
-
const domains = [];
|
|
86
|
-
if (detail.domains?.commonName) {
|
|
87
|
-
domains.push(detail.domains.commonName);
|
|
88
|
-
}
|
|
89
|
-
domains.push(...detail.domains.altNames);
|
|
90
|
-
//去重
|
|
91
|
-
return uniq(domains);
|
|
92
|
-
}
|
|
93
|
-
getAltNames() {
|
|
94
|
-
const { detail } = this.getCrtDetail();
|
|
95
|
-
return detail.domains.altNames;
|
|
96
|
-
}
|
|
97
|
-
static getMainDomain(crt) {
|
|
98
|
-
const { detail } = CertReader.readCertDetail(crt);
|
|
99
|
-
return CertReader.getMainDomainFromDetail(detail);
|
|
100
|
-
}
|
|
101
|
-
getMainDomain() {
|
|
102
|
-
const { detail } = this.getCrtDetail();
|
|
103
|
-
return CertReader.getMainDomainFromDetail(detail);
|
|
104
|
-
}
|
|
105
|
-
static getMainDomainFromDetail(detail) {
|
|
106
|
-
let domain = detail?.domains?.commonName;
|
|
107
|
-
if (domain == null) {
|
|
108
|
-
domain = detail?.domains?.altNames?.[0];
|
|
109
|
-
}
|
|
110
|
-
if (domain == null) {
|
|
111
|
-
domain = "unknown";
|
|
112
|
-
}
|
|
113
|
-
return domain;
|
|
114
|
-
}
|
|
115
|
-
saveToFile(type, filepath) {
|
|
116
|
-
if (!this.cert[type]) {
|
|
117
|
-
return;
|
|
118
|
-
}
|
|
119
|
-
if (filepath == null) {
|
|
120
|
-
//写入临时目录
|
|
121
|
-
filepath = path.join(os.tmpdir(), "/certd/tmp/", Math.floor(Math.random() * 1000000) + `_cert.${type}`);
|
|
122
|
-
}
|
|
123
|
-
const dir = path.dirname(filepath);
|
|
124
|
-
if (!fs.existsSync(dir)) {
|
|
125
|
-
fs.mkdirSync(dir, { recursive: true });
|
|
126
|
-
}
|
|
127
|
-
if (type === "crt" || type === "key" || type === "ic" || type === "oc" || type === "one" || type === "p7b") {
|
|
128
|
-
fs.writeFileSync(filepath, this.cert[type]);
|
|
129
|
-
}
|
|
130
|
-
else {
|
|
131
|
-
fs.writeFileSync(filepath, Buffer.from(this.cert[type], "base64"));
|
|
132
|
-
}
|
|
133
|
-
return filepath;
|
|
134
|
-
}
|
|
135
|
-
async readCertFile(opts) {
|
|
136
|
-
const logger = opts.logger;
|
|
137
|
-
logger.info("将证书写入本地缓存文件");
|
|
138
|
-
const tmpCrtPath = this.saveToFile("crt");
|
|
139
|
-
const tmpKeyPath = this.saveToFile("key");
|
|
140
|
-
const tmpPfxPath = this.saveToFile("pfx");
|
|
141
|
-
const tmpIcPath = this.saveToFile("ic");
|
|
142
|
-
const tmpOcPath = this.saveToFile("oc");
|
|
143
|
-
const tmpDerPath = this.saveToFile("der");
|
|
144
|
-
const tmpJksPath = this.saveToFile("jks");
|
|
145
|
-
const tmpOnePath = this.saveToFile("one");
|
|
146
|
-
const tmpP7bPath = this.saveToFile("p7b");
|
|
147
|
-
logger.info("本地文件写入成功");
|
|
148
|
-
try {
|
|
149
|
-
return await opts.handle({
|
|
150
|
-
reader: this,
|
|
151
|
-
tmpCrtPath,
|
|
152
|
-
tmpKeyPath,
|
|
153
|
-
tmpPfxPath,
|
|
154
|
-
tmpDerPath,
|
|
155
|
-
tmpIcPath,
|
|
156
|
-
tmpJksPath,
|
|
157
|
-
tmpOcPath,
|
|
158
|
-
tmpP7bPath,
|
|
159
|
-
tmpOnePath,
|
|
160
|
-
});
|
|
161
|
-
}
|
|
162
|
-
catch (err) {
|
|
163
|
-
logger.error("处理失败", err);
|
|
164
|
-
throw err;
|
|
165
|
-
}
|
|
166
|
-
finally {
|
|
167
|
-
//删除临时文件
|
|
168
|
-
logger.info("清理临时文件");
|
|
169
|
-
function removeFile(filepath) {
|
|
170
|
-
if (filepath) {
|
|
171
|
-
fs.unlinkSync(filepath);
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
removeFile(tmpCrtPath);
|
|
175
|
-
removeFile(tmpKeyPath);
|
|
176
|
-
removeFile(tmpPfxPath);
|
|
177
|
-
removeFile(tmpOcPath);
|
|
178
|
-
removeFile(tmpDerPath);
|
|
179
|
-
removeFile(tmpIcPath);
|
|
180
|
-
removeFile(tmpJksPath);
|
|
181
|
-
removeFile(tmpOnePath);
|
|
182
|
-
removeFile(tmpP7bPath);
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
buildCertFileName(suffix, applyTime, prefix = "cert") {
|
|
186
|
-
let domain = this.getMainDomain();
|
|
187
|
-
domain = domain.replaceAll(".", "_").replaceAll("*", "_");
|
|
188
|
-
const timeStr = dayjs(applyTime).format("YYYYMMDDHHmmss");
|
|
189
|
-
return `${prefix}_${domain}_${timeStr}.${suffix}`;
|
|
190
|
-
}
|
|
191
|
-
buildCertName(prefix = "") {
|
|
192
|
-
let domain = this.getMainDomain();
|
|
193
|
-
domain = domain.replaceAll(".", "_").replaceAll("*", "_");
|
|
194
|
-
return `${prefix}_${domain}_${dayjs().format("YYYYMMDDHHmmssSSS")}`;
|
|
195
|
-
}
|
|
196
|
-
static appendTimeSuffix(name) {
|
|
197
|
-
if (name == null) {
|
|
198
|
-
name = "certd";
|
|
199
|
-
}
|
|
200
|
-
return name + "_" + dayjs().format("YYYYMMDDHHmmssSSS");
|
|
201
|
-
}
|
|
202
|
-
static buildCertName(cert) {
|
|
203
|
-
return new CertReader(cert).buildCertName();
|
|
204
|
-
}
|
|
205
|
-
}
|
package/dist/convert.d.ts
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import { ILogger } from "@certd/basic";
|
|
2
|
-
import type { CertInfo } from "../cert-plugin/acme.js";
|
|
3
|
-
import { CertReader, CertReaderHandleContext } from "../cert-plugin/cert-reader.js";
|
|
4
|
-
export { CertReader };
|
|
5
|
-
export type { CertInfo };
|
|
6
|
-
export declare class CertConverter {
|
|
7
|
-
logger: ILogger;
|
|
8
|
-
constructor(opts: {
|
|
9
|
-
logger: ILogger;
|
|
10
|
-
});
|
|
11
|
-
convert(opts: {
|
|
12
|
-
cert: CertInfo;
|
|
13
|
-
pfxPassword: string;
|
|
14
|
-
pfxArgs: string;
|
|
15
|
-
}): Promise<{
|
|
16
|
-
pfx: string;
|
|
17
|
-
der: string;
|
|
18
|
-
jks: string;
|
|
19
|
-
p7b: string;
|
|
20
|
-
}>;
|
|
21
|
-
exec(cmd: string): Promise<void>;
|
|
22
|
-
private convertPfx;
|
|
23
|
-
private convertDer;
|
|
24
|
-
convertP7b(opts: CertReaderHandleContext): Promise<string>;
|
|
25
|
-
convertJks(opts: CertReaderHandleContext, pfxPassword?: string): Promise<string>;
|
|
26
|
-
}
|
package/dist/convert.js
DELETED
|
@@ -1,123 +0,0 @@
|
|
|
1
|
-
import { sp } from "@certd/basic";
|
|
2
|
-
import { CertReader } from "../cert-plugin/cert-reader.js";
|
|
3
|
-
import path from "path";
|
|
4
|
-
import os from "os";
|
|
5
|
-
import fs from "fs";
|
|
6
|
-
export { CertReader };
|
|
7
|
-
export class CertConverter {
|
|
8
|
-
logger;
|
|
9
|
-
constructor(opts) {
|
|
10
|
-
this.logger = opts.logger;
|
|
11
|
-
}
|
|
12
|
-
async convert(opts) {
|
|
13
|
-
const certReader = new CertReader(opts.cert);
|
|
14
|
-
let pfx;
|
|
15
|
-
let der;
|
|
16
|
-
let jks;
|
|
17
|
-
let p7b;
|
|
18
|
-
const handle = async (ctx) => {
|
|
19
|
-
// 调用openssl 转pfx
|
|
20
|
-
pfx = await this.convertPfx(ctx, opts.pfxPassword, opts.pfxArgs);
|
|
21
|
-
// 转der
|
|
22
|
-
der = await this.convertDer(ctx);
|
|
23
|
-
jks = await this.convertJks(ctx, opts.pfxPassword);
|
|
24
|
-
p7b = await this.convertP7b(ctx);
|
|
25
|
-
};
|
|
26
|
-
await certReader.readCertFile({ logger: this.logger, handle });
|
|
27
|
-
return {
|
|
28
|
-
pfx,
|
|
29
|
-
der,
|
|
30
|
-
jks,
|
|
31
|
-
p7b,
|
|
32
|
-
};
|
|
33
|
-
}
|
|
34
|
-
async exec(cmd) {
|
|
35
|
-
process.env.LANG = "zh_CN.GBK";
|
|
36
|
-
await sp.spawn({
|
|
37
|
-
cmd: cmd,
|
|
38
|
-
logger: this.logger,
|
|
39
|
-
});
|
|
40
|
-
}
|
|
41
|
-
async convertPfx(opts, pfxPassword, pfxArgs) {
|
|
42
|
-
const { tmpCrtPath, tmpKeyPath } = opts;
|
|
43
|
-
const pfxPath = path.join(os.tmpdir(), "/certd/tmp/", Math.floor(Math.random() * 1000000) + "_cert.pfx");
|
|
44
|
-
const dir = path.dirname(pfxPath);
|
|
45
|
-
if (!fs.existsSync(dir)) {
|
|
46
|
-
fs.mkdirSync(dir, { recursive: true });
|
|
47
|
-
}
|
|
48
|
-
let passwordArg = "-passout pass:";
|
|
49
|
-
if (pfxPassword) {
|
|
50
|
-
passwordArg = `-password pass:${pfxPassword}`;
|
|
51
|
-
}
|
|
52
|
-
// 兼容server 2016,旧版本不能用sha256
|
|
53
|
-
const oldPfxCmd = `openssl pkcs12 ${pfxArgs} -export -out ${pfxPath} -inkey ${tmpKeyPath} -in ${tmpCrtPath} ${passwordArg}`;
|
|
54
|
-
// const newPfx = `openssl pkcs12 -export -out ${pfxPath} -inkey ${tmpKeyPath} -in ${tmpCrtPath} ${passwordArg}`;
|
|
55
|
-
await this.exec(oldPfxCmd);
|
|
56
|
-
const fileBuffer = fs.readFileSync(pfxPath);
|
|
57
|
-
const pfxCert = fileBuffer.toString("base64");
|
|
58
|
-
fs.unlinkSync(pfxPath);
|
|
59
|
-
return pfxCert;
|
|
60
|
-
//
|
|
61
|
-
// const applyTime = new Date().getTime();
|
|
62
|
-
// const filename = reader.buildCertFileName("pfx", applyTime);
|
|
63
|
-
// this.saveFile(filename, fileBuffer);
|
|
64
|
-
}
|
|
65
|
-
async convertDer(opts) {
|
|
66
|
-
const { tmpCrtPath } = opts;
|
|
67
|
-
const derPath = path.join(os.tmpdir(), "/certd/tmp/", Math.floor(Math.random() * 1000000) + `_cert.der`);
|
|
68
|
-
const dir = path.dirname(derPath);
|
|
69
|
-
if (!fs.existsSync(dir)) {
|
|
70
|
-
fs.mkdirSync(dir, { recursive: true });
|
|
71
|
-
}
|
|
72
|
-
await this.exec(`openssl x509 -outform der -in ${tmpCrtPath} -out ${derPath}`);
|
|
73
|
-
const fileBuffer = fs.readFileSync(derPath);
|
|
74
|
-
const derCert = fileBuffer.toString("base64");
|
|
75
|
-
fs.unlinkSync(derPath);
|
|
76
|
-
return derCert;
|
|
77
|
-
}
|
|
78
|
-
async convertP7b(opts) {
|
|
79
|
-
const { tmpCrtPath } = opts;
|
|
80
|
-
const p7bPath = path.join(os.tmpdir(), "/certd/tmp/", Math.floor(Math.random() * 1000000) + `_cert.p7b`);
|
|
81
|
-
const dir = path.dirname(p7bPath);
|
|
82
|
-
if (!fs.existsSync(dir)) {
|
|
83
|
-
fs.mkdirSync(dir, { recursive: true });
|
|
84
|
-
}
|
|
85
|
-
//openssl crl2pkcs7 -nocrl \
|
|
86
|
-
// -certfile your_domain.crt \
|
|
87
|
-
// -certfile intermediate.crt \
|
|
88
|
-
// -out chain.p7b
|
|
89
|
-
await this.exec(`openssl crl2pkcs7 -nocrl -certfile ${tmpCrtPath} -out ${p7bPath}`);
|
|
90
|
-
const fileBuffer = fs.readFileSync(p7bPath);
|
|
91
|
-
const p7bCert = fileBuffer.toString();
|
|
92
|
-
fs.unlinkSync(p7bPath);
|
|
93
|
-
return p7bCert;
|
|
94
|
-
}
|
|
95
|
-
async convertJks(opts, pfxPassword = "") {
|
|
96
|
-
const jksPassword = pfxPassword || "123456";
|
|
97
|
-
try {
|
|
98
|
-
const randomStr = Math.floor(Math.random() * 1000000) + "";
|
|
99
|
-
const p12Path = path.join(os.tmpdir(), "/certd/tmp/", randomStr + `_cert.p12`);
|
|
100
|
-
const { tmpCrtPath, tmpKeyPath } = opts;
|
|
101
|
-
let passwordArg = "-passout pass:";
|
|
102
|
-
if (jksPassword) {
|
|
103
|
-
passwordArg = `-password pass:${jksPassword}`;
|
|
104
|
-
}
|
|
105
|
-
await this.exec(`openssl pkcs12 -export -in ${tmpCrtPath} -inkey ${tmpKeyPath} -out ${p12Path} -name certd ${passwordArg}`);
|
|
106
|
-
const jksPath = path.join(os.tmpdir(), "/certd/tmp/", randomStr + `_cert.jks`);
|
|
107
|
-
const dir = path.dirname(jksPath);
|
|
108
|
-
if (!fs.existsSync(dir)) {
|
|
109
|
-
fs.mkdirSync(dir, { recursive: true });
|
|
110
|
-
}
|
|
111
|
-
await this.exec(`keytool -importkeystore -srckeystore ${p12Path} -srcstoretype PKCS12 -srcstorepass "${jksPassword}" -destkeystore ${jksPath} -deststoretype PKCS12 -deststorepass "${jksPassword}" `);
|
|
112
|
-
fs.unlinkSync(p12Path);
|
|
113
|
-
const fileBuffer = fs.readFileSync(jksPath);
|
|
114
|
-
const certBase64 = fileBuffer.toString("base64");
|
|
115
|
-
fs.unlinkSync(jksPath);
|
|
116
|
-
return certBase64;
|
|
117
|
-
}
|
|
118
|
-
catch (e) {
|
|
119
|
-
this.logger.error("转换jks失败", e);
|
|
120
|
-
return;
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
}
|