the new Type-based ASN1 Stream implementation

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

the new Type-based ASN1 Stream implementation

Rob Withers
With inspiration from VW, I've implemented a new ASN1 Stream and  
associated Type hierarchy.  I briefly explain the new system below.  
This has been published alongside the existing ASN1Value  
implementation, in the Cryptography repository, so that users of  
ASN1Value won't currently be impacted.  However, I would like to  
deprecate the old implementation this week, so this is a call to port  
uses of ASN1Value to ASN1DERStream.  Please contact me if you are a  
user and I will help you port your code.  I will be porting the X509  
Certificate implementation over the next few days, so users of this  
framework should also contact me, as the public api will change.

The new ASN1 framework has a Type hierarchy that supports both basic  
types and user-defined types.

The support for basic types is sufficient to replace the current  
ASN1Value impl.  Below are the replacements.

        ASN1Value fromAsnDer: stream.
becomes
        (ASN1DERStream onStream: stream) decode.

and

        anASN1Value encodeAsnDer
becomes
        ASN1DERStream new encode: anASN1Value.

The user-defined types are stored in an instance of an ASN1Module,  
for each module defined in the image.  For instance, the X509 Module  
is registered as #x509.  To access the forthcoming Certificate type  
definition in the X509 Module (I have to port X509 first), you would  
write:

        type := (ASN1Module name: #x509) find: #Certificate.

In defining a type, you can specify a base type with an new name, or  
you can specify sequences and other constructs.  With sequences, you  
can add a mapping class, so the elements of the sequence will be set  
to various ivars.  You can add elements with optional, default or  
explicit and implicit tagging.  Here is a sample of a user-defined  
type, a sequence, mapped to an ASN1TestModel, and a Boolean element  
mapped to the #testSlot1 ivar:

        ((ASN1Module name: #test) sequence: #TestSequence)
                add: #testSlot1 type: #ASN1BooleanType;
                mapping: ASN1TestModel;
                yourself.

Now you can encode and decode using the defined Type, and this will  
marshal all values into ivars defined on various mapping classes.    
Here is the corresponding code:

        type := (ASN1Module name: #test) find: #TestSequence.
        obj := (ASN1DERStream onBytes: bytes) decodeWithType: type.

and

        type := (ASN1Module name: #test) find: #TestSequence.
        bytes := ASN1DERStream new encode: obj withType: type.

Additional examples, especially of the variety of basic types and  
user-defined type specifications, can be found in the CryptoASN1Test,  
once you have loaded the latest Cryptography-ASN1 and Cryptography-
Tests.

enjoy!