mirror of
https://github.com/m8tin/cis.git
synced 2026-11-02 14:17:24 +01:00
Improved tpm secrets script
This commit is contained in:
1 file changed
+26
-4
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
AES_STORE_FILE=~/tpm/aes-key-safe
|
AES_STORE_FILE=~/tpm/aes-key-safe
|
||||||
SECRETS_STORE_FILE=~/tpm/secrets
|
SECRETS_STORE_FILE=~/tpm/secrets
|
||||||
SECRETS_PLAIN_PATH=/run/user/$(id -u)/secrets/
|
SECRETS_PLAIN_PATH="/run/secrets/$(id -u)/"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -76,6 +76,28 @@ function checkOrCreateSafe() {
|
|||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function prepareRestrictedTmpfsDirectoryFromPath() {
|
||||||
|
# Startet mit /, endet mit /, mindestens 3 Schrägstriche
|
||||||
|
if [[ "${1:?"prepareRestrictedTmpfsDirectoryFromPath(): Missing first parameter PATH (with tailing '/')"}" =~ ^/.*([^/]+/){2,}$ ]]; then
|
||||||
|
local _PATH="${1%/*}"
|
||||||
|
local _BASE="${_PATH%/*}"
|
||||||
|
|
||||||
|
mkdir -p -m 1777 "${_BASE}"
|
||||||
|
! df --output=fstype "${_BASE}" | grep -qF 'tmpfs' \
|
||||||
|
&& return 1
|
||||||
|
|
||||||
|
! [[ "$(stat -c "%a" "${_BASE}")" == "1777" ]] \
|
||||||
|
&& return 1
|
||||||
|
|
||||||
|
mkdir -p -m 700 "${_PATH}"
|
||||||
|
! [[ "$(stat -c "%a" "${_PATH}")" == "700" ]] \
|
||||||
|
&& return 1
|
||||||
|
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
function seal() {
|
function seal() {
|
||||||
# Ensure all secrets have restricte permissions before tar
|
# Ensure all secrets have restricte permissions before tar
|
||||||
! chmod -R go-rwx "${SECRETS_PLAIN_PATH}" \
|
! chmod -R go-rwx "${SECRETS_PLAIN_PATH}" \
|
||||||
@@ -88,7 +110,7 @@ function seal() {
|
|||||||
-u "${AES_STORE_FILE}.pub" \
|
-u "${AES_STORE_FILE}.pub" \
|
||||||
-c "${TMPDIR%/}/safe.ctx"
|
-c "${TMPDIR%/}/safe.ctx"
|
||||||
openssl enc -aes-256-cbc -md sha256 -pbkdf2 -iter 600000 -salt \
|
openssl enc -aes-256-cbc -md sha256 -pbkdf2 -iter 600000 -salt \
|
||||||
-in <(tar -czvf - "${SECRETS_PLAIN_PATH%/}/"*) \
|
-in <(tar -czvf - -C "${SECRETS_PLAIN_PATH}" .) \
|
||||||
-out "${SECRETS_STORE_FILE}.tar.gz.aes" \
|
-out "${SECRETS_STORE_FILE}.tar.gz.aes" \
|
||||||
-pass file:<(tpm2_unseal -c "${TMPDIR%/}/safe.ctx")
|
-pass file:<(tpm2_unseal -c "${TMPDIR%/}/safe.ctx")
|
||||||
}
|
}
|
||||||
@@ -97,7 +119,7 @@ function unseal() {
|
|||||||
! [ -f "${SECRETS_STORE_FILE}.tar.gz.aes" ] \
|
! [ -f "${SECRETS_STORE_FILE}.tar.gz.aes" ] \
|
||||||
&& return 1
|
&& return 1
|
||||||
|
|
||||||
! mkdir -m 1700 -p "${SECRETS_PLAIN_PATH}" \
|
! prepareRestrictedTmpfsDirectoryFromPath "${SECRETS_PLAIN_PATH}" \
|
||||||
&& return 1
|
&& return 1
|
||||||
|
|
||||||
tpm2_createprimary -C o -Q -c "${TMPDIR%/}/primary.ctx"
|
tpm2_createprimary -C o -Q -c "${TMPDIR%/}/primary.ctx"
|
||||||
@@ -109,7 +131,7 @@ function unseal() {
|
|||||||
openssl enc -d -aes-256-cbc -md sha256 -pbkdf2 -iter 600000 \
|
openssl enc -d -aes-256-cbc -md sha256 -pbkdf2 -iter 600000 \
|
||||||
-in "${SECRETS_STORE_FILE}.tar.gz.aes" \
|
-in "${SECRETS_STORE_FILE}.tar.gz.aes" \
|
||||||
-out - \
|
-out - \
|
||||||
-pass file:<(tpm2_unseal -c "${TMPDIR%/}/safe.ctx") | tar -xzvf - -C /
|
-pass file:<(tpm2_unseal -c "${TMPDIR%/}/safe.ctx") | tar -xzvf - --no-overwrite-dir -C "${SECRETS_PLAIN_PATH}"
|
||||||
}
|
}
|
||||||
|
|
||||||
function usage() {
|
function usage() {
|
||||||
|
|||||||
Reference in new issue
Block a user