Skip to content

Commit 92599d6

Browse files
dupondjepeter-boden
authored andcommitted
packaging: migrate legacy .p12 on openssl upgrade
When restoring a backup from CentOS 8 onto a CentOS 9 system, the p12 files that were generated are invalid, because they use a legacy encryption on CentOS 9. So, upon restore, we try to read the p12 files. When this fails we try to read the p12 file with the `legacy` flag. If this succeeds, we re-encrypt the p12 file with a newer encryption algorythm. The old p12 files is backup up. We log all actions so that the user knows what happened, and can act upon errors. Signed-off-by: Peter Boden <[email protected]>
1 parent e056ad2 commit 92599d6

File tree

1 file changed

+117
-0
lines changed

1 file changed

+117
-0
lines changed

packaging/bin/engine-backup.sh.in

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,25 @@ engine_setup_service_enabled() {
120120
--file ${INSTALL_ROOT}etc/ovirt-engine-setup.conf
121121
}
122122

123+
get_engine_constant() {
124+
local constant_path="$1"
125+
local setup_dir="${ENGINE_USR}/setup"
126+
127+
local result
128+
result=$(PYTHONPATH="${setup_dir}" python3 -c "
129+
from ovirt_engine_setup.engine import constants as oenginecons
130+
print(${constant_path})
131+
" 2>&1)
132+
local exit_code=$?
133+
134+
if [ ${exit_code} -ne 0 ]; then
135+
log "Warning: Failed to get engine constant ${constant_path}: ${result}"
136+
return 1
137+
fi
138+
139+
echo "${result}"
140+
}
141+
123142
engine_enabled() {
124143
engine_setup_service_enabled "OVESETUP_ENGINE_CORE/enable"
125144
}
@@ -1872,6 +1891,102 @@ resetHAVMStatus() {
18721891
|| logdie "Failed resetting HA VM status"
18731892
}
18741893

1894+
reencryptLegacyP12() {
1895+
local file="$1"
1896+
local pki_password="$2"
1897+
local timestamp="$3"
1898+
1899+
log "Found legacy encryption in ${file}, re-encrypting to AES-256"
1900+
1901+
mv -f "${file}" "${file}.backup.${timestamp}" || logdie "Failed to backup ${file}"
1902+
1903+
local temp_key="${TEMP_FOLDER}/temp_key_${timestamp}_$$.pem"
1904+
local temp_certs="${TEMP_FOLDER}/temp_certs_${timestamp}_$$.pem"
1905+
local result=1
1906+
1907+
openssl pkcs12 -in "${file}.backup.${timestamp}" -passin "pass:${pki_password}" -legacy \
1908+
-nocerts -out "${temp_key}" -passout "pass:${pki_password}" 2>> "${LOG}" && \
1909+
openssl pkcs12 -in "${file}.backup.${timestamp}" -passin "pass:${pki_password}" -legacy \
1910+
-nokeys -out "${temp_certs}" 2>> "${LOG}"
1911+
local extract_success=$?
1912+
1913+
if [ ${extract_success} -eq 0 ]; then
1914+
openssl pkcs12 -export -in "${temp_certs}" -inkey "${temp_key}" \
1915+
-passin "pass:${pki_password}" -passout "pass:${pki_password}" \
1916+
-out "${file}" 2>> "${LOG}"
1917+
local reencrypt_success=$?
1918+
1919+
if [ ${reencrypt_success} -eq 0 ]; then
1920+
output " - Successfully re-encrypted $(basename "${file}") (backup: $(basename "${file}").backup.${timestamp})"
1921+
result=0
1922+
else
1923+
log "Warning: Failed to re-encrypt ${file}, restoring from backup"
1924+
mv -f "${file}.backup.${timestamp}" "${file}" || logdie "Failed to restore ${file}"
1925+
fi
1926+
else
1927+
log "Warning: Failed to extract key/cert from ${file}, restoring from backup"
1928+
mv -f "${file}.backup.${timestamp}" "${file}" || logdie "Failed to restore ${file}"
1929+
fi
1930+
1931+
rm -f "${temp_key}" "${temp_certs}"
1932+
return ${result}
1933+
}
1934+
1935+
migrateLegacyCerts() {
1936+
output 'Checking certificates for legacy encryption:'
1937+
1938+
# Re-encrypt old p12 files that use legacy encryption (3DES/RC2) incompatible with OpenSSL >=3.0
1939+
# More info on https://github.com/openssl/openssl/discussions/23089
1940+
1941+
local pki_config_file=$(get_engine_constant "oenginecons.FileLocations.OVIRT_ENGINE_SERVICE_CONFIG_PKI")
1942+
local default_pki_password=$(get_engine_constant "oenginecons.Const.PKI_PASSWORD")
1943+
local pki_keys_dir=$(get_engine_constant "oenginecons.FileLocations.OVIRT_ENGINE_PKIKEYSDIR")
1944+
1945+
if [ -z "${pki_keys_dir}" ] || [ -z "${default_pki_password}" ]; then
1946+
output " - Warning: Could not determine PKI configuration, skipping certificate check"
1947+
log "PKI config file: '${pki_config_file}', password: '${default_pki_password}', keys dir: '${pki_keys_dir}'"
1948+
return 0
1949+
fi
1950+
1951+
local pki_password="${default_pki_password}"
1952+
if [ -f "${pki_config_file}" ]; then
1953+
. "${pki_config_file}"
1954+
[ -n "${ENGINE_PKI_ENGINE_STORE_PASSWORD}" ] && pki_password="${ENGINE_PKI_ENGINE_STORE_PASSWORD}"
1955+
fi
1956+
1957+
output " - Scanning ${pki_keys_dir}"
1958+
1959+
local timestamp=$(date +"%Y%m%d%H%M%S")
1960+
local processed_count=0
1961+
for file in ${pki_keys_dir}/*.p12; do
1962+
[ -f "${file}" ] || continue
1963+
1964+
openssl pkcs12 -info -in "${file}" -passin "pass:${pki_password}" -noout &>/dev/null
1965+
local can_read=$?
1966+
1967+
if [ ${can_read} -ne 0 ]; then
1968+
log "Cannot read ${file} with standard openssl, trying with -legacy"
1969+
openssl pkcs12 -info -in "${file}" -passin "pass:${pki_password}" -legacy -noout &>/dev/null
1970+
can_read=$?
1971+
1972+
if [ ${can_read} -eq 0 ]; then
1973+
if reencryptLegacyP12 "${file}" "${pki_password}" "${timestamp}"; then
1974+
processed_count=$((processed_count + 1))
1975+
fi
1976+
else
1977+
log "Cannot read ${file} even with -legacy flag"
1978+
output " - Warning: Cannot read $(basename "${file}") (may be corrupt or use wrong password)"
1979+
fi
1980+
fi
1981+
done
1982+
1983+
if [ ${processed_count} -eq 0 ]; then
1984+
output " - Check complete (no legacy certificates found)"
1985+
else
1986+
output " - Check complete (re-encrypted ${processed_count} certificate(s))"
1987+
fi
1988+
}
1989+
18751990
restoreFiles() {
18761991
local paths="$1"
18771992
local archive="$2"
@@ -1929,6 +2044,8 @@ __EOF__
19292044
sed -i "${INSTALL_ROOT}/^${ESC_APACHE_CONFIGURED_LINE}\$/d" "${POSTINSTALL_RELATIVE}"
19302045
fi
19312046

2047+
migrateLegacyCerts
2048+
19322049
if selinuxenabled; then
19332050
echo "${paths}" | while read -r path; do
19342051
if [ -e "${path}" ]; then

0 commit comments

Comments
 (0)