title: node-forge self signed certificate for https module
body: <p>OS: Windows 10<br>
  Node.js version: node-v8.11.4-win-x64<br>
  node-forge version: 0.7.7-dev<br></p>
  
  <p>People,
  i'm having a hard time trying to create an node.js https server.<br>
  The idea behind the server is that it's only for local loopback requests.<br>
  With it in mind and for the sake of privacy i thought of disposable self signed certificates renewed at each server restart.<br>
  Following suggestions i'm trying to use the node-forge module to generate the disposable certificates.<br></p>
  
  <p>Reading their instructions at <a href="https://www.npmjs.com/package/node-forge" rel="noreferrer">https://www.npmjs.com/package/node-forge</a> i came with a very simple code:</p>
  
  <pre><code>var https = require('https');
  var forge = require('node-forge');
      forge.options.usePureJavaScript = true; 
  
  var pki = forge.pki;
  var keys = pki.rsa.generateKeyPair(2048);
  var cert = 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()+1);
  
  var attrs = [
       {name:'commonName',value:'example.org'}
      ,{name:'countryName',value:'US'}
      ,{shortName:'ST',value:'Virginia'}
      ,{name:'localityName',value:'Blacksburg'}
      ,{name:'organizationName',value:'Test'}
      ,{shortName:'OU',value:'Test'}
  ];
  cert.setSubject(attrs);
  cert.setIssuer(attrs);
  cert.sign(keys.privateKey);
  
  var pem_pkey = pki.publicKeyToPem(keys.publicKey);
  var pem_cert = pki.certificateToPem(cert);
  
  console.log(pem_pkey);
  console.log(pem_cert);
  
  https.createServer( { key:pem_pkey, cert:pem_cert },(req,res)=&gt;
  {
      res.writeHead(200, {'Content-Type': 'text/plain'});
      res.end('Hello World\n');
  }).listen(443);
  </code></pre>
  
  <p><br>then it emits an error i make absolutely no idea what's about:</p>
  
  <pre><code>E:\forge_case\node-v8.11.4-win-x64&gt;node.exe index.js
  -----BEGIN PUBLIC KEY-----
  MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAkTmxd1fWK+XL2Cp5+n82
  mS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DWZatn7RvLXK2EveWd3AGpFZrMV/PwTGRmSCxv9
  9H3Szy/G2ahlfgV889gCdHoCi4r7pz7UaciPiHdL9Y2LK2f/S2MCAwEAATANBgkq
  hkiG9w0BAQUFAAOCAQEAgTGiTF6BDLX3w1PfJxXYYzVBoN8NlL979mcfRVhvLH/N
  lEaaQvGhDSrp97s9K0kEw3A16WzUwnysoX1uOdlHHp78v2tXaffDzXTnH75CN4pd
  qNHuy05AFIoiBujZR67OR4ipcKZ9USYz0QZysMFyzCxum2btLPvrs+onIxdvsuA+
  Xe9vFxj+AJ4hv03tDuQXj+pZ2dflWyy5ZdaMDeUjWtSK9MHPn6rs5Fs340B4NnxL
  3Y6RKDj4CerGbtufR73UNirNfmbqiAybZ6vG1RaL2oBiHdoJfpB/FLm7QFn34apq
  A1Kd1VPShBC4G/4S+0E6U84D4xNJcjT6NfzDGBD6Ng==
  -----END CERTIFICATE-----
  
  _tls_common.js:104
        c.context.setKey(options.key, options.passphrase);
                  ^
  
  Error: error:0906D06C:PEM routines:PEM_read_bio:no start line
      at Object.createSecureContext (_tls_common.js:104:17)
      at Server (_tls_wrap.js:805:25)
      at new Server (https.js:54:14)
      at Object.createServer (https.js:76:10)
      at Object.&lt;anonymous&gt; (E:\forge_case\node-v8.11.4-win-x64\index.js:33:7)
      at Module._compile (module.js:652:30)
      at Object.Module._extensions..js (module.js:663:10)
      at Module.load (module.js:565:32)
      at tryModuleLoad (module.js:505:12)
      at Function.Module._load (module.js:497:3)
  </code></pre>
  
  <p>Any ideas? thanks!</p>