forked from postgres/postgres
-
Notifications
You must be signed in to change notification settings - Fork 14
PG-1923 WIP 256-bit encryption (take 2) #587
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
dAdAbird
wants to merge
2
commits into
percona:TDE_REL_17_STABLE
Choose a base branch
from
dAdAbird:32_byte_keys_take2
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.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,8 +22,11 @@ | |
#include "pg_tde_fe.h" | ||
#endif | ||
|
||
#define PG_TDE_WAL_KEY_FILE_MAGIC 0x014B4557 /* version ID value = WEK 01 */ | ||
#define PG_TDE_WAL_KEY_FILE_NAME "wal_keys" | ||
#define PG_TDE_WAL_KEY_FILE_MAGIC_OLD 0x014B4557 /* old version ID value = WEK 01 */ | ||
#define PG_TDE_WAL_KEY_FILE_NAME_OLD "wal_keys" | ||
|
||
#define PG_TDE_WAL_KEY_FILE_MAGIC 0x024B4557 /* version ID value = WEK 02 */ | ||
#define PG_TDE_WAL_KEY_FILE_NAME "wal_keys_v2" | ||
|
||
typedef struct WalKeyFileHeader | ||
{ | ||
|
@@ -40,12 +43,12 @@ typedef struct WalKeyFileHeader | |
* encrypting/decrypting existing keys from the key files, so any changes here | ||
* might break existing clusters. | ||
*/ | ||
typedef struct WalKeyFileEntry | ||
typedef struct WalKeyFileEntryOld | ||
{ | ||
uint32 _unused1; /* Part of AAD, is 1 or 2 in existing entries */ | ||
uint32 _unused2; /* Part of AAD */ | ||
|
||
uint8 encrypted_key_data[INTERNAL_KEY_LEN]; | ||
uint8 encrypted_key_data[INTERNAL_KEY_OLD_LEN]; | ||
uint8 key_base_iv[INTERNAL_KEY_IV_LEN]; | ||
|
||
uint32 range_type; /* WalEncryptionRangeType */ | ||
|
@@ -55,6 +58,20 @@ typedef struct WalKeyFileEntry | |
/* IV and tag used when encrypting the key itself */ | ||
unsigned char entry_iv[MAP_ENTRY_IV_SIZE]; | ||
unsigned char aead_tag[MAP_ENTRY_AEAD_TAG_SIZE]; | ||
} WalKeyFileEntryOld; | ||
|
||
typedef struct WalKeyFileEntry | ||
{ | ||
uint32 key_len; | ||
uint32 range_type; /* WalEncryptionRangeType */ | ||
WalLocation range_start; | ||
|
||
/* IV and tag used when encrypting the key itself */ | ||
unsigned char entry_iv[MAP_ENTRY_IV_SIZE]; | ||
unsigned char aead_tag[MAP_ENTRY_AEAD_TAG_SIZE]; | ||
|
||
uint8 key_base_iv[INTERNAL_KEY_IV_LEN]; | ||
uint8 encrypted_key_data[INTERNAL_KEY_MAX_LEN]; | ||
} WalKeyFileEntry; | ||
|
||
static WALKeyCacheRec *tde_wal_key_cache = NULL; | ||
|
@@ -178,7 +195,7 @@ pg_tde_wal_last_range_set_location(WalLocation loc) | |
* with the actual lsn by the first WAL write. | ||
*/ | ||
void | ||
pg_tde_create_wal_range(WalEncryptionRange *range, WalEncryptionRangeType type) | ||
pg_tde_create_wal_range(WalEncryptionRange *range, WalEncryptionRangeType type, int key_len) | ||
{ | ||
TDEPrincipalKey *principal_key; | ||
|
||
|
@@ -199,7 +216,7 @@ pg_tde_create_wal_range(WalEncryptionRange *range, WalEncryptionRangeType type) | |
range->end.lsn = MaxXLogRecPtr; | ||
range->end.tli = MaxTimeLineID; | ||
|
||
pg_tde_generate_internal_key(&range->key); | ||
pg_tde_generate_internal_key(&range->key, key_len); | ||
|
||
pg_tde_write_wal_key_file_entry(range, principal_key); | ||
|
||
|
@@ -615,12 +632,13 @@ pg_tde_wal_range_from_entry(const TDEPrincipalKey *principal_key, WalKeyFileEntr | |
range->start = entry->range_start; | ||
range->end.tli = MaxTimeLineID; | ||
range->end.lsn = MaxXLogRecPtr; | ||
range->key.key_len = entry->key_len; | ||
|
||
memcpy(range->key.base_iv, entry->key_base_iv, INTERNAL_KEY_IV_LEN); | ||
if (!AesGcmDecrypt(principal_key->keyData, | ||
if (!AesGcmDecrypt(principal_key->keyData, principal_key->keyLength, | ||
entry->entry_iv, MAP_ENTRY_IV_SIZE, | ||
(unsigned char *) entry, offsetof(WalKeyFileEntry, encrypted_key_data), | ||
entry->encrypted_key_data, INTERNAL_KEY_LEN, | ||
(unsigned char *) entry, offsetof(WalKeyFileEntry, range_start), | ||
entry->encrypted_key_data, entry->key_len, | ||
range->key.key, | ||
entry->aead_tag, MAP_ENTRY_AEAD_TAG_SIZE)) | ||
ereport(ERROR, | ||
|
@@ -664,13 +682,7 @@ pg_tde_initialize_wal_key_file_entry(WalKeyFileEntry *entry, | |
|
||
memset(entry, 0, sizeof(WalKeyFileEntry)); | ||
|
||
/* | ||
* We set this field here so that existing file entries will be consistent | ||
* and future use of this field easier. Some existing entries will have 2 | ||
* here. | ||
*/ | ||
entry->_unused1 = 1; | ||
|
||
entry->key_len = range->key.key_len; | ||
entry->range_type = range->type; | ||
entry->range_start = range->start; | ||
memcpy(entry->key_base_iv, range->key.base_iv, INTERNAL_KEY_IV_LEN); | ||
|
@@ -680,10 +692,10 @@ pg_tde_initialize_wal_key_file_entry(WalKeyFileEntry *entry, | |
errcode(ERRCODE_INTERNAL_ERROR), | ||
errmsg("could not generate iv for wal key file entry: %s", ERR_error_string(ERR_get_error(), NULL))); | ||
|
||
AesGcmEncrypt(principal_key->keyData, | ||
AesGcmEncrypt(principal_key->keyData, principal_key->keyLength, | ||
entry->entry_iv, MAP_ENTRY_IV_SIZE, | ||
(unsigned char *) entry, offsetof(WalKeyFileEntry, encrypted_key_data), | ||
range->key.key, INTERNAL_KEY_LEN, | ||
(unsigned char *) entry, offsetof(WalKeyFileEntry, range_start), | ||
range->key.key, range->key.key_len, | ||
entry->encrypted_key_data, | ||
entry->aead_tag, MAP_ENTRY_AEAD_TAG_SIZE); | ||
} | ||
|
@@ -834,11 +846,34 @@ pg_tde_get_server_key_info(void) | |
*/ | ||
fd = pg_tde_open_wal_key_file_basic(get_wal_key_file_path(), O_RDONLY, true); | ||
|
||
/* The file does not exist. */ | ||
if (fd < 0) | ||
return NULL; | ||
if (fd >= 0) | ||
pg_tde_wal_key_file_header_read(get_wal_key_file_path(), fd, &fheader, &bytes_read); | ||
else | ||
{ | ||
/* | ||
* TODO: An ugly hack for now, we need to get a key info when rewriting | ||
* an old file... | ||
*/ | ||
char old_wal_key_file_path[MAXPGPATH] = {0}; | ||
|
||
join_path_components(old_wal_key_file_path, pg_tde_get_data_dir(), PG_TDE_WAL_KEY_FILE_NAME_OLD); | ||
|
||
fd = pg_tde_open_wal_key_file_basic(old_wal_key_file_path, O_RDONLY, true); | ||
|
||
/* The file does not exist */ | ||
if (fd < 0) | ||
return NULL; | ||
|
||
pg_tde_wal_key_file_header_read(get_wal_key_file_path(), fd, &fheader, &bytes_read); | ||
bytes_read = pg_pread(fd, &fheader, sizeof(WalKeyFileHeader), 0); | ||
|
||
if (bytes_read > 0 && (bytes_read != sizeof(WalKeyFileHeader) | ||
|| fheader.file_version != PG_TDE_WAL_KEY_FILE_MAGIC_OLD)) | ||
{ | ||
ereport(FATAL, | ||
errcode_for_file_access(), | ||
errmsg("old WAL key file \"%s\" is corrupted: %m", old_wal_key_file_path)); | ||
} | ||
} | ||
|
||
CloseTransientFile(fd); | ||
|
||
|
@@ -894,4 +929,173 @@ pg_tde_delete_server_key(void) | |
/* Remove whole key map file */ | ||
durable_unlink(get_wal_key_file_path(), ERROR); | ||
} | ||
|
||
/* | ||
* Functions for rewriting old wal_keys into a new format file. | ||
* | ||
* TODO: The old format should be deprecated. And this code should be removed | ||
* eventually. | ||
*/ | ||
static int | ||
pg_tde_open_old_wal_key_file_read(const char *filename, | ||
bool ignore_missing, | ||
off_t *curr_pos) | ||
{ | ||
int fd; | ||
WalKeyFileHeader fheader; | ||
off_t bytes_read = 0; | ||
|
||
Assert(LWLockHeldByMeInMode(tde_lwlock_enc_keys(), LW_SHARED) || | ||
LWLockHeldByMeInMode(tde_lwlock_enc_keys(), LW_EXCLUSIVE)); | ||
|
||
fd = pg_tde_open_wal_key_file_basic(filename, O_RDONLY | PG_BINARY, ignore_missing); | ||
if (ignore_missing && fd < 0) | ||
return fd; | ||
|
||
bytes_read = pg_pread(fd, &fheader, sizeof(WalKeyFileHeader), 0); | ||
|
||
/* File is empty */ | ||
if (bytes_read == 0) | ||
return fd; | ||
|
||
if (bytes_read != sizeof(WalKeyFileHeader) | ||
|| fheader.file_version != PG_TDE_WAL_KEY_FILE_MAGIC_OLD) | ||
{ | ||
ereport(FATAL, | ||
errcode_for_file_access(), | ||
errmsg("old WAL key file \"%s\" is corrupted: %m", filename)); | ||
} | ||
*curr_pos = bytes_read; | ||
|
||
return fd; | ||
} | ||
|
||
static bool | ||
pg_tde_read_one_wal_key_file_old_entry(int fd, | ||
WalKeyFileEntryOld *entry, | ||
off_t *offset) | ||
{ | ||
off_t bytes_read = 0; | ||
|
||
Assert(entry); | ||
Assert(offset); | ||
|
||
bytes_read = pg_pread(fd, entry, sizeof(WalKeyFileEntryOld), *offset); | ||
|
||
/* We've reached the end of the file. */ | ||
if (bytes_read != sizeof(WalKeyFileEntryOld)) | ||
return false; | ||
|
||
*offset += bytes_read; | ||
|
||
return true; | ||
} | ||
|
||
static WalEncryptionRange * | ||
pg_tde_wal_range_from_old_entry(const TDEPrincipalKey *principal_key, WalKeyFileEntryOld *entry) | ||
{ | ||
WalEncryptionRange *range = tde_wal_prealloc_range == NULL ? palloc0_object(WalEncryptionRange) : tde_wal_prealloc_range; | ||
|
||
tde_wal_prealloc_range = NULL; | ||
|
||
Assert(principal_key); | ||
|
||
range->type = entry->range_type; | ||
range->start = entry->range_start; | ||
range->end.tli = MaxTimeLineID; | ||
range->end.lsn = MaxXLogRecPtr; | ||
range->key.key_len = INTERNAL_KEY_OLD_LEN; | ||
|
||
memcpy(range->key.base_iv, entry->key_base_iv, INTERNAL_KEY_IV_LEN); | ||
if (!AesGcmDecrypt(principal_key->keyData, principal_key->keyLength, | ||
entry->entry_iv, MAP_ENTRY_IV_SIZE, | ||
(unsigned char *) entry, offsetof(WalKeyFileEntryOld, encrypted_key_data), | ||
entry->encrypted_key_data, INTERNAL_KEY_OLD_LEN, | ||
range->key.key, | ||
entry->aead_tag, MAP_ENTRY_AEAD_TAG_SIZE)) | ||
ereport(ERROR, | ||
errmsg("Failed to decrypt key, incorrect principal key or corrupted key file %u", principal_key->keyLength)); | ||
|
||
return range; | ||
} | ||
|
||
void | ||
pg_tde_update_wal_keys_file(void) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think something like |
||
{ | ||
DIR *dir; | ||
LWLock *lock_pk = tde_lwlock_enc_keys(); | ||
struct dirent *file; | ||
TDEPrincipalKey *principal_key = NULL; | ||
TDESignedPrincipalKeyInfo signed_key_info; | ||
|
||
/* | ||
* No real need in lock here as the func should be called only on the server | ||
* start, but GetPrincipalKey() expects lock. | ||
*/ | ||
LWLockAcquire(lock_pk, LW_EXCLUSIVE); | ||
|
||
/* | ||
* No need to loop through readdir for wal_keys obviously, but it's rather | ||
* to demo how it'll work for smgr _keys files... | ||
*/ | ||
dir = opendir(pg_tde_get_data_dir()); | ||
if (dir == NULL && errno != ENOENT) | ||
elog(ERROR, "could not open directory \"%s\": %m", | ||
pg_tde_get_data_dir()); | ||
|
||
while (errno = 0, (file = readdir(dir)) != NULL) | ||
{ | ||
if (strcmp(file->d_name, PG_TDE_WAL_KEY_FILE_NAME_OLD) == 0) | ||
{ | ||
char wal_key_file_path[MAXPGPATH] = {0}; | ||
off_t read_pos, | ||
write_pos; | ||
int old_fd, | ||
new_fd; | ||
|
||
join_path_components(wal_key_file_path, pg_tde_get_data_dir(), PG_TDE_WAL_KEY_FILE_NAME_OLD); | ||
|
||
old_fd = pg_tde_open_old_wal_key_file_read(wal_key_file_path, false, &read_pos); | ||
|
||
/* | ||
* The old file exists and it's not empty, hece a principal key | ||
* should exist as well. | ||
*/ | ||
if (principal_key == NULL) | ||
{ | ||
principal_key = GetPrincipalKey(GLOBAL_DATA_TDE_OID, LW_EXCLUSIVE); | ||
if (principal_key == NULL) | ||
{ | ||
ereport(ERROR, | ||
errmsg("could not get server principal key"), | ||
errdetail("Failed to updated format of WAL keys.")); | ||
} | ||
pg_tde_sign_principal_key_info(&signed_key_info, principal_key); | ||
} | ||
new_fd = pg_tde_open_wal_key_file_write(get_wal_key_file_path(), &signed_key_info, true, &write_pos); | ||
|
||
while (1) | ||
{ | ||
WalKeyFileEntryOld old_entry; | ||
WalKeyFileEntry new_entry; | ||
WalEncryptionRange *range; | ||
|
||
if (!pg_tde_read_one_wal_key_file_old_entry(old_fd, &old_entry, &read_pos)) | ||
break; | ||
|
||
range = pg_tde_wal_range_from_old_entry(principal_key, &old_entry); | ||
pg_tde_initialize_wal_key_file_entry(&new_entry, principal_key, range); | ||
pg_tde_write_one_wal_key_file_entry(new_fd, &new_entry, &write_pos, get_wal_key_file_path()); | ||
pfree(range); | ||
} | ||
|
||
CloseTransientFile(old_fd); | ||
CloseTransientFile(new_fd); | ||
durable_unlink(wal_key_file_path, ERROR); | ||
} | ||
} | ||
|
||
closedir(dir); | ||
LWLockRelease(lock_pk); | ||
} | ||
#endif |
Oops, something went wrong.
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.
I wonder if it's worth to store cipher type here as well for future needs?
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.
Yeah, good point.