2020-06-15 00:08:23 +00:00
|
|
|
import 'dart:typed_data';
|
|
|
|
|
|
|
|
import 'x509_certificate.dart';
|
|
|
|
|
|
|
|
import 'asn1_identifier.dart';
|
|
|
|
import 'oid.dart';
|
|
|
|
|
|
|
|
class ASN1Object {
|
|
|
|
/// This property contains the DER encoded object
|
2021-01-28 16:10:15 +00:00
|
|
|
Uint8List? encoded;
|
2020-06-15 00:08:23 +00:00
|
|
|
|
2021-02-12 16:14:13 +00:00
|
|
|
/// This property contains the decoded object whenever is possible
|
2020-06-15 00:08:23 +00:00
|
|
|
dynamic value;
|
|
|
|
|
2021-01-28 16:10:15 +00:00
|
|
|
ASN1Identifier? identifier;
|
2020-06-15 00:08:23 +00:00
|
|
|
|
2021-01-28 16:10:15 +00:00
|
|
|
List<ASN1Object>? sub;
|
2020-06-15 00:08:23 +00:00
|
|
|
|
2021-01-28 16:10:15 +00:00
|
|
|
ASN1Object? parent;
|
2020-06-15 00:08:23 +00:00
|
|
|
|
2021-01-28 16:10:15 +00:00
|
|
|
ASN1Object? subAtIndex(int index) {
|
|
|
|
if (sub != null && index >= 0 && index < sub!.length) {
|
|
|
|
return sub![index];
|
2020-06-15 00:08:23 +00:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-01-28 16:10:15 +00:00
|
|
|
ASN1Object? firstSub() {
|
2020-06-15 22:25:29 +00:00
|
|
|
if (subCount() > 0) {
|
2021-01-28 16:10:15 +00:00
|
|
|
return sub!.first;
|
2020-06-15 22:25:29 +00:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-01-28 16:10:15 +00:00
|
|
|
ASN1Object? lastSub() {
|
2020-06-15 22:25:29 +00:00
|
|
|
if (subCount() > 0) {
|
2021-01-28 16:10:15 +00:00
|
|
|
return sub!.last;
|
2020-06-15 22:25:29 +00:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-06-15 00:08:23 +00:00
|
|
|
int subCount() {
|
|
|
|
return sub?.length ?? 0;
|
|
|
|
}
|
|
|
|
|
2021-01-28 16:10:15 +00:00
|
|
|
ASN1Object? findOid({OID? oid, String? oidValue}) {
|
2020-06-15 00:08:23 +00:00
|
|
|
oidValue = oid != null ? oid.toValue() : oidValue;
|
|
|
|
for (var child in (sub ?? <ASN1Object>[])) {
|
2020-06-21 22:09:35 +00:00
|
|
|
if (child.identifier?.tagNumber() ==
|
|
|
|
ASN1IdentifierTagNumber.OBJECT_IDENTIFIER) {
|
2020-06-15 00:08:23 +00:00
|
|
|
if (child.value == oidValue) {
|
|
|
|
return child;
|
|
|
|
}
|
2020-06-21 22:09:35 +00:00
|
|
|
} else {
|
2020-06-15 00:08:23 +00:00
|
|
|
var result = child.findOid(oidValue: oidValue);
|
|
|
|
if (result != null) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
String get description {
|
|
|
|
return printAsn1();
|
|
|
|
}
|
|
|
|
|
|
|
|
String printAsn1({insets = ""}) {
|
2020-06-21 22:09:35 +00:00
|
|
|
var output = insets;
|
2021-01-28 16:10:15 +00:00
|
|
|
output += identifier?.description.toUpperCase() ?? "";
|
2020-06-21 22:09:35 +00:00
|
|
|
output += (value != null ? ": $value" : "");
|
|
|
|
if (identifier?.typeClass() == ASN1IdentifierClass.UNIVERSAL &&
|
|
|
|
identifier?.tagNumber() == ASN1IdentifierTagNumber.OBJECT_IDENTIFIER) {
|
|
|
|
var descr = OID.fromValue(value?.toString() ?? "")?.name();
|
|
|
|
if (descr != null) {
|
|
|
|
output += " ($descr)";
|
2020-06-15 00:08:23 +00:00
|
|
|
}
|
2020-06-21 22:09:35 +00:00
|
|
|
}
|
2021-01-28 16:10:15 +00:00
|
|
|
output += sub != null && sub!.length > 0 ? " {" : "";
|
2020-06-21 22:09:35 +00:00
|
|
|
output += "\n";
|
|
|
|
for (var item in (sub ?? <ASN1Object>[])) {
|
|
|
|
output += item.printAsn1(insets: insets + " ");
|
|
|
|
}
|
2021-01-28 16:10:15 +00:00
|
|
|
output += sub != null && sub!.length > 0 ? "}\n" : "";
|
2020-06-21 22:09:35 +00:00
|
|
|
return output;
|
2020-06-15 00:08:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
String toString() {
|
|
|
|
return description;
|
|
|
|
}
|
|
|
|
|
2021-01-28 16:10:15 +00:00
|
|
|
ASN1Object? atIndex(X509BlockPosition x509blockPosition) {
|
|
|
|
if (sub != null && x509blockPosition.index < sub!.length) {
|
|
|
|
return sub![x509blockPosition.index];
|
2020-06-15 00:08:23 +00:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2021-02-12 16:14:13 +00:00
|
|
|
|
|
|
|
String? get asString {
|
2021-02-24 23:00:46 +00:00
|
|
|
if (value is String) {
|
|
|
|
return value;
|
2021-02-12 16:14:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (sub != null && sub!.length > 0) {
|
|
|
|
for (var item in sub!) {
|
|
|
|
var itemAsString = item.asString;
|
|
|
|
if (itemAsString != null) {
|
|
|
|
return itemAsString;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
2020-06-21 22:09:35 +00:00
|
|
|
}
|