Important Information for ASN1.js V1 Users
ASN1.js V2 (ES2015 version) is incompatible with ASN1.js V1 code.
Features of the library
- Based on latest features of JavaScipt language from ES2015 standard;
- ASN1js is a "base layer" for full-featured JS library PKIjs, which is using Web Cryptography API and has all classes, neccessary to work with PKI-related data;
- Fully object-oriented library. Inhiritence is using everywhere inside the lib;
- Working with HTML5 data objects (ArrayBuffer, Uint8Array etc.);
- Working with all ASN.1:2008 types;
- Working with BER encoded data;
- All types inside the library constantly stores information about all ASN.1 sub blocks (tag block, length block or value block);
- User may have access to any byte inside any ASN.1 sub-block;
- Any sub-block may have unlimited length, as it described in ASN.1 standard (even "tag block");
- Same with ASN.1 date-time types: for major types like UTCTime and GeneralizedTime there are automatic convertion between "JS date type - ASN.1 date-time type" + vice versa;
- Working with "easy-to-understand" ASN.1 schemas (pre-defined or built by user);
- Has special types to work with ASN.1 schemas:
- User can name any block inside ASN.1 schema and easily get information by name;
- Ability to parse internal data inside a primitively encoded data types and automatically validate it against special schema;
- All types inside library are dynamic;
- All types can be initialized in static or dynamic ways.
- ASN1js fully tested against ASN.1:2008 TestSuite.
//region How to create new ASN. structuresvar sequence = ;sequencevalueBlockvalue;var sequence_buffer = sequence; // Encode current sequence to BER (in ArrayBuffer)var current_size = sequence_bufferbyteLength;var integer_data = 8;var integer_view = integer_data;integer_view0 = 0x01;integer_view1 = 0x01;integer_view2 = 0x01;integer_view3 = 0x01;integer_view4 = 0x01;integer_view5 = 0x01;integer_view6 = 0x01;integer_view7 = 0x01;sequencevalueBlockvalue; // Put too long for decoding Integer valuesequence_buffer = sequence;current_size = sequence_bufferbyteLength;//endregion
//region How to create new ASN.1 structures by calling constuctors with parametersvar sequence2 =value:value: 1isHexOnly: truevalueHex: integer_data;//endregion
//region How to validate ASN.1 against pre-defined schemavar asn1_schema =name: "block1"value:name: "block2"name: "block3"optional: true // This block is absent inside data, but it's "optional". Hence verification against the schema will be passed.;//endregionvar variant1 = orgpkijs; // Verify schema together with decoding of raw datavar variant1_verified = variant1verified;var variant1_result = variant1result; // Verified decoded data with all block names inside
//region How to use "internal schemas" for primitevely encoded data typesvar primitive_octetstring = valueHex: encoded_sequence ; // Create a primitively encoded OctetString where internal data is an encoded Sequencevar asn1_schema_internal =name: "outer_block"primitiveSchema:name: "block1"value:name: "block2";var variant6 = orgpkijs;var variant6_verified = variant4verified;var variant6_block1_tag_num = variant6resultblock1idBlocktagNumber;var variant6_block2_tag_num = variant6resultblock2idBlocktagNumber;//endregion
More examples could be found in "examples" directory or inside PKIjs library.
Related source code
- C++ ASN1:2008 BER coder/decoder - the "father" of ASN1js project;
- Freely available ASN.1:2008 test suite - the suite which can help you to validate (and better understand) any ASN.1 coder/decoder;
- NPM package for ASN.1:2008 test suite
There are several commercial products, enterprise solitions as well as open source project based on versions of ASN1js. You should, however, do your own code and security review before utilization in a production application before utilizing any open source library to ensure it will meet your needs.
Author 2014-2018, Yury Strozhevsky.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.