Skip to content

CI4: Migrating encryption from CI3 to CI4 #3657

Closed

Description

@objecttothis

@opensourcepos/org thoughts?
The standard encryption cipher used in CI4 is aes-256-ctr. This is inherently more secure than CI3's aes-128-cbc, but that also means that as of CI4.3.1 it still cannot decrypt CI3 encrypted text. As of 4.3.0 there is supposed to be backward compatible, but it's incorrectly trying to decrypt the data with a aes-256-ctr cipher.

I have been able to import the CI3 encryption class as a library and it works to encrypt CI3 versions of the data. The solution here is not to continue using CI3 encryption.

Instead we need to migrate the encrypted data to CI4. To do this we need to create a migration that will pull down all the rows that are encrypted, decrypt them, then re-encrypt them using CI4 encryption and send them back to the database tables. A couple of things to think through:

I believe AES-128-CBC uses a 16 character key, though will accept a 32 character key (I think it just truncates it). We need to decide how to handle situations where the install has a 16 character key, as that will not be acceptable once the data has been decrypted.

IMO the most obvious solution here would be to pull down data, decrypt it, then generate a new key, copy the old key into the .env as a commented line and replace it with a new randomly generated 32 character key. We need to think through all the implications of this though. By copying the line in the .env and then commenting it out, they don't lose their old key in case it's needed.

Activity

  1. added this to the 3.4.0 milestone on Feb 18, 2023
  2. self-assigned this
    on Feb 18, 2023
  3. objecttothis commented on Feb 18, 2023

    @objecttothis
    MemberAuthor

    Additionally the key replacer could check the length of the current key and do nothing if it's already 32 characters.

  4. objecttothis commented on Feb 19, 2023

    @objecttothis
    MemberAuthor

    After further inspection it seems that pretty much everyone will need to have their encryption key upgraded. This is because the aes-128-cbc encryption algorithm we were using have a key length of 32 characters, but the native 256 bit aes-256-ctr encryption algorithm uses a 64 character key.

    With that I've added to the existing ci4 migration so that it

    1. Pulls CI3 encrypted data from the database (only existing in the app_config table)
    2. Upgrades or creates the key if it's less than 64 characters and comments out the old key (in case someone wants to keep it).
    3. Re-encrypts the data in CI4 format with the new encryption key.
    4. Saves that to the database in hex format.

    I'm still doing some testing on it. I need to make sure it works for new installs or existing.

  5. jekkos commented on Feb 20, 2023

    @jekkos
    Member

    good idea indeed to keep the old key around , in case something goes wrong. Probably a good test is to read the newly encrypted value back again and decrypt it, to see if it's the same.

  6. objecttothis commented on Feb 20, 2023

    @objecttothis
    MemberAuthor

    good idea indeed to keep the old key around , in case something goes wrong. Probably a good test is to read the newly encrypted value back again and decrypt it, to see if it's the same.

    I did this in my testing, but didn't leave it in the code once I was satisfied that it was encrypting, then able to decrypt it. That said, I could potentially add into the migration a check and if it doesn't encrypt/decrypt properly then don't upload it to the database, revert the encryption key and abort the migration.

  7. objecttothis commented on Feb 27, 2023

    @objecttothis
    MemberAuthor

    This is done.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions