crypt = C:\Temp\EMPLOYEE30\EMPLOYEE30.FDB { KeyHolderPlugin = KeyHolder }Also, you can declare KeyHolder plugin for all databases at the server, for this add the following parameter to firebird.conf:
KeyHolderPlugin = KeyHolderor, simply copy firebird.conf at step 2 (see below)
isql localhost:C:\Temp\EMPLOYEE30\EMPLOYEE30.FDB -user SYSDBA -pass masterkey SQL>alter database encrypt with dbcrypt key red; SQL> show database; Database: localhost:C:\Temp\EMPLOYEE30\EMPLOYEE30.FDB Owner: ADMINISTRATOR PAGE_SIZE 8192 Number of DB pages allocated = 326 Number of DB pages used = 301 Number of DB pages free = 25 Sweep interval = 20000 Forced Writes are OFF Transaction - oldest = 2881 Transaction - oldest active = 2905 Transaction - oldest snapshot = 2905 Transaction - Next = 2909 ODS = 12.0 Database encrypted Default Character set: NONE
alter database encrypt with dbcrypt key red;
After that, the database is encrypted with server-side authentication: the keys are located in the file KeyHolder.conf.
In the table below you can see files we have on the server to enable the encryption, and what we need on the client-side:
On the server's side (Firebird-3.0.3.32900-0_x64) | On the client's side |
---|---|
Mandatory files: | Mandatory files: |
plugins/dbcrypt.dll | fbclient.dll |
plugins/keyholder.dll | fbcrypt.dll |
DbCrypt.conf | libcrypto -1_1.dll |
libssl-1_1-x64.dll | Optional files: |
libcrypto-1_1-x64.dll | firebird.conf |
fbcrypt.dll | |
Files for gbak with encryption: | |
gbak.exe | |
firebird.msg | |
Optional files: | |
plugins/KeyHolder.conf (for initial encryption in development mode) | |
firebird.conf (contains parameter to set encryption plugin) |
To imitate such an environment, we need to remove (or simply rename) the file with keys (KeyHolder.conf) from the folder plugins.
Without KeyHolder.conf, the encryption plugin will require receiving the key from the connected application. The example of such an application is included in ExampleApplications – there is a compiled version and full sources for it on Delphi XE8. There are also examples for Lazarus, .NET, and PHP.
The code to initialize an encrypted connection is very simple – before the usual connection, several calls should be done to send an appropriate key. After that, the client application works with Firebird as usual.
Run the demo application to test the work with the encrypted database, it is in the folder \Example_Delphi_EnhancedCryptTestClient\Win32\Debug.
Do the following steps:
Please note: the test application can connect to the encrypted database only through TCP/IP, xnet is not supported.
In the example of the client application, all database operations (connection, transaction start, transaction's commit, query start, etc) are made in a very straightforward way to demonstrate all steps of the operation against the encrypted database. You can use this code as an example for the implementation of encryption in your applications.
We all know how important backup and restore for the database health and performance, so, in order to perform backup and restore for the encrypted databases, we have developed gbak.exe with the encryption support, and included it into the FEPF.
It is important to say, that this gbak.exe produces the encrypted backup file: it encrypts the backup with the same key as for the database encryption.
If you run gbak.exe from the plugin files with the switch -?, you will see the new parameters of gbak.exe, which are used to work with the encrypted databases:
-KEYFILE name of a file with DB and backup crypt key(s) -KEYNAME name of a key to be used for encryption -KEY key value in "0x5A," notationLet's consider how to use gbak.exe with encrypted databases and backups.
Example of backup with the encryption key in the key file:
gbak.exe -b -KEYFILE h:\Firebird\Firebird-3.0.3.32900-0_Win32\examplekeyfile.txt -KEYNAME RED localhost:h:\employee_30.fdb h:\testenc4.fbk -user SYSDBA -pass masterkeyHere, in the parameter -KEYFILE we specify the location of the files with keys, and in -KEYNAME - the name of the key being used. Please note, that the file examplekeyfile.txt has the same structure as KeyHolder.conf.
Example of backup with the explicit key:
gbak -b -KEY 0xec,0xa1,0x52,0xf6,0x4d,0x27,0xda,0x93,0x53,0xe5,0x48,0x86,0xb9,0x7d,0xe2,0x8f,0x3b,0xfa,0xb7,0x91,0x22,0x5b,0x59,0x15,0x82,0x35,0xf5,0x30,0x1f,0x04,0xdc,0x75, -keyname RED localhost:h:\employee30\employee30.fdb h:\testenc303.fbk -user SYSDBA -pass masterkeyHere, we specify the key value in the parameter -KEY, and the name of the key in the parameter -KEYNAME. It is necessary to specify key name even if we supply the explicit key value.
See below examples of the restore commands:
Example of restore with the encryption key in the keyfile:
gbak -c -v -keyfile h:\Firebird\Firebird-3.0.3.32900-0_Win32\examplekeyfile.txt -keyname white h:\testenc4.fbk localhost:h:\employeeenc4.fdb -user SYSDBA -pass masterkey
Example of restore with the explicit key:
gbak -c -v -key 0xec,0xa1,0x52,0xf6,0x4d,0x27,0xda,0x93,0x53,0xe5,0x48,0x86,0xb9,0x7d,0xe2,0x8f,0x3b,0xfa,0xb7,0x91,0x22,0x5b,0x59,0x15,0x82,0x35,0xf5,0x30,0x1f,0x04,0xdc,0x75, -keyname RED h:\testenc4.fbk localhost:h:\employeeenc4.fdb -user SYSDBA -pass masterkey
If you restore from an unencrypted backup file with encryption keys (gbak -c -keyfile ... -keyname ...) , the restored database will be encrypted.