index.js
1.47 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
"use strict";
/**
* Copyright 2018 Google LLC
*
* Distributed under MIT license.
* See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
*/
Object.defineProperty(exports, "__esModule", { value: true });
const fs = require("fs");
const forge = require("node-forge");
const util_1 = require("util");
const readFile = util_1.promisify(fs.readFile);
function getPem(filename, callback) {
if (callback) {
getPemAsync(filename)
.then(pem => callback(null, pem))
.catch(err => callback(err, null));
}
else {
return getPemAsync(filename);
}
}
exports.getPem = getPem;
function getPemAsync(filename) {
return readFile(filename, { encoding: 'base64' }).then(keyp12 => {
return convertToPem(keyp12);
});
}
/**
* Converts a P12 in base64 encoding to a pem.
* @param p12base64 String containing base64 encoded p12.
* @returns a string containing the pem.
*/
function convertToPem(p12base64) {
const p12Der = forge.util.decode64(p12base64);
const p12Asn1 = forge.asn1.fromDer(p12Der);
const p12 = forge.pkcs12.pkcs12FromAsn1(p12Asn1, 'notasecret');
const bags = p12.getBags({ friendlyName: 'privatekey' });
if (bags.friendlyName) {
const privateKey = bags.friendlyName[0].key;
const pem = forge.pki.privateKeyToPem(privateKey);
return pem.replace(/\r\n/g, '\n');
}
else {
throw new Error('Unable to get friendly name.');
}
}
//# sourceMappingURL=index.js.map