fix(Certificate/Key Parser): handle multiple PEM certificate in one file

Fix #74
This commit is contained in:
ShareVB committed 2025-05-17 18:42:48 +02:00
1 parent e2a6580851
commit 48854592b0
4 files changed
+10477 -13176

No files matched your search

@@ -18,6 +18,22 @@ import * as openpgp from 'openpgp';
import * as forge from 'node-forge';
import { type LabelValue, getCSRLabelValues, getCertificateLabelValues, getFingerprintLabelValues, getPGPPrivateKeyLabelValuesAsync, getPGPPublicKeyLabelValuesAsync, getPrivateKeyLabelValues, getPublicKeyLabelValues, getSignatureLabelValues } from './certificate-key-parser.infos';
export async function getKeysOrCertificatesInfosAsync(keyOrCertificateValue: string | Buffer, passphrase: string) {
const parts = keyOrCertificateValue.toString().trim().split(/(-----BEGIN [^-]+-----\n)/).filter(s => s !== '');
if (!parts.length) {
return [await getKeyOrCertificateInfosAsync(keyOrCertificateValue, passphrase)];
}
const parsedPEMs: Array<{
values: LabelValue[]
certificateX509DER?: undefined | string
}> = [];
for (let i = 0; i < parts.length; i += 2) {
const pemPart = parts[i] + parts[i + 1];
parsedPEMs.push(await getKeyOrCertificateInfosAsync(pemPart, passphrase));
}
return parsedPEMs;
}
export async function getKeyOrCertificateInfosAsync(keyOrCertificateValue: string | Buffer, passphrase: string) {
try {
const canParse = (value: string | Buffer, parseFunction: (value: string | Buffer) => any) => {
@@ -1,7 +1,7 @@
<script setup lang="ts">
import { Buffer } from 'node:buffer';
import { getKeyOrCertificateInfosAsync } from './certificate-key-parser.service';
import { getKeysOrCertificatesInfosAsync } from './certificate-key-parser.service';
import { type LabelValue } from './certificate-key-parser.infos';
import { useDownloadFileFromBase64 } from '@/composable/downloadBase64';
@@ -37,7 +37,7 @@ function downloadX509DERFile() {
}
}
const parsedSections = computedAsync<LabelValue[]>(async () => {
const parsedSections = computedAsync<LabelValue[][]>(async () => {
const inputContent = inputKeyOrCertificate.value;
const file = fileInput.value;
let inputKeyOrCertificateValue: string | Buffer = '';
@@ -47,9 +47,15 @@ const parsedSections = computedAsync<LabelValue[]>(async () => {
else if (inputType.value === 'content' && inputContent) {
inputKeyOrCertificateValue = inputContent;
}
const { values, certificateX509DER: certPEM } = await getKeyOrCertificateInfosAsync(inputKeyOrCertificateValue, passphrase.value);
certificateX509DER.value = certPEM || '';
return values;
const parsed = await getKeysOrCertificatesInfosAsync(inputKeyOrCertificateValue, passphrase.value);
if (parsed.length === 1) {
const { values, certificateX509DER: certPEM } = parsed[0];
certificateX509DER.value = certPEM || '';
return [values];
}
else {
return parsed.map(p => p.values);
}
});
</script>
@@ -96,20 +102,26 @@ const parsedSections = computedAsync<LabelValue[]>(async () => {
<n-divider />
<input-copyable
v-for="{ label, value, multiline } of parsedSections"
:key="label"
:label="label"
:data-test-id="label"
label-position="left"
label-width="100px"
label-align="right"
<c-card
v-for="(part, partIndex) of parsedSections"
:key="partIndex"
mb-2
>
<input-copyable
v-for="{ label, value, multiline } of part"
:key="label"
:label="label"
:data-test-id="label"
label-position="left"
label-width="100px"
label-align="right"
autosize mb-2
:multiline="multiline"
:value="value"
placeholder="Not Set"
/>
autosize mb-2
:multiline="multiline"
:value="value"
placeholder="Not Set"
/>
</c-card>
<div v-if="certificateX509DER !== ''" flex justify-center>
<c-button @click="downloadX509DERFile()">