-
-
Notifications
You must be signed in to change notification settings - Fork 645
Tutorial for ASN1 DER Primitive Encoder
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.
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 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
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.
- 0x01 KJUR.asn1.DERBoolean
- 0x02 KJUR.asn1.DERInteger
- 0x03 KJUR.asn1.DERBitString
- 0x04 KJUR.asn1.DEROctetString
- 0x05 KJUR.asn1.DERNull
- 0x06 KJUR.asn1.DERObjectIdentifier
- 0x0c KJUR.asn1.DERUTF8String
- 0x12 KJUR.asn1.DERNumericString
- 0x13 KJUR.asn1.DERPrintableString
- 0x14 KJUR.asn1.DERTeletexString
- 0x16 KJUR.asn1.DERIA5String
- 0x17 KJUR.asn1.DERUTCTime
- 0x18 KJUR.asn1.DERGeneralizedString
- 0x30 KJUR.asn1.DERSequence
- 0x31 KJUR.asn1.DERSet
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.