Skip to content

Commit

Permalink
feat(new tool): SSL Certificate Converter
Browse files Browse the repository at this point in the history
Fix CorentinTh#1245
Handle JKS, PEM, DER, P12 as input
Handle PEM and DER as output
  • Loading branch information
sharevb committed Sep 22, 2024
1 parent f5c4ab1 commit 1a241d8
Show file tree
Hide file tree
Showing 6 changed files with 316 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/tools/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { tool as base64FileConverter } from './base64-file-converter';
import { tool as base64StringConverter } from './base64-string-converter';
import { tool as basicAuthGenerator } from './basic-auth-generator';
import { tool as emailNormalizer } from './email-normalizer';
import { tool as sslCertConverter } from './ssl-cert-converter';

import { tool as asciiTextDrawer } from './ascii-text-drawer';

Expand Down Expand Up @@ -164,7 +165,15 @@ export const toolsByCategory: ToolCategory[] = [
},
{
name: 'Network',
components: [ipv4SubnetCalculator, ipv4AddressConverter, ipv4RangeExpander, macAddressLookup, macAddressGenerator, ipv6UlaGenerator],
components: [
ipv4SubnetCalculator,
ipv4AddressConverter,
ipv4RangeExpander,
macAddressLookup,
macAddressGenerator,
ipv6UlaGenerator,
sslCertConverter,
],
},
{
name: 'Math',
Expand Down
12 changes: 12 additions & 0 deletions src/tools/ssl-cert-converter/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { ShieldChevron } from '@vicons/tabler';
import { defineTool } from '../tool';

export const tool = defineTool({
name: 'SSL Certificate converter',
path: '/ssl-cert-converter',
description: 'Convert SSL Certificate from different formats',
keywords: ['ssl', 'certificate', 'pem', 'der', 'jks', 'converter'],
component: () => import('./ssl-cert-converter.vue'),
icon: ShieldChevron,
createdAt: new Date('2024-08-15'),
});
15 changes: 15 additions & 0 deletions src/tools/ssl-cert-converter/ssl-cert-converter.e2e.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { test, expect } from '@playwright/test';

test.describe('Tool - Ssl cert converter', () => {
test.beforeEach(async ({ page }) => {
await page.goto('/ssl-cert-converter');
});

test('Has correct title', async ({ page }) => {
await expect(page).toHaveTitle('Ssl cert converter - IT Tools');
});

test('', async ({ page }) => {

});
});
48 changes: 48 additions & 0 deletions src/tools/ssl-cert-converter/ssl-cert-converter.service.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { Buffer } from 'node:buffer';
import { describe, expect, it } from 'vitest';
import { convertCertificate } from './ssl-cert-converter.service';

const formatsData = [
{
title: 'JKS',
input: Buffer.from(
'/u3+7QAAAAIAAAABAAAAAQAGamtzLWpzAAABeDENGOMAAAB/MH0wDgYKKwYBBAEqAhEBAQUABGstagMT9RkL6lTrGvx2untoFmXM13xjQjMKfxFU/iQHuk3Y44LeB5oP9/e8KEe6nK1NTQhaTRrKyMZGJhs5Oro+TLowYerbBiJJ2DKyBTVjMDCZj8f29hOXpxQpIVv6IEAlFJwL3TQNydxjdgAAAAEABVguNTA5AAAB5jCCAeIwggGHoAMCAQICBCljjXAwDAYIKoZIzj0EAwIFADBmMQ8wDQYDVQQGEwZqa3MtanMxDzANBgNVBAgTBmprcy1qczEPMA0GA1UEBxMGamtzLWpzMQ8wDQYDVQQKEwZsZW5jaHYxDzANBgNVBAsTBmprcy1qczEPMA0GA1UEAxMGamtzLWpzMB4XDTIxMDMxNDE0MDQwNVoXDTIxMDYxMjE0MDQwNVowZjEPMA0GA1UEBhMGamtzLWpzMQ8wDQYDVQQIEwZqa3MtanMxDzANBgNVBAcTBmprcy1qczEPMA0GA1UEChMGbGVuY2h2MQ8wDQYDVQQLEwZqa3MtanMxDzANBgNVBAMTBmprcy1qczBZMBMGByqGSM49AgEGCCqGSM49AwEHA0IABEXhUAIulQWBEFCRBqPl7t/FKuI6hzWYpdBpwWZSRiDkp9A0xeIJyHazINyyx2xIDyCvR1vrqIhMuFxJIWxp8DmjITAfMB0GA1UdDgQWBBRpec0VpSfJmWjyzDtiUWk5difuTjAMBggqhkjOPQQDAgUAA0cAMEQCIE2hdtgyJZgO+gGZrCBx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NURhUjNkU2tEMTBxWmR5Q0EKV0JFPQotLS0tLUVORCBFTkNSWVBURUQgUFJJVkFURSBLRVktLS0tLQo=',
'base64').toString('binary'),

pass: '',
convertedCount: 1,
},
{
title: 'DER',
input: Buffer.from(
'MIIC+zCCAeOgAwIBAgIJAOxwUeP5dw46MA0GCSqGSIb3DQEBCwUAMBQxEjAQBgNVBAMMCWxvY2FsaG9zdDAeFw0xNzA1MzExNDExNTNaFw00NDEwMTYxNDExNTNaMBQxEjAQBgNVBAMMCWxvY2FsaG9zdDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBALEPP29L/6fGvrA6wVD1kaPMS5o2O8h7AwIgnV8Fne74q/5NyMWrd/s5jAzug+yMRqLD8UbrA2s9vwKwkflKxHFZeIh1BtttTd31HmyPiE5tqp2/N2SqIm6bVb/8R4+i6CkW+K8SWk4+hQbEeAaAzNxq1oH/QKqkq1CzVsTjoLiUW2+dWw1iC0ZGefoablyICuIfBBH4OSK5KOKA8oKdOHoucYMOHXbXvsJVaNP+wJa8+qaDot45s2tliDljX06xNzOwSWSGzFbjiyEm5YuhO1VEIojQ+fe4EfToD00OrC5eTUI2BQ5eJ7FA8udjqAE86xmj9f63GxTbHvUyXx3ve8MCAwEAAaNQME4wHQYDVR0OBBYEFLW83bDG7ZWwluyWcueSk5A/hCk6MB8GA1UdIwQYMBaAFLW83bDG7ZWwluyWcueSk5A/hCk6MAwGA1UdEwQFMAMBAf8wDQYJKoZIhvcNAQELBQADggEBAIfVtAO6VOwUQ2zp4QI698uV3ESxYIg1K6kTTWkvXkvYMlG5Wuvsan83V4zCTpclKSqePPr73eN9HkcGEOEXU+zM1eS4RfW8TDj9eLbGt0cBGzWC4gXJiMEx6Rr0VpcnjF5u9Iqty+YWQ/8MH10kTeOojuAtOmcrVe3VwB8bDx+PwHd0ZZCTWxi+qU/ST5jOEokiihaW68aHOc6IXgNRChPpI01gZVUFRevaJP5g1Yk41cSegGEPkmNO1X8ps2wrIzV28Ih5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45Rnk8cn+ONSeG8BiD6Z5wxcXZQ1use8QksFbE2aDKIVhi3gnQnp5r+jxBnEwYrl5u3E98sA5rIHi2cllyLpzmKiqjb3QxC+aEu20EJCdMZ9IiSdXdse1CFboztDNafwWTsvUp1XFUF8f69cDzkbH7x+/E3h6P7HyxmmSEr5Xdwmg4sQAy5sY43lsNPDSa+qfurqRyKMk3eFERFSsvDtR/K8CLsNA4IcQsbWBFdLamfkDZsow8Roei+qAOEX8gXc72RUYKsjhGa7zXScHKCAe+f22KGQVUIhoDZWNOVPVSLZN3vP3hFXmX+ZXNw1kBS29filpHDp3+S1gnCXD33Uh1BnqMSUwIwYJKoZIhvcNAQkVMRYEFCosIveCkCjVTq8eGBQN74TQ5GpZMDEwITAJBgUrDgMCGgUABBSQaubCDTe3BlNcJeUlEZqFCneD6gQI97YoYVQLu8QCAggA',
'base64').toString('binary'),
pass: 'chef$123',
convertedCount: 1,
},
];

describe('ssl-cert-converter', () => {
for (const format of formatsData) {
const { input, pass, convertedCount, title } = format;
it(`Convert '${title}' format to correct values`, () => {
expect(convertCertificate(input, pass)).toHaveLength(convertedCount);
});
}
});
119 changes: 119 additions & 0 deletions src/tools/ssl-cert-converter/ssl-cert-converter.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
import type { Buffer } from 'node:buffer';
import {
parseCertificate,
} from 'sshpk';

import type {
Certificate,
CertificateFormat,
} from 'sshpk';

import * as forge from 'node-forge';
import jks from 'jks-js';

function convertPKCS12ToPem(p12base64: forge.Bytes | forge.util.ByteBuffer, password: string) {
const p12Asn1 = forge.asn1.fromDer(p12base64, false);
const p12 = forge.pkcs12.pkcs12FromAsn1(p12Asn1, false, password);

const pemKey = getKeyFromP12(p12);
const { pemCertificate, commonName } = getCertificateFromP12(p12);

return { pemKey, pemCertificate, commonName };
}

function getKeyFromP12(p12: forge.pkcs12.Pkcs12Pfx) {
const keyData = p12.getBags({ bagType: forge.pki.oids.pkcs8ShroudedKeyBag });
let pkcs8Key = keyData[forge.pki.oids.pkcs8ShroudedKeyBag]![0];

if (typeof pkcs8Key === 'undefined') {
pkcs8Key = keyData[forge.pki.oids.keyBag]![0];
}

if (typeof pkcs8Key === 'undefined') {
throw new TypeError('Unable to get private key.');
}

return forge.pki.privateKeyToPem(pkcs8Key.key!);
}

function getCertificateFromP12(p12: any) {
const certData = p12.getBags({ bagType: forge.pki.oids.certBag });
const certificate = certData[forge.pki.oids.certBag][0];

const pemCertificate = forge.pki.certificateToPem(certificate.cert);
const commonName = certificate.cert.subject.attributes[0].value;
return { pemCertificate, commonName };
}

export function convertCertificate(
inputKeyOrCertificateValue: string | Buffer,
password: string) {
const canParse = (value: any, parseFunction: (value: any) => any) => {
try {
return parseFunction(value);
}
catch (e: any) {
// console.log(e);
return null;
}
};

const cert = canParse(inputKeyOrCertificateValue, (value) => {
for (const format of ['openssh', 'pem', 'x509']) {
try {
return parseCertificate(value, format as CertificateFormat);
}
catch {
}
}
return null;
}) as Certificate;
if (cert) {
return [{
alias: '#default',
key: null,
der: canParse(cert, c => c.toBuffer('x509')),
pem: cert.toString('pem'),
}];
}

const pkcs12 = canParse(inputKeyOrCertificateValue, (value) => {
return convertPKCS12ToPem(forge.util.createBuffer(value, 'raw'), password);
});
if (pkcs12) {
return [{
alias: pkcs12.commonName,
key: pkcs12.pemKey,
der: canParse(pkcs12.pemCertificate, pemCert => parseCertificate(pemCert, 'pem').toBuffer('x509')),
pem: pkcs12.pemCertificate,
}];
}

const parsedJKS = canParse(inputKeyOrCertificateValue, (value) => {
return jks.toPem(
value,
password,
);
});
if (parsedJKS) {
return Object.entries(parsedJKS).map(([k, v]) => {
if (typeof v === 'string') {
return {
alias: k,
key: null,
der: canParse(v, pemCert => parseCertificate(pemCert, 'pem').toBuffer('x509')),
pem: v,
};
}
const { cert, key } = v as { cert: string; key: string };
return {
alias: k,
key,
der: canParse(cert, pemCert => parseCertificate(pemCert, 'pem').toBuffer('x509')),
pem: cert,
};
});
}

return null;
}
Loading

0 comments on commit 1a241d8

Please sign in to comment.