Skip to content

asn1: Allow Enum Like Object identifiers #14891

@loewexy

Description

@loewexy

While working with the new asn1 implementation, i missed one specific feature that would make working with OIDs much simpler.

Currently there is just x509.ObjectIdentifier, which means i can build a sequence like the following:

@asn1.sequence
class Test:
  a: x509.ObjectIdentifier

However this has the disadvantage that for every object creation i have to build an object identifier from a string like so.

Test(a=x509.ObjectIdentifier('1.2.3.4'))

It is possible to create Constants for that or put them as Class Variables on a class however it would be much nicer from a type safety approach if we could have something like the following.

class Algorithm(OIDEnum):
  SHA_256 = '1.2.3.4'
  SHA_384 = '1.2.3.5'
  SHA_512 = '1.2.3.6'

@asn1.sequence
class Test:
  a: Algorithm

obj = Test(a=Algorithm.SHA_256)

obj.a is Algorithm.SHA_256

The Algorithm class should behave like a Enum with singleton members and stuff. This would make it explicitly clear that the field a has to be an OID that represents exactly one of the given options. Also the type checker could catch errors where invalid OIDs are provided. Also when we have parsed an object we can be sure that in the field a must be exactly one of the declared OIDs. So parsing should fail if an invalid OID is provided.

I really are not sure if making a separate OIDEnum is required or we could just use Enum and declare the values as x509.ObjectIdentifier, or automatically convert the string values of needed. Or a decorator instead of subclassing from OIDEnum would be better. But some sort of type safe integrated mapping of names to OIDs would be very helpful.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions