javascriptnode.jspkinode-forge

nodeforge cert.setSubject : Attribute type is not specified


New to typescript, and I am working with nodeforge to generate CA and I do not know how to resolve this error.

PS: Vscode did not show red underline, but nextjs throws the error.

    const cert = pki.createCertificate();
    const attrs = [
      { name: 'commonName', value: 'My First CA' },
      { name: 'organizationUnitName', value: 'Organization Unit' },
      { name: 'organizationName', value: 'My Organization' },
    ] //this is returned by buildSubjectFromOptions

    cert.setSubject(attrs as pki.CertificateField[]);
    cert.setIssuer(attrs as pki.CertificateField[]);

At the last 2 lines, I am getting the error Attribute type is not specified, even though I followed vs code instructions as below

enter image description here

enter image description here


Solution

  • I had this similar issue as well. I looked at the X509.js and oids.js library files of node-forge.

    oids.js:

    _IN('2.5.4.10', 'organizationName')
    _IN('2.5.4.10', 'organizationName')
    

    X509.js:

    _shortNames['OU'] = oids['organizationalUnitName'];
    _shortNames['organizationalUnitName'] = 'OU';
    

    It seems that your attribute "organizationUnitName" is actually stored in pki.oids/x509.js as "organizationalUnitName". If you fix your typo from "organizationUnitName" to "organizationalUnitName" should hopefully resolve the issue.

    Hope this helps