-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
56 lines (52 loc) · 1.79 KB
/
index.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
const {
exportCertificateAndKey,
exportCertificateAndKeyAsync,
exportAllCertificates,
exportAllCertificatesAsync
} = require('bindings')('macos_export_certificate_and_key');
const { randomBytes } = require('crypto');
const util = require('util');
const { promisify } = util;
function validateSubjectAndThumbprint(subject, thumbprint) {
if (!subject && !thumbprint) {
throw new Error('Need to specify either `subject` or `thumbprint`');
}
if (subject && thumbprint) {
throw new Error('Cannot specify both `subject` and `thumbprint`');
}
if (subject && typeof subject !== 'string') {
throw new Error('`subject` needs to be a string');
}
if (thumbprint && !util.types.isUint8Array(thumbprint)) {
throw new Error('`thumbprint` needs to be a Uint8Array');
}
}
function exportCertificateAndPrivateKey({
subject,
thumbprint
}) {
validateSubjectAndThumbprint(subject, thumbprint);
const passphrase = randomBytes(12).toString('hex');
const pfx = exportCertificateAndKey(
subject ? { subject } : { thumbprint },
passphrase
);
return { passphrase, pfx };
};
async function exportCertificateAndPrivateKeyAsync({
subject,
thumbprint
}) {
validateSubjectAndThumbprint(subject, thumbprint);
const passphrase = (await promisify(randomBytes)(12)).toString('hex');
const pfx = await promisify(exportCertificateAndKeyAsync)(
subject ? { subject } : { thumbprint },
passphrase
);
return { passphrase, pfx };
};
module.exports = exportCertificateAndPrivateKey;
module.exports.exportCertificateAndPrivateKey = exportCertificateAndPrivateKey;
module.exports.exportCertificateAndPrivateKeyAsync = exportCertificateAndPrivateKeyAsync;
module.exports.exportSystemCertificates = exportAllCertificates;
module.exports.exportSystemCertificatesAsync = promisify(exportAllCertificatesAsync);