mirror of
https://github.com/sharevb/it-tools.git
synced 2026-11-22 22:02:38 +03:00
feat(CSR and x509 Generators): handle Subject Alternative Names
Fix #31
This commit is contained in:
1 parent
371517ff98
commit
637ba07ef3
6 files changed
+232
-67
No files matched your search
@@ -1,22 +1,7 @@
|
|||||||
import { pki } from 'node-forge';
|
import { pki } from 'node-forge';
|
||||||
import workerScript from 'node-forge/dist/prime.worker.min?url';
|
import { generateRSAPairs, getSubjectAlternativeNames } from '@/utils/crypto';
|
||||||
|
|
||||||
export { generateCSR };
|
export async function generateCSR(config: {
|
||||||
|
|
||||||
function generateRSAPairs({ bits = 2048 }) {
|
|
||||||
return new Promise<pki.rsa.KeyPair>((resolve, reject) =>
|
|
||||||
pki.rsa.generateKeyPair({ bits, workerScript }, (err, keyPair) => {
|
|
||||||
if (err) {
|
|
||||||
reject(err);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
resolve(keyPair);
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
async function generateCSR(config: {
|
|
||||||
bits?: number
|
bits?: number
|
||||||
password?: string
|
password?: string
|
||||||
commonName?: string
|
commonName?: string
|
||||||
@@ -25,6 +10,7 @@ async function generateCSR(config: {
|
|||||||
state?: string
|
state?: string
|
||||||
organizationName?: string
|
organizationName?: string
|
||||||
organizationalUnit?: string
|
organizationalUnit?: string
|
||||||
|
subjectAlternativeNames?: string
|
||||||
contactEmail?: string
|
contactEmail?: string
|
||||||
} = {}): Promise<{
|
} = {}): Promise<{
|
||||||
publicKeyPem: string
|
publicKeyPem: string
|
||||||
@@ -59,6 +45,18 @@ async function generateCSR(config: {
|
|||||||
value: config.contactEmail,
|
value: config.contactEmail,
|
||||||
}].filter(attr => attr.value !== null && attr.value?.trim() !== ''));
|
}].filter(attr => attr.value !== null && attr.value?.trim() !== ''));
|
||||||
|
|
||||||
|
const extensions = [];
|
||||||
|
const sanExtension = getSubjectAlternativeNames(config.subjectAlternativeNames);
|
||||||
|
if (sanExtension) {
|
||||||
|
extensions.push(sanExtension);
|
||||||
|
}
|
||||||
|
if (extensions.length) {
|
||||||
|
csr.setAttributes([{
|
||||||
|
name: 'extensionRequest',
|
||||||
|
extensions,
|
||||||
|
}]);
|
||||||
|
}
|
||||||
|
|
||||||
// sign certification request
|
// sign certification request
|
||||||
csr.sign(privateKey);
|
csr.sign(privateKey);
|
||||||
|
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ const city = ref('Paris');
|
|||||||
const state = ref('FR');
|
const state = ref('FR');
|
||||||
const country = ref('France');
|
const country = ref('France');
|
||||||
const contactEmail = ref('');
|
const contactEmail = ref('');
|
||||||
|
const subjectAlternativeNames = ref('');
|
||||||
const emptyCSR = { csrPem: '', privateKeyPem: '', publicKeyPem: '' };
|
const emptyCSR = { csrPem: '', privateKeyPem: '', publicKeyPem: '' };
|
||||||
|
|
||||||
const [certs, refreshCerts] = computedRefreshableAsync(
|
const [certs, refreshCerts] = computedRefreshableAsync(
|
||||||
@@ -39,6 +40,7 @@ const [certs, refreshCerts] = computedRefreshableAsync(
|
|||||||
state: state.value,
|
state: state.value,
|
||||||
organizationName: organizationName.value,
|
organizationName: organizationName.value,
|
||||||
organizationalUnit: organizationalUnit.value,
|
organizationalUnit: organizationalUnit.value,
|
||||||
|
subjectAlternativeNames: subjectAlternativeNames.value,
|
||||||
contactEmail: contactEmail.value,
|
contactEmail: contactEmail.value,
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
@@ -134,6 +136,19 @@ const [certs, refreshCerts] = computedRefreshableAsync(
|
|||||||
</n-form-item>
|
</n-form-item>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<div>
|
||||||
|
<n-form-item
|
||||||
|
label="Subject Alternative Names:"
|
||||||
|
label-placement="top"
|
||||||
|
>
|
||||||
|
<n-input
|
||||||
|
v-model:value="subjectAlternativeNames"
|
||||||
|
placeholder="DNS Names, emails, IP, URI..."
|
||||||
|
type="textarea"
|
||||||
|
/>
|
||||||
|
</n-form-item>
|
||||||
|
</div>
|
||||||
|
|
||||||
<div>
|
<div>
|
||||||
<n-form-item
|
<n-form-item
|
||||||
label="Private Key passphrase:"
|
label="Private Key passphrase:"
|
||||||
@@ -159,17 +174,17 @@ const [certs, refreshCerts] = computedRefreshableAsync(
|
|||||||
<div v-if="commonNameValidation.isValid">
|
<div v-if="commonNameValidation.isValid">
|
||||||
<div>
|
<div>
|
||||||
<h3>Certificate Signing Request</h3>
|
<h3>Certificate Signing Request</h3>
|
||||||
<TextareaCopyable :value="certs.csrPem" />
|
<TextareaCopyable :value="certs.csrPem" :download-file-name="`${organizationName}.csr`" />
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div>
|
<div>
|
||||||
<h3>Public key</h3>
|
<h3>Public key</h3>
|
||||||
<TextareaCopyable :value="certs.publicKeyPem" :word-wrap="true" />
|
<TextareaCopyable :value="certs.publicKeyPem" word-wrap :download-file-name="`${organizationName}.pem`" />
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div>
|
<div>
|
||||||
<h3>Private key</h3>
|
<h3>Private key</h3>
|
||||||
<TextareaCopyable :value="certs.privateKeyPem" />
|
<TextareaCopyable :value="certs.privateKeyPem" :download-file-name="`${organizationName}.key`" />
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -1,36 +1,7 @@
|
|||||||
import { asn1, md, pki, random, util } from 'node-forge';
|
import { asn1, md, pki, random, util } from 'node-forge';
|
||||||
import workerScript from 'node-forge/dist/prime.worker.min?url';
|
import { generateRSAPairs, getSubjectAlternativeNames, toPositiveHex } from '@/utils/crypto';
|
||||||
|
|
||||||
export { generateSSLCertificate };
|
export async function generateSSLCertificate(config: {
|
||||||
|
|
||||||
function generateRSAPairs({ bits = 2048 }) {
|
|
||||||
return new Promise<pki.rsa.KeyPair>((resolve, reject) =>
|
|
||||||
pki.rsa.generateKeyPair({ bits, workerScript }, (err, keyPair) => {
|
|
||||||
if (err) {
|
|
||||||
reject(err);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
resolve(keyPair);
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
// a hexString is considered negative if it's most significant bit is 1
|
|
||||||
// because serial numbers use ones' complement notation
|
|
||||||
// this RFC in section 4.1.2.2 requires serial numbers to be positive
|
|
||||||
// http://www.ietf.org/rfc/rfc5280.txt
|
|
||||||
function toPositiveHex(hexString: string) {
|
|
||||||
let mostSiginficativeHexAsInt = Number.parseInt(hexString[0], 16);
|
|
||||||
if (mostSiginficativeHexAsInt < 8) {
|
|
||||||
return hexString;
|
|
||||||
}
|
|
||||||
|
|
||||||
mostSiginficativeHexAsInt -= 8;
|
|
||||||
return mostSiginficativeHexAsInt.toString() + hexString.substring(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
async function generateSSLCertificate(config: {
|
|
||||||
bits?: number
|
bits?: number
|
||||||
password?: string
|
password?: string
|
||||||
commonName?: string
|
commonName?: string
|
||||||
@@ -40,6 +11,7 @@ async function generateSSLCertificate(config: {
|
|||||||
organizationName?: string
|
organizationName?: string
|
||||||
organizationalUnit?: string
|
organizationalUnit?: string
|
||||||
contactEmail?: string
|
contactEmail?: string
|
||||||
|
subjectAlternativeNames?: string
|
||||||
days?: number
|
days?: number
|
||||||
} = {}): Promise<{
|
} = {}): Promise<{
|
||||||
fingerprint: string
|
fingerprint: string
|
||||||
@@ -83,19 +55,28 @@ async function generateSSLCertificate(config: {
|
|||||||
cert.setSubject(attrs);
|
cert.setSubject(attrs);
|
||||||
cert.setIssuer(attrs);
|
cert.setIssuer(attrs);
|
||||||
|
|
||||||
cert.publicKey = publicKey;
|
const extensions: any[] = [
|
||||||
|
{
|
||||||
|
name: 'basicConstraints',
|
||||||
|
cA: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'keyUsage',
|
||||||
|
keyCertSign: true,
|
||||||
|
digitalSignature: true,
|
||||||
|
nonRepudiation: true,
|
||||||
|
keyEncipherment: true,
|
||||||
|
dataEncipherment: true,
|
||||||
|
}];
|
||||||
|
const sanExtension = getSubjectAlternativeNames(config.subjectAlternativeNames);
|
||||||
|
if (sanExtension) {
|
||||||
|
extensions.push(sanExtension);
|
||||||
|
}
|
||||||
|
if (extensions.length) {
|
||||||
|
cert.setExtensions(extensions);
|
||||||
|
}
|
||||||
|
|
||||||
cert.setExtensions([{
|
cert.publicKey = publicKey;
|
||||||
name: 'basicConstraints',
|
|
||||||
cA: true,
|
|
||||||
}, {
|
|
||||||
name: 'keyUsage',
|
|
||||||
keyCertSign: true,
|
|
||||||
digitalSignature: true,
|
|
||||||
nonRepudiation: true,
|
|
||||||
keyEncipherment: true,
|
|
||||||
dataEncipherment: true,
|
|
||||||
}]);
|
|
||||||
|
|
||||||
cert.sign(privateKey);
|
cert.sign(privateKey);
|
||||||
|
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ const city = ref('Paris');
|
|||||||
const state = ref('FR');
|
const state = ref('FR');
|
||||||
const country = ref('France');
|
const country = ref('France');
|
||||||
const contactEmail = ref('');
|
const contactEmail = ref('');
|
||||||
|
const subjectAlternativeNames = ref('');
|
||||||
const emptyCSR = { certificatePem: '', privateKeyPem: '', publicKeyPem: '', fingerprint: '' };
|
const emptyCSR = { certificatePem: '', privateKeyPem: '', publicKeyPem: '', fingerprint: '' };
|
||||||
|
|
||||||
const [certs, refreshCerts] = computedRefreshableAsync(
|
const [certs, refreshCerts] = computedRefreshableAsync(
|
||||||
@@ -41,6 +42,7 @@ const [certs, refreshCerts] = computedRefreshableAsync(
|
|||||||
organizationName: organizationName.value,
|
organizationName: organizationName.value,
|
||||||
organizationalUnit: organizationalUnit.value,
|
organizationalUnit: organizationalUnit.value,
|
||||||
contactEmail: contactEmail.value,
|
contactEmail: contactEmail.value,
|
||||||
|
subjectAlternativeNames: subjectAlternativeNames.value,
|
||||||
days: days.value,
|
days: days.value,
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
@@ -149,6 +151,19 @@ const [certs, refreshCerts] = computedRefreshableAsync(
|
|||||||
</n-form-item>
|
</n-form-item>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<div>
|
||||||
|
<n-form-item
|
||||||
|
label="Subject Alternative Names:"
|
||||||
|
label-placement="top"
|
||||||
|
>
|
||||||
|
<n-input
|
||||||
|
v-model:value="subjectAlternativeNames"
|
||||||
|
placeholder="DNS Names, emails, IP, URI..."
|
||||||
|
type="textarea"
|
||||||
|
/>
|
||||||
|
</n-form-item>
|
||||||
|
</div>
|
||||||
|
|
||||||
<div>
|
<div>
|
||||||
<n-form-item
|
<n-form-item
|
||||||
label="Private Key passphrase:"
|
label="Private Key passphrase:"
|
||||||
@@ -174,22 +189,22 @@ const [certs, refreshCerts] = computedRefreshableAsync(
|
|||||||
<div v-if="commonNameValidation.isValid">
|
<div v-if="commonNameValidation.isValid">
|
||||||
<div>
|
<div>
|
||||||
<h3>Certificate (PEM)</h3>
|
<h3>Certificate (PEM)</h3>
|
||||||
<TextareaCopyable :value="certs.certificatePem" />
|
<TextareaCopyable :value="certs.certificatePem" :download-file-name="`${organizationName}.crt`" />
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div>
|
<div>
|
||||||
<h3>Fingerprint:</h3>
|
<h3>Fingerprint:</h3>
|
||||||
<TextareaCopyable :value="certs.fingerprint" :word-wrap="true" />
|
<TextareaCopyable :value="certs.fingerprint" word-wrap />
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div>
|
<div>
|
||||||
<h3>Public key</h3>
|
<h3>Public key</h3>
|
||||||
<TextareaCopyable :value="certs.publicKeyPem" :word-wrap="true" />
|
<TextareaCopyable :value="certs.publicKeyPem" word-wrap :download-file-name="`${organizationName}.pem`" />
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div>
|
<div>
|
||||||
<h3>Private key</h3>
|
<h3>Private key</h3>
|
||||||
<TextareaCopyable :value="certs.privateKeyPem" />
|
<TextareaCopyable :value="certs.privateKeyPem" :download-file-name="`${organizationName}.key`" />
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -0,0 +1,75 @@
|
|||||||
|
import { describe, expect, it } from 'vitest';
|
||||||
|
import { SANType_DNSName, SANType_DirectoryName, SANType_IPAddress, SANType_OtherName, SANType_Rfc822Name, SANType_UniformResourceIdentifier, getSANType, getSubjectAlternativeNames, toPositiveHex } from './crypto';
|
||||||
|
|
||||||
|
describe('toPositiveHex function', () => {
|
||||||
|
it('should return the same hex string if the most significant hex is less than 8', () => {
|
||||||
|
expect(toPositiveHex('2a3f')).toBe('2a3f');
|
||||||
|
expect(toPositiveHex('7fff')).toBe('7fff');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should modify the most significant hex digit if it is greater than or equal to 8', () => {
|
||||||
|
expect(toPositiveHex('8a3f')).toBe('0a3f');
|
||||||
|
expect(toPositiveHex('f234')).toBe('7234');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('getSANType function', () => {
|
||||||
|
it('should return SANType_UniformResourceIdentifier for URI-like strings', () => {
|
||||||
|
expect(getSANType('http://example.com')).toBe(SANType_UniformResourceIdentifier);
|
||||||
|
expect(getSANType('https://example.com')).toBe(SANType_UniformResourceIdentifier);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return SANType_DNSName for DNS-like strings', () => {
|
||||||
|
expect(getSANType('example.com')).toBe(SANType_DNSName);
|
||||||
|
expect(getSANType('*.example.com')).toBe(SANType_DNSName);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return SANType_Rfc822Name for email addresses', () => {
|
||||||
|
expect(getSANType('user@example.com')).toBe(SANType_Rfc822Name);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return SANType_IPAddress for valid IP addresses', () => {
|
||||||
|
expect(getSANType('192.168.0.1')).toBe(SANType_IPAddress);
|
||||||
|
expect(getSANType('255.255.255.255')).toBe(SANType_IPAddress);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return SANType_DirectoryName for strings containing "DC="', () => {
|
||||||
|
expect(getSANType('DC=example')).toBe(SANType_DirectoryName);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return SANType_OtherName for all other cases', () => {
|
||||||
|
expect(getSANType('randomString')).toBe(SANType_OtherName);
|
||||||
|
expect(getSANType('')).toBe(SANType_OtherName);
|
||||||
|
expect(getSANType('1234')).toBe(SANType_OtherName);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('getSubjectAlternativeNames function', () => {
|
||||||
|
it('should return null if input is empty or undefined', () => {
|
||||||
|
expect(getSubjectAlternativeNames(undefined)).toBeNull();
|
||||||
|
expect(getSubjectAlternativeNames('')).toBeNull();
|
||||||
|
expect(getSubjectAlternativeNames(' ')).toBeNull();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return a structured object with parsed SAN values', () => {
|
||||||
|
const input = 'http://example.com\nuser@example.com\n192.168.0.1';
|
||||||
|
const result = getSubjectAlternativeNames(input);
|
||||||
|
expect(result).toEqual({
|
||||||
|
name: 'subjectAltName',
|
||||||
|
altNames: [
|
||||||
|
{ type: SANType_UniformResourceIdentifier, value: 'http://example.com' },
|
||||||
|
{ type: SANType_Rfc822Name, value: 'user@example.com' },
|
||||||
|
{ type: SANType_IPAddress, ip: '192.168.0.1' },
|
||||||
|
],
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should ignore empty lines and trim whitespace', () => {
|
||||||
|
const input = ' \n http://example.com \n \n user@example.com \n';
|
||||||
|
const result = getSubjectAlternativeNames(input);
|
||||||
|
expect(result?.altNames).toEqual([
|
||||||
|
{ type: SANType_UniformResourceIdentifier, value: 'http://example.com' },
|
||||||
|
{ type: SANType_Rfc822Name, value: 'user@example.com' },
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,81 @@
|
|||||||
|
import { isIP } from 'is-ip';
|
||||||
|
import { pki } from 'node-forge';
|
||||||
|
import workerScript from 'node-forge/dist/prime.worker.min?url';
|
||||||
|
|
||||||
|
export function generateRSAPairs({ bits = 2048 }) {
|
||||||
|
return new Promise<pki.rsa.KeyPair>((resolve, reject) =>
|
||||||
|
pki.rsa.generateKeyPair({ bits, workerScript }, (err, keyPair) => {
|
||||||
|
if (err) {
|
||||||
|
reject(err);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
resolve(keyPair);
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// a hexString is considered negative if it's most significant bit is 1
|
||||||
|
// because serial numbers use ones' complement notation
|
||||||
|
// this RFC in section 4.1.2.2 requires serial numbers to be positive
|
||||||
|
// http://www.ietf.org/rfc/rfc5280.txt
|
||||||
|
export function toPositiveHex(hexString: string) {
|
||||||
|
let mostSiginficativeHexAsInt = Number.parseInt(hexString[0], 16);
|
||||||
|
if (mostSiginficativeHexAsInt < 8) {
|
||||||
|
return hexString;
|
||||||
|
}
|
||||||
|
|
||||||
|
mostSiginficativeHexAsInt -= 8;
|
||||||
|
return mostSiginficativeHexAsInt.toString() + hexString.substring(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
export const SANType_OtherName = 0; // OtherName,
|
||||||
|
export const SANType_Rfc822Name = 1; // email address
|
||||||
|
export const SANType_DNSName = 2; // DNS Name,
|
||||||
|
export const SANType_X400Address = 3; // ORAddress,
|
||||||
|
export const SANType_DirectoryName = 4; // Name,
|
||||||
|
// export const SANType_EdiPartyName = 5;
|
||||||
|
export const SANType_UniformResourceIdentifier = 6; // URI,
|
||||||
|
export const SANType_IPAddress = 7; // IP
|
||||||
|
export const SANType_RegisteredID = 8; // OBJECT IDENTIFIER
|
||||||
|
|
||||||
|
export function getSANType(value: string): number {
|
||||||
|
if (value?.includes('://')) {
|
||||||
|
return SANType_UniformResourceIdentifier;
|
||||||
|
}
|
||||||
|
if (value?.includes('@')) {
|
||||||
|
return SANType_Rfc822Name;
|
||||||
|
}
|
||||||
|
if (isIP(value)) {
|
||||||
|
return SANType_IPAddress;
|
||||||
|
}
|
||||||
|
if (value?.includes('.') && /^[\*\.a-zA-Z\d-]+$/.test(value)) {
|
||||||
|
return SANType_DNSName;
|
||||||
|
}
|
||||||
|
if (value?.includes('DC=')) {
|
||||||
|
return SANType_DirectoryName;
|
||||||
|
}
|
||||||
|
return SANType_OtherName;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getSubjectAlternativeNames(values: string | undefined) {
|
||||||
|
if (!values?.trim()) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
name: 'subjectAltName',
|
||||||
|
altNames: (values || '').split('\n').map(v => v?.trim()).filter(v => v && v !== '').map((v) => {
|
||||||
|
const valueType = getSANType(v.trim());
|
||||||
|
if (valueType === SANType_IPAddress) {
|
||||||
|
return {
|
||||||
|
type: valueType,
|
||||||
|
ip: v.trim(),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
type: valueType,
|
||||||
|
value: v.trim(),
|
||||||
|
};
|
||||||
|
}),
|
||||||
|
};
|
||||||
|
}
|
||||||
Reference in new issue
Block a user