digitalbazaar / forge

A native implementation of TLS in Javascript and tools to write crypto-based and network-heavy webapps

Home Page:https://digitalbazaar.com/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

jim-reespotter opened this issue · 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)

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: DefinitelyTyped/DefinitelyTyped#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;