Skip to content

Commit

Permalink
FIXED upgrade Recrypt not working with changed cipher (#4245)
Browse files Browse the repository at this point in the history
* FIX legacy cipher change

* FIX Recrypt Custom fields column names

* FIX ReCrypt Clean un-needed code
  • Loading branch information
TonisOrmisson authored and snipe committed Oct 18, 2017
1 parent ed4aa7d commit a4eeff0
Showing 1 changed file with 13 additions and 17 deletions.
30 changes: 13 additions & 17 deletions app/Console/Commands/RecryptFromMcrypt.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public function handle()
// If not, we can try to use the current APP_KEY if looks like it's old
$legacy_key = env('LEGACY_APP_KEY');
$key_parts = explode(':', $legacy_key);
$legacy_cipher = env('LEGACY_CIPHER');
$errors = array();

if (!$legacy_key) {
Expand All @@ -60,6 +61,7 @@ public function handle()
if (strlen($legacy_key) == 32) {
$legacy_length_check = true;
} elseif (array_key_exists('1', $key_parts) && (strlen($key_parts[1])==44)) {
$legacy_key = base64_decode($key_parts[1],true);
$legacy_length_check = true;
} else {
$legacy_length_check = false;
Expand Down Expand Up @@ -91,13 +93,17 @@ public function handle()
}


$mcrypter = new McryptEncrypter($legacy_key);
if($legacy_cipher){
$mcrypter = new McryptEncrypter($legacy_key,$legacy_cipher);
}else{
$mcrypter = new McryptEncrypter($legacy_key);
}
$settings = Setting::getSettings();

if ($settings->ldap_password=='') {
$this->comment('INFO: No LDAP password found. Skipping... ');
}

/** @var CustomField[] $custom_fields */
$custom_fields = CustomField::where('field_encrypted','=', 1)->get();
$this->comment('INFO: Retrieving encrypted custom fields...');

Expand All @@ -110,32 +116,22 @@ public function handle()


// Get all assets with a value in any of the fields that were encrypted
/** @var Asset[] $assets */
$assets = $query->get();

$bar = $this->output->createProgressBar(count($assets));

foreach ($custom_fields as $encrypted_field) {

// Try to decrypt the payload using the legacy app key
try {
$decrypted_field = $mcrypter->decrypt($encrypted_field);
$this->comment($decrypted_field);
} catch (\Exception $e) {
$errors[] = ' - ERROR: Could not decrypt field ['.$encrypted_field->name.']: '.$e->getMessage();
}
$bar->advance();
}


foreach ($assets as $asset) {
foreach ($custom_fields as $encrypted_field) {
$columnName = $encrypted_field->db_column;

// Make sure the value isn't null
if ($asset->{$encrypted_field}!='') {
if ($asset->{$columnName}!='') {
// Try to decrypt the payload using the legacy app key
try {
$decrypted_field = $mcrypter->decrypt($asset->{$encrypted_field});
$asset->{$encrypted_field} = \Crypt::encrypt($decrypted_field);
$decrypted_field = $mcrypter->decrypt($asset->{$columnName});
$asset->{$columnName} = \Crypt::encrypt($decrypted_field);
$this->comment($decrypted_field);
} catch (\Exception $e) {
$errors[] = ' - ERROR: Could not decrypt field ['.$encrypted_field->name.']: '.$e->getMessage();
Expand Down

0 comments on commit a4eeff0

Please sign in to comment.