-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathindex.mjs
More file actions
281 lines (233 loc) · 7.3 KB
/
Copy pathindex.mjs
File metadata and controls
281 lines (233 loc) · 7.3 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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import forge from 'node-forge'
import path from 'path'
import cp from 'child_process'
import os from 'os'
import util from 'util'
import _fs from 'fs'
import sudo from 'sudo-prompt'
const __dirname = path.dirname(new URL(import.meta.url).pathname);
var exec = util.promisify(cp.exec)
// not using fs.promise because we're supporting Node 8.
var fs = {}
for (let [name, method] of Object.entries(_fs)) {
if (typeof method === 'function')
fs[name] = util.promisify(method)
}
async function ensureDirectory(directory) {
try {
await fs.stat(directory)
} catch(err) {
await fs.mkdir(directory)
}
}
// accepts PEM string, removes the --- header/footer, and calculates sha1 hash/thumbprint/fingerprint
function pemToHash(pem) {
return pem.toString()
.replace('-----BEGIN CERTIFICATE-----', '')
.replace('-----END CERTIFICATE-----', '')
.replace(/\r+/g, '')
.replace(/\n+/g, '')
.trim()
}
function isNodeForgeCert(arg) {
if (typeof arg !== 'object') return false
return arg.version !== undefined
&& arg.serialNumber !== undefined
&& arg.signature !== undefined
&& arg.publicKey !== undefined
}
const LINUX_CERT_DIR = '/usr/share/ca-certificates/extra/'
// Not tested. I don't have a mac. help needed.
var MAC_DIR
if (os.platform() === 'darwin') {
let [major, minor] = os.release().split('.').map(Number)
// if (major >= 10 || minor >= 14)
MAC_DIR = '/Library/Keychains/System.keychain' // works on major: 16, minor: 7
// MAC_DIR = '/System/Library/Keychains/SystemRootCertificates.keychain'
}
class CertStruct {
constructor(arg) {
if (Buffer.isBuffer(arg))
arg = arg.toString()
if (typeof arg === 'string') {
if (arg.includes('-----BEGIN CERTIFICATE-----'))
this.pem = arg
else
this.path = arg
} else if (typeof arg === 'object') {
this.path = arg.path || arg.path
this.serialNumber = arg.serialNumber
if (isNodeForgeCert(arg))
this.pem = forge.pki.certificateToPem(arg)
else
this.pem = arg.pem || arg.cert || arg.data
}
}
async ensureCertReadFromFs() {
if (this.pem) return
if (!this.path) return
this.pem = (await fs.readFile(this.path)).toString()
}
get name() {
if (this._name) return this._name
if (this.path) {
this._name = path.basename(this.path)
if (this._name.endsWith('.crt') && this._name.endsWith('.cer'))
this._name = this._name.slice(0, -4)
else if (this._name.endsWith('.pem'))
this._name = this._name.slice(0, -5)
} else if (this.certificate) {
let attributes = certificate.subject.attributes
if (attributes) {
var obj = attributes.find(obj => obj.shortName === 'CN')
|| attributes.find(obj => obj.shortName === 'O')
if (obj)
this._name = obj.value.toLowerCase().replace(/\W+/g, '-')
}
}
return this._name
}
get certificate() {
if (this._certificate) return this._certificate
if (this.pem) return this._certificate = forge.pki.certificateFromPem(this.pem)
}
set certificate(object) {
this._certificate = object
}
get serialNumber() {
if (this._serialNumber) return this._serialNumber
if (this.certificate) return this._serialNumber = this.certificate.serialNumber
}
set serialNumber(string) {
this._serialNumber = string
}
}
// https://manuals.gfi.com/en/kerio/connect/content/server-configuration/ssl-certificates/adding-trusted-root-certificates-to-the-server-1605.html
class CertStore {
// Only works on linux (ubuntu, debian).
// Finds certificate in /usr/share/ca-certificates/extra/ by its serial number.
// Returns path to the certificate if found, otherwise undefined.
static async _findLinuxCert(arg) {
await arg.ensureCertReadFromFs()
let filenames = await fs.readdir(LINUX_CERT_DIR)
for (let fileName of filenames) {
let filepath = LINUX_CERT_DIR + fileName
let pem = await fs.readFile(filepath)
let certificate = forge.pki.certificateFromPem(pem)
if (arg.serialNumber === certificate.serialNumber)
return filepath
}
}
static async createTempFileIfNeeded(arg) {
if (arg.path) return
arg.tempPath = `temp-${Date.now()}-${Math.random()}.crt`
await fs.writeFile(arg.tempPath, arg.pem)
}
static async deleteTempFileIfNeeded(arg) {
if (!arg.tempPath) return
await fs.unlink(arg.tempPath)
}
// SUGARY METHODS
static async install(arg) {
arg = new CertStruct(arg)
if (!arg.path && !arg.pem)
throw new Error('path to or contents of the certificate has to be defined.')
try {
return await this._install(arg)
} catch(err) {
throw new Error(`Couldn't install certificate.\n${err.stack}`)
} finally {
this.deleteTempFileIfNeeded(arg)
}
}
static async delete(arg) {
arg = new CertStruct(arg)
try {
return await this._delete(arg)
} catch(err) {
throw new Error(`Couldn't delete certificate.\n${err.stack}`)
}
}
static async isInstalled(arg) {
arg = new CertStruct(arg)
try {
return await this._isInstalled(arg)
} catch(err) {
throw new Error(`Couldn't find if certificate is installed.\n${err.stack}`)
}
}
}
class WindowsCertStore extends CertStore {
static async _install(arg) {
await this.createTempFileIfNeeded(arg)
await exec(`certutil -addstore -user -f root "${arg.path || arg.tempPath}"`)
}
static async _delete(arg) {
await arg.ensureCertReadFromFs()
await exec(`certutil -delstore -user root ${arg.serialNumber}`)
}
static async _isInstalled(arg) {
await arg.ensureCertReadFromFs()
try {
await exec(`certutil -verifystore -user root ${arg.serialNumber}`)
return true
} catch(err) {
// certutil always fails if the serial number is not found.
return false
}
}
}
class LinuxCertStore extends CertStore {
static async _install(arg) {
await ensureDirectory(LINUX_CERT_DIR)
var targetPath = LINUX_CERT_DIR + arg.name + '.crt'
if (!arg.pem && arg.path)
arg.pem = await fs.readFile(arg.path)
await fs.writeFile(targetPath, arg.pem)
await exec('update-ca-certificates')
}
static async _delete(arg) {
var targetPath = await this._findLinuxCert(arg)
if (targetPath) await fs.unlink(targetPath)
}
static async _isInstalled(arg) {
return !!(await this._findLinuxCert(arg))
}
}
// Not tested. I don't have a mac. help needed.
class MacCertStore extends CertStore {
static async _install(arg) {
await this.createTempFileIfNeeded(arg)
const certPath = path.join(__dirname, arg.tempPath)
const cmd = `security add-trusted-cert -d -k "${MAC_DIR}" "${certPath}"`
await new Promise((resolve, reject) => {
sudo.exec(cmd, err => err ? reject(err) : resolve() )
})
}
static async _delete(arg) {
await arg.ensureCertReadFromFs()
var fingerPrint = forge.md.sha1.create().update(forge.asn1.toDer(forge.pki.certificateToAsn1(arg.certificate)).getBytes()).digest().toHex()
const cmd = `security delete-certificate -Z ${fingerPrint} "${MAC_DIR}"`
await new Promise((resolve, reject) => {
sudo.exec(cmd, err => err ? reject(err) : resolve())
})
}
static async _isInstalled(arg) {
const allCerts = await exec(`security find-certificate -a -p`)
const pem = arg.pem.replace(/\r/g, '')
return allCerts.stdout.includes(pem)
}
}
var PlatformSpecificCertStore
switch (process.platform) {
case 'win32':
PlatformSpecificCertStore = WindowsCertStore
break
case 'darwin':
PlatformSpecificCertStore = MacCertStore
break
default:
PlatformSpecificCertStore = LinuxCertStore
break
}
export default PlatformSpecificCertStore