Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
Signed-off-by: Nguyen Van Nguyen <[email protected]>
  • Loading branch information
nguyennv committed Mar 25, 2024
1 parent b747e8d commit d7d6b54
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 25 deletions.
5 changes: 5 additions & 0 deletions lib/src/openpgp.dart
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ class OpenPGP {
) async =>
PublicKey.fromArmored(armoredPublicKey);

/// Read an armored OpenPGP public key list.
static List<PublicKey> readPublicKeys(final String armoredPublicKeys) {
return PublicKey.readPublicKeys(armoredPublicKeys);
}

/// Sign a cleartext message.
static Future<SignedMessage> sign(
final String cleartext,
Expand Down
17 changes: 13 additions & 4 deletions lib/src/packet/packet_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,7 @@ class PacketList extends ListBase<ContainedPacket> {
}

Uint8List encode() => Uint8List.fromList(
packets
.map((packet) => packet.encode())
.expand((byte) => byte)
.toList(growable: false),
packets.map((packet) => packet.encode()).expand((byte) => byte).toList(growable: false),
);

PacketList filterByTags([final List<PacketTag> tags = const []]) {
Expand All @@ -126,6 +123,18 @@ class PacketList extends ListBase<ContainedPacket> {
return this;
}

List<int> indexOfTags([final List<PacketTag> tags = const []]) {
final indexes = <int>[];
for (var i = 0; i < packets.length; i++) {
final packet = packets[i];
if (tags.contains(packet.tag)) {
indexes.add(i);
}
}

return indexes;
}

@override
int get length => packets.length;

Expand Down
18 changes: 8 additions & 10 deletions lib/src/type/key.dart
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,7 @@ abstract class Key {
for (final user in users) {
for (var signature in user.selfCertifications) {
if (signature.keyFlags != null &&
!(signature.keyFlags!.isEncryptStorage ||
signature.keyFlags!.isEncryptCommunication)) {
!(signature.keyFlags!.isEncryptStorage || signature.keyFlags!.isEncryptCommunication)) {
return false;
}
}
Expand All @@ -107,8 +106,7 @@ abstract class Key {
bool get aeadSupported {
for (final user in users) {
for (var signature in user.selfCertifications) {
if (signature.features != null &&
signature.features!.supportAeadEncryptedData) {
if (signature.features != null && signature.features!.supportAeadEncryptedData) {
return true;
}
}
Expand Down Expand Up @@ -184,9 +182,9 @@ abstract class Key {
final DateTime? date,
}) async {
if (revocationSignatures.isNotEmpty) {
for (var revocation in revocationSignatures) {
if (signature == null ||
revocation.issuerKeyID.id == signature.issuerKeyID.id) {
final revocationKeyIDs = <String>[];
for (final revocation in revocationSignatures) {
if (signature == null || revocation.issuerKeyID.id == signature.issuerKeyID.id) {
if (await revocation.verify(
keyPacket,
keyPacket.writeForSign(),
Expand All @@ -195,7 +193,9 @@ abstract class Key {
return true;
}
}
revocationKeyIDs.add(revocation.issuerKeyID.id);
}
return revocationKeyIDs.isNotEmpty;
}
return false;
}
Expand Down Expand Up @@ -242,9 +242,7 @@ abstract class Key {
...revocationSignatures,
...directSignatures,
...users.map((user) => user.toPacketList()).expand((packet) => packet),
...subkeys
.map((subkey) => subkey.toPacketList())
.expand((packet) => packet),
...subkeys.map((subkey) => subkey.toPacketList()).expand((packet) => packet),
]);

static Map<String, dynamic> readPacketList(final PacketList packetList) {
Expand Down
29 changes: 25 additions & 4 deletions lib/src/type/public_key.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// file that was distributed with this source code.

import '../armor/armor.dart';
import '../enum/packet_tag.dart';
import '../enum/armor_type.dart';
import '../packet/packet_list.dart';
import '../packet/key_packet.dart';
Expand Down Expand Up @@ -41,6 +42,28 @@ class PublicKey extends Key {
);
}

static List<PublicKey> readPublicKeys(String armored) {
final armor = Armor.decode(armored);
if (armor.type != ArmorType.publicKey) {
throw ArgumentError('Armored text not of public key type');
}
final publicKeys = <PublicKey>[];
final packetList = PacketList.packetDecode(armor.data);
final indexes = packetList.indexOfTags([PacketTag.publicKey]);
for (var i = 0; i < indexes.length; i++) {
if (indexes.asMap().containsKey(i + 1)) {
publicKeys.add(
PublicKey.fromPacketList(
PacketList(
packetList.packets.sublist(indexes[i], indexes[i + 1]),
),
),
);
}
}
return publicKeys;
}

@override
PublicKeyPacket get keyPacket => super.keyPacket as PublicKeyPacket;

Expand All @@ -67,8 +90,7 @@ class PublicKey extends Key {
}
}
}
if (isSigningKey ||
(keyID.isNotEmpty && keyID != keyPacket.keyID.toString())) {
if (isSigningKey || (keyID.isNotEmpty && keyID != keyPacket.keyID.toString())) {
throw StateError('Could not find valid encryption key packet.');
}
return keyPacket.publicKey;
Expand All @@ -91,8 +113,7 @@ class PublicKey extends Key {
}
}
}
if (isEncryptionKey ||
(keyID.isNotEmpty && keyID != keyPacket.keyID.toString())) {
if (isEncryptionKey || (keyID.isNotEmpty && keyID != keyPacket.keyID.toString())) {
throw StateError('Could not find valid verification key packet.');
}
return keyPacket;
Expand Down
11 changes: 6 additions & 5 deletions lib/src/type/subkey.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ class Subkey {
if (keyPacket.isEncryptionKey) {
for (final signature in bindingSignatures) {
if (signature.keyFlags != null &&
!(signature.keyFlags!.isEncryptStorage ||
signature.keyFlags!.isEncryptCommunication)) {
!(signature.keyFlags!.isEncryptStorage || signature.keyFlags!.isEncryptCommunication)) {
return false;
}
}
Expand Down Expand Up @@ -104,9 +103,9 @@ class Subkey {
final DateTime? date,
}) async {
if (mainKey != null && revocationSignatures.isNotEmpty) {
for (var revocation in revocationSignatures) {
if (signature == null ||
revocation.issuerKeyID.id == signature.issuerKeyID.id) {
final revocationKeyIDs = <String>[];
for (final revocation in revocationSignatures) {
if (signature == null || revocation.issuerKeyID.id == signature.issuerKeyID.id) {
if (await revocation.verify(
mainKey!.keyPacket,
Uint8List.fromList([
Expand All @@ -118,7 +117,9 @@ class Subkey {
return true;
}
}
revocationKeyIDs.add(revocation.issuerKeyID.id);
}
return revocationKeyIDs.isNotEmpty;
}
return false;
}
Expand Down
6 changes: 4 additions & 2 deletions lib/src/type/user.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ class User {
final DateTime? date,
}) async {
if (mainKey != null && revocationSignatures.isNotEmpty) {
final revocationKeyIDs = <String>[];
for (var revocation in revocationSignatures) {
if (signature == null ||
revocation.issuerKeyID.id == signature.issuerKeyID.id) {
if (signature == null || revocation.issuerKeyID.id == signature.issuerKeyID.id) {
if (await revocation.verifyUserCertification(
mainKey!.keyPacket,
userID: userID,
Expand All @@ -50,7 +50,9 @@ class User {
return true;
}
}
revocationKeyIDs.add(revocation.issuerKeyID.id);
}
return revocationKeyIDs.isNotEmpty;
}
return false;
}
Expand Down

0 comments on commit d7d6b54

Please sign in to comment.