20
20
#include "access/xlog_internal.h"
21
21
#include "access/xloginsert.h"
22
22
#include "utils/builtins.h"
23
- #include "utils/hsearch.h"
24
23
#include "miscadmin.h"
25
24
26
25
#include "access/pg_tde_tdemap.h"
27
26
#include "access/pg_tde_xlog.h"
28
27
#include "catalog/tde_global_space.h"
29
28
#include "catalog/tde_principal_key.h"
30
29
#include "encryption/enc_aes.h"
30
+ #include "encryption/enc_tde.h"
31
31
#include "keyring/keyring_api.h"
32
32
33
33
#include <openssl/rand.h>
@@ -66,24 +66,6 @@ typedef struct TDEFileHeader
66
66
TDESignedPrincipalKeyInfo signed_key_info ;
67
67
} TDEFileHeader ;
68
68
69
- typedef struct
70
- {
71
- RelFileLocator rel ;
72
- InternalKey key ;
73
- } TempRelKeyEntry ;
74
-
75
- #ifndef FRONTEND
76
-
77
- /* Arbitrarily picked small number of temporary relations */
78
- #define INIT_TEMP_RELS 16
79
-
80
- /*
81
- * Each backend has a hashtable that stores the keys for all temporary tables.
82
- */
83
- static HTAB * TempRelKeys = NULL ;
84
-
85
- #endif
86
-
87
69
static WALKeyCacheRec * tde_wal_key_cache = NULL ;
88
70
static WALKeyCacheRec * tde_wal_key_last_rec = NULL ;
89
71
@@ -98,138 +80,45 @@ static int pg_tde_open_file_read(const char *tde_filename, bool ignore_missing,
98
80
static WALKeyCacheRec * pg_tde_add_wal_key_to_cache (InternalKey * cached_key , XLogRecPtr start_lsn );
99
81
100
82
#ifndef FRONTEND
101
- static InternalKey * pg_tde_create_smgr_key_temp (const RelFileLocator * newrlocator );
102
- static InternalKey * pg_tde_create_smgr_key_perm (const RelFileLocator * newrlocator );
103
- static void pg_tde_generate_internal_key (InternalKey * int_key , TDEMapEntryType entry_type );
104
83
static int pg_tde_file_header_write (const char * tde_filename , int fd , const TDESignedPrincipalKeyInfo * signed_key_info , off_t * bytes_written );
105
84
static void pg_tde_sign_principal_key_info (TDESignedPrincipalKeyInfo * signed_key_info , const TDEPrincipalKey * principal_key );
106
85
static void pg_tde_write_one_map_entry (int fd , const TDEMapEntry * map_entry , off_t * offset , const char * db_map_path );
107
- static void pg_tde_write_key_map_entry (const RelFileLocator * rlocator , InternalKey * rel_key_data , TDEPrincipalKey * principal_key );
108
- static void pg_tde_free_key_map_entry (const RelFileLocator * rlocator );
86
+ static void pg_tde_write_key_map_entry (const RelFileLocator * rlocator , const InternalKey * rel_key_data , TDEPrincipalKey * principal_key );
109
87
static int keyrotation_init_file (const TDESignedPrincipalKeyInfo * signed_key_info , char * rotated_filename , const char * filename , off_t * curr_pos );
110
88
static void finalize_key_rotation (const char * path_old , const char * path_new );
111
89
static int pg_tde_open_file_write (const char * tde_filename , const TDESignedPrincipalKeyInfo * signed_key_info , bool truncate , off_t * curr_pos );
112
90
113
- InternalKey *
114
- pg_tde_create_smgr_key (const RelFileLocatorBackend * newrlocator )
115
- {
116
- if (RelFileLocatorBackendIsTemp (* newrlocator ))
117
- return pg_tde_create_smgr_key_temp (& newrlocator -> locator );
118
- else
119
- return pg_tde_create_smgr_key_perm (& newrlocator -> locator );
120
- }
121
-
122
- static InternalKey *
123
- pg_tde_create_smgr_key_temp (const RelFileLocator * newrlocator )
124
- {
125
- InternalKey * rel_key_data = palloc_object (InternalKey );
126
- TempRelKeyEntry * entry ;
127
- bool found ;
128
-
129
- pg_tde_generate_internal_key (rel_key_data , TDE_KEY_TYPE_SMGR );
130
-
131
- if (TempRelKeys == NULL )
132
- {
133
- HASHCTL ctl ;
134
-
135
- ctl .keysize = sizeof (RelFileLocator );
136
- ctl .entrysize = sizeof (TempRelKeyEntry );
137
- TempRelKeys = hash_create ("pg_tde temporary relation keys" ,
138
- INIT_TEMP_RELS ,
139
- & ctl ,
140
- HASH_ELEM | HASH_BLOBS );
141
- }
142
-
143
- entry = (TempRelKeyEntry * ) hash_search (TempRelKeys ,
144
- newrlocator ,
145
- HASH_ENTER , & found );
146
- Assert (!found );
147
-
148
- entry -> key = * rel_key_data ;
149
-
150
- return rel_key_data ;
151
- }
152
-
153
- static InternalKey *
154
- pg_tde_create_smgr_key_perm (const RelFileLocator * newrlocator )
91
+ void
92
+ pg_tde_save_smgr_key (RelFileLocator rel , const InternalKey * rel_key_data , bool write_xlog )
155
93
{
156
- InternalKey * rel_key_data = palloc_object (InternalKey );
157
94
TDEPrincipalKey * principal_key ;
158
95
LWLock * lock_pk = tde_lwlock_enc_keys ();
159
96
XLogRelKey xlrec = {
160
- .rlocator = * newrlocator ,
97
+ .rlocator = rel ,
161
98
};
162
99
163
- pg_tde_generate_internal_key (rel_key_data , TDE_KEY_TYPE_SMGR );
164
-
165
100
LWLockAcquire (lock_pk , LW_EXCLUSIVE );
166
- principal_key = GetPrincipalKey (newrlocator -> dbOid , LW_EXCLUSIVE );
101
+ principal_key = GetPrincipalKey (rel . dbOid , LW_EXCLUSIVE );
167
102
if (principal_key == NULL )
168
103
{
169
104
ereport (ERROR ,
170
105
errmsg ("principal key not configured" ),
171
106
errhint ("create one using pg_tde_set_key before using encrypted tables" ));
172
107
}
173
108
174
- pg_tde_write_key_map_entry (newrlocator , rel_key_data , principal_key );
109
+ pg_tde_write_key_map_entry (& rel , rel_key_data , principal_key );
175
110
LWLockRelease (lock_pk );
176
111
177
- /*
178
- * It is fine to write the to WAL after writing to the file since we have
179
- * not WAL logged the SMGR CREATE event either.
180
- */
181
- XLogBeginInsert ();
182
- XLogRegisterData ((char * ) & xlrec , sizeof (xlrec ));
183
- XLogInsert (RM_TDERMGR_ID , XLOG_TDE_ADD_RELATION_KEY );
184
-
185
- return rel_key_data ;
186
- }
187
-
188
- void
189
- pg_tde_create_smgr_key_perm_redo (const RelFileLocator * newrlocator )
190
- {
191
- InternalKey rel_key_data ;
192
- InternalKey * old_key ;
193
- TDEPrincipalKey * principal_key ;
194
- LWLock * lock_pk = tde_lwlock_enc_keys ();
195
-
196
- if ((old_key = pg_tde_get_key_from_file (newrlocator , TDE_KEY_TYPE_SMGR )))
197
- {
198
- pfree (old_key );
199
- return ;
200
- }
201
-
202
- pg_tde_generate_internal_key (& rel_key_data , TDE_KEY_TYPE_SMGR );
203
-
204
- LWLockAcquire (lock_pk , LW_EXCLUSIVE );
205
- principal_key = GetPrincipalKey (newrlocator -> dbOid , LW_EXCLUSIVE );
206
- if (principal_key == NULL )
112
+ if (write_xlog )
207
113
{
208
- ereport (ERROR ,
209
- errmsg ("principal key not configured" ),
210
- errhint ("create one using pg_tde_set_key before using encrypted tables" ));
114
+ /*
115
+ * It is fine to write the to WAL after writing to the file since we
116
+ * have not WAL logged the SMGR CREATE event either.
117
+ */
118
+ XLogBeginInsert ();
119
+ XLogRegisterData ((char * ) & xlrec , sizeof (xlrec ));
120
+ XLogInsert (RM_TDERMGR_ID , XLOG_TDE_ADD_RELATION_KEY );
211
121
}
212
-
213
- pg_tde_write_key_map_entry (newrlocator , & rel_key_data , principal_key );
214
- LWLockRelease (lock_pk );
215
- }
216
-
217
- static void
218
- pg_tde_generate_internal_key (InternalKey * int_key , TDEMapEntryType entry_type )
219
- {
220
- int_key -> type = entry_type ;
221
- int_key -> start_lsn = InvalidXLogRecPtr ;
222
-
223
- if (!RAND_bytes (int_key -> key , INTERNAL_KEY_LEN ))
224
- ereport (ERROR ,
225
- errcode (ERRCODE_INTERNAL_ERROR ),
226
- errmsg ("could not generate internal key: %s" ,
227
- ERR_error_string (ERR_get_error (), NULL )));
228
- if (!RAND_bytes (int_key -> base_iv , INTERNAL_KEY_IV_LEN ))
229
- ereport (ERROR ,
230
- errcode (ERRCODE_INTERNAL_ERROR ),
231
- errmsg ("could not generate IV: %s" ,
232
- ERR_error_string (ERR_get_error (), NULL )));
233
122
}
234
123
235
124
const char *
@@ -275,18 +164,6 @@ pg_tde_create_wal_key(InternalKey *rel_key_data, const RelFileLocator *newrlocat
275
164
LWLockRelease (tde_lwlock_enc_keys ());
276
165
}
277
166
278
- void
279
- DeleteSMGRRelationKey (RelFileLocatorBackend rel )
280
- {
281
- if (RelFileLocatorBackendIsTemp (rel ))
282
- {
283
- Assert (TempRelKeys );
284
- hash_search (TempRelKeys , & rel .locator , HASH_REMOVE , NULL );
285
- }
286
- else
287
- pg_tde_free_key_map_entry (& rel .locator );
288
- }
289
-
290
167
/*
291
168
* Deletes the key map file for a given database.
292
169
*/
@@ -463,7 +340,7 @@ pg_tde_write_one_map_entry(int fd, const TDEMapEntry *map_entry, off_t *offset,
463
340
* concurrent in place updates leading to data conflicts.
464
341
*/
465
342
void
466
- pg_tde_write_key_map_entry (const RelFileLocator * rlocator , InternalKey * rel_key_data , TDEPrincipalKey * principal_key )
343
+ pg_tde_write_key_map_entry (const RelFileLocator * rlocator , const InternalKey * rel_key_data , TDEPrincipalKey * principal_key )
467
344
{
468
345
char db_map_path [MAXPGPATH ];
469
346
int map_fd ;
@@ -518,16 +395,14 @@ pg_tde_write_key_map_entry(const RelFileLocator *rlocator, InternalKey *rel_key_
518
395
* This fucntion is called by the pg_tde SMGR when storage is unlinked on
519
396
* transaction commit/abort.
520
397
*/
521
- static void
522
- pg_tde_free_key_map_entry (const RelFileLocator * rlocator )
398
+ void
399
+ pg_tde_free_key_map_entry (const RelFileLocator rlocator )
523
400
{
524
401
char db_map_path [MAXPGPATH ];
525
402
File map_fd ;
526
403
off_t curr_pos = 0 ;
527
404
528
- Assert (rlocator );
529
-
530
- pg_tde_set_db_file_path (rlocator -> dbOid , db_map_path );
405
+ pg_tde_set_db_file_path (rlocator .dbOid , db_map_path );
531
406
532
407
LWLockAcquire (tde_lwlock_enc_keys (), LW_EXCLUSIVE );
533
408
@@ -542,7 +417,7 @@ pg_tde_free_key_map_entry(const RelFileLocator *rlocator)
542
417
if (!pg_tde_read_one_map_entry (map_fd , & map_entry , & curr_pos ))
543
418
break ;
544
419
545
- if (map_entry .type != MAP_ENTRY_EMPTY && map_entry .spcOid == rlocator -> spcOid && map_entry .relNumber == rlocator -> relNumber )
420
+ if (map_entry .type != MAP_ENTRY_EMPTY && map_entry .spcOid == rlocator . spcOid && map_entry .relNumber == rlocator . relNumber )
546
421
{
547
422
TDEMapEntry empty_map_entry = {
548
423
.type = MAP_ENTRY_EMPTY ,
@@ -1084,57 +959,27 @@ pg_tde_get_principal_key_info(Oid dbOid)
1084
959
return signed_key_info ;
1085
960
}
1086
961
1087
- static InternalKey *
1088
- pg_tde_get_temporary_rel_key (const RelFileLocator * rel )
1089
- {
1090
- #ifndef FRONTEND
1091
- TempRelKeyEntry * entry ;
1092
-
1093
- if (TempRelKeys == NULL )
1094
- return NULL ;
1095
-
1096
- entry = hash_search (TempRelKeys , rel , HASH_FIND , NULL );
1097
-
1098
- if (entry )
1099
- {
1100
- InternalKey * key = palloc_object (InternalKey );
1101
-
1102
- * key = entry -> key ;
1103
- return key ;
1104
- }
1105
- #endif
1106
-
1107
- return NULL ;
1108
- }
1109
-
1110
962
/*
1111
963
* Figures out whether a relation is encrypted or not, but without trying to
1112
964
* decrypt the key if it is.
1113
965
*/
1114
966
bool
1115
- IsSMGRRelationEncrypted ( RelFileLocatorBackend rel )
967
+ pg_tde_has_smgr_key ( RelFileLocator rel )
1116
968
{
1117
969
bool result ;
1118
970
TDEMapEntry map_entry ;
1119
971
char db_map_path [MAXPGPATH ];
1120
972
1121
- Assert (rel .locator .relNumber != InvalidRelFileNumber );
1122
-
1123
- if (RelFileLocatorBackendIsTemp (rel ))
1124
- #ifndef FRONTEND
1125
- return TempRelKeys && hash_search (TempRelKeys , & rel .locator , HASH_FIND , NULL );
1126
- #else
1127
- return false;
1128
- #endif
973
+ Assert (rel .relNumber != InvalidRelFileNumber );
1129
974
1130
- pg_tde_set_db_file_path (rel .locator . dbOid , db_map_path );
975
+ pg_tde_set_db_file_path (rel .dbOid , db_map_path );
1131
976
1132
977
if (access (db_map_path , F_OK ) == -1 )
1133
978
return false;
1134
979
1135
980
LWLockAcquire (tde_lwlock_enc_keys (), LW_SHARED );
1136
981
1137
- result = pg_tde_find_map_entry (& rel . locator , TDE_KEY_TYPE_SMGR , db_map_path , & map_entry );
982
+ result = pg_tde_find_map_entry (& rel , TDE_KEY_TYPE_SMGR , db_map_path , & map_entry );
1138
983
1139
984
LWLockRelease (tde_lwlock_enc_keys ());
1140
985
return result ;
@@ -1144,14 +989,11 @@ IsSMGRRelationEncrypted(RelFileLocatorBackend rel)
1144
989
* Returns TDE key for a given relation.
1145
990
*/
1146
991
InternalKey *
1147
- GetSMGRRelationKey ( RelFileLocatorBackend rel )
992
+ pg_tde_get_smgr_key ( RelFileLocator rel )
1148
993
{
1149
- Assert (rel .locator . relNumber != InvalidRelFileNumber );
994
+ Assert (rel .relNumber != InvalidRelFileNumber );
1150
995
1151
- if (RelFileLocatorBackendIsTemp (rel ))
1152
- return pg_tde_get_temporary_rel_key (& rel .locator );
1153
- else
1154
- return pg_tde_get_key_from_file (& rel .locator , TDE_KEY_TYPE_SMGR );
996
+ return pg_tde_get_key_from_file (& rel , TDE_KEY_TYPE_SMGR );
1155
997
}
1156
998
1157
999
/*
0 commit comments