node-http-server – Rev 10

Subversion Repositories:
Rev:
#!/usr/bin/env node

/*************************************************************************/
/*    Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3    */
/*************************************************************************/

const forge = require('node-forge');
const moment = require('moment');

module.exports = {
    // Generate certificates on the fly using incremental serials.
    generate: (name, domain, keySize, callback) => {
        process.nextTick(() => {
            // Generate 1024-bit key-pair.
            const keys = forge
                .pki
                .rsa
                .generateKeyPair(keySize);
            // Create self-signed certificate.
            const cert = forge
                .pki
                .createCertificate();
            cert.serialNumber = moment().format('x');
            cert.publicKey = keys.publicKey;
            cert
                .validity
                .notBefore = moment().toDate();
            cert
                .validity
                .notAfter
                .setFullYear(
                    cert
                    .validity
                    .notBefore
                    .getFullYear() + 1
                );
            cert.setSubject([{
                name: 'commonName',
                value: domain
            }, {
                name: 'organizationName',
                value: name
            }]);
            cert.setIssuer([{
                name: 'commonName',
                value: domain
            }, {
                name: 'organizationName',
                value: name
            }]);

            // Self-sign certificate.
            cert.sign(
                keys.privateKey,
                forge
                .md
                .sha256
                .create()
            );

            // Summon the callback with the certificate block.
            callback({
                privateKey: forge
                    .pki
                    .privateKeyToPem(
                        keys
                        .privateKey
                    ),
                publicKey: forge
                    .pki
                    .publicKeyToPem(
                        keys
                        .publicKey
                    ),
                certificate: forge
                    .pki
                    .certificateToPem(cert)
            });
        });
    }
};

Generated by GNU Enscript 1.6.5.90.