-
Notifications
You must be signed in to change notification settings - Fork 31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Uptake public key hash changes #1440
Merged
Merged
Changes from 6 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
48509a7
feat: Uptake public key hash changes
sitaram-kalluri f6ed188
fix: Override at_persistence_secondary_server package to consume publ…
sitaram-kalluri a3dc9a5
Merge branch 'trunk' into 1417-uptake-pubkeyhash-changes
sitaram-kalluri 8e1ac5e
fix: Update the unit tests and version in pubspec.yaml
sitaram-kalluri 181c955
Merge remote-tracking branch 'origin/1417-uptake-pubkeyhash-changes' …
sitaram-kalluri bc3ed75
fix: Modify existing functional tests to assert on publicKeyHash
sitaram-kalluri 02242b3
fix: Use random IVs in enrollment_setup.dart replacing legacy IVs
sitaram-kalluri d42b7df
Merge branch 'trunk' into 1417-uptake-pubkeyhash-changes
gkc 44aade2
fix: In at_notification.dart check for "publicKeyHash" for null check
sitaram-kalluri e9ee174
Merge branch 'trunk' into 1417-uptake-pubkeyhash-changes
gkc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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
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
170 changes: 170 additions & 0 deletions
170
packages/at_client/test/encryption_decryption_test.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,170 @@ | ||
import 'dart:io'; | ||
|
||
import 'package:at_chops/at_chops.dart'; | ||
import 'package:at_client/at_client.dart'; | ||
import 'package:at_client/src/decryption_service/shared_key_decryption.dart'; | ||
import 'package:at_client/src/encryption_service/shared_key_encryption.dart'; | ||
import 'package:at_commons/at_builders.dart'; | ||
import 'package:mocktail/mocktail.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
class MockRemoteSecondary extends Mock implements RemoteSecondary {} | ||
|
||
void main() { | ||
String currentAtSign = '@alice'; | ||
String namespace = 'unit.test'; | ||
|
||
MockRemoteSecondary mockRemoteSecondary = MockRemoteSecondary(); | ||
|
||
late AtChops atChops; | ||
late AtClient atClient; | ||
|
||
setUp(() async { | ||
AtClientPreference atClientPreference = AtClientPreference() | ||
..isLocalStoreRequired = true | ||
..hiveStoragePath = 'test/unit_test_storage/hive' | ||
..commitLogPath = 'test/unit_test_storage/commit'; | ||
|
||
AtEncryptionKeyPair atEncryptionKeyPair = | ||
AtChopsUtil.generateAtEncryptionKeyPair(); | ||
AtPkamKeyPair atPkamKeyPair = AtChopsUtil.generateAtPkamKeyPair(); | ||
AtChopsKeys atChopsKeys = | ||
AtChopsKeys.create(atEncryptionKeyPair, atPkamKeyPair); | ||
atChopsKeys.selfEncryptionKey = | ||
AtChopsUtil.generateSymmetricKey(EncryptionKeyType.aes256); | ||
atChops = AtChopsImpl(atChopsKeys); | ||
|
||
atClient = await AtClientImpl.create( | ||
currentAtSign, namespace, atClientPreference, | ||
remoteSecondary: mockRemoteSecondary, atChops: atChops); | ||
|
||
// During decryption, fetches the encryption public key from local keystore. | ||
// So, store the encryption public key into local secondary keystore. | ||
await atClient.getLocalSecondary()?.putValue( | ||
'public:publickey$currentAtSign', | ||
atEncryptionKeyPair.atPublicKey.publicKey); | ||
}); | ||
|
||
tearDownAll(() { | ||
Directory('test/unit_test_storage').deleteSync(recursive: true); | ||
}); | ||
|
||
group('A group of tests related to encryption and decryption of shared keys', | ||
() { | ||
test( | ||
'A test to verify encryption and decryption of shared key is successful - with publicKeyHash', | ||
() async { | ||
registerFallbackValue(LLookupVerbBuilder()); | ||
AtKey sharedKey = | ||
(AtKey.shared('email', namespace: namespace, sharedBy: currentAtSign) | ||
..sharedWith('@bob')) | ||
.build(); | ||
String value = '[email protected]'; | ||
|
||
when(() => mockRemoteSecondary | ||
.executeVerb(any(that: EncryptedSharedKeyMatcher()))) | ||
.thenAnswer((_) => Future.value('data:null')); | ||
|
||
// Returns encryption public key of sharedWith atSign. | ||
// For unit test, reusing the current AtSign encryptionPublicKey. | ||
when(() => mockRemoteSecondary | ||
.executeVerb(any(that: EncryptionPublicKeyMatcher()))) | ||
.thenAnswer((_) => Future.value( | ||
atChops.atChopsKeys.atEncryptionKeyPair?.atPublicKey.publicKey)); | ||
|
||
// Encryption | ||
SharedKeyEncryption sharedKeyEncryption = SharedKeyEncryption(atClient); | ||
String encryptedValue = | ||
await sharedKeyEncryption.encrypt(sharedKey, value); | ||
expect(sharedKey.metadata.pubKeyHash?.hash.isNotEmpty, true); | ||
expect(sharedKey.metadata.pubKeyHash?.hashingAlgo, 'sha512'); | ||
expect(sharedKey.metadata.sharedKeyEnc?.isNotEmpty, true); | ||
expect(sharedKey.metadata.pubKeyCS?.isNotEmpty, true); | ||
|
||
// Explicitly setting pubKeyCS to null, so that pubKeyHash will only be used | ||
// during decryption. | ||
sharedKey.metadata.pubKeyCS = null; | ||
expect(sharedKey.metadata.pubKeyCS, null); | ||
|
||
// Decryption | ||
SharedKeyDecryption sharedKeyDecryption = SharedKeyDecryption(atClient); | ||
String decryptedValue = | ||
await sharedKeyDecryption.decrypt(sharedKey, encryptedValue); | ||
expect(decryptedValue, value); | ||
}); | ||
|
||
test('A test to verify exception is thrown when publicKeyHash mismatch', | ||
() async { | ||
registerFallbackValue(LLookupVerbBuilder()); | ||
AtKey sharedKey = | ||
(AtKey.shared('email', namespace: namespace, sharedBy: currentAtSign) | ||
..sharedWith('@bob')) | ||
.build(); | ||
String value = '[email protected]'; | ||
|
||
when(() => mockRemoteSecondary | ||
.executeVerb(any(that: EncryptedSharedKeyMatcher()))) | ||
.thenAnswer((_) => Future.value('data:null')); | ||
|
||
// Returns encryption public key of sharedWith atSign. | ||
// For unit test, reusing the current AtSign encryptionPublicKey. | ||
when(() => mockRemoteSecondary | ||
.executeVerb(any(that: EncryptionPublicKeyMatcher()))) | ||
.thenAnswer((_) => Future.value( | ||
atChops.atChopsKeys.atEncryptionKeyPair?.atPublicKey.publicKey)); | ||
|
||
// Encryption | ||
SharedKeyEncryption sharedKeyEncryption = SharedKeyEncryption(atClient); | ||
String encryptedValue = | ||
await sharedKeyEncryption.encrypt(sharedKey, value); | ||
expect(sharedKey.metadata.pubKeyHash?.hash.isNotEmpty, true); | ||
expect(sharedKey.metadata.pubKeyHash?.hashingAlgo, 'sha512'); | ||
expect(sharedKey.metadata.sharedKeyEnc?.isNotEmpty, true); | ||
expect(sharedKey.metadata.pubKeyCS?.isNotEmpty, true); | ||
|
||
// Explicitly setting pubKeyCS to null, so that pubKeyHash will only be used | ||
// during decryption. | ||
sharedKey.metadata.pubKeyCS = null; | ||
expect(sharedKey.metadata.pubKeyCS, null); | ||
// Explicity changing the publicKeyHash value to mimic change in publicKeyHash | ||
// value. | ||
sharedKey.metadata.pubKeyHash = | ||
PublicKeyHash('dummy_hash_value', HashingAlgoType.sha512.name); | ||
|
||
// Decryption | ||
SharedKeyDecryption sharedKeyDecryption = SharedKeyDecryption(atClient); | ||
expect( | ||
() async => | ||
await sharedKeyDecryption.decrypt(sharedKey, encryptedValue), | ||
throwsA(predicate((dynamic e) => | ||
e is AtPublicKeyChangeException && | ||
e.message == | ||
'Public key has changed. Cannot decrypt shared key ${sharedKey.toString()}'))); | ||
}); | ||
}); | ||
} | ||
|
||
class EncryptedSharedKeyMatcher extends Matcher { | ||
@override | ||
Description describe(Description description) { | ||
return description; | ||
} | ||
|
||
@override | ||
bool matches(item, Map matchState) { | ||
return item.atKey.key.startsWith(AtConstants.atEncryptionSharedKey); | ||
} | ||
} | ||
|
||
class EncryptionPublicKeyMatcher extends Matcher { | ||
@override | ||
Description describe(Description description) { | ||
// TODO: implement describe | ||
throw UnimplementedError(); | ||
} | ||
|
||
@override | ||
bool matches(item, Map matchState) { | ||
return item.atKey.key.startsWith('publickey'); | ||
} | ||
} |
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems bad
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure Gary, will revisit the code and modify the IF condition.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the
MonitorVerbHandler.dart
file ofat_secondary_server
, thepubKeyHash
object is converted into a JSON-encoded string and sent to the client. When the pubKeyHash value is null, the jsonEncode method returns a string with the value "null", which causes metadata.pubKeyHash to be set to"null"
instead ofnull
.Code snippet from MonitorVerbHandler.dart:
"pubKeyHash": jsonEncode(atNotification.atMetadata?.pubKeyHash?.toJson())
To ensure that metadata.publicKeyHash contains a null object on the client side, modify the code in
MonitorVerbHandler.dart
as follows:@gkc : Let me know if this approach looks good or suggest an alternate approach please.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep looks fine. Please add unit test(s) to cover
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, Thanks Gary.