Skip to content

Commit e9b1900

Browse files
authored
Merge pull request #1476 from atsign-foundation/fix/apkam-non-zero-iv
fix: Updated APKAM enrollment to use server generated IVs
2 parents 4cd1cf7 + 3df4ebf commit e9b1900

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

packages/at_client_mobile/lib/src/auth/at_auth_service_impl.dart

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,7 @@ class AtAuthServiceImpl implements AtAuthService {
449449
var privateKeyCommand =
450450
'keys:get:keyName:$enrollmentIdFromServer.${AtConstants.defaultEncryptionPrivateKey}.__manage$_atSign\n';
451451
String encryptionPrivateKeyFromServer;
452+
String? encryptionPrivateKeyIV;
452453
try {
453454
var getPrivateKeyResult =
454455
await _atLookUp?.executeCommand('$privateKeyCommand\n', auth: true);
@@ -458,13 +459,17 @@ class AtAuthServiceImpl implements AtAuthService {
458459
getPrivateKeyResult = getPrivateKeyResult.replaceFirst('data:', '');
459460
var privateKeyResultJson = jsonDecode(getPrivateKeyResult);
460461
encryptionPrivateKeyFromServer = privateKeyResultJson['value'];
462+
encryptionPrivateKeyIV = privateKeyResultJson['iv'];
461463
} on Exception catch (e) {
462464
throw AtEnrollmentException(
463465
'Exception while getting encrypted private key/self key from server: $e');
464466
}
465467
AtEncryptionResult? atEncryptionResult = atChops.decryptString(
466468
encryptionPrivateKeyFromServer, EncryptionKeyType.aes256,
467-
keyName: 'apkamSymmetricKey', iv: AtChopsUtil.generateIVLegacy());
469+
keyName: 'apkamSymmetricKey',
470+
iv: encryptionPrivateKeyIV != null
471+
? InitialisationVector(base64Decode(encryptionPrivateKeyIV))
472+
: AtChopsUtil.generateIVLegacy());
468473
return atEncryptionResult.result;
469474
}
470475

@@ -476,6 +481,7 @@ class AtAuthServiceImpl implements AtAuthService {
476481
var selfEncryptionKeyCommand =
477482
'keys:get:keyName:$enrollmentIdFromServer.${AtConstants.defaultSelfEncryptionKey}.__manage$_atSign\n';
478483
String selfEncryptionKeyFromServer;
484+
String? selfEncryptionKeyIV;
479485
try {
480486
String? encryptedSelfEncryptionKey = await _atLookUp
481487
?.executeCommand('$selfEncryptionKeyCommand\n', auth: true);
@@ -488,13 +494,17 @@ class AtAuthServiceImpl implements AtAuthService {
488494
encryptedSelfEncryptionKey.replaceFirst('data:', '');
489495
var selfEncryptionKeyResultJson = jsonDecode(encryptedSelfEncryptionKey);
490496
selfEncryptionKeyFromServer = selfEncryptionKeyResultJson['value'];
497+
selfEncryptionKeyIV = selfEncryptionKeyResultJson['iv'];
491498
} on Exception catch (e) {
492499
throw AtEnrollmentException(
493500
'Exception while getting encrypted private key/self key from server: $e');
494501
}
495502
AtEncryptionResult? atEncryptionResult = atChops.decryptString(
496503
selfEncryptionKeyFromServer, EncryptionKeyType.aes256,
497-
keyName: 'apkamSymmetricKey', iv: AtChopsUtil.generateIVLegacy());
504+
keyName: 'apkamSymmetricKey',
505+
iv: selfEncryptionKeyIV != null
506+
? InitialisationVector(base64Decode(selfEncryptionKeyIV))
507+
: AtChopsUtil.generateIVLegacy());
498508
return atEncryptionResult.result;
499509
}
500510

0 commit comments

Comments
 (0)