This commit is contained in:
xiaojunnuo
2024-10-29 23:37:18 +08:00
parent aaaf8d7db3
commit b3e0546f78
5 changed files with 101 additions and 18 deletions
@@ -17,16 +17,20 @@ export class CertConverter {
async convert(opts: { cert: CertInfo; pfxPassword: string }): Promise<{
pfxPath: string;
derPath: string;
p12Path: string;
}> {
const certReader = new CertReader(opts.cert);
let pfxPath: string;
let derPath: string;
let p12Path: string;
const handle = async (ctx: CertReaderHandleContext) => {
// 调用openssl 转pfx
pfxPath = await this.convertPfx(ctx, opts.pfxPassword);
// 转der
derPath = await this.convertDer(ctx);
p12Path = await this.convertP12(ctx, opts.pfxPassword);
};
await certReader.readCertFile({ logger: this.logger, handle });
@@ -34,6 +38,7 @@ export class CertConverter {
return {
pfxPath,
derPath,
p12Path,
};
}
@@ -88,4 +93,25 @@ export class CertConverter {
// const filename = reader.buildCertFileName("der", applyTime);
// this.saveFile(filename, fileBuffer);
}
async convertP12(opts: CertReaderHandleContext, pfxPassword: string) {
const { tmpCrtPath, tmpKeyPath } = opts;
const p12Path = path.join(os.tmpdir(), "/certd/tmp/", Math.floor(Math.random() * 1000000) + "", `cert.p12`);
const dir = path.dirname(p12Path);
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
}
try {
let passwordArg = "-passout pass:";
if (pfxPassword) {
passwordArg = `-password pass:${pfxPassword}`;
}
await this.exec(`openssl pkcs12 -export -in ${tmpCrtPath} -inkey ${tmpKeyPath} -out ${p12Path} -name certd ${passwordArg}`);
return p12Path;
} catch (e) {
this.logger.error("转换jks失败", e);
return;
}
}
}