Currently early stage proof-of-concept.
Requires modified version of asn1c, available here: https://github.com/danielcode/asn1c
Works by wrapping C produced by asn1c.
Creates a separate class for each ASN.1 type.
Supports subset of primitive types and these structured types:
- SEQUENCE
- SEQUENCE OF
- CHOICE
ASN.1 Type | Status | Ruby Class |
---|---|---|
ANY | Not Implemented | |
BIT_STRING | Implemented | Asn1::Type::BitString |
BMPString | Implemented | Asn1::Type::BMPString |
BOOLEAN | Implemented | Asn1::Type::Boolean |
ENUMERATED | Implemented | N/A |
GeneralString | Unsupported | N/A |
GeneralizedTime | Implemented | Asn1::Type::GeneralizedTime |
GraphicString | Unsupported | N/A |
IA5String | Implemented | Asn1::Type::IA5String |
INTEGER | Implemented | Asn1::Type::Integer |
ISO646String | Implemented | Asn1::Type::ISO646String |
NULL | Implemented | Asn1::Type::Null |
NumericString | Implemented | Asn1::Type::NumericString |
OBJECT_IDENTIFIER | Not Implemented | |
OCTET_STRING | Implemented | Asn1::Type::OctetString |
ObjectDescriptor | Unsupported | |
PrintableString | Implemented | Asn1::Type::PrintableString |
REAL | Not Implemented | Asn1::Type::Real |
RELATIVE-OID | Not Implemented | |
T61String | Unsupported | N/A |
TeletexString | Implemented | Asn1::Type::TeletexString |
UTCTime | Not Implemented | |
UTF8String | Implemented | Asn1::Type::UTF8String |
UniversalString | Implemented | Asn1::Type::UniversalString |
VideotexString | Unsupported | |
VisibleString | Implemented | Asn1::Type::VisibleString |
CHOICE | Implemented | N/A |
SEQUENCE | Implemented | N/A |
SEQUENCE_OF | Implemented | N/A |
SET | Not Implemented | N/A |
SET_OF | Not Implemented | N/A |
- clone and install asn1c:
- git clone https://github.com/danielcode/asn1c.git
- cd asn1c
- ./configure --prefix=
- make; make check; make install
- clone asn1:
- git clone https://github.com/danielcode/asn1.git
- cd asn1/ext/asn1
- asn1c -fall-defs-global -fwide-types Test.asn1
- rm Makefile.am.sample
- rm converter-sample.c
- cd ../..
- rake compile
- rake install_gem
$ irb
> require 'asn1/asn1'
=> true
> encoded = Asn1::Integer.encode :der, 1234567890
=> "\x02\x04I\x96\x02\xD2"
> Asn1::Integer.decode :der, encoded
=> 1234567890
> encoded = Asn1::IA5String.encode :der, 'Hello, world!'
=> "\x16\rHello, world!"
> Asn1::IA5String.decode :der, encoded
=> "Hello, world!"
> x = Asn1::Type::SimpleSequence.new
=> #<Asn1::Type::SimpleSequence:0x00000801ca1b20>
> x.simpleString = "Hello, world!"
=> "Hello, world!"
> x.simpleInt = 1
=> 1
> y = Asn1::Schema::SimpleSequence.encode :der, x
=> "0\x12\x02\x01\x01\x16\rHello, world!"
> Asn1::Schema::SimpleSequence.decode :der, y
=> #<Asn1::Type::SimpleSequence:0x00000801c6b728 @simpleString="Hello, world!", @simpleInt=1>
> require 'base64'
=> true
> puts Base64.encode64(y)
MBICAQEWDUhlbGxvLCB3b3JsZCE=
Now, try decoding the base64 encoded string here: http://www.lapo.it/asn1js/
Types are held under Asn1::Type. They include all primitive types and types defined by the user.
Primitive types currently include:
- Asn1::Type::Boolean
- Asn1::Type::IA5String
- Asn1::Type::Integer
- Asn1::Type::Null
- Asn1::Type::Real
- Asn1::Type::UTF8String
All primitive types have two class methods:
- encode(format, object)
- decode(format, object) where format is one of :xer, :der or :per. However, :per does not currently work.
Each type alias in an ASN.1 module maps to a type under Asn1::Type.
E.g. MyInt ::= INTEGER => Asn1::Type::MyInt
Complex types have an associated schema, defined under Asn1::Schema