| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 1995, 2026, Oracle and/or its affiliates. |
| 4 |
|
| 5 |
This program is free software; you can redistribute it and/or modify |
| 6 |
it under the terms of the GNU General Public License, version 2.0, |
| 7 |
as published by the Free Software Foundation. |
| 8 |
|
| 9 |
This program is designed to work with certain software (including |
| 10 |
but not limited to OpenSSL) that is licensed under separate terms, |
| 11 |
as designated in a particular file or component or in included license |
| 12 |
documentation. The authors of MySQL hereby grant you an additional |
| 13 |
permission to link the program and your derivative works with the |
| 14 |
separately licensed software that they have either included with |
| 15 |
the program or referenced in the documentation. |
| 16 |
|
| 17 |
This program is distributed in the hope that it will be useful, |
| 18 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 |
GNU General Public License, version 2.0, for more details. |
| 21 |
|
| 22 |
You should have received a copy of the GNU General Public License |
| 23 |
along with this program; if not, write to the Free Software |
| 24 |
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 25 |
|
| 26 |
*****************************************************************************/ |
| 27 |
|
| 28 |
/**************************************************/ /** |
| 29 |
@file log/log0encryption.cc |
| 30 |
|
| 31 |
*******************************************************/ |
| 32 |
|
| 33 |
#ifndef UNIV_HOTBACKUP |
| 34 |
|
| 35 |
/* std::memcpy, std::memcmp, std::memset */ |
| 36 |
#include <cstring> |
| 37 |
|
| 38 |
/* log_get_checkpoint_lsn */ |
| 39 |
#include "log0chkp.h" |
| 40 |
|
| 41 |
#include "log0encryption.h" |
| 42 |
|
| 43 |
/* log_files_mutex_own */ |
| 44 |
#include "log0files_governor.h" |
| 45 |
|
| 46 |
/* log_encryption_header_{read,write} */ |
| 47 |
#include "log0files_io.h" |
| 48 |
|
| 49 |
/* log_t::m_encryption_metadata */ |
| 50 |
#include "log0sys.h" |
| 51 |
|
| 52 |
/* LOG_HEADER_ENCRYPTION_INFO_OFFSET */ |
| 53 |
#include "log0types.h" |
| 54 |
|
| 55 |
/* log_writer_mutex_own */ |
| 56 |
#include "log0write.h" |
| 57 |
|
| 58 |
/* Encryption::X */ |
| 59 |
#include "os0enc.h" |
| 60 |
|
| 61 |
/* srv_force_recovery */ |
| 62 |
#include "srv0srv.h" |
| 63 |
|
| 64 |
/* IB_mutex_guard */ |
| 65 |
#include "ut0mutex.h" |
| 66 |
|
| 67 |
/**************************************************/ /** |
| 68 |
|
| 69 |
@name Log - encryption. |
| 70 |
|
| 71 |
*******************************************************/ |
| 72 |
|
| 73 |
/** @{ */ |
| 74 |
|
| 75 |
/** Finds redo log file which has the current log encryption header. |
| 76 |
Asserts that the file has been found. |
| 77 |
@param[in] log redo log |
| 78 |
@return iterator to the file containing current log encryption header */ |
| 79 |
static Log_files_dict::Const_iterator log_encryption_file(const log_t &log) { |
| 80 |
auto file = log.m_files.find(log_get_checkpoint_lsn(log)); |
| 81 |
ut_a(file != log.m_files.end()); |
| 82 |
return file; |
| 83 |
} |
| 84 |
|
| 85 |
dberr_t log_encryption_read(log_t &log) { |
| 86 |
return log_encryption_read(log, *log_encryption_file(log)); |
| 87 |
} |
| 88 |
|
| 89 |
dberr_t log_encryption_read(log_t &log, const Log_file &file) { |
| 90 |
ut_a(srv_force_recovery < SRV_FORCE_NO_LOG_REDO); |
| 91 |
ut_a(log_sys != nullptr); |
| 92 |
|
| 93 |
IB_mutex_guard writer_latch{&(log.writer_mutex), UT_LOCATION_HERE}; |
| 94 |
IB_mutex_guard files_latch{&(log.m_files_mutex), UT_LOCATION_HERE}; |
| 95 |
std::memset(log.m_encryption_buf, 0x00, OS_FILE_LOG_BLOCK_SIZE); |
| 96 |
|
| 97 |
auto file_handle = file.open(Log_file_access_mode::READ_ONLY); |
| 98 |
ut_a(file_handle.is_open()); |
| 99 |
|
| 100 |
byte log_block_buf[OS_FILE_LOG_BLOCK_SIZE] = {}; |
| 101 |
|
| 102 |
const dberr_t err = log_encryption_header_read(file_handle, log_block_buf); |
| 103 |
if (err != DB_SUCCESS) { |
| 104 |
return DB_ERROR; |
| 105 |
} |
| 106 |
|
| 107 |
if (Encryption::is_encrypted_with_v3(log_block_buf + |
| 108 |
LOG_HEADER_ENCRYPTION_INFO_OFFSET)) { |
| 109 |
/* Make sure the keyring is loaded. */ |
| 110 |
if (!Encryption::check_keyring()) { |
| 111 |
ib::error(ER_IB_MSG_1238) << "Redo log was encrypted," |
| 112 |
<< " but keyring is not loaded."; |
| 113 |
return DB_ERROR; |
| 114 |
} |
| 115 |
|
| 116 |
Encryption_metadata encryption_metadata; |
| 117 |
|
| 118 |
if (Encryption::decode_encryption_info( |
| 119 |
encryption_metadata, |
| 120 |
log_block_buf + LOG_HEADER_ENCRYPTION_INFO_OFFSET, true)) { |
| 121 |
log_files_update_encryption(log, encryption_metadata); |
| 122 |
|
| 123 |
ib::info(ER_IB_MSG_1239) << "Read redo log encryption" |
| 124 |
<< " metadata successful."; |
| 125 |
|
| 126 |
std::memcpy(log.m_encryption_buf, log_block_buf, OS_FILE_LOG_BLOCK_SIZE); |
| 127 |
|
| 128 |
return DB_SUCCESS; |
| 129 |
|
| 130 |
} else { |
| 131 |
ib::error(ER_IB_MSG_1241) << "Cannot read the encryption" |
| 132 |
" information in log file header, please" |
| 133 |
" check if keyring is loaded."; |
| 134 |
return DB_ERROR; |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
return DB_SUCCESS; |
| 139 |
} |
| 140 |
|
| 141 |
bool log_file_header_fill_encryption( |
| 142 |
const Encryption_metadata &encryption_metadata, bool encrypt_key, |
| 143 |
byte *buf) { |
| 144 |
byte encryption_info[Encryption::INFO_SIZE]; |
| 145 |
|
| 146 |
if (!Encryption::fill_encryption_info(encryption_metadata, encrypt_key, |
| 147 |
encryption_info)) { |
| 148 |
return false; |
| 149 |
} |
| 150 |
|
| 151 |
static_assert(LOG_HEADER_ENCRYPTION_INFO_OFFSET + Encryption::INFO_SIZE < |
| 152 |
OS_FILE_LOG_BLOCK_SIZE, |
| 153 |
"Encryption information is too big."); |
| 154 |
|
| 155 |
std::memset(buf, 0x00, OS_FILE_LOG_BLOCK_SIZE); |
| 156 |
|
| 157 |
std::memcpy(buf + LOG_HEADER_ENCRYPTION_INFO_OFFSET, encryption_info, |
| 158 |
Encryption::INFO_SIZE); |
| 159 |
|
| 160 |
return true; |
| 161 |
} |
| 162 |
|
| 163 |
/** Writes the encryption information into the log encryption header in |
| 164 |
the log file containing current checkpoint LSN (log.last_checkpoint_lsn). |
| 165 |
Updates: log.m_encryption_buf. |
| 166 |
@param[in,out] log redo log |
| 167 |
@return DB_SUCCESS or DB_ERROR */ |
| 168 |
static dberr_t log_encryption_write_low(log_t &log) { |
| 169 |
ut_ad(log_files_mutex_own(log)); |
| 170 |
ut_ad(log_writer_mutex_own(log)); |
| 171 |
|
| 172 |
byte log_block_buf[OS_FILE_LOG_BLOCK_SIZE]; |
| 173 |
|
| 174 |
if (log_can_encrypt(log)) { |
| 175 |
if (!log_file_header_fill_encryption(log.m_encryption_metadata, true, |
| 176 |
log_block_buf)) { |
| 177 |
return DB_ERROR; |
| 178 |
} |
| 179 |
} else { |
| 180 |
std::memset(log_block_buf, 0x00, OS_FILE_LOG_BLOCK_SIZE); |
| 181 |
} |
| 182 |
|
| 183 |
std::memcpy(log.m_encryption_buf, log_block_buf, OS_FILE_LOG_BLOCK_SIZE); |
| 184 |
|
| 185 |
auto file_handle = |
| 186 |
log_encryption_file(log)->open(Log_file_access_mode::WRITE_ONLY); |
| 187 |
ut_a(file_handle.is_open()); |
| 188 |
|
| 189 |
return log_encryption_header_write(file_handle, log.m_encryption_buf); |
| 190 |
} |
| 191 |
|
| 192 |
bool log_can_encrypt(const log_t &log) { |
| 193 |
return log.m_encryption_metadata.can_encrypt(); |
| 194 |
} |
| 195 |
|
| 196 |
dberr_t log_encryption_on_master_key_changed(log_t &log) { |
| 197 |
IB_mutex_guard writer_latch{&(log.writer_mutex), UT_LOCATION_HERE}; |
| 198 |
IB_mutex_guard files_latch{&(log.m_files_mutex), UT_LOCATION_HERE}; |
| 199 |
|
| 200 |
if (!log_can_encrypt(log)) { |
| 201 |
return DB_SUCCESS; |
| 202 |
} |
| 203 |
|
| 204 |
/* Re-encrypt log's encryption metadata and write them to disk. */ |
| 205 |
return log_encryption_write_low(log); |
| 206 |
} |
| 207 |
|
| 208 |
dberr_t log_encryption_generate_metadata(log_t &log) { |
| 209 |
IB_mutex_guard writer_latch{&(log.writer_mutex), UT_LOCATION_HERE}; |
| 210 |
IB_mutex_guard files_latch{&(log.m_files_mutex), UT_LOCATION_HERE}; |
| 211 |
|
| 212 |
Encryption_metadata encryption_metadata; |
| 213 |
|
| 214 |
Encryption::set_or_generate(Encryption::AES, nullptr, nullptr, |
| 215 |
encryption_metadata); |
| 216 |
|
| 217 |
log_files_update_encryption(log, encryption_metadata); |
| 218 |
|
| 219 |
const auto err = log_encryption_write_low(log); |
| 220 |
if (err != DB_SUCCESS) { |
| 221 |
log_files_update_encryption(log, {}); |
| 222 |
return err; |
| 223 |
} |
| 224 |
|
| 225 |
return DB_SUCCESS; |
| 226 |
} |
| 227 |
|
| 228 |
/** @} */ |
| 229 |
|
| 230 |
#endif /* !UNIV_HOTBACKUP */ |