Skip to content

Commit

Permalink
server: recreate cert with valid date range on startup
Browse files Browse the repository at this point in the history
  • Loading branch information
koush committed Dec 10, 2023
1 parent c9568df commit 1f22218
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
22 changes: 15 additions & 7 deletions server/src/cert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,20 @@ const { pki } = forge;

export const CURRENT_SELF_SIGNED_CERTIFICATE_VERSION = 'v2';

export function createSelfSignedCertificate() {

// generate a keypair and create an X.509v3 certificate
const keys = pki.rsa.generateKeyPair(2048);
export function createSelfSignedCertificate(serviceKey?: string) {
let privateKey: ReturnType<typeof pki.privateKeyFromPem>;
const cert = pki.createCertificate();
cert.publicKey = keys.publicKey;

if (serviceKey) {
privateKey = pki.privateKeyFromPem(serviceKey);
cert.publicKey = pki.rsa.setPublicKey(privateKey.n, privateKey.e);
}
else {
// generate a keypair and create an X.509v3 certificate
const keys = pki.rsa.generateKeyPair(2048);
privateKey = keys.privateKey;
cert.publicKey = keys.publicKey;
}

// NOTE: serialNumber is the hex encoded value of an ASN.1 INTEGER.
// Conforming CAs should ensure serialNumber is:
Expand Down Expand Up @@ -65,9 +73,9 @@ export function createSelfSignedCertificate() {
}]);

// self-sign certificate
cert.sign(keys.privateKey);
cert.sign(privateKey);
return {
serviceKey: pki.privateKeyToPem(keys.privateKey),
serviceKey: pki.privateKeyToPem(privateKey),
certificate: pki.certificateToPem(cert),
version: CURRENT_SELF_SIGNED_CERTIFICATE_VERSION,
};
Expand Down
14 changes: 8 additions & 6 deletions server/src/scrypted-server-main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,15 +124,19 @@ async function start(mainFilename: string, options?: {
await db.open();

let certSetting = await db.tryGet(Settings, 'certificate') as Settings;
let keyPair: ReturnType<typeof createSelfSignedCertificate> = certSetting?.value;

if (certSetting?.value?.version !== CURRENT_SELF_SIGNED_CERTIFICATE_VERSION) {
const cert = createSelfSignedCertificate();
keyPair = createSelfSignedCertificate();

certSetting = new Settings();
certSetting._id = 'certificate';
certSetting.value = cert;
certSetting.value = keyPair;
certSetting = await db.upsert(certSetting);
}
else {
keyPair = createSelfSignedCertificate(keyPair.serviceKey);
}

const basicAuth = httpAuth.basic({
realm: 'Scrypted',
Expand All @@ -147,15 +151,13 @@ async function start(mainFilename: string, options?: {
callback(sha === user.passwordHash || password === user.token);
});

const keys = certSetting.value;

const httpsServerOptions = process.env.SCRYPTED_HTTPS_OPTIONS_FILE
? JSON.parse(fs.readFileSync(process.env.SCRYPTED_HTTPS_OPTIONS_FILE).toString())
: {};

const mergedHttpsServerOptions = Object.assign({
key: keys.serviceKey,
cert: keys.certificate
key: keyPair.serviceKey,
cert: keyPair.certificate
}, httpsServerOptions);
const secure = https.createServer(mergedHttpsServerOptions, app);
const insecure = http.createServer(app);
Expand Down

0 comments on commit 1f22218

Please sign in to comment.