typescriptpkiasn.1certificate-revocation

Parsing a certificate revocation list in nodejs


I need to parse a crl (pem formatted) to check which certificates are revoked. I used to do this using this example, which worked fine until I switched to typescript. Now, I have

import { fromBER } from 'asn1js';
import { CertificateRevocationList } from 'pkijs';
import fs from'fs';


fs.readFile('./pathToMyCrlFile.crl', (err, crlData) => {
  if (err) {
    throw err;
  }
  const buffer = new Uint8Array(crlData).buffer;
  const asn1crl = fromBER(buffer);
  const crl = new CertificateRevocationList({
    schema: asn1crl.result
  })

  for (const { userCertificate } of crl.revokedCertificates) {
    // do something
  }
})

and I get a

Cannot read properties of undefined (reading 'slice'). Stacktrace: TypeError: Cannot read properties of
undefined (reading 'slice') at RelativeDistinguishedNames.fromSchema 
(<project_path>/node_modules/pkijs/build/index.js:408:72)

I've checked the PEM and it's valid. I'm not sure what to do and I'd appreciate any nudge in the right direction. Many thanks in advance!


Solution

  • So, apparently I can use the static .fromBER method that is part of the CertificateRevocationList. So, to update the code from above, it should be

    import { CertificateRevocationList } from 'pkijs';
    import fs from'fs';
    
    
    fs.readFile('./pathToMyCrlFile.crl', (err, crlData) => {
      if (err) {
        throw err;
      }
      const bytes = new Uint8Array(crlData);
      const crlObject = CertificateRevocationList.fromBER(bytes.buffer);
    
      for (const { userCertificate } of crlObject.revokedCertificates) {
        // do something
      }
    })