initial upload
This commit is contained in:
parent
987c99d00b
commit
bb6c0147db
44 changed files with 1884 additions and 131 deletions
84
functions/pki.createCA.mjs
Normal file
84
functions/pki.createCA.mjs
Normal file
|
|
@ -0,0 +1,84 @@
|
|||
|
||||
import {
|
||||
default as forge
|
||||
} from "node-forge";
|
||||
|
||||
import {
|
||||
writeFileSync,
|
||||
mkdirSync
|
||||
} from "fs";
|
||||
|
||||
import {
|
||||
randomBytes
|
||||
} from "crypto";
|
||||
|
||||
// Funktion zur Generierung und Speicherung eines Root-CA-Zertifikats
|
||||
export function generateRootCA(Params) {
|
||||
const uid = randomBytes(4).toString("hex");
|
||||
// Generiere ein neues Schlüsselpaar
|
||||
const keys = forge.pki.rsa.generateKeyPair(4096);
|
||||
|
||||
// Erstelle ein neues Zertifikat
|
||||
const cert = forge.pki.createCertificate();
|
||||
cert.publicKey = keys.publicKey;
|
||||
// cert.serialNumber = '01';
|
||||
cert.validity.notBefore = new Date();
|
||||
cert.validity.notAfter = new Date();
|
||||
cert.validity.notAfter.setFullYear(cert.validity.notBefore.getFullYear() + 10);
|
||||
|
||||
const attrs = [{
|
||||
name: 'commonName',
|
||||
value: 'My Root CA'
|
||||
}, {
|
||||
name: 'countryName',
|
||||
value: 'US'
|
||||
}, {
|
||||
shortName: 'ST',
|
||||
value: 'California'
|
||||
}, {
|
||||
name: 'localityName',
|
||||
value: 'San Francisco'
|
||||
}, {
|
||||
name: 'organizationName',
|
||||
value: 'My Organization'
|
||||
}, {
|
||||
shortName: 'OU',
|
||||
value: 'My Organizational Unit'
|
||||
}];
|
||||
|
||||
cert.setSubject(attrs);
|
||||
cert.setIssuer(attrs);
|
||||
|
||||
// Erweiterungen hinzufügen
|
||||
cert.setExtensions([{
|
||||
name: 'basicConstraints',
|
||||
cA: true
|
||||
}, {
|
||||
name: 'keyUsage',
|
||||
keyCertSign: true,
|
||||
cRLSign: true
|
||||
}, {
|
||||
name: 'subjectKeyIdentifier'
|
||||
}]);
|
||||
|
||||
// Zertifikat mit dem privaten Schlüssel signieren
|
||||
cert.sign(keys.privateKey, forge.md.sha256.create());
|
||||
|
||||
// Zertifikat und Schlüssel als PEM kodieren
|
||||
const pemCert = forge.pki.certificateToPem(cert);
|
||||
const pemPrivateKey = forge.pki.privateKeyToPem(keys.privateKey);
|
||||
const pemPublicKey = forge.pki.publicKeyToPem(keys.publicKey);
|
||||
|
||||
// Erstelle das RootCA Verzeichnis
|
||||
mkdirSync(`datastore/certificates/${uid}`)
|
||||
|
||||
// Zertifikat und Schlüssel in Dateien speichern
|
||||
writeFileSync(`datastore/certificates/${uid}/rootCA.crt`, pemCert);
|
||||
writeFileSync(`datastore/certificates/${uid}/rootCA.key`, pemPrivateKey);
|
||||
writeFileSync(`datastore/certificates/${uid}/rootCA.pub`, pemPublicKey);
|
||||
|
||||
console.log('Root CA-Zertifikat und Schlüssel wurden generiert und gespeichert.');
|
||||
}
|
||||
|
||||
// Funktion aufrufen, um das Root-CA-Zertifikat zu generieren
|
||||
// generateRootCA();
|
||||
Loading…
Add table
Add a link
Reference in a new issue