-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
147 lines (130 loc) · 4.27 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
'use strict';
const {
exportCertificateAndKey,
exportCertificateAndKeyAsync,
exportAllCertificates,
exportAllCertificatesAsync,
storeTypes
} = require('bindings')('win_export_cert');
const { randomBytes, X509Certificate } = require('crypto');
const util = require('util');
const { promisify } = util;
const DEFAULT_STORE_TYPE_LIST = ['CERT_SYSTEM_STORE_LOCAL_MACHINE', 'CERT_SYSTEM_STORE_CURRENT_USER'];
function validateStoreTypeList(storeTypeList) {
storeTypeList = storeTypeList || DEFAULT_STORE_TYPE_LIST;
if (!Array.isArray(storeTypeList) ||
storeTypeList.length < 1 ||
!storeTypeList.every(st => typeof st === 'number' || Object.keys(storeTypes).includes(st))) {
throw new Error(`storeTypeList needs to be an array of valid store types`);
}
return storeTypeList.map(st => typeof st === 'number' ? st : storeTypes[st]);
}
function addExportedCertificatesToSet(set, list) {
for (const cert of list) {
// X509Certificate was added in Node.js 15 and accepts DER as input, but .toString() returns PEM
set.add(new X509Certificate(cert).toString());
}
}
function exportSystemCertificates({
store,
storeTypeList
} = {}) {
storeTypeList = validateStoreTypeList(storeTypeList);
const result = new Set();
for (const storeType of storeTypeList) {
addExportedCertificatesToSet(result, exportAllCertificates(store || 'ROOT', storeType));
}
return [...result];
}
async function exportSystemCertificatesAsync({
store,
storeTypeList
} = {}) {
storeTypeList = validateStoreTypeList(storeTypeList);
const result = new Set();
for (const storeType of storeTypeList) {
addExportedCertificatesToSet(result, await promisify(exportAllCertificatesAsync)(store || 'ROOT', storeType));
}
return [...result];
}
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(opts = {}) {
let {
subject,
thumbprint,
store,
storeTypeList,
requirePrivKey
} = opts;
storeTypeList = validateStoreTypeList(storeTypeList);
if (storeTypeList.length !== 1) {
let err;
for (const storeType of storeTypeList) {
try {
return exportCertificateAndPrivateKey({ ...opts, storeTypeList: [storeType] });
} catch(err_) {
err = err_;
}
}
throw err;
}
validateSubjectAndThumbprint(subject, thumbprint);
requirePrivKey = requirePrivKey !== false;
const passphrase = randomBytes(12).toString('hex');
const pfx = exportCertificateAndKey(
passphrase,
store || 'MY',
storeTypeList[0],
subject ? { subject } : { thumbprint },
requirePrivKey);
return { passphrase, pfx };
}
async function exportCertificateAndPrivateKeyAsync(opts = {}) {
let {
subject,
thumbprint,
store,
storeTypeList,
requirePrivKey
} = opts;
storeTypeList = validateStoreTypeList(storeTypeList);
if (storeTypeList.length !== 1) {
let err;
for (const storeType of storeTypeList) {
try {
return await exportCertificateAndPrivateKeyAsync({ ...opts, storeTypeList: [storeType] });
} catch(err_) {
err = err_;
}
}
throw err;
}
validateSubjectAndThumbprint(subject, thumbprint);
requirePrivKey = requirePrivKey !== false;
const passphrase = (await promisify(randomBytes)(12)).toString('hex');
const pfx = await promisify(exportCertificateAndKeyAsync)(
passphrase,
store || 'MY',
storeTypeList[0],
subject ? { subject } : { thumbprint },
requirePrivKey);
return { passphrase, pfx };
}
module.exports = exportCertificateAndPrivateKey;
module.exports.exportCertificateAndPrivateKey = exportCertificateAndPrivateKey;
module.exports.exportCertificateAndPrivateKeyAsync = exportCertificateAndPrivateKeyAsync;
module.exports.exportSystemCertificates = exportSystemCertificates;
module.exports.exportSystemCertificatesAsync = exportSystemCertificatesAsync;