MySQL 9.2.0
Source Code Documentation
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
rpl_log_encryption.h
Go to the documentation of this file.
1/* Copyright (c) 2018, 2024, Oracle and/or its affiliates.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is designed to work with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24#ifndef RPL_LOG_ENCRYPTION_INCLUDED
25#define RPL_LOG_ENCRYPTION_INCLUDED
26
27#include <openssl/evp.h>
28#include <stdint.h>
29#include <map>
30#include <string>
31
32#include "my_inttypes.h"
33#include "mysys/stream_cipher.h"
34
35class Basic_istream;
36class Basic_ostream;
37class THD;
38
39/**
40 @file rpl_log_encryption.h
41
42 @brief This file includes the major components for encrypting/decrypting
43 binary log files.
44
45 * Replication logs
46
47 Here, replication logs includes both the binary and relay log files.
48
49 * File Level Encryption
50
51 - All standard binary log file data (including BINLOG_MAGIC) in replication
52 logs are encrypted.
53
54 - A replication log file is either encrypted or not (standard binary log
55 file). It is not possible that part of a log file is encrypted and part
56 of it is non-encrypted.
57
58 - There is an encryption header in the begin of each encrypted replication
59 log file.
60
61 <pre>
62 +--------------------+
63 | Encryption Header |
64 +--------------------+
65 | Encrypted Data |
66 +--------------------+
67 </pre>
68
69 The encrypted replication file header includes necessary information to
70 decrypt the encrypted data of the file (the standard binary log file
71 data). For detail, check Rpl_encryption_header class.
72
73 * Two Tier Keys
74
75 Replication logs are encrypted with two tier keys. A 'File Password' for
76 encrypting the standard binary log file data and a 'Replication Encryption
77 Key' for encrypting the 'File Password'.
78
79 - File password
80
81 Each replication log file has a password. A file key used to encrypt the
82 file is generated from the file password. The encrypted 'File Password'
83 is stored into encryption header of the file. For details, check
84 Rpl_encryption_header class.
85
86 - Replication encryption key
87
88 A replication encryption key is used to encrypt/decrypt the file
89 password stored in an encrypted replication file header. It is generated
90 by keyring and stored in/retrieved from keyring.
91*/
92#ifdef MYSQL_SERVER
93
94/**
95 The Rpl_encryption class is the container for the binlog encryption feature
96 generic and server instance functions.
97*/
99 public:
101 std::string m_id;
103 };
104
105 Rpl_encryption() = default;
110
111 enum class Keyring_status {
112 SUCCESS = 0,
114 KEY_NOT_FOUND = 2,
121 };
122 /**
123 A wrapper function to throw a binlog encryption keyring error.
124 The wrapper will decide if the error will be reported to the client session
125 or to the server error log according to current_thd.
126
127 @param error The Keyring_status to be reported.
128 */
130 /**
131 A wrapper function to throw a replication logs encryption keyring error,
132 reporting also the key ID.
133 The wrapper will decide if the error will be reported to the client session
134 or to the server error log according to current_thd.
135
136 @param error The Keyring_status to be reported.
137 @param key_id The key ID to appear in the error message.
138 */
139 static void report_keyring_error(Keyring_status error, const char *key_id);
140
141 /**
142 Replication encryption master key rotation process is recoverable. The
143 steps defined in the enum class below are the steps from which the rotation
144 process may continue after an unexpected interruption.
145 */
146 enum class Key_rotation_step {
147 START,
155 };
156
157 /**
158 Initialize the rpl_encryption instance. This initialization shall be called
159 after generating/loading the server UUID and before opening new binary and
160 relay log files for writing.
161
162 When the replication_logs_encrypt option is on at server startup, the
163 initialization process will try to recover master key and may generate
164 a new replication master key if needed.
165
166 @retval false Success.
167 @retval true Error.
168 */
169 bool initialize();
170 /**
171 Remove remaining old/new master key index in order to cleanup any previous
172 master key rotation.
173
174 @retval false Success.
175 @retval true Error.
176 */
178 /**
179 Recover the replication encryption master key from keyring.
180
181 The recovery of the master key process starts by trying to read the
182 replication master key information from keyring (the master key sequence
183 number, and the master key itself).
184
185 Then, if detected that a key rotation did not completed properly, tries to
186 continue the master key rotation.
187
188 When recovery is successful, the m_master_key_recovered flag is set true.
189
190 @retval false Success.
191 @retval true Error.
192 */
193 bool recover_master_key();
194 /**
195 Return the current replication encryption master key.
196
197 @return The current replication encryption master key.
198 */
199 const Rpl_encryption_key get_master_key();
200
201 /**
202 Get the key with given key ID. The key to be returned will be retrieved
203 from the keyring or from a cached copy in memory.
204
205 @param[in] key_id ID of the key to be returned.
206 @param[in] key_type Expected type of the key to be returned.
207
208 @return A pair containing the status of the operation (Keyring_status) and
209 a Key_string. Errors shall be checked by consulting the status.
210 */
211 static std::pair<Keyring_status, Key_string> get_key(
212 const std::string &key_id, const std::string &key_type);
213
214 /**
215 Get the key with given key ID. The key to be returned will be retrieved
216 from the keyring or from a cached copy in memory.
217
218 @param[in] key_id ID of the key to be returned.
219 @param[in] key_type Expected type of the key to be returned.
220 @param[in] key_size Expected size of the key to be returned.
221
222 @return A pair containing the status of the operation (Keyring_status) and
223 a Key_string. Errors shall be checked by consulting the status.
224 */
225 static std::pair<Keyring_status, Key_string> get_key(
226 const std::string &key_id, const std::string &key_type, size_t key_size);
227
228 /**
229 Enable binlog encryption option. It will generate a new global key if
230 there is no master key yet. Then rotate replication logs to make encryption
231 effective immediately.
232
233 Replication logs rotation errors don't fail, but they will throw a warning.
234
235 @param[in] thd the thd object of the session.
236
237 @retval false Success.
238 @retval true Error. If error happens when generating new key, it will fail.
239 */
240 bool enable(THD *thd);
241 /**
242 Disable binlog encryption option. It rotates replication logs to make
243 encryption ineffective immediately.
244
245 Replication logs rotation errors don't fail, but they will throw a warning.
246
247 @param[in] thd the thd object of the session.
248 */
249 void disable(THD *thd);
250 /**
251 Return is the replication logs encryption feature is enabled.
252
253 @retval false The feature is disabled.
254 @retval true The feature is enabled.
255 */
256 bool is_enabled();
257 const bool &get_enabled_var();
259 /**
260 Purge unused master keys from Keyring.
261
262 @retval false Success.
263 @retval true Error.
264 */
265 bool purge_unused_keys();
266 /**
267 Rotate the master key.
268
269 @param step Step to start the process (it might be recovering).
270 @param new_master_key_seqno When recovering, this is the new master key
271 sequence number detected by recovery process.
272 @retval false Success.
273 @retval true Error.
274 */
276 uint32_t new_master_key_seqno = 0);
277
278 private:
279 /* Define the keyring key type for keys storing sequence numbers */
280 static const char *SEQNO_KEY_TYPE;
281 /* Define the keyring key length for keys storing sequence numbers */
282 static const int SEQNO_KEY_LENGTH = 16;
283 /*
284 Sys_binlog_encryption uses m_enabled as the storage of global var
285 binlog_encryption.
286 */
287 bool m_enabled = false;
288 /*
289 Sys_binlog_rotate_encryption_master_key_at_startup uses
290 m_rotate_at_startup as the storage of global var
291 binlog_rotate_encryption_master_key_at_startup.
292 */
294#ifndef NDEBUG
295 /*
296 This variable is only used to assert that enable(), disable() and
297 get_master_key() functions are called only after initialize() was called.
298 */
299 bool m_initialized = false;
300#endif
301 /*
302 The replication logs encryption only needs to recover the current
303 replication master key if the binlog_encryption option is enabled.
304
305 This flag will be set true after a successful replication master key
306 recovery.
307 */
309 /* The sequence number of the replication master key. */
310 uint32_t m_master_key_seqno = 0;
311 /* The current replication master key */
313 /*
314 Flag to avoid double logs rotation when enabling the option and
315 recovering from master key rotation.
316 */
318
319 /**
320 Fetch a key from keyring. When error happens, it either reports an error to
321 user or write an error to log accordingly.
322
323 @param[in] key_id ID of the key to be returned.
324 @param[in] key_type Expected type of the key to be returned.
325
326 @return A tuple containing the status of the operation (Keyring_status), a
327 pointer to the fetched key (nullptr if the key was not fetched) and
328 the returned key size. Errors shall be checked by consulting the
329 status.
330 */
331 static std::tuple<Keyring_status, void *, size_t> fetch_key_from_keyring(
332 const std::string &key_id, const std::string &key_type);
333
334 /**
335 Rotate replication logs excluding relay logs of group replication channels.
336 If error happens, it will either report a warning to session user.
337
338 @param[in] thd The thd object of current session.
339 */
340 void rotate_logs(THD *thd);
341
342 /**
343 Get a sequence number from the keyring. The sequence number to be returned
344 will be extracted from the key retrieved from the keyring. No caching shall
345 be used for this function.
346
347 @param[in] key_id ID of the key to extract the sequence number from.
348
349 @return A pair containing the status of the operation (Keyring_status) and
350 a sequence number. Errors shall be checked by consulting the status.
351 */
352 std::pair<Rpl_encryption::Keyring_status, uint32_t> get_seqno_from_keyring(
353 std::string key_id);
354 /**
355 Set a sequence number into a key and store it into keyring.
356
357 @param[in] key_id ID of the key to set the sequence number.
358 @param[in] seqno The sequence number to be set.
359
360 @retval false Success.
361 @retval true Error.
362 */
363 bool set_seqno_on_keyring(std::string key_id, uint32_t seqno);
364 /**
365 Remove a key from the keyring.
366
367 @param[in] key_id ID of the key to be removed from keyring.
368
369 @retval false Success.
370 @retval true Error.
371 */
372 bool remove_key_from_keyring(std::string key_id);
373 /**
374 Returns the key ID of the keyring key that stores the master key sequence
375 number.
376
377 @return The key ID.
378 */
379 std::string get_master_key_seqno_key_id();
380 /**
381 Get the master key sequence number from keyring.
382
383 @return A pair containing the status of the operation (Keyring_status) and
384 a sequence number. Errors shall be checked by consulting the status.
385 */
386 std::pair<Rpl_encryption::Keyring_status, uint32_t>
388 /**
389 Set the master key sequence number into a key and store it into keyring.
390
391 @retval false Success.
392 @retval true Error.
393 */
395 /**
396 Remove the master key sequence number key from the keyring.
397
398 @retval false Success.
399 @retval true Error.
400 */
402 /**
403 Returns the key ID of the keyring key that stores the "new" master key
404 sequence number.
405
406 @return The key ID.
407 */
409 /**
410 Returns the key ID of the keyring key that stores the "last_purged"
411 master key sequence number.
412
413 @return The key ID.
414 */
416 /**
417 Returns the key ID of the keyring key that stores the "old" master key
418 sequence number.
419
420 @return The key ID.
421 */
423 /**
424 Get the "new" master key sequence number from keyring.
425
426 @return A pair containing the status of the operation (Keyring_status) and
427 a sequence number. Errors shall be checked by consulting the status.
428 */
429 std::pair<Rpl_encryption::Keyring_status, uint32_t>
431 /**
432 Get the "old" master key sequence number from keyring.
433
434 @return A pair containing the status of the operation (Keyring_status) and
435 a sequence number. Errors shall be checked by consulting the status.
436 */
437 std::pair<Rpl_encryption::Keyring_status, uint32_t>
439 /**
440 Get the "last_purged" master key sequence number from keyring.
441
442 @return A pair containing the status of the operation (Keyring_status) and
443 a sequence number. Errors shall be checked by consulting the status.
444 */
445 std::pair<Rpl_encryption::Keyring_status, uint32_t>
447 /**
448 Set the "new" master key sequence number into a key and store it into
449 keyring.
450
451 @retval false Success.
452 @retval true Error.
453 */
455 /**
456 Set the "last_purged" master key sequence number into a key and store it
457 into keyring.
458
459 @retval false Success.
460 @retval true Error.
461 */
463 /**
464 Set the "old" master key sequence number into a key and store it into
465 keyring.
466
467 @retval false Success.
468 @retval true Error.
469 */
471 /**
472 Remove the "new" master key sequence number key from the keyring.
473
474 @retval false Success.
475 @retval true Error.
476 */
478 /**
479 Remove the "last_purged" master key sequence number key from the keyring.
480
481 @retval false Success.
482 @retval true Error.
483 */
485 /**
486 Remove the "old" master key sequence number key from the keyring.
487
488 @retval false Success.
489 @retval true Error.
490 */
492 /**
493 Generate a new replication master key on keyring and retrieve it.
494
495 @param[in] seqno The sequence number of the master key.
496
497 @retval false Success.
498 @retval true Error.
499 */
501};
502
504#endif // MYSQL_SERVER
505
506/**
507 @class Rpl_encryption_header
508
509 This is the base class to serialize and deserialize a replication log file
510 encryption header.
511
512 The new encrypted binary log file format is composed of two parts:
513
514 <pre>
515 +---------------------+
516 | Encryption Header |
517 +---------------------+
518 | Encrypted Data |
519 +---------------------+
520 </pre>
521
522 The encryption header exists only in the begin of encrypted replication log
523 files.
524
525 <pre>
526 +------------------------+----------------------------------------------+
527 | MAGIC HEADER (4 bytes) | Replication logs encryption version (1 byte) |
528 +------------------------+----------------------------------------------+
529 | Version specific encryption header data |
530 +-----------------------------------------------------------------------+
531 Encryption Header Format
532 </pre>
533
534 <table>
535 <caption>Encryption Header Format</caption>
536 <tr>
537 <th>Name</th>
538 <th>Format</th>
539 <th>Description</th>
540 </tr>
541 <tr>
542 <td>Magic Header</td>
543 <td>4 Bytes</td>
544 <td>
545 The content is always 0xFD62696E. It is similar to Binlog Magic Header.
546 Binlog magic header is: 0xFE62696e.
547 </td>
548 <tr>
549 <td>Replication logs encryption version</td>
550 <td>1 Byte</td>
551 <td>
552 The replication logs encryption version defines how the header shall be
553 deserialized and how the Encrypted Data shall be decrypted.
554 </td>
555 </tr>
556 <tr>
557 <td>Version specific encryption data header</td>
558 <td>Depends on the version field</td>
559 <td>
560 Data required to fetch a replication key from keyring and deserialize
561 the Encrypted Data.
562 </td>
563 </tr>
564 </table>
565*/
567 public:
568 /* Same as BINLOG_MAGIC_SIZE */
569 static const int ENCRYPTION_MAGIC_SIZE = 4;
570 /* The magic for an encrypted replication log file */
571 static const char *ENCRYPTION_MAGIC;
572
573 virtual ~Rpl_encryption_header();
574
575 /**
576 Deserialize the replication encrypted log file header from the given stream.
577 This function shall be called right after reading the magic from the stream.
578 It will read the version of the encrypted log file header, instantiate a
579 proper Rpl_encryption_header based on version and delegate the rest of the
580 header deserialization to the new instance.
581
582 @param istream The stream containing the header to deserialize.
583
584 @return A Rpl_encryption_header on success or nullptr on failure.
585 */
586 static std::unique_ptr<Rpl_encryption_header> get_header(
587 Basic_istream *istream);
588 /**
589 Generate a new replication encryption header based on the default
590 replication encrypted log file header version.
591
592 @return A Rpl_encryption_header of default version.
593 */
594 static std::unique_ptr<Rpl_encryption_header> get_new_default_header();
595 /**
596 Serialize the header into an output stream.
597
598 @param ostream The output stream to serialize the header.
599
600 @retval false Success.
601 @retval true Error.
602 */
603 virtual bool serialize(Basic_ostream *ostream) = 0;
604 /**
605 Deserialize encryption header from a stream.
606
607 @param[in] istream The input stream for deserializing the encryption
608 header.
609
610 @retval false Success.
611 @retval true Error.
612 */
613 virtual bool deserialize(Basic_istream *istream) = 0;
614 /**
615 Get the header version.
616
617 @return The header version.
618 */
619 virtual char get_version() const = 0;
620 /**
621 Return the header size to be taken into account when serializing an
622 deserializing encrypted file headers from replication log files.
623
624 @return The size of the header for the header version.
625 */
626 virtual int get_header_size() = 0;
627 /**
628 Decrypt the file password.
629 */
631 /**
632 Factory to generate ciphers to encrypt streams based on current header.
633
634 @return A Stream_cipher for this header version or nullptr on failure.
635 */
636 virtual std::unique_ptr<Stream_cipher> get_encryptor() = 0;
637 /**
638 Factory to generate ciphers to decrypt streams based on current header.
639
640 @return A Stream_cipher for this header version or nullptr on failure.
641 */
642 virtual std::unique_ptr<Stream_cipher> get_decryptor() = 0;
643 /**
644 Setup the header with current master key and generates a new random file
645 password. This function shall be called when creating new replication
646 log files.
647
648 @return The new file password, or an empty password if error happens.
649 */
651#ifdef MYSQL_SERVER
652 /**
653 Encrypt a file password using current replication encryption master key.
654
655 @param[in] password_str The plain file password.
656
657 @retval false Success.
658 @retval true Error.
659 */
660 virtual bool encrypt_file_password(Key_string password_str) = 0;
661#endif
662 /**
663 Build a key id prefix using default header version.
664
665 @return A key ID prefix.
666 */
667 static std::string key_id_prefix();
668 /**
669 Build a key id using the given sequence number using default header version.
670
671 @param[in] seqno The sequence number used to build key id.
672
673 @return A key ID with a sequence number.
674 */
675 static std::string seqno_to_key_id(uint32_t seqno);
676 /**
677 Build a key id using the given suffix using default header version.
678
679 @param[in] suffix The suffix used to build key id.
680
681 @return A key ID with a suffix.
682 */
683 static std::string key_id_with_suffix(const char *suffix);
684 /**
685 Return the default header version encryption key type.
686
687 @return The encrypted key type.
688 */
689 static const char *get_key_type();
690
691 protected:
692 /* Offset of the version field in the header */
694 /* Size of the version field in the header */
695 static const int VERSION_SIZE = 1;
696 /* Offset of the optional header fields in the header */
698
699 private:
700 /* The default header version for new headers */
701 static const char m_default_version = 1;
702};
703
704/**
705 @class Rpl_encryption_header_v1
706
707 <pre>
708 +------------------------+----------------------------------------------+
709 | MAGIC HEADER (4 bytes) | Replication logs encryption version (1 byte) |
710 +------------------------+----------------------------------------------+
711 | Replication Encryption Key ID (60 to 69 bytes) |
712 +-----------------------------------------------------------------------+
713 | Encrypted File Password (33 bytes) |
714 +-----------------------------------------------------------------------+
715 | IV For Encrypting File Password (17 bytes) |
716 +-----------------------------------------------------------------------+
717 | Padding (388 to 397 bytes) |
718 +-----------------------------------------------------------------------+
719 Encrypted binary log file header format version 1
720 </pre>
721
722 <table>
723 <caption>Encrypted binary log file header format version 1</caption>
724 <tr>
725 <th>Name</th>
726 <th>Format</th>
727 <th>Description</th>
728 </tr>
729 <tr>
730 <td>Replication Encryption Key ID</td>
731 <td>
732 Variable length field that uses Type, Length, Value (TLV) format. Type
733 takes 1 byte. Length takes 1 byte. Values takes Length bytes.
734 </td>
735 <td>
736 ID of the key that shall be retrieved from keyring to be used to decrypt
737 the file password field.
738 </td>
739 </tr>
740 <tr>
741 <td>Encrypted File Password</td>
742 <td>
743 Fixed length field that uses Type, Value format. Type takes 1 byte.
744 Value takes 32 bytes.</td>
745 <td>It is the encrypted file password.</td>
746 </tr>
747 <tr>
748 <td>IV for Encrypting File Password</td>
749 <td>
750 Fixed length field that uses Type, Value format. Type takes 1 byte.
751 Value takes 16 bytes.</td>
752 <td>
753 The iv, together with the key, is used to encrypt/decrypt the
754 file password.
755 </td>
756 </tr>
757 <tr>
758 <td>Padding</td>
759 <td>Variable length, all bytes are 0.</td>
760 <td>
761 Encryption header has 512 bytes. Above fields don't take all bytes. All
762 unused bytes are filled with 0 as padding.
763 </td>
764 </tr>
765 </table>
766*/
768 public:
769 static const char *KEY_TYPE;
770 static const int KEY_LENGTH = 32;
771 static const int HEADER_SIZE = 512;
772 static const int IV_FIELD_SIZE = 16;
773 static const int PASSWORD_FIELD_SIZE = 32;
774
776
777 ~Rpl_encryption_header_v1() override;
778
779 bool serialize(Basic_ostream *ostream) override;
780 bool deserialize(Basic_istream *istream) override;
781 char get_version() const override;
782 int get_header_size() override;
784 std::unique_ptr<Stream_cipher> get_encryptor() override;
785 std::unique_ptr<Stream_cipher> get_decryptor() override;
787#ifdef MYSQL_SERVER
788 bool encrypt_file_password(Key_string password_str) override;
789#endif
790
791 /**
792 Build a key id prefix.
793 */
794 static std::string key_id_prefix();
795 /**
796 Build a key id using the given sequence number.
797
798 @param[in] seqno The sequence number used to build key id.
799 */
800 static std::string seqno_to_key_id(uint32_t seqno);
801 /**
802 Build a key id using the given suffix.
803
804 @param[in] suffix The suffix used to build key id.
805 */
806 static std::string key_id_with_suffix(const char *suffix);
807
808 private:
809 /* The prefix for key IDs */
810 static const char *KEY_ID_PREFIX;
811 /* Expected field types */
816 };
817 /* This header implementation version */
818 char m_version = 1;
819 /* The key ID of the keyring key that encrypted the password */
820 std::string m_key_id;
821 /* The encrypted file password */
823 /* The IV used to encrypt/decrypt the file password */
825};
826#endif // RPL_LOG_ENCRYPTION_INCLUDED
The abstract class for basic byte input streams which provides read operations.
Definition: basic_istream.h:35
The abstract class for basic output streams which provides write operation.
Definition: basic_ostream.h:37
Definition: rpl_log_encryption.h:767
static std::string seqno_to_key_id(uint32_t seqno)
Build a key id using the given sequence number.
Definition: rpl_log_encryption.cc:1260
~Rpl_encryption_header_v1() override
Definition: rpl_log_encryption.cc:1036
@ KEY_ID
Definition: rpl_log_encryption.h:813
@ IV_FOR_FILE_PASSWORD
Definition: rpl_log_encryption.h:815
@ ENCRYPTED_FILE_PASSWORD
Definition: rpl_log_encryption.h:814
Key_string decrypt_file_password() override
Decrypt the file password.
Definition: rpl_log_encryption.cc:1165
static std::string key_id_with_suffix(const char *suffix)
Build a key id using the given suffix.
Definition: rpl_log_encryption.cc:1269
static const int PASSWORD_FIELD_SIZE
Definition: rpl_log_encryption.h:773
static const int HEADER_SIZE
Definition: rpl_log_encryption.h:771
Key_string m_encrypted_password
Definition: rpl_log_encryption.h:822
bool serialize(Basic_ostream *ostream) override
Serialize the header into an output stream.
Definition: rpl_log_encryption.cc:1038
bool encrypt_file_password(Key_string password_str) override
Encrypt a file password using current replication encryption master key.
Definition: rpl_log_encryption.cc:1200
Rpl_encryption_header_v1()=default
char m_version
Definition: rpl_log_encryption.h:818
static const char * KEY_TYPE
Definition: rpl_log_encryption.h:769
Key_string m_iv
Definition: rpl_log_encryption.h:824
std::string m_key_id
Definition: rpl_log_encryption.h:820
std::unique_ptr< Stream_cipher > get_encryptor() override
Factory to generate ciphers to encrypt streams based on current header.
Definition: rpl_log_encryption.cc:1191
static const int KEY_LENGTH
Definition: rpl_log_encryption.h:770
static const int IV_FIELD_SIZE
Definition: rpl_log_encryption.h:772
std::unique_ptr< Stream_cipher > get_decryptor() override
Factory to generate ciphers to decrypt streams based on current header.
Definition: rpl_log_encryption.cc:1195
static std::string key_id_prefix()
Build a key id prefix.
Definition: rpl_log_encryption.cc:1252
Key_string generate_new_file_password() override
Setup the header with current master key and generates a new random file password.
Definition: rpl_log_encryption.cc:1231
int get_header_size() override
Return the header size to be taken into account when serializing an deserializing encrypted file head...
Definition: rpl_log_encryption.cc:1161
bool deserialize(Basic_istream *istream) override
Deserialize encryption header from a stream.
Definition: rpl_log_encryption.cc:1066
static const char * KEY_ID_PREFIX
Definition: rpl_log_encryption.h:810
char get_version() const override
Get the header version.
Definition: rpl_log_encryption.cc:1159
This is the base class to serialize and deserialize a replication log file encryption header.
Definition: rpl_log_encryption.h:566
virtual std::unique_ptr< Stream_cipher > get_decryptor()=0
Factory to generate ciphers to decrypt streams based on current header.
static const char * get_key_type()
Return the default header version encryption key type.
Definition: rpl_log_encryption.cc:1029
static const int VERSION_SIZE
Definition: rpl_log_encryption.h:695
static const int VERSION_OFFSET
Definition: rpl_log_encryption.h:693
virtual ~Rpl_encryption_header()
Definition: rpl_log_encryption.cc:960
static std::unique_ptr< Rpl_encryption_header > get_header(Basic_istream *istream)
Deserialize the replication encrypted log file header from the given stream.
Definition: rpl_log_encryption.cc:973
static std::string seqno_to_key_id(uint32_t seqno)
Build a key id using the given sequence number using default header version.
Definition: rpl_log_encryption.cc:1021
static std::unique_ptr< Rpl_encryption_header > get_new_default_header()
Generate a new replication encryption header based on the default replication encrypted log file head...
Definition: rpl_log_encryption.cc:1011
virtual Key_string generate_new_file_password()=0
Setup the header with current master key and generates a new random file password.
virtual bool serialize(Basic_ostream *ostream)=0
Serialize the header into an output stream.
virtual int get_header_size()=0
Return the header size to be taken into account when serializing an deserializing encrypted file head...
static std::string key_id_with_suffix(const char *suffix)
Build a key id using the given suffix using default header version.
Definition: rpl_log_encryption.cc:1025
static const char * ENCRYPTION_MAGIC
Definition: rpl_log_encryption.h:571
static const int ENCRYPTION_MAGIC_SIZE
Definition: rpl_log_encryption.h:569
static const int OPTIONAL_FIELD_OFFSET
Definition: rpl_log_encryption.h:697
virtual std::unique_ptr< Stream_cipher > get_encryptor()=0
Factory to generate ciphers to encrypt streams based on current header.
static const char m_default_version
Definition: rpl_log_encryption.h:701
virtual bool deserialize(Basic_istream *istream)=0
Deserialize encryption header from a stream.
virtual Key_string decrypt_file_password()=0
Decrypt the file password.
virtual char get_version() const =0
Get the header version.
static std::string key_id_prefix()
Build a key id prefix using default header version.
Definition: rpl_log_encryption.cc:1017
virtual bool encrypt_file_password(Key_string password_str)=0
Encrypt a file password using current replication encryption master key.
The Rpl_encryption class is the container for the binlog encryption feature generic and server instan...
Definition: rpl_log_encryption.h:98
Rpl_encryption_key m_master_key
Definition: rpl_log_encryption.h:312
static const char * SEQNO_KEY_TYPE
Definition: rpl_log_encryption.h:280
Rpl_encryption & operator=(const Rpl_encryption &)=delete
static std::tuple< Keyring_status, void *, size_t > fetch_key_from_keyring(const std::string &key_id, const std::string &key_type)
Fetch a key from keyring.
Definition: rpl_log_encryption.cc:468
bool remove_old_master_key_seqno_from_keyring()
Remove the "old" master key sequence number key from the keyring.
Definition: rpl_log_encryption.cc:905
uint32_t m_master_key_seqno
Definition: rpl_log_encryption.h:310
Rpl_encryption(Rpl_encryption &&)=delete
bool enable(THD *thd)
Enable binlog encryption option.
Definition: rpl_log_encryption.cc:405
std::string get_master_key_seqno_key_id()
Returns the key ID of the keyring key that stores the master key sequence number.
Definition: rpl_log_encryption.cc:816
bool remove_new_master_key_seqno_from_keyring()
Remove the "new" master key sequence number key from the keyring.
Definition: rpl_log_encryption.cc:893
void disable(THD *thd)
Disable binlog encryption option.
Definition: rpl_log_encryption.cc:437
bool purge_unused_keys()
Purge unused master keys from Keyring.
Definition: rpl_log_encryption.cc:506
bool m_initialized
Definition: rpl_log_encryption.h:299
std::pair< Rpl_encryption::Keyring_status, uint32_t > get_last_purged_master_key_seqno_from_keyring()
Get the "last_purged" master key sequence number from keyring.
Definition: rpl_log_encryption.cc:869
static void report_keyring_error(Keyring_status error)
A wrapper function to throw a binlog encryption keyring error.
Definition: rpl_log_encryption.cc:49
bool recover_master_key()
Recover the replication encryption master key from keyring.
Definition: rpl_log_encryption.cc:193
std::string get_new_master_key_seqno_key_id()
Returns the key ID of the keyring key that stores the "new" master key sequence number.
Definition: rpl_log_encryption.cc:839
Rpl_encryption & operator=(Rpl_encryption &&)=delete
std::pair< Rpl_encryption::Keyring_status, uint32_t > get_master_key_seqno_from_keyring()
Get the master key sequence number from keyring.
Definition: rpl_log_encryption.cc:821
Keyring_status
Definition: rpl_log_encryption.h:111
std::pair< Rpl_encryption::Keyring_status, uint32_t > get_seqno_from_keyring(std::string key_id)
Get a sequence number from the keyring.
Definition: rpl_log_encryption.cc:741
bool set_last_purged_master_key_seqno_on_keyring(uint32 seqno)
Set the "last_purged" master key sequence number into a key and store it into keyring.
Definition: rpl_log_encryption.cc:881
Key_rotation_step
Replication encryption master key rotation process is recoverable.
Definition: rpl_log_encryption.h:146
Rpl_encryption()=default
bool remove_key_from_keyring(std::string key_id)
Remove a key from the keyring.
Definition: rpl_log_encryption.cc:787
bool m_master_key_recovered
Definition: rpl_log_encryption.h:308
bool set_master_key_seqno_on_keyring(uint32 seqno)
Set the master key sequence number into a key and store it into keyring.
Definition: rpl_log_encryption.cc:827
bool m_rotate_at_startup
Definition: rpl_log_encryption.h:293
bool generate_master_key_on_keyring(uint32 seqno)
Generate a new replication master key on keyring and retrieve it.
Definition: rpl_log_encryption.cc:911
std::pair< Rpl_encryption::Keyring_status, uint32_t > get_new_master_key_seqno_from_keyring()
Get the "new" master key sequence number from keyring.
Definition: rpl_log_encryption.cc:855
bool remove_last_purged_master_key_seqno_from_keyring()
Remove the "last_purged" master key sequence number key from the keyring.
Definition: rpl_log_encryption.cc:899
bool rotate_master_key(Key_rotation_step step=Key_rotation_step::START, uint32_t new_master_key_seqno=0)
Rotate the master key.
Definition: rpl_log_encryption.cc:569
bool set_new_master_key_seqno_on_keyring(uint32 seqno)
Set the "new" master key sequence number into a key and store it into keyring.
Definition: rpl_log_encryption.cc:875
void rotate_logs(THD *thd)
Rotate replication logs excluding relay logs of group replication channels.
Definition: rpl_log_encryption.cc:732
static std::pair< Keyring_status, Key_string > get_key(const std::string &key_id, const std::string &key_type)
Get the key with given key ID.
Definition: rpl_log_encryption.cc:371
Rpl_encryption(const Rpl_encryption &)=delete
std::string get_last_purged_master_key_seqno_key_id()
Returns the key ID of the keyring key that stores the "last_purged" master key sequence number.
Definition: rpl_log_encryption.cc:844
bool is_enabled()
Return is the replication logs encryption feature is enabled.
Definition: rpl_log_encryption.cc:450
bool set_old_master_key_seqno_on_keyring(uint32 seqno)
Set the "old" master key sequence number into a key and store it into keyring.
Definition: rpl_log_encryption.cc:887
const bool & get_enabled_var()
Definition: rpl_log_encryption.cc:459
const bool & get_master_key_rotation_at_startup_var()
Definition: rpl_log_encryption.cc:461
bool initialize()
Initialize the rpl_encryption instance.
Definition: rpl_log_encryption.cc:106
bool remove_master_key_seqno_from_keyring()
Remove the master key sequence number key from the keyring.
Definition: rpl_log_encryption.cc:833
static const int SEQNO_KEY_LENGTH
Definition: rpl_log_encryption.h:282
bool remove_remaining_seqnos_from_keyring()
Remove remaining old/new master key index in order to cleanup any previous master key rotation.
Definition: rpl_log_encryption.cc:131
const Rpl_encryption_key get_master_key()
Return the current replication encryption master key.
Definition: rpl_log_encryption.cc:362
bool set_seqno_on_keyring(std::string key_id, uint32_t seqno)
Set a sequence number into a key and store it into keyring.
Definition: rpl_log_encryption.cc:753
bool m_enabled
Definition: rpl_log_encryption.h:287
std::pair< Rpl_encryption::Keyring_status, uint32_t > get_old_master_key_seqno_from_keyring()
Get the "old" master key sequence number from keyring.
Definition: rpl_log_encryption.cc:862
std::string get_old_master_key_seqno_key_id()
Returns the key ID of the keyring key that stores the "old" master key sequence number.
Definition: rpl_log_encryption.cc:849
bool m_skip_logs_rotation
Definition: rpl_log_encryption.h:317
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_lexer_thd.h:36
Some integer typedefs for easier portability.
uint32_t uint32
Definition: my_inttypes.h:67
int key_type
Definition: method.h:38
Rpl_encryption rpl_encryption
Definition: rpl_log_encryption.cc:47
This file includes core components for encrypting/decrypting binary log files.
std::basic_string< unsigned char, my_char_traits< unsigned char > > Key_string
Definition: stream_cipher.h:42
Definition: sql_resultset.h:36
Definition: rpl_log_encryption.h:100
Key_string m_value
Definition: rpl_log_encryption.h:102
std::string m_id
Definition: rpl_log_encryption.h:101