Skip to content

Tutorial for ASN1 DER Primitive Encoder

Kenji Urushima edited this page Nov 6, 2016 · 5 revisions

TOP | Wiki | DOWNLOADS | TUTORIALS | API REFERENCE | Online Tool | DEMO | NODE TOOL


ITU-T X.690 ASN.1 DER format is used for network protocol, digital certificate or signature format. The KJUR.asn1.DER* classes are used for hexadecimal encode of such ASN.1 DER objects.

KJUR.asn1 ASN.1 DER classes are very similar to ASN.1 classes of BouncyCastle JCE Library. If you know it you can learn KJUR.asn1 classes easily.

Getting Started

When you want to generate this data structure:

SEQUENCE {
    INTEGER 3
    UTF8String 'apple'
    OBJECT IDENTIFIER serialNumber (2 5 4 5) }

you can get its hexadecimal encoded data by following code:

var seq =
    new KJUR.asn1.DERSequence({'array': 
                               [new KJUR.asn1.DERInteger({'int':3}),
                                new KJUR.asn1.DERUTF8String({'str':'apple'}),
                                new KJUR.asn1.DERObjectIdentifier({'oid':'2.5.4.5'})]});
var hDATA = seq.getEncodedHex(); // result will be 300f0201030c056170706c650603550405

ASN.1 primitive classes constructor may have a argument as initial parameters such like {'int': 3}. Superclass of the ASN.1 primitive class is KJUR.asn1.ASN1Object and it has getEncodedHex() method. It returns a hexadecimal encoded string of the ASN1Object.

ASN.1 TLV structure

ASN.1 has tag(T), length(L) and value(V) structure. For example, above hexadecimal data has following TLV structure.

[30][0f] - SEQUENCE len=0f
    [02][01][03]         - INTEGER len=1 Val=3
    [0c][05][6170706c65] - UTF8STRING len=5 Val=apple
    [06][03][550405]     - OID len=3 Val=2.5.4.5

Supported ASN.1 DER Primitive Classes

Here is the list of tag value and ASN.1 DER primitive class API documents. See API documents for what parameters can specified for each class.

Context Specific Tagged Object

When you find tag such like '[0]' or '[3]' it is called as 'Context Specific Tag'. A ASN.1 tag will differ if it is implicit or explicit and structured or simple. Here is examples.

IMPLICIT [4] INTEGER 3               -> 860103 (value part is 03 and integer type can be found implicitly) 
  new DERTaggedObject({'explicit':false,'tag':'84','obj':new DERInteger({'int':3})});
EXPILCIT [4] INTEGER 3               -> 8603020103 (value part is INTEGER 3 explicitly->020103)
  new DERTaggedObject({'explicit':true,'tag':'84','obj':new DERInteger({'int':3})});
IMPLICIT [4] SEQUENCE { INTEGER 3 }  -> a403020103
  var seq = new DERSequence({'array':[new DERInteger({'int':3})]});
  new DERTaggedObject({'explicit':false,'tag':'a4','obj':seq});
EXPLICIT [4] SEQUENCE { INTEGER 3 }  -> a4053003020103
  var seq = new DERSequence({'array':[new DERInteger({'int':3})]});
  new DERTaggedObject({'explicit':true,'tag':'a4','obj':seq});

Tag will be 'a?' if value is structured. And tag will be '8?' if value is not structured type.


Clone this wiki locally