diff --git a/src/tools/ssh-key-converter/index.ts b/src/tools/ssh-key-converter/index.ts new file mode 100644 index 00000000..8209b299 --- /dev/null +++ b/src/tools/ssh-key-converter/index.ts @@ -0,0 +1,12 @@ +import { defineTool } from '../tool'; + +export const tool = defineTool({ + name: 'SSH Key Format Converter', + path: '/ssh-key-converter', + description: 'Convert SSH keys between different formats', + keywords: ['ssh', 'key', 'rfc4716', 'pkcs1', 'pem', 'pkcs8', 'converter'], + component: () => import('./ssh-key-converter.vue'), + icon: defineAsyncComponent(() => import('@vicons/tabler/es/ShieldChevron')), + createdAt: new Date('2026-11-29'), + category: 'Crypto', +}); diff --git a/src/tools/ssh-key-converter/ssh-key-converter.service.test.ts b/src/tools/ssh-key-converter/ssh-key-converter.service.test.ts new file mode 100644 index 00000000..faf1efb2 --- /dev/null +++ b/src/tools/ssh-key-converter/ssh-key-converter.service.test.ts @@ -0,0 +1,147 @@ +import { describe, expect, it } from 'vitest'; + +import { detectFormat, openSSHToRFC4716, rfc4716ToOpenSSH } from './ssh-key-converter.service'; + +const OPENSSH_ED25519 = 'ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIMaABCDEFGHIJKLMNopqrstuvwxyz1234567890 test@example.com'; + +describe('detectFormat', () => { + it('detects openssh rsa key', () => { + expect(detectFormat('ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC')).toBe('ssh'); + }); + + it('detects openssh ed25519 key', () => { + expect(detectFormat(OPENSSH_ED25519)).toBe('ssh'); + }); + + it('detects ecdsa key', () => { + expect(detectFormat('ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTY=')).toBe('ssh'); + }); + + it('detects rfc4716 key', () => { + expect( + detectFormat(` +---- BEGIN SSH2 PUBLIC KEY ---- +AAAA +---- END SSH2 PUBLIC KEY ---- +`), + ).toBe('rfc4716'); + }); + + it('detects pem public key', () => { + expect( + detectFormat(` +-----BEGIN PUBLIC KEY----- +AAAA +-----END PUBLIC KEY----- +`), + ).toBe('pem'); + }); + + it('detects rsa pem public key', () => { + expect( + detectFormat(` +-----BEGIN RSA PUBLIC KEY----- +AAAA +-----END RSA PUBLIC KEY----- +`), + ).toBe('pem'); + }); + + it('detects pkcs8 private key', () => { + expect( + detectFormat(` +-----BEGIN PRIVATE KEY----- +AAAA +-----END PRIVATE KEY----- +`), + ).toBe('pkcs8'); + }); + + it('throws for unknown format', () => { + expect(() => detectFormat('this is not a key')).toThrow('Cannot detect key format'); + }); +}); + +describe('openSSHToRFC4716', () => { + it('creates valid rfc4716 envelope', () => { + const result = openSSHToRFC4716(OPENSSH_ED25519); + + expect(result).toContain('BEGIN SSH2 PUBLIC KEY'); + + expect(result).toContain('END SSH2 PUBLIC KEY'); + + expect(result).toContain('Comment: "test@example.com"'); + + expect(result).toContain('AAAAC3NzaC1lZDI1NTE5'); + }); + + it('supports commentless keys', () => { + const result = openSSHToRFC4716('ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC'); + + expect(result).not.toContain('Comment:'); + }); + + it('throws for malformed openssh input', () => { + expect(() => openSSHToRFC4716('not-a-key')).toThrow('Invalid OpenSSH key'); + }); + + it('wraps long base64 content', () => { + const longBase64 = 'A'.repeat(200); + + const result = openSSHToRFC4716(`ssh-rsa ${longBase64}`); + + const lines = result.split('\n'); + + const payloadLines = lines.filter((line) => !line.startsWith('----') && !line.startsWith('Comment:')); + + expect(payloadLines.some((line) => line.length <= 72)).toBe(true); + }); +}); + +describe('rfc4716ToOpenSSH', () => { + it('converts rfc4716 to openssh', () => { + const blob = 'AAAAC3NzaC1lZDI1NTE5AAAAIMaABCDEFGHIJKLMNopqrstuvwxyz1234567890'; + + const rfc = ` +---- BEGIN SSH2 PUBLIC KEY ---- +Comment: "user@test" +${blob} +---- END SSH2 PUBLIC KEY ---- +`; + + const result = rfc4716ToOpenSSH(rfc); + + expect(result.startsWith('ssh-ed25519 ')).toBe(true); + + expect(result.endsWith(blob)).toBe(true); + }); + + it('ignores comments', () => { + const blob = 'AAAAC3NzaC1lZDI1NTE5AAAAIMaABCDEFGHIJKLMNopqrstuvwxyz1234567890'; + + const result = rfc4716ToOpenSSH(` +---- BEGIN SSH2 PUBLIC KEY ---- +Comment: "one" +Comment: "two" +${blob} +---- END SSH2 PUBLIC KEY ---- +`); + + expect(result.startsWith('ssh-ed25519 ')).toBe(true); + }); +}); + +describe('round trip conversions', () => { + it('preserves payload through openssh->rfc4716 conversion', () => { + const openssh = OPENSSH_ED25519; + + const rfc = openSSHToRFC4716(openssh); + + const payload = rfc + .split('\n') + .filter((line) => !line.startsWith('----') && !line.startsWith('Comment:')) + .join(''); + + expect(payload).toBe(openssh.split(' ')[1]); + }); +}); diff --git a/src/tools/ssh-key-converter/ssh-key-converter.service.ts b/src/tools/ssh-key-converter/ssh-key-converter.service.ts new file mode 100644 index 00000000..c1ea8a8a --- /dev/null +++ b/src/tools/ssh-key-converter/ssh-key-converter.service.ts @@ -0,0 +1,66 @@ +export type KeyFormat = 'auto' | 'ssh' | 'rfc4716' | 'pem' | 'pkcs8'; + +export function detectFormat(key: string): KeyFormat { + const text = key.trim(); + + if (text.includes('BEGIN SSH2 PUBLIC KEY')) { + return 'rfc4716'; + } + + if (text.startsWith('ssh-rsa') || text.startsWith('ssh-ed25519') || text.startsWith('ecdsa-sha2-')) { + return 'ssh'; + } + + if (text.includes('BEGIN PUBLIC KEY') || text.includes('BEGIN RSA PUBLIC KEY')) { + return 'pem'; + } + + if (text.includes('BEGIN PRIVATE KEY')) { + return 'pkcs8'; + } + + throw new Error('Cannot detect key format'); +} + +export function rfc4716ToOpenSSH(rfc: string): string { + const lines = rfc + .split(/\r?\n/) + .map((x) => x.trim()) + .filter(Boolean); + + const payload = lines.filter((line) => !line.startsWith('----') && !line.startsWith('Comment:')).join(''); + + const blob = Buffer.from(payload, 'base64'); + + const typeLength = blob.readUInt32BE(0); + + const type = blob.subarray(4, 4 + typeLength).toString(); + + return `${type} ${payload}`; +} + +export function openSSHToRFC4716(openssh: string): string { + const parts = openssh.trim().split(/\s+/); + + if (parts.length < 2) { + throw new Error('Invalid OpenSSH key'); + } + + const comment = parts.slice(2).join(' '); + + const base64 = parts[1]; + + const output: string[] = ['---- BEGIN SSH2 PUBLIC KEY ----']; + + if (comment) { + output.push(`Comment: "${comment}"`); + } + + for (let i = 0; i < base64.length; i += 72) { + output.push(base64.slice(i, i + 72)); + } + + output.push('---- END SSH2 PUBLIC KEY ----'); + + return output.join('\n'); +} diff --git a/src/tools/ssh-key-converter/ssh-key-converter.vue b/src/tools/ssh-key-converter/ssh-key-converter.vue new file mode 100644 index 00000000..095e8f64 --- /dev/null +++ b/src/tools/ssh-key-converter/ssh-key-converter.vue @@ -0,0 +1,102 @@ + + +