Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions contrib/pg_tde/src/access/pg_tde_tdemap.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ typedef struct TDEMapEntry
uint32 type; /* Part of AAD */
uint32 _unused3; /* Part of AAD */

uint8 encrypted_key_data[INTERNAL_KEY_LEN];
uint8 encrypted_key_data[INTERNAL_KEY_MAX_LEN];
uint8 key_base_iv[INTERNAL_KEY_IV_LEN];

uint32 _unused1; /* Will be 1 in existing files entries. */
Expand Down Expand Up @@ -380,7 +380,7 @@ pg_tde_sign_principal_key_info(TDESignedPrincipalKeyInfo *signed_key_info, const
errcode(ERRCODE_INTERNAL_ERROR),
errmsg("could not generate iv for key map: %s", ERR_error_string(ERR_get_error(), NULL)));

AesGcmEncrypt(principal_key->keyData,
AesGcmEncrypt(principal_key->keyData, principal_key->keyLength,
signed_key_info->sign_iv, MAP_ENTRY_IV_SIZE,
(unsigned char *) &signed_key_info->data, sizeof(signed_key_info->data),
NULL, 0,
Expand Down Expand Up @@ -409,10 +409,10 @@ pg_tde_initialize_map_entry(TDEMapEntry *map_entry, const TDEPrincipalKey *princ
errcode(ERRCODE_INTERNAL_ERROR),
errmsg("could not generate iv for key map: %s", ERR_error_string(ERR_get_error(), NULL)));

AesGcmEncrypt(principal_key->keyData,
AesGcmEncrypt(principal_key->keyData, principal_key->keyLength,
map_entry->entry_iv, MAP_ENTRY_IV_SIZE,
(unsigned char *) map_entry, offsetof(TDEMapEntry, encrypted_key_data),
rel_key_data->key, INTERNAL_KEY_LEN,
rel_key_data->key, INTERNAL_KEY_MAX_LEN,
map_entry->encrypted_key_data,
map_entry->aead_tag, MAP_ENTRY_AEAD_TAG_SIZE);
}
Expand Down Expand Up @@ -573,7 +573,7 @@ pg_tde_count_encryption_keys(Oid dbOid)
bool
pg_tde_verify_principal_key_info(TDESignedPrincipalKeyInfo *signed_key_info, const KeyData *principal_key_data)
{
return AesGcmDecrypt(principal_key_data->data,
return AesGcmDecrypt(principal_key_data->data, principal_key_data->len,
signed_key_info->sign_iv, MAP_ENTRY_IV_SIZE,
(unsigned char *) &signed_key_info->data, sizeof(signed_key_info->data),
NULL, 0,
Expand All @@ -588,10 +588,10 @@ tde_decrypt_rel_key(const TDEPrincipalKey *principal_key, TDEMapEntry *map_entry

Assert(principal_key);

if (!AesGcmDecrypt(principal_key->keyData,
if (!AesGcmDecrypt(principal_key->keyData, principal_key->keyLength,
map_entry->entry_iv, MAP_ENTRY_IV_SIZE,
(unsigned char *) map_entry, offsetof(TDEMapEntry, encrypted_key_data),
map_entry->encrypted_key_data, INTERNAL_KEY_LEN,
map_entry->encrypted_key_data, INTERNAL_KEY_MAX_LEN,
key->key,
map_entry->aead_tag, MAP_ENTRY_AEAD_TAG_SIZE))
ereport(ERROR,
Expand Down
250 changes: 227 additions & 23 deletions contrib/pg_tde/src/access/pg_tde_xlog_keys.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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 */
Expand All @@ -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;
Copy link
Collaborator

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?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, good point.

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;
Expand Down Expand Up @@ -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;

Expand All @@ -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);

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand All @@ -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);
}
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think something like migrate fits better to this function name rather than update

{
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
Loading
Loading