forge icon indicating copy to clipboard operation
forge copied to clipboard

No method to add issuer on pem encoded CSR

Open hazinstore opened this issue 9 years ago • 1 comments

When you create a CSR and encode it to PEM, then decode the PEM encoded CSR, there doesn't appear to be any method of adding an issuer.

In a normal situation a client would generate a CSR then send it to the CA for signing. In our app one user would act as client, but another user would act as CA.

Alice generates a CSR and may not necessarily know the issuer details for Bob at the time. She would send Bob the CSR, and Bob would add his issuer details, sign it and return the cert to Alice.

Maybe I'm missing something here, but there doesn't seem to be a clear path to accomplishing this. My read of the code is that Alice would have to contact Bob ahead of time and acquire his issuer details, all of them, create and sign the CSR herself with Bob's private key???

That can't be right???

Code I'm using... Alice

//Generate a CSR based on conf, see config.json for details
function generateCSR(conf, issuer,privateKey){
  if(!conf){
    conf = config;
  }
  //create a new certificate
  var csr = forge.pki.createCertificationRequest();
  csr.publicKey = pki.setRsaPublicKey(privateKey.n, privateKey.e);;
  csr.setSubject(conf.subject);
 
  csr.setAttributes(conf.attributes);
  var md = forge.md.sha256.create();
  csr.sign(privateKey,md);
  // convert certification request to PEM-format
  let pem = forge.pki.certificationRequestToPem(csr);
  return pem;
}

Bob

//We always deal with a PEM encoded CSR
function generateCertFromCSR(pem,conf){
  if(!conf){
    conf = config;
  }
  console.log("generating cert from csr: "+JSON.stringify(pem));
  // convert a Forge certification request from PEM-format
  let csr = pki.certificationRequestFromPem(pem,true,true);
  //console.log("csr decoded: "+JSON.stringify(csr));
  
  csr.validity.notBefore = new Date();
  csr.validity.notAfter = new Date();
  csr.validity.notAfter.setFullYear(csr.validity.notBefore.getFullYear() + 1);
 
  //Serial number should be the sha256X2 hash of the bytes of the cert info + date
  let md = forge.md.sha256.create();
  csr.serial = md.digest(md.digest(JSON.stringify(csr.cert)+JSON.stringify(new Date()))).toHex();
  
  //This line fails with setIssuer not found
  csr.setIssuer(conf.issuer);
  
  csr.sign(sessionKey.privateKey,md);
  console.log("signed cert: "+JSON.stringify(csr));
  return pki.certificateToPem(csr);
}

hazinstore avatar Dec 11 '16 14:12 hazinstore

Ok so as it turns out, the proper thing to do is copy the CSR field by field into a new cert object and then sign that.

This would be a lot easier if there was a cert from csr option BTW.

hazinstore avatar Dec 12 '16 03:12 hazinstore