PeculiarVentures / x509

@peculiar/x509 is an easy to use TypeScript/Javascript library based on @peculiar/asn1-schema that makes generating X.509 Certificates and Certificate Requests as well as validating certificate chains easy
https://peculiarventures.github.io/x509/
MIT License
86 stars 14 forks source link

Example of how to create a custom x509 extension #38

Open CMCDragonkai opened 2 years ago

CMCDragonkai commented 2 years ago

I'm coming from using node-forge, where it was relatively easy to create custom extensions.

  extensions.push({
    name: 'nodeSignature',
    id: config.oids.extensions.nodeSignature,
    critical: true,
    value: asn1.create(
      asn1.Class.APPLICATION,
      asn1.Type.OCTETSTRING, // or asn1.type.IA5STRING
      false,
      nodeSignature,
    ),
  });

I'm looking for a way to create a custom extension with this library.

I found that most of the extensions classes extend the Extension class.

However they all seem to take values that are already encoded ASN1 objects as buffer sources.

The @peculiar/asn1-schema doesn't have a lot of documentation. The only example is KeyUsage which ends up using @peculiar/asn1-x509 https://github.com/PeculiarVentures/asn1-schema/blob/master/packages/x509/src/extensions/key_usage.ts. But it's not clear if BitString is supposed to be a raw byte string that is already encoded.

Is there an ASN1 builder available to be used here that can do the above?

CMCDragonkai commented 2 years ago

I have found that it's possible to do this:

import * as asn1js from 'asn1js';

const stringPrimitive = new asn1js.IA5String({
  value: 'abc',
  idBlock: {
    tagClass: 0x56,
    tagNumber: 22
  }
});

const stringPrimitiveEncoded = stringPrimitive.toBER();

This ends up creating the IA5String value I'm looking for.

One thing I noticed is that using the @AsnProp decorators, I end up with a sequence all the time.

If I use AsnTypeTypes.Choice I get what I want, but I just want a primitive structure here.

How does one use asn1-schema to set the APPLICATION class?