How the Encryption Plugin for Firebird works
How it works: technical overview
Two plugins, one key and a simple rule: the key comes with each connection and is never stored on the server.
- DbCrypt encrypts pages
- KeyHolder manages keys
- AES-256 · key of 32 bytes
- 4-8% typical decrease of performance
Pages
What is encrypted
A Firebird database consists of pages. The plugin encrypts the pages with your data. Metadata is on data pages, so table names and procedure texts are encrypted too.
- EncryptedData pages, index pages, BLOB pages and generator pages.
- Not encryptedService pages and the database header. The header has the plugin name and the key name, but not the key.
- In the backgroundOne command starts encryption. A server thread encrypts the pages while users work.
- Checked at connectionIf the key value is wrong, the server sees it at once and refuses the connection.
SQL> ALTER DATABASE ENCRYPT WITH "DbCrypt" KEY Red; C:\> gstat -h employee.fdb Attributes encrypted, plugin DbCrypt Encryption key name: RED
Key exchange
The server keeps no key
A key file on the server is a bad idea: a person who steals the server gets the database and the key together. So each connection brings the key, and the server keeps it only in memory.
The key goes from the client plugin to the server plugin in encrypted form. The exchange is protected by RSA key pairs, which are built into the plugins.
- 1The receiver asksthe server plugin needs the key
- 2A random challengethe sender makes it for this exchange only
- 3A one-time key pairthe receiver signs it with its built-in key
- 4The sender checksa false plugin cannot make this signature
- 5✓ The key is sentencrypted with the one-time key
- Never in open formNeither in the network nor between the plugins inside the server.
- No replayA recorded exchange is useless: the next one has a new challenge and a new key pair.
- A false plugin gets nothingA changed
DbCryptor a false server cannot sign the answer. - One packageThe client library,
KeyHolderandDbCryptof one package trust each other.
Each type of program has its way
- Applications with source codeThe library
fbcrypt: three calls before the connection. Examples for Lazarus, Python and PHP. - Applications without source code
KeySource.dll, the built-in dialog or a pass phrase. Details.
- Java and .NETThe drivers send the key in a connection property. The server needs
UnsafeClient = trueandWireCrypt = Required. - Utilities and scriptsThe plugin
KeyHolderStdinreads the key from the input stream. The key is not in a file or in the command line.
Control
Know who uses the key
Individual client keys
Make a separate key for each workstation, branch or customer from one database key. The keys work only on a server with your license. The server writes to firebird.log which key connected.
Key check hook
Put your library KeyChecker near the plugin. It gets the key name and the full path of the program each time a key is used. Use it to write your own audit log.
Backups
The backup is encrypted too
The most frequent mistake: the database is encrypted, but the backup is near it in open form. The gbak utility gets decrypted data from the server. The plugin package contains a gbak that encrypts the backup file with the same key.
- 1gbak sends the keyfrom
KeySource.dllor from the input stream - 2The server reads pagesand decrypts them for the utility
- 3gbak encrypts the fileswitch
-KEYHOLDERin Firebird 4.0 and 5.0 - 4Checkthe file has no readable table names
- 5✓ Test restoreinto an encrypted database
- gbakEncrypted backup and restore into an encrypted database.
- nbackupCopies pages as they are: the copy stays encrypted with the same key.
- Ready scriptThe example script
50-backup.ps1makes the backup, checks it and does a test restore. - From your programThe Services API has the same options. The examples show a backup from Python.
Old backups can be read only with the key that encrypted them. Keep old keys while you need these backups.
Operations
Monitoring and key change
How far is encryption
SQL> select mon$crypt_page * 100.0 / mon$pages from mon$database; C:\> gstat -e employee.fdb Data pages: total 116, encrypted 116, non-crypted 0 Index pages: total 99, encrypted 99, non-crypted 0
Encryption is complete when the number of non-encrypted pages is zero. In Firebird 4.0 and 5.0, MON$CRYPT_STATE shows the state, and show database in isql prints it in words.
How to change the key
Decrypt the database and encrypt it with a new key, or make a backup and restore it with a new key. Give the new key a new name.
A lost key cannot be restored. Keep a copy of the key apart from the server and from the backups.
Compatibility
Versions and platforms
| Function | Firebird 3.0 | Firebird 4.0 | Firebird 5.0 |
|---|---|---|---|
| Database encryption, AES-256 | yes (3.0.3+) | yes | yes |
| KeySource.dll: no changes in the application | yes | yes | yes |
| Pass phrase instead of a key | yes | yes | yes |
Encrypted gbak backup | yes, automatic | switch -KEYHOLDER | switch -KEYHOLDER |
| Built-in key dialog | yes | yes | yes |
| Individual client keys, log of connections | no | no | yes |
Client library fbclient | from the plugin package or HQbird | package, HQbird or official | package, HQbird or official |
State in MON$CRYPT_STATE | no | yes | yes |
| OpenSSL | 1.1 | 1.1 | 3, or 1.1 |
| Windows x64 server, 32- and 64-bit clients | yes | yes | yes |
| Linux x64 | yes | yes | yes |
- HQbirdThe plugin files are a part of HQbird: server plugins, client files, utilities. You add the license file.
- Standard FirebirdAn installation script for Windows or Linux downloads the package, copies the files and runs the tests.
- License fileNecessary where the
DbCryptplugin works: the server, an embedded application folder, the folder ofgbak. - PerformanceEncryption decreases performance by about 4 to 8 percent. The exact number depends on the hardware and the settings.
| Language | Examples |
|---|---|
| Delphi, Lazarus, Free Pascal | console and multi-thread programs with SQLdb, an IBX application, FireDAC notes, KeySource templates |
| Python | firebird-driver with fbcrypt, with KeySource and with a callback; encryption with monitoring; backup through the Services API |
| Java | JDBC (Jaybird), HikariCP data source, Spring Boot, native client with fbcrypt or KeySource |
| .NET | ADO.NET, connection pool, Entity Framework Core |
| PHP | PDO with fbcrypt through FFI, PDO with KeySource |
| C++ | KeySource and KeyChecker libraries |
| All of them | backup with gbak and the key from the input stream |