Skip to content

Commit 2f64b7e

Browse files
committed
feat: add CSR parsing
1 parent 5732ce7 commit 2f64b7e

File tree

2 files changed

+69
-6
lines changed

2 files changed

+69
-6
lines changed

components.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ declare module '@vue/runtime-core' {
3333
CCollapse: typeof import('./src/ui/c-collapse/c-collapse.vue')['default']
3434
'CCollapse.demo': typeof import('./src/ui/c-collapse/c-collapse.demo.vue')['default']
3535
CDiffEditor: typeof import('./src/ui/c-diff-editor/c-diff-editor.vue')['default']
36+
CertificateKeyParser: typeof import('./src/tools/certificate-key-parser/certificate-key-parser.vue')['default']
3637
CFileUpload: typeof import('./src/ui/c-file-upload/c-file-upload.vue')['default']
3738
'CFileUpload.demo': typeof import('./src/ui/c-file-upload/c-file-upload.demo.vue')['default']
3839
ChmodCalculator: typeof import('./src/tools/chmod-calculator/chmod-calculator.vue')['default']

src/tools/certificate-key-parser/certificate-key-parser.vue

Lines changed: 68 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import type {
1616
} from 'sshpk';
1717
import { Base64 } from 'js-base64';
1818
import * as openpgp from 'openpgp';
19+
import * as forge from 'node-forge';
1920
import { useDownloadFileFromBase64 } from '@/composable/downloadBase64';
2021
2122
function buf2Hex(buffer: ArrayBuffer) { // buffer is an ArrayBuffer
@@ -254,6 +255,67 @@ const parsedSections = computedAsync<LabelValue[]>(async () => {
254255
] as LabelValue[];
255256
}
256257
258+
const csr = canParse(inputKeyOrCertificateValue, (value) => {
259+
return forge.pki.certificationRequestFromPem(value.toString(), true, false);
260+
}) as forge.pki.Certificate;
261+
if (csr) {
262+
return [
263+
{
264+
label: 'Type: ',
265+
value: 'Certificate Signing Request',
266+
},
267+
{
268+
label: 'Subject: ',
269+
value: csr.subject.attributes.map(a => JSON.stringify(a, null, 2)).join('\n'),
270+
multiline: true,
271+
},
272+
{
273+
label: 'Issuer: ',
274+
value: csr.issuer?.toString(),
275+
multiline: true,
276+
},
277+
{
278+
label: 'Validity: ',
279+
value: JSON.stringify(csr.validity, null, 2),
280+
},
281+
{
282+
label: 'Signature: ',
283+
value: csr.signature,
284+
},
285+
{
286+
label: 'Signature Oid: ',
287+
value: csr.signatureOid?.toString(),
288+
},
289+
{
290+
label: 'Signature parameters: ',
291+
value: JSON.stringify(csr.signatureParameters, null, 2),
292+
},
293+
{
294+
label: 'Signing info: ',
295+
value: JSON.stringify(csr.siginfo, null, 2),
296+
},
297+
{
298+
label: 'Serial: ',
299+
value: csr.serialNumber?.toString(),
300+
},
301+
{
302+
label: 'Extensions: ',
303+
value: JSON.stringify(csr.extensions, null, 2),
304+
multiline: true,
305+
},
306+
{
307+
label: 'Public Key: ',
308+
value: onErrorReturnErrorMessage(() => forge.pki.publicKeyToPem(csr.publicKey)),
309+
multiline: true,
310+
},
311+
{
312+
label: 'Public Key Fingerprint:',
313+
value: onErrorReturnErrorMessage(() => forge.pki.getPublicKeyFingerprint(csr.publicKey)?.toHex()),
314+
multiline: true,
315+
},
316+
] as LabelValue[];
317+
}
318+
257319
const fingerprint = canParse(inputKeyOrCertificateValue, value => parseFingerprint(value.toString())) as Fingerprint;
258320
if (fingerprint) {
259321
return [
@@ -280,11 +342,11 @@ const parsedSections = computedAsync<LabelValue[]>(async () => {
280342
value: 'PGP Private Key',
281343
},
282344
{
283-
label: ': ',
345+
label: 'Creation Time: ',
284346
value: pgpPrivateKey.getCreationTime().toString(),
285347
},
286348
{
287-
label: ': ',
349+
label: 'Expiration Time: ',
288350
value: (await pgpPrivateKey.getExpirationTime())?.toString() || '',
289351
},
290352
{
@@ -301,7 +363,7 @@ const parsedSections = computedAsync<LabelValue[]>(async () => {
301363
},
302364
{
303365
label: 'Key ID(s): ',
304-
value: pgpPrivateKey.getKeyIDs().map(k => k.toString()).join(' ; '),
366+
value: pgpPrivateKey.getKeyIDs().map(k => k.toHex()).join(' ; '),
305367
},
306368
] as LabelValue[];
307369
}
@@ -314,11 +376,11 @@ const parsedSections = computedAsync<LabelValue[]>(async () => {
314376
value: 'PGP Public Key',
315377
},
316378
{
317-
label: ': ',
379+
label: 'Creation Time: ',
318380
value: pgpPublicKey.getCreationTime().toString(),
319381
},
320382
{
321-
label: ': ',
383+
label: 'Expiration Time: ',
322384
value: (await pgpPublicKey.getExpirationTime())?.toString() || '',
323385
},
324386
{
@@ -335,7 +397,7 @@ const parsedSections = computedAsync<LabelValue[]>(async () => {
335397
},
336398
{
337399
label: 'Key ID(s): ',
338-
value: pgpPublicKey.getKeyIDs().map(k => k.toString()).join(' ; '),
400+
value: pgpPublicKey.getKeyIDs().map(k => k.toHex()).join(' ; '),
339401
},
340402
] as LabelValue[];
341403
}

0 commit comments

Comments
 (0)