@@ -5,7 +5,6 @@ import 'package:at_client/src/util/encryption_util.dart';
5
5
import 'package:at_commons/at_builders.dart' ;
6
6
import 'package:at_commons/at_commons.dart' ;
7
7
import 'package:at_utils/at_logger.dart' ;
8
- import 'package:meta/meta.dart' ;
9
8
import 'package:at_chops/at_chops.dart' ;
10
9
11
10
/// Class responsible for decrypting the value of shared key's that are not owned
@@ -14,13 +13,12 @@ import 'package:at_chops/at_chops.dart';
14
13
/// CurrentAtSign: @bob
15
14
/// lookup:phone@alice
16
15
class SharedKeyDecryption implements AtKeyDecryption {
17
- @visibleForTesting
18
- final AtClient atClient;
16
+ final AtClient _atClient;
19
17
late final AtSignLogger _logger;
20
18
21
- SharedKeyDecryption (this .atClient ) {
19
+ SharedKeyDecryption (this ._atClient ) {
22
20
_logger =
23
- AtSignLogger ('SharedKeyDecryption (${atClient .getCurrentAtSign ()})' );
21
+ AtSignLogger ('SharedKeyDecryption (${_atClient .getCurrentAtSign ()})' );
24
22
}
25
23
26
24
@override
@@ -31,64 +29,76 @@ class SharedKeyDecryption implements AtKeyDecryption {
31
29
exceptionScenario: ExceptionScenario .decryptionFailed);
32
30
}
33
31
String ? encryptedSharedKey;
34
- if (atKey.metadata != null && atKey.metadata ! .pubKeyCS != null ) {
32
+ if (atKey.metadata != null ) {
35
33
encryptedSharedKey = atKey.metadata! .sharedKeyEnc;
36
- String ? currentAtSignPublicKey;
37
- try {
38
- currentAtSignPublicKey = (await atClient
39
- .getLocalSecondary ()!
40
- .getEncryptionPublicKey (atClient.getCurrentAtSign ()! ))
41
- ? .trim ();
42
- } on KeyNotFoundException {
43
- throw AtPublicKeyNotFoundException (
44
- 'Failed to fetch the current atSign public key - public:publickey${atClient .getCurrentAtSign ()!}' ,
45
- intent: Intent .fetchEncryptionPublicKey,
46
- exceptionScenario: ExceptionScenario .localVerbExecutionFailed);
47
- }
48
- if (currentAtSignPublicKey != null &&
49
- atKey.metadata! .pubKeyCS !=
50
- EncryptionUtil .md5CheckSum (currentAtSignPublicKey)) {
51
- throw AtPublicKeyChangeException (
52
- 'Public key has changed. Cannot decrypt shared key ${atKey .toString ()}' ,
53
- intent: Intent .fetchEncryptionPublicKey,
54
- exceptionScenario: ExceptionScenario .encryptionFailed);
55
- }
56
- } else {
57
- encryptedSharedKey = await _getEncryptedSharedKey (atKey);
58
34
}
59
- if (encryptedSharedKey == null ||
60
- encryptedSharedKey.isEmpty ||
61
- encryptedSharedKey == 'null' ) {
35
+ encryptedSharedKey ?? = await _getEncryptedSharedKey (atKey);
36
+ if (encryptedSharedKey.isEmpty || encryptedSharedKey == 'null' ) {
62
37
throw SharedKeyNotFoundException ('shared encryption key not found' ,
63
38
intent: Intent .fetchEncryptionSharedKey,
64
39
exceptionScenario: ExceptionScenario .fetchEncryptionKeys);
65
40
}
66
- String decryptedValue = '' ;
41
+ String ? currentAtSignPublicKey;
42
+ try {
43
+ currentAtSignPublicKey = (await _atClient
44
+ .getLocalSecondary ()!
45
+ .getEncryptionPublicKey (_atClient.getCurrentAtSign ()! ))
46
+ ? .trim ();
47
+ } on KeyNotFoundException {
48
+ throw AtPublicKeyNotFoundException (
49
+ 'Failed to fetch the current atSign public key - public:publickey${_atClient .getCurrentAtSign ()!}' ,
50
+ intent: Intent .fetchEncryptionPublicKey,
51
+ exceptionScenario: ExceptionScenario .localVerbExecutionFailed);
52
+ }
53
+ if (currentAtSignPublicKey != null &&
54
+ atKey.metadata != null &&
55
+ atKey.metadata! .pubKeyCS != null &&
56
+ atKey.metadata! .pubKeyCS !=
57
+ EncryptionUtil .md5CheckSum (currentAtSignPublicKey)) {
58
+ throw AtPublicKeyChangeException (
59
+ 'Public key has changed. Cannot decrypt shared key ${atKey .toString ()}' ,
60
+ intent: Intent .fetchEncryptionPublicKey,
61
+ exceptionScenario: ExceptionScenario .decryptionFailed);
62
+ }
63
+
64
+ AtEncryptionResult decryptionResultFromAtChops;
67
65
try {
68
- final decryptionResult = atClient.atChops!
66
+ InitialisationVector iV;
67
+ if (atKey.metadata? .ivNonce != null ) {
68
+ iV = AtChopsUtil .generateIVFromBase64String (atKey.metadata! .ivNonce! );
69
+ } else {
70
+ iV = AtChopsUtil .generateIVLegacy ();
71
+ }
72
+ final decryptionResult = _atClient.atChops!
69
73
.decryptString (encryptedSharedKey, EncryptionKeyType .rsa2048);
70
- decryptedValue = EncryptionUtil .decryptValue (
71
- encryptedValue, decryptionResult.result,
72
- ivBase64: atKey.metadata? .ivNonce);
74
+ var encryptionAlgo = AESEncryptionAlgo (AESKey (
75
+ DefaultResponseParser ().parse (decryptionResult.result).response));
76
+ decryptionResultFromAtChops = _atClient.atChops! .decryptString (
77
+ encryptedValue, EncryptionKeyType .aes256,
78
+ encryptionAlgorithm: encryptionAlgo, iv: iV);
73
79
} on AtKeyException catch (e) {
74
80
e.stack (AtChainedException (
75
81
Intent .decryptData,
76
82
ExceptionScenario .decryptionFailed,
77
83
'Failed to decrypt ${atKey .toString ()}' ));
78
84
rethrow ;
85
+ } on AtDecryptionException catch (e) {
86
+ _logger.severe (
87
+ 'decryption exception during of key: ${atKey .key }. Reason: ${e .toString ()}' );
88
+ rethrow ;
79
89
}
80
- return decryptedValue ;
90
+ return decryptionResultFromAtChops.result ;
81
91
}
82
92
83
93
Future <String > _getEncryptedSharedKey (AtKey atKey) async {
84
94
String ? encryptedSharedKey = '' ;
85
95
var localLookupSharedKeyBuilder = LLookupVerbBuilder ()
86
96
..atKey = AtConstants .atEncryptionSharedKey
87
- ..sharedWith = atClient .getCurrentAtSign ()
97
+ ..sharedWith = _atClient .getCurrentAtSign ()
88
98
..sharedBy = atKey.sharedBy
89
99
..isCached = true ;
90
100
try {
91
- encryptedSharedKey = await atClient
101
+ encryptedSharedKey = await _atClient
92
102
.getLocalSecondary ()!
93
103
.executeVerb (localLookupSharedKeyBuilder);
94
104
} on KeyNotFoundException {
@@ -102,7 +112,7 @@ class SharedKeyDecryption implements AtKeyDecryption {
102
112
..atKey = AtConstants .atEncryptionSharedKey
103
113
..sharedBy = atKey.sharedBy
104
114
..auth = true ;
105
- encryptedSharedKey = await atClient
115
+ encryptedSharedKey = await _atClient
106
116
.getRemoteSecondary ()!
107
117
.executeVerb (sharedKeyLookUpBuilder);
108
118
encryptedSharedKey =
0 commit comments