forked from postgres/postgres
-
Notifications
You must be signed in to change notification settings - Fork 10
Proof-of-concept: Protect decrypted relation keys #446
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
AndersAstrand
wants to merge
2
commits into
percona:TDE_REL_17_STABLE
Choose a base branch
from
AndersAstrand:tde/only-keep-encrypted-keys-in-relcache-poc
base: TDE_REL_17_STABLE
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+384
−89
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
#include "postgres.h" | ||
|
||
#include <openssl/crypto.h> | ||
#include <openssl/err.h> | ||
#include <openssl/rand.h> | ||
|
||
#include "utils/resowner.h" | ||
|
||
#include "encryption/enc_aes.h" | ||
#include "encryption/tde_keys.h" | ||
|
||
#ifdef FRONTEND | ||
#include "pg_tde_fe.h" | ||
#endif | ||
|
||
static void ResourceOwnerReleaseDecryptedTdeKey(Datum resource); | ||
static DecryptedTdeKey *tde_keys_alloc_decrypted_key(void); | ||
|
||
static const ResourceOwnerDesc tde_keys_resource_owner_desc = | ||
{ | ||
.name = "pg_tde decrypted key", | ||
.release_phase = RESOURCE_RELEASE_BEFORE_LOCKS, | ||
.release_priority = RELEASE_PRIO_FIRST, | ||
.ReleaseResource = ResourceOwnerReleaseDecryptedTdeKey, | ||
.DebugPrint = NULL, | ||
}; | ||
|
||
EncryptedTdeKey * | ||
tde_keys_encrypt_key(const DecryptedTdeKey *decrypted_key, | ||
const uint8 *encryption_key, | ||
const uint8 *additional_authentication_data, | ||
int additional_authentication_data_size) | ||
{ | ||
EncryptedTdeKey *encrypted_key = palloc0_object(EncryptedTdeKey); | ||
|
||
memcpy(encrypted_key->key_iv, decrypted_key->iv, TDE_KEY_IV_SIZE); | ||
|
||
if (!RAND_bytes(encrypted_key->iv, TDE_KEY_ENCRYPTION_IV_SIZE)) | ||
ereport(ERROR, | ||
errmsg("could not generate iv for key encryption: %s", | ||
ERR_error_string(ERR_get_error(), NULL))); | ||
|
||
AesGcmEncrypt(encryption_key, | ||
|
||
encrypted_key->iv, | ||
TDE_KEY_ENCRYPTION_IV_SIZE, | ||
|
||
additional_authentication_data, | ||
additional_authentication_data_size, | ||
|
||
decrypted_key->data, | ||
TDE_KEY_SIZE, | ||
|
||
encrypted_key->key_data, | ||
|
||
encrypted_key->aead_tag, | ||
TDE_KEY_ENCRYPTION_AEAD_TAG_SIZE); | ||
|
||
return encrypted_key; | ||
} | ||
|
||
DecryptedTdeKey * | ||
tde_keys_decrypt_key(EncryptedTdeKey *encrypted_key, | ||
const uint8 *decryption_key, | ||
const uint8 *additional_authentication_data, | ||
int additional_authentication_data_size) | ||
{ | ||
DecryptedTdeKey *decrypted_key; | ||
|
||
Assert(encrypted_key); | ||
Assert(decryption_key); | ||
|
||
decrypted_key = tde_keys_alloc_decrypted_key(); | ||
|
||
if (!AesGcmDecrypt(decryption_key, | ||
|
||
encrypted_key->iv, | ||
TDE_KEY_ENCRYPTION_IV_SIZE, | ||
|
||
additional_authentication_data, | ||
additional_authentication_data_size, | ||
|
||
encrypted_key->key_data, | ||
TDE_KEY_SIZE, | ||
|
||
decrypted_key->data, | ||
|
||
encrypted_key->aead_tag, | ||
TDE_KEY_ENCRYPTION_AEAD_TAG_SIZE)) | ||
{ | ||
tde_keys_free_decrypted_key(decrypted_key); | ||
return NULL; | ||
} | ||
|
||
memcpy(decrypted_key->iv, encrypted_key->key_iv, TDE_KEY_IV_SIZE); | ||
|
||
return decrypted_key; | ||
} | ||
|
||
void | ||
tde_keys_free_decrypted_key(DecryptedTdeKey *decrypted_key) | ||
{ | ||
Assert(decrypted_key->owner); | ||
|
||
ResourceOwnerForget(decrypted_key->owner, | ||
PointerGetDatum(decrypted_key), | ||
&tde_keys_resource_owner_desc); | ||
OPENSSL_secure_clear_free(decrypted_key, sizeof(DecryptedTdeKey)); | ||
} | ||
|
||
DecryptedTdeKey * | ||
tde_keys_generate_decrypted_key(void) | ||
{ | ||
DecryptedTdeKey *decrypted_key = tde_keys_alloc_decrypted_key(); | ||
|
||
if (!RAND_bytes(decrypted_key->data, TDE_KEY_SIZE)) | ||
ereport(ERROR, | ||
errmsg("could not generate key: %s", | ||
ERR_error_string(ERR_get_error(), NULL))); | ||
|
||
if (!RAND_bytes(decrypted_key->iv, TDE_KEY_IV_SIZE)) | ||
ereport(ERROR, | ||
errmsg("could not generate iv: %s", | ||
ERR_error_string(ERR_get_error(), NULL))); | ||
|
||
return decrypted_key; | ||
} | ||
|
||
static void | ||
ResourceOwnerReleaseDecryptedTdeKey(Datum resource) | ||
{ | ||
OPENSSL_secure_clear_free(DatumGetPointer(resource), sizeof(DecryptedTdeKey)); | ||
} | ||
|
||
static DecryptedTdeKey * | ||
tde_keys_alloc_decrypted_key(void) | ||
{ | ||
DecryptedTdeKey *decrypted_key; | ||
|
||
ResourceOwnerEnlarge(CurrentResourceOwner); | ||
|
||
decrypted_key = OPENSSL_secure_zalloc(sizeof(DecryptedTdeKey)); | ||
|
||
decrypted_key->owner = CurrentResourceOwner; | ||
ResourceOwnerRemember(decrypted_key->owner, | ||
PointerGetDatum(decrypted_key), | ||
&tde_keys_resource_owner_desc); | ||
|
||
return decrypted_key; | ||
} | ||
|
||
EncryptedTdeKey * | ||
tde_keys_new_encrypted_key(const uint8 *encryption_key, | ||
const uint8 *additional_authentication_data, | ||
int additional_authentication_data_size) | ||
{ | ||
DecryptedTdeKey *decrypted_key; | ||
EncryptedTdeKey *encrypted_key; | ||
|
||
decrypted_key = tde_keys_generate_decrypted_key(); | ||
encrypted_key = tde_keys_encrypt_key(decrypted_key, | ||
encryption_key, | ||
additional_authentication_data, | ||
additional_authentication_data_size); | ||
tde_keys_free_decrypted_key(decrypted_key); | ||
|
||
return encrypted_key; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The change to
uint8
seems very arbitrary. Doesn't PostgreSQL useunsigned char
everywhere for this?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That said I prefer
uint8
overunsigned char
, but that is not what I think we or PostgreSQL are using. So now you introduced a new way. Also it does seem you have not changedAesGcmDecrypt
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The changes in pg_tde_tdemap.c was just to support the way I calculated aad in the other file, so that rotations wouldn't break everything. They are very PoC.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The postgres code uses
uint8
about as commonly asunsigned char
. I picked the one I like :D. The type wasn't the point there at all but instead to use the full rlocator as aad.For WAL keys we currently use a fake rlocator that's the same for all of them so they need something else (probably start_lsn).
EDIT: excluding
contrib/
unsigned char
seems to be used about 66% of the time.