forge icon indicating copy to clipboard operation
forge copied to clipboard

Discrepancy between CertificateRequest in node-forge and @types/node-forge

Open jim-reespotter opened this issue 2 years ago • 1 comments

I have an angular app generating a CSR it works except for adding extensions/attributes to the CSR (I can successfully ad a subject, public key, get it signed, the resulting cert is valid).

In Angular:

import * as forge from 'node-forge';
@Injectable({
  providedIn: 'root'
})
export class PkiService {

  createCSR(subject: string, displayName: string, key: string): string {
    let csrData = forge.pki.createCertificationRequest();
    csrData.publicKey = this.keyPair.publicKey;
    csrData.setSubject([{
      name: 'commonName', value: displayName
    }]);

// this compiles, but fails to run 
    csrData.setExtensions(
      [{
        name: 'subjectAltName',
        altNames: [{
          type: 0, // othername
          value: subject
        }]
      }]
    );

/*
//    this fails to compile (Property 'setAttributes' does not exist on type 'CertificateRequest')
//    but this is correct as per https://github.com/digitalbazaar/forge#pkcs10 I think
    csrData.setAttributes(
      [{
        name: 'extensionRequest',
        extensions: [{
          name: 'subjectAltName',
          altNames: [{
            type: 0, // othername
            value: subject
          }]
        }]
      }]
    );
*/

    csrData.sign(this.keyPair.privateKey, forge.md.sha256.create());
  ...

ng serve, In Chrome, 'setAttributes' is a valid metod but setExtensions isn't: image

I get the error: image

package.json I have dependenices: "node-forge": "^1.3.2" I have run: npm install @types/node-forge npm install node-forge

No mention of node-forge in app-module.ts

It looks to me like the definition of CertificateRequest differ in:

  • userUI/node_modules/@types/node-forge/index.d.ts (480 defines setExtensions)
  • userUI/node_modules/node-forge/lib/x509.js (1733 defines function returned from createCertificationRequest, contains setAttributes but no setExtensions)

jim-reespotter avatar May 24 '23 15:05 jim-reespotter

Yeah, I ran into the exact same issue. I'm not very good at programming at all, but I got it locally working by addign the following code below the getAttribute() in index.d.ts line 562.

I've opend an issue here: https://github.com/DefinitelyTyped/DefinitelyTyped/discussions/66973

This worked for me:

/**
 * Sets attributes of this certificate request.
 *
 * @param attrs the array of subject attributes to use.
 */
setAttributes(attr: CertificateField[]): Attribute | null;

Ichnafi avatar Oct 08 '23 20:10 Ichnafi