p-hatan/ mirror

pub repository

mirror.p-hatan.ir

asn1lib

1.6.1

An ASN1 parser library for Dart. Encodes / decodes from ASN1 Objects to BER bytes

Download archive

Readme

ASN.1 Parser for Dart

Encodes & decodes ASN1 using BER encoding.

Samples

Encoding

import 'package:asn1lib/asn1lib.dart';

var s = ASN1Sequence();
s.add(ASN1Integer(23));
s.add(ASN1OctetString('This is a test'));
s.add(ASN1Boolean(true));

// GET the BER Stream
var bytes = s
.
encodedBytes;

Decoding

import 'package:asn1lib/asn1lib.dart';

// e.g. bytes from the Encoding Example
var p = ASN1Parser(bytes);
var s2 = p.nextObject();
// s2 is a sequence...


// relaxed parsing, returning a generic ASN1Object from (yet) unsupported structures, instead of throwing
var p2 = ASN1Parser(bytes, relaxedParsing: true);
var s3 = p2.nextObject();
// s3 is a sequence...

ASN1 Object classes

The library has been most thoroughly tested against ASN1 primitive class types, such as strings, integers, sequences and sets.

There is minimal support for ASN1 Application, Context-specific and Private classes. If the parser encounters these tags, it will wrap them in an a corresponding object ( ASN1Application, ASN1ContextSpecific, ASN1Private). You can force decode these to another type, by doing something like this:

    var s = parser.nextObject();
    // We expect this is a context-specific class
    expect(s, isA<ASN1ContextSpecific>());
    // which is also an ASN1Object...
    expect(s, isA<ASN1Object>());
    // We expect this is a sequence, so "recast" to a sequence type
    var sequence = ASN1Sequence.fromBytes(s.encodedBytes);
    expect(sequence, isA<ASN1Sequence>());

Breaking Changes

As of 1.6, the default encoding of ASN1OctetString("some string") has changed. It now encodes octets using utf8 encoding. This is different than Dart's default utf16 encoding.

If you need to encode to utf16, you should convert to bytes first:

var x = ASN1OctetString(Uint8List.fromList('some string'.codeUnits));
// new getter
var y = x.utf16StringValue;

The getter ASN1OctetString.stringValue is deprecated. You should use ASN1OctetString.utf8StringValue or ASN1OctetString.utf16StringValue instead.

Issues

ASN1 parsing is complex, so bugs are expected. If you find a bug, please provide a test case that demonstrates the issue, along with the expected output. Bonus points if it is formatted as a dart test.

References

Changelog

Changelog

1.6.1

  • ASN1OctetString.toString() now returns the decoded bytes as a UTF-8 string. This is mostly for convenience / ergnomics. If you want the bytes as a list, use the valueBytes getter.

1.6.0

  • Breaking Change for encoding`ASN1OctetString("some string"). The default encoding to octets now uses utf8. See README.md for more information.

1.5.9

  • Added ASN1OctetString.utf8StringValue getter to return the decoded bytes as a UTF-8 string.

1.5.8

  • Removed the dependency on the collection package (only used in tests)

1.5.7

  • Fixes 73. You should be able to add more objects to a sequence and get encodedBytes between each add.

1.5.6

  • Equals method needs to use deep list equality!

1.5.5

  • Equals method was comparing unencoded object.

1.5.4

  • Add equals and hashCode to ASN1Object

1.5.3

  • Fixes #68

1.5.2

  • Support the ability to parse objects with extended tags (more than one byte). #65
  • Simplify by converting ASN1Length to Dart records

1.5.0

  • Make a number of fields final that should not be changed.
  • Add types to a few members.
  • Remove nullability from a field fields.
  • Require Dart 3.0

1.4.1

  • Fix null assertion when accessing valueBytes before encodedBytes.

1.4.0

  • Reverted parsing of APPLICATION tags to the older behavior. If the constructed bit is set, the tag is parsed as an ASN1Sequence. The recent updates broke LDAP ASN1 parsing.

1.3.0

  • Adds classes to handle non primitive classes: ASN1Application,ASN1ContextSpecific, ASN1Private.
  • This is a potential change in behavior for consumers that expect some of these classes to be parsed as primitive or sequence types. See [README.md]
  • The encodeApplicationTagsAsObject option on ASN1Parser has been dropped as it is now the default. Consumers should reparse bytes they know to be a sequence, etc.

1.2.2

  • Fix #62. Allow context specific tags to be cast to an asn1 sequence.

1.2.1

  • Fix #57

1.2.0

  • Fix #61. Adds a flag to the parser encodeApplicationTagsAsObject that will force APPLICATION objects to be encoded as ASN1Objects, skipping any attempt to create sequences.

1.1.1

  • fix: Use the correct value for unusedBits in ASN1BitString.fromBytes

1.1.0

  • Add optional flag whether to relax parsing, off by default, for backwards-compatibility

1.0.3

  • Add ASN1BMPString support

1.0.2

  • Better toString() for ASN1ObjectIdentifier

1.0.1

  • Add ASN1NumericString support

1.0.0

  • Bumped to 1.0.0 as this is now null safe
  • Code cleanup

0.9.0

  • Requires Dart 2.12
  • Enable null-safety

0.8.1

  • Allow OID octets to be 0
  • Fix ASN1Integer.toString() typo

0.8.0

  • Add ASN1IpAddress object based on ASN1OctetString
  • Add tests for ASN1IpAddress

0.7.1

  • Allow parsing of unrecognized application tags into generic ASN1Objects
  • Fix parser test filename typo
  • Add new tag tests

0.7.0

  • Improve object identifier tests
  • Fix ASN1ObjectIdentifier static methods fromBytes() and fromComponents()

0.6.5

  • Add new object identifiers

0.6.4

  • Fix ASN1Object.preEncoded bytes range
  • Add unit tests

0.6.3

  • Add ASN1TeletextString Parser
  • Add unit tests

0.6.2

  • Small fix for the ASN1GeneralizedTime parser

0.6.1

  • Add sha1WithRSAEncryption object identifier
  • Add ASN1GeneralizedTime Parser
  • Add unit tests

0.6.0

  • Fix all linter warnings

0.5.15

  • Add businessCategory object identifier

0.5.14

  • Add DN for subjectAltName

0.5.13

  • Merged #37. Improved length computing in the ASN1Parser.
  • Added new unit tests.

0.5.12

  • Merged #36. Improved length computing in the ASN1Parser.

0.5.11

  • Merged #34. Added example

0.5.10

  • Merged #32. Update length in asn1 parser.

0.5.9

  • Merged #31. Improve fromBytes. Logic from Java Bouncy Castle.

0.5.8

Merged #30

  • Add IA5String
  • Add associated tests
  • Make sure parser handles IA5 and UT8 String
  • Add large test case with PEM Certificate that includes IA5 and UTF8 Strings
  • Cleanup README

0.5.7

Merged #29

  • Expand AS1NNull to accept tag
  • Associated test cases
  • Added David Janes as author

0.5.6

Merged #28

Add ASN1UTF8String

0.5.5

Merged #27

Updates to ASN1ObjectIdentifier to:

  • allow creation from dotted number lists
  • allow creation from dotted number strings
  • allow names to be registered, for shorthands
  • allow bulk name registration
  • commonly used names (obviously, what this is could be expanded)

0.5.4

Fixed #26

0.5.1

Add AS1Integer.fromInt factory method

0.5.0

  • Convert use of BigInteger to dart SDK BigInt

0.4.3

  • Updates for dart 2

0.4.2

  • Added contentBytes() getter
  • Removed Int64List dependency and use bignum's BigInteger instead

Installing

export PUB_HOSTED_URL=https://pub.mirror.p-hatan.ir
flutter pub add asn1lib
pubspec.yaml
name: asn1lib
version: 1.6.1
description: An ASN1 parser library for Dart. Encodes / decodes from ASN1 Objects to BER bytes
homepage: https://github.com/wstrange/asn1lib
environment:
  sdk: ^3.7.0

# dependencies:
# No dependencies!

dev_dependencies:
  collection: ^1.18.0
  dart_flutter_team_lints: ^3.0.0
  test: ^1.25.2

# PEM certs are for testing - and are OK to publish
false_secrets:
  - /test/resource/*.pem
  - test/*.dart

Version history

VersionPublishedArchiveSHA-256
0.1.02013-10-20T20:19:26.926400ZDownloadf8ff759cd1e612d02b34275e0eaefd5f92700c33b0939a125bd4dcc8fb4c23ce
0.1.12013-11-09T21:41:53.834490ZDownload4c3b94b156b8813d3ecf2af8cc58ab7dc6e008cbb93e706be0fd4d6b39fd53fe
0.2.02014-03-14T15:30:07.593110ZDownloadf41fea6430e0bfb69a19f91eeb6a3d98314238e01333ba7ae0a92abb0ff661d4
0.3.02014-03-28T04:25:31.355600ZDownload6a59955b0cd131d6bd7e0b6fba1999471f56a3dcaf9099ab63a82bc0c05d243d
0.3.22014-06-09T15:38:25.680850ZDownload791cc528e63f9f731361f4119ccb4e2d650c5f76a9003b364b51a7f1286c48c1
0.3.32014-11-09T22:03:51.268780ZDownload05b5f650bd0a1f833fa71b5e54815d53492c839e861997fdbd5391180a053f8b
0.3.42015-02-28T03:08:22.723540ZDownload78c8199061b26bd600d39bab00f10b826b90edf4a80879197b197e30181b9ea6
0.3.52015-03-03T01:51:50.173280ZDownloadc3cc06bdf812d432ad067b9f0005399dc738130b4f3345630bd030dcbc44c3e8
0.4.02015-03-03T21:31:57.493690ZDownload01e03bd162a96dbd3d559af1a8964211a15f3d4c3fee7b05826e61d8ad0951b1
0.4.12015-03-10T20:34:30.544420ZDownload3c69bcd2c7c14a3f43d99bf9d8ec5696836ddfe7899a4f31f1db70dc051012b1
0.4.22016-07-04T23:52:14.298ZDownload3f2e479652c173e94dc1db9dcd95f2d1100e5f7c77443a6f9ffd7a0f3fa0f96b
0.4.32018-06-04T00:18:55.841306ZDownload70443e4f337b48bc7b550fa7b907fcf34c8c17e90593c13637f7f4160eb79cc7
0.5.02018-06-15T23:36:32.978508ZDownload8061e754cacfff44b60428be30408a2863fbb6c77c0642a6bb119c9147bae7ca
0.5.12018-07-09T00:25:21.623907ZDownloadac7a7f5627373e4d4080aa1f9df0729043e2afa5482999c8b892e22a4ebe80af
0.5.22018-07-28T22:32:10.209737ZDownloadd72f7d3c204e440f1ab1a517716a56d8d541694045b4c135b35962bd886db95b
0.5.32018-10-06T19:09:40.960806ZDownloadd1d0dee6b40b26c5ede8bace9e4df017eeb8e05c6b0c17515bcaf2cc813bed06
0.5.42019-03-14T01:04:28.484222ZDownload9a702226275c877082f2bb7e3caec1a0320b8117a2ebe2b887e694ceb50099e4
0.5.52019-03-15T00:57:28.794556ZDownloadea7afc1a1089ccbf6c1a7c24ea833a54e858c30cecab61b07330a5c94e426295
0.5.62019-03-15T15:04:44.224673ZDownload328c9ae2ed7edba6141ed05ccf0907e1e8d8b2803a649abc64840d1f9883ca58
0.5.72019-03-15T20:30:32.218435ZDownload02ddd1790a357118ed48f7db84e0c942f40e31a1790a40cdcce78fd5b881d9d5
0.5.82019-03-28T13:36:45.389570ZDownload0b14a4bc25b1f9d4ff1157c48a6401fc95de03440b84b8d1488ba6856662278f
0.5.92019-10-18T14:01:46.314072ZDownloada963cd30ce08643e968e321ac3f37ead37fa853da400ab4323fef46b7f7e4986
0.5.102019-10-20T19:51:17.715161ZDownloadb1be1c6eddd6524f5166b72700e3f9c135d3fada2e27f9d3b9f510071a0d4f38
0.5.112019-10-21T13:37:52.513677ZDownload49090626102b2e6a757ee1bcdb64b980c2347bdf51699e73a5fc6a7a5d291f54
0.5.122019-11-04T22:36:53.452022ZDownload0c332889959d926c8687f26dc278b1a22482511b9d88d187786dd6f286c3091a
0.5.132019-11-05T13:45:49.622009ZDownload2cdda13cd818cd0eebef36714d10fec6575d2eefa7d5ff4adeeb8774b8fd37cb
0.5.142019-11-11T14:09:00.036912ZDownload73b48ecdabe28cf2a9d0afa3f0a804c011b43669df3d50f9941514d753f64f76
0.5.152019-12-13T15:30:40.133142ZDownload8364f425ce378c8cb0e3c306a64a78635515e91f0ade4ca62de609d280ff35ce
0.6.02020-01-14T10:50:54.193001ZDownloadc109a7f9e2c2f962186510d19b9c6ee7bdbd853af2a7392620fe826b07c756d9
0.6.12020-02-18T10:41:24.101279ZDownloadb0e9f9d6ada3623dcdef9ccdf18749e3db77e18bb9b9ed6ffea4e4f143a1b2a6
0.6.22020-02-18T10:48:04.624360ZDownload099dbdd19f76047e372b01475c61e4020c25ffbfa97ad38241ab6698040c60f3
0.6.32020-02-19T14:26:44.974957ZDownload3e06db5c325957881a52512d5d621bddbfea13d871b7731c26bc2bb01ef7b52e
0.6.42020-02-21T18:15:56.815649ZDownload011c6001c2ad115ea6f736f2208aad35d5e66b85033a83765908770de2cae46f
0.6.52020-06-29T12:04:30.968670ZDownload69a5f88e9a5dfee9021bb8340a4d9d5e764c9e1fd3d4ae6b8aafe689cf8a9b3c
0.7.02020-09-18T21:35:01.348684ZDownload098f2207c09d6cfaec64ec4192fd194e6de2de0cfdf91276f570c77f54a0d3d5
0.8.02020-09-23T00:10:26.561023ZDownload8cd01b86f1fb925c481b7ab18ce6abfb7e973f0233dcd463621adcbaad8d4d72
0.8.12020-09-23T17:52:20.039379ZDownloadf4bf427060dd9cf67044d8370d4afdd244c7fdb8a85a868e07d2b7e01c5291d9
0.9.0-nullsafety.02020-11-26T03:44:28.135254ZDownloaddbf4f16ab1e3f53a846404857c1df0038d0beca4c8c0b1a0e091d227ec6b8566
1.0.02021-03-22T22:36:55.035817ZDownload3fe6429f90bf78422becd2234212148b4911863260eb53c088cd154d39c5b049
1.0.12021-06-01T17:32:03.023679ZDownload5eea318a1e940415cad5350110adca0fb1c8ebff818b152da0bb2d185cb3cffc
1.0.22021-06-07T15:00:54.671561ZDownload58455d2724bc6e0889fc410debe3cc31ca6bd808a47c194de726ce295dce199f
1.0.32021-11-14T15:48:13.221637ZDownload35e6681078cf198c5d5e83fdc5fcf4ad39a5f29564c181b370b5c29361f28660
1.1.02022-01-19T16:36:27.509210ZDownloadc273725e171cea7e69c8953181202a2850297bcc7617916d83b396cd791a2dcd
1.1.12022-09-14T22:55:17.802541ZDownloadc8e7571a1e9177db4c9b8de1b8f0e462dda18f397eed40b3787d90171d6251e7
1.2.02022-10-18T15:49:19.681034ZDownloadd7f1708143914ebf1d876de719998a6dad75194e85f5e85a2500402a5fc2de5f
1.2.12022-10-20T23:29:38.195756ZDownload6f331278f2dd6d317539cb875e3ea937a5b8a225afbd7d726846f34bc8b6e75c
1.2.22022-10-27T02:14:17.812107ZDownload061c6d821ee65d9565240a439dc42222a6c481820da2fe7dc449a57a79bc909f
1.3.02022-11-14T23:33:15.662519ZDownload17b53aa5f2983420a99534c067b99c03715fff8a0ec7b55627dd3397a765c2cf
1.4.02022-11-17T22:38:14.142828ZDownloadab96a1cb3beeccf8145c52e449233fe68364c9641623acd3adad66f8184f1039
1.4.12023-06-19T14:47:16.815052ZDownloadb74e3842a52c61f8819a1ec8444b4de5419b41a7465e69d4aa681445377398b0
1.5.02023-07-26T01:15:14.423717ZDownload21afe4333076c02877d14f4a89df111e658a6d466cbfc802eb705eb91bd5adfd
1.5.22024-01-30T19:36:43.176844ZDownloadc9c85fedbe2188b95133cbe960e16f5f448860f7133330e272edbbca5893ddc6
1.5.32024-05-10T17:24:59.762085ZDownload58082b3f0dca697204dbab0ef9ff208bfaea7767ea771076af9a343488428dda
1.5.42024-09-12T19:59:05.579320ZDownload2ca377ad4d677bbadca278e0ba4da4e057b80a10b927bfc8f7d8bda8fe2ceb75
1.5.52024-09-20T02:17:49.972542ZDownload6b151826fcc95ff246cd219a0bf4c753ea14f4081ad71c61939becf3aba27f70
1.5.62024-09-27T00:04:50.949862ZDownloadbf1a19d6ebea1b3b6151304936955d7d73e1f00b75e544c01a60fb2e832ffe1d
1.5.72024-10-31T22:30:45.301376ZDownload2fec8d0ed3b8998573edbf2713efc0a687276f13db9a5ba96fca2dd16c6bde13
1.5.82024-11-01T14:19:16.800163ZDownload4bae5ae63e6d6dd17c4aac8086f3dec26c0236f6a0f03416c6c19d830c367cf5
1.5.92025-02-08T23:26:32.467275ZDownload1c296cd268f486cabcc3930e9b93a8133169305f18d722916e675959a88f6d2c
1.6.02025-03-02T22:06:14.872140ZDownload068190d6c99c436287936ba5855af2e1fa78d8083ae65b4db6a281780da727ae
1.6.12025-03-22T19:54:29.755398ZDownloaded64c586b5e0476050ff719ea28a94b2831f8857ba2a90f8f05dc276a88456fa
1.6.22025-03-23T00:26:06.677418ZDownloade02d018628c870ef2d7f03e33f9ad179d89ff6ec52ca6c56bcb80bcef979867f
1.6.32025-04-20T21:28:19.682145ZDownloade8953f3698626f6f40c32da22c3348b2c6383e0db224402c53c8b8f3215b1e85
1.6.42025-04-25T14:35:31.443965ZDownload0511d6be23b007e95105ae023db599aea731df604608978dada7f9faf2637623
1.6.52025-06-24T21:29:53.822873ZDownload9a8f69025044eb466b9b60ef3bc3ac99b4dc6c158ae9c56d25eeccf5bc56d024
1.6.62026-10-26T21:06:26.707785ZDownload50beaed6a392d0df12f3fbc8db818ff702fe562e35bea04a5ec693ed57996111
1.6.72026-10-26T23:17:28.026051ZDownload1dde50d4a33db4794651221cca7a192d605de2003c03407b4c8f50a8cd28ff42
1.6.82026-10-28T23:48:37.982279ZDownloadcfb3cf6a53158918d52dda61bc26f0cc8612a0d3b9dc29e87eaa3423902ca636

Dependencies

No direct dependencies declared.