mirrored from https://www.bouncycastle.org/repositories/pc-dart
-
Notifications
You must be signed in to change notification settings - Fork 128
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'Ephenodrom-master' into master
- Loading branch information
Showing
39 changed files
with
2,898 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
library asn1; | ||
|
||
export 'asn1/asn1_encoding_rule.dart'; | ||
export 'asn1/asn1_object.dart'; | ||
export 'asn1/asn1_parser.dart'; | ||
export 'asn1/asn1_tags.dart'; | ||
export 'asn1/asn1_utils.dart'; | ||
export 'asn1/primitives/asn1_bit_string.dart'; | ||
export 'asn1/primitives/asn1_boolean.dart'; | ||
export 'asn1/primitives/asn1_enumerated.dart'; | ||
export 'asn1/primitives/asn1_ia5_string.dart'; | ||
export 'asn1/primitives/asn1_integer.dart'; | ||
export 'asn1/primitives/asn1_null.dart'; | ||
export 'asn1/primitives/asn1_object_identifier.dart'; | ||
export 'asn1/primitives/asn1_octet_string.dart'; | ||
export 'asn1/primitives/asn1_printable_string.dart'; | ||
export 'asn1/primitives/asn1_sequence.dart'; | ||
export 'asn1/primitives/asn1_set.dart'; | ||
export 'asn1/primitives/asn1_utc_time.dart'; | ||
export 'asn1/primitives/asn1_utf8_string.dart'; | ||
export 'asn1/unsupported_asn1_encoding_rule_exception.dart'; | ||
export 'asn1/unsupported_asn1_tag_exception.dart'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
enum ASN1EncodingRule { | ||
/// Normal DER encoding rules | ||
ENCODING_DER, | ||
|
||
/// BER encoding where the length is described in a long form | ||
ENCODING_BER_LONG_LENGTH_FORM, | ||
|
||
/// BER Constructed encoding with definite length | ||
ENCODING_BER_CONSTRUCTED, | ||
|
||
/// BER encoding with padded bits to make the length of the value bytes a multiple of eight. Only used for ASN1BitString | ||
ENCODING_BER_PADDED, | ||
|
||
/// BER Constructed encoding with indefinite length | ||
ENCODING_BER_CONSTRUCTED_INDEFINITE_LENGTH | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
import 'dart:typed_data'; | ||
|
||
import 'package:pointycastle/asn1/asn1_object.dart'; | ||
import 'package:pointycastle/asn1/asn1_tags.dart'; | ||
import 'package:pointycastle/asn1/asn1_utils.dart'; | ||
import 'package:pointycastle/asn1/primitives/asn1_bit_string.dart'; | ||
import 'package:pointycastle/asn1/primitives/asn1_boolean.dart'; | ||
import 'package:pointycastle/asn1/primitives/asn1_ia5_string.dart'; | ||
import 'package:pointycastle/asn1/primitives/asn1_integer.dart'; | ||
import 'package:pointycastle/asn1/primitives/asn1_null.dart'; | ||
import 'package:pointycastle/asn1/primitives/asn1_object_identifier.dart'; | ||
import 'package:pointycastle/asn1/primitives/asn1_octet_string.dart'; | ||
import 'package:pointycastle/asn1/primitives/asn1_printable_string.dart'; | ||
import 'package:pointycastle/asn1/primitives/asn1_sequence.dart'; | ||
import 'package:pointycastle/asn1/primitives/asn1_set.dart'; | ||
import 'package:pointycastle/asn1/primitives/asn1_utc_time.dart'; | ||
import 'package:pointycastle/asn1/primitives/asn1_utf8_string.dart'; | ||
import 'package:pointycastle/asn1/unsupported_asn1_tag_exception.dart'; | ||
|
||
/// | ||
/// The ASN1Parser to parse bytes into ASN1 Objects | ||
/// | ||
class ASN1Parser { | ||
/// | ||
/// The bytes to parse | ||
/// | ||
final Uint8List bytes; | ||
|
||
/// | ||
/// The current position in the byte array. | ||
/// | ||
/// The inital value is 0. | ||
/// | ||
int _position = 0; | ||
|
||
ASN1Parser(this.bytes); | ||
|
||
/// | ||
/// Returns true if there is still an object to parse. Otherwise false. | ||
/// | ||
bool hasNext() { | ||
return _position < bytes.length; | ||
} | ||
|
||
/// | ||
/// Parses the next object in the [bytes]. | ||
/// | ||
ASN1Object nextObject() { | ||
// Get the curren tag in the list bytes | ||
var tag = bytes[_position]; | ||
|
||
// Get the length of the value bytes for the current object | ||
var length = ASN1Utils.decodeLength(bytes.sublist(_position)); | ||
|
||
var valueStartPosition = | ||
ASN1Utils.calculateValueStartPosition(bytes.sublist(_position)); | ||
if (_position < length + valueStartPosition) { | ||
length = length + valueStartPosition; | ||
} else { | ||
length = bytes.length - _position; | ||
} | ||
|
||
// Create new view from the bytes | ||
var offset = _position + bytes.offsetInBytes; | ||
var subBytes = Uint8List.view(bytes.buffer, offset, length); | ||
|
||
// Parse the view and the tag to an ASN1Object | ||
var isConstructed = ASN1Utils.isConstructed(tag); | ||
ASN1Object obj; | ||
if (isConstructed) { | ||
obj = _createConstructed(tag, subBytes); | ||
} else { | ||
obj = _createPrimitive(tag, subBytes); | ||
} | ||
|
||
// Update the position | ||
_position = _position + obj.totalEncodedByteLength; | ||
return obj; | ||
} | ||
|
||
/// | ||
/// Creates a constructed ASN1Object depending on the given [tag] and [bytes] | ||
/// | ||
ASN1Object _createConstructed(int tag, Uint8List bytes) { | ||
switch (tag) { | ||
case ASN1Tags.SEQUENCE: // sequence | ||
return ASN1Sequence.fromBytes(bytes); | ||
case ASN1Tags.SET: | ||
return ASN1Set.fromBytes(bytes); | ||
case ASN1Tags.IA5_STRING_CONSTRUCTED: | ||
return ASN1IA5String.fromBytes(bytes); | ||
case ASN1Tags.BIT_STRING_CONSTRUCTED: | ||
return ASN1BitString.fromBytes(bytes); | ||
case ASN1Tags.OCTET_STRING_CONSTRUCTED: | ||
return ASN1OctetString.fromBytes(bytes); | ||
case ASN1Tags.PRINTABLE_STRING_CONSTRUCTED: | ||
return ASN1PrintableString.fromBytes(bytes); | ||
case 0xA0: | ||
case 0xA1: | ||
case 0xA2: | ||
case 0xA3: | ||
return ASN1Object.fromBytes(bytes); | ||
default: | ||
throw UnsupportedASN1TagException(tag); | ||
} | ||
} | ||
|
||
/// | ||
/// Creates a primitive ASN1Object depending on the given [tag] and [bytes] | ||
/// | ||
ASN1Object _createPrimitive(int tag, Uint8List bytes) { | ||
switch (tag) { | ||
case ASN1Tags.OCTET_STRING: | ||
return ASN1OctetString.fromBytes(bytes); | ||
case ASN1Tags.UTF8_STRING: | ||
return ASN1UTF8String.fromBytes(bytes); | ||
case ASN1Tags.IA5_STRING: | ||
return ASN1IA5String.fromBytes(bytes); | ||
case ASN1Tags.INTEGER: | ||
case ASN1Tags.ENUMERATED: | ||
return ASN1Integer.fromBytes(bytes); | ||
case ASN1Tags.BOOLEAN: | ||
return ASN1Boolean.fromBytes(bytes); | ||
case ASN1Tags.OBJECT_IDENTIFIER: | ||
return ASN1ObjectIdentifier.fromBytes(bytes); | ||
case ASN1Tags.BIT_STRING: | ||
return ASN1BitString.fromBytes(bytes); | ||
case ASN1Tags.NULL: | ||
return ASN1Null.fromBytes(bytes); | ||
case ASN1Tags.PRINTABLE_STRING: | ||
return ASN1PrintableString.fromBytes(bytes); | ||
case ASN1Tags.UTC_TIME: | ||
return ASN1UtcTime.fromBytes(bytes); | ||
default: | ||
throw UnsupportedASN1TagException(tag); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.