-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Description
This is a meta-issue/design issue for tracking a declarative ASN.1 API for Cryptography!
The goal: an importable Python API that users of Cryptography can define ASN.1 structures with, which can then be ser/de'd to and from DER (and only DER).
A rough sketch, demonstrating the basic idioms of the API:
from cryptography.hazmat import asn1
# Corresponding to:
#
# Signature ::= Sequence {
# r INTEGER,
# s INTEGER
# }
@asn1.sequence
class Signature:
r: int
s: int
sig = Signature.from_der(b"...")
raw: bytes = sig.to_der()This declarative API should have fully generality/expressivity with respect to ASN.1's own feature set, including qualifiers like EXPLICIT and IMPLICIT:
@asn1.sequence
class Signature:
r: Annotated[int, asn1.explicit(0)]
s: Annotated[int, asn1.implicit(1)]This would also (naturally) include generality over user-defined types:
@asn1.sequence
class Frob:
...
@asn1.sequence
class FrobHolder:
frob: FrobKey design constraints:
- This API should be 100% declarative: there should be no imperative effects on ASN.1 ser/de
Open design questions:
-
What's the best way to handle
ANY?asn1.Anyas a generic TLV type, similar to rust-asn1? -
What's the best way to handle
ANY DEFINED BY?Maybe something like this:
@asn1.sequence class VaryMe: content_type: asn1.ObjectIdentifier content: Annotated[Varied, asn1.defined_by("content_type")] # or maybe we can do this with the standard enum.Enum? # or maybe @asn1.varied? class Varied(asn1.Enum): foo: Annotated[VariantA, asn1.defined_by(SOME_OID)] bar: Annotated[VariantB, asn1.defined_by(ANOTHER_OID)]
-
To what extent/how can we best support trivial "native" Python types (
int,str, etc.) versus "synthetic" types?- Moreover, what's the appropriate isomorph for
str? ProbablyUTF8String, with all other string-ish types beingbytes?
- Moreover, what's the appropriate isomorph for
-
What about non-trivial native types like
list[T],set[T], etc? Should we support these with fixed mappings (e.g.list[T] -> SEQUENCE OF), or should we have our own types that don't require as much object conversion (e.g.asn.List[T])? -
To what extent should we support
datetimeas a time type/map betweendatetimeandUTCTime/GeneralizedTime?- One pitfall that we want to avoid is surprising serializations, e.g. a user really wants
UTCTime OR GeneralizedTimebut instead gets onlyGeneralizedTime
- One pitfall that we want to avoid is surprising serializations, e.g. a user really wants
-
What's the best way to handle ASN.1 type constraints, e.g. ranged integers and min/max sequence/set lengths?
- Probably additional fields on
Annotated, e.g.Annotated[list[T], asn1.size(1...10)] - Not all of these make sense for an MVP, since plenty are obscure/not widely used (e.g. contained subtypes)
- Probably additional fields on
Open integration questions:
- Where should this live within
cryptography? Doescryptography.hazmat.asn1make sense, or should it becryptography.asn1, or something else?
There are probably many other questions too, and I'm sure I've missed some in my notes π
CC @facutuesca