Skip to content

Commit

Permalink
Refactor thrown exceptions"
Browse files Browse the repository at this point in the history
  • Loading branch information
smlu committed May 29, 2024
1 parent a94fbff commit af94f05
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
11 changes: 7 additions & 4 deletions lib/src/crypto/aa_pubkey.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,29 @@ class AAPublicKey {

AAPublicKeyType get type => _type;

/// Parses AAPublicKey from [encPubKey] bytes which should be DER encoded ASN.1 bytes.
///
/// Throws [Exception] when provided bytes doesn't contain correct DER encoded ASN.1 AAPublicKey format.
AAPublicKey.fromBytes(final Uint8List encPubKey) : _encPubKey = encPubKey {
// Parse key type and SubjectPublicKey bytes

final tvPubKeyInfo = TLV.decode(encPubKey);
if (tvPubKeyInfo.tag.value != 0x30) { // Sequence
throw EfParseError(
throw Exception(
"Invalid SubjectPublicKeyInfo tag=${tvPubKeyInfo.tag.value.hex()}, expected tag=30"
);
}

final tvAlg = TLV.decode(tvPubKeyInfo.value);
if (tvAlg.tag.value != 0x30) { // Sequence
throw EfParseError(
throw Exception(
"Invalid AlgorithmIdentifier tag=${tvAlg.tag.value.hex()}, expected tag=30"
);
}

final tvAlgOID = TLV.decode(tvAlg.value);
if (tvAlg.tag.value != 0x06) { // OID
throw EfParseError(
throw Exception(
"Invalid Algorithm OID object tag=${tvAlgOID.tag.value.hex()}, expected tag=06"
);
}
Expand All @@ -59,7 +62,7 @@ class AAPublicKey {

_subPubKeyBytes = tvPubKeyInfo.value.sublist(tvAlg.encodedLen);
if (_subPubKeyBytes[0] != 0x03) { // Bit String
throw EfParseError(
throw Exception(
"Invalid SubjectPublicKey object tag=${_subPubKeyBytes[0].hex()}, expected tag=03"
);
}
Expand Down
6 changes: 5 additions & 1 deletion lib/src/lds/df1/efdg15.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ class EfDG15 extends DataGroup {

@override
void parseContent(final Uint8List content) {
_pubkey = AAPublicKey.fromBytes(content);
try {
_pubkey = AAPublicKey.fromBytes(content);
} on Exception catch(e) {
throw EfParseError("Failed to parse AAPublicKey from EF.DG15: $e");
}
}
}

0 comments on commit af94f05

Please sign in to comment.