-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
180 lines (176 loc) · 5.88 KB
/
index.ts
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
/*
* Created on Thu Aug 04 2022
*
* Copyright (c) 2022 Smart DCC Limited
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import { argv as _argv, exit } from 'process'
import yargs from 'yargs'
import { hideBin } from 'yargs/helpers'
import got from 'got'
import { parse as contentType } from 'content-type'
import AdmZip from 'adm-zip'
import { dirname } from 'node:path/posix'
import { createPrivateKey, X509Certificate } from 'crypto'
import { query } from '../src/certificateSearch'
import { stat } from 'fs/promises'
import { KeyStoreDB } from '../src/db'
import { KeyUsage } from '../src/certificateMetadata'
yargs(hideBin(_argv))
.command(
['build <boxedAddress> [output] [options]', '$0'],
'build a json keystore database from a DCC Boxed server by downloading the crypto.zip package',
(yargs) =>
yargs
.positional('boxedAddress', {
describe: 'ip address of DCC Boxed server',
type: 'string',
})
.positional('output', {
describe: 'output filename (json)',
type: 'string',
default: 'keystore.json',
})
.option('serial', {
describe:
'certificate serial numbers to lookup from local smki service',
type: 'array',
}),
async (args) => {
if (
await stat(args.output)
.then(() => true)
.catch(() => false)
) {
console.log(`[E] output file ${args.output} already exists`)
return
}
const db = await KeyStoreDB.new(args.output)
console.log(`[*] server: ${args.boxedAddress}`)
console.log(`[*] checking for: org-crypto.zip`)
const result = await got(
`http://${args.boxedAddress}/assets/crypto/org-crypto.zip`,
{
timeout: { lookup: 500, connect: 1000, request: 5000 },
method: 'HEAD',
throwHttpErrors: false,
}
)
if (result.statusCode !== 200) {
console.log(
`[E] request was not successful: ${
result.errored ?? result.statusCode
}`
)
exit(1)
}
if (
result.headers['content-type'] === undefined ||
result.headers['content-length'] === undefined
) {
console.log('[E] unexpected result returned from server')
exit(1)
}
const ct = contentType(result.headers['content-type'])
if (ct.type !== 'application/zip') {
console.log(`[E] expected zip file, received: ${ct.type}`)
exit(1)
}
if (Number(result.headers['content-length']) <= 0) {
console.log(
`[E] expected content length > 0: ${result.headers['content-length']}`
)
exit(1)
}
console.log(
`[*] found file with size ${result.headers['content-length']}`
)
const resultCrypto = await got(
`http://${args.boxedAddress}/assets/crypto/org-crypto.zip`,
{
timeout: { lookup: 500, connect: 1000, request: 5000 },
method: 'GET',
responseType: 'buffer',
headers: { 'Accept-Encoding': 'identity' },
}
)
if (
resultCrypto.body.length !== Number(result.headers['content-length'])
) {
console.log(
`[E] received content length not as expected: ${resultCrypto.body.length}`
)
exit(1)
}
const zip = new AdmZip(resultCrypto.body, { readEntries: true })
await Promise.all(
zip.getEntries().map(async (e) => {
if (
!e.isDirectory &&
e.name.endsWith('.pem') &&
dirname(e.entryName).search('cert') >= 0
) {
console.log(`[*] found: ${e.name}`)
const keyFileName = e.entryName
.replace(/\/cert\//, '/key/')
.replace(/.pem$/, '.key')
const keyEntry = zip.getEntry(keyFileName)
if (keyEntry === null) {
console.log(`[W] could not find ${keyFileName}`)
return
}
const certPem = e.getData()
const certificate = new X509Certificate(certPem)
const privKeyPem = keyEntry.getData()
const privKey = createPrivateKey(privKeyPem)
if (!certificate.checkPrivateKey(privKey)) {
console.log(`[W] failed pub/private key check for ${e.name}`)
return
}
try {
const meta = await db.push({
certificate,
private: privKey,
name: e.name.replace(/\.pem$/, ''),
})
console.log(
`[*] stored key pair ${meta.eui.toString()} ${meta.keyUsage.map(
(x) => KeyUsage[x]
)}`
)
} catch (err) {
console.log(`[W] bad org cert ${e.name} ${err}`)
}
}
})
)
if (Array.isArray(args.serial)) {
for (const serial of args.serial) {
const qr = await query(String(serial), `${args.boxedAddress}`)
if (qr) {
db.push({
certificate: qr?.x509,
})
console.log(
`[*] fetched and stored certificate ${qr.meta.eui.toString()} ${qr.meta.keyUsage.map(
(x) => KeyUsage[x]
)}`
)
}
}
}
}
)
.parse()