2

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!

Alb
  • 1,063
  • 9
  • 33
  • This seems like a runtime error and not a TypeScript issue. Can you share a little more code of how this is being invoked? I don't see any reference to `slice` in the linked answer, so it's not clear what the context is. – akivajgordon Jan 10 '23 at 15:19
  • @akivajgordon the slice is happening [here](https://github.com/PeculiarVentures/PKI.js/blob/e71b99c0b41c705c0ed27502161272c208a1ee33/src/RelativeDistinguishedNames.ts#L150), inside the `pkijs` module when I'm creating a new instance of `CertificateRevocationList`. – Alb Jan 10 '23 at 15:48

1 Answers1

0

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
  }
})
Alb
  • 1,063
  • 9
  • 33