Skip to content

Commit

Permalink
Use jose, not pwmake, for password generation (#418)
Browse files Browse the repository at this point in the history
This change removes password generation with pwmake
and uses jose to do so. It has been checked that
generation is similar, as jose uses OpenSSL.
Apart from that, we will introduce --force-password
so that pwquality configuration does not bother on
LUKS operations

Resolves: #199

Signed-off-by: Sergio Arroutbi <[email protected]>
  • Loading branch information
sarroutbi authored Jun 7, 2023
1 parent bf9e1cd commit 4d23eda
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 27 deletions.
38 changes: 13 additions & 25 deletions src/luks/clevis-luks-common-functions.in
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@

CLEVIS_UUID="cb6e8904-81ff-40da-a84a-07ab9ab5715e"

# Length, in bytes, used for password generated for LUKS key
# This value corresponds to an entropy of 256 bits if the password
# was generated by pwmake or similar tool
JOSE_PASSWORD_LENGTH=40

enable_debugging() {
# Automatically enable debugging if in initramfs phase and rd.debug
if [ -e /usr/lib/dracut-lib.sh ]; then
Expand Down Expand Up @@ -788,7 +793,7 @@ clevis_luks_add_key() {
fi
local pbkdf_args="--pbkdf pbkdf2 --pbkdf-force-iterations 1000"

printf '%s' "${input}" | cryptsetup luksAddKey --batch-mode \
printf '%s' "${input}" | cryptsetup luksAddKey --force-password --batch-mode \
--key-slot "${SLT}" \
"${DEV}" \
${pbkdf_args} \
Expand Down Expand Up @@ -818,11 +823,11 @@ clevis_luks_update_key() {
local input extra_args=
input="$(printf '%s\n%s' "${KEY}" "${NEWKEY}")"
if [ -n "${KEYFILE}" ]; then
extra_args="$(printf -- '--key-file %s' "${KEYFILE}")"
extra_args="$(printf -- '--key-file %s --force-password' "${KEYFILE}")"
input="$(printf '%s' "${NEWKEY}")"
fi
if [ -n "${EXISTING_TOKEN_ID}" ]; then
extra_args="$(printf -- '--token-id %s' "${EXISTING_TOKEN_ID}")"
extra_args="$(printf -- '--token-id %s --force-password' "${EXISTING_TOKEN_ID}")"
input="$(printf '%s' "${NEWKEY}")"
fi

Expand Down Expand Up @@ -884,26 +889,10 @@ clevis_luks_save_key_to_slot() {

# clevis_luks_generate_key() generates a new key for use with clevis.
clevis_luks_generate_key() {
local DEV="${1}"
[ -z "${DEV}" ] && return 1

local dump filter bits
local MAX_ENTROPY_BITS=256 # Maximum allowed by pwmake.
dump=$(cryptsetup luksDump "${DEV}")
if cryptsetup isLuks --type luks1 "${DEV}"; then
filter="$(echo "${dump}" | sed -rn 's|MK bits:[ \t]*([0-9]+)|\1|p')"
elif cryptsetup isLuks --type luks2 "${DEV}"; then
filter="$(echo -n "${dump}" | \
sed -rn 's|^\s+Key:\s+([0-9]+) bits\s*$|\1|p')"
else
return 1
fi

bits="$(echo -n "${filter}" | sort -n | tail -n 1)"
if [ "${bits}" -gt "${MAX_ENTROPY_BITS}" ]; then
bits="${MAX_ENTROPY_BITS}"
fi
pwmake "${bits}"
local input
input=$(printf '{"kty":"oct","bytes":%s}' "${JOSE_PASSWORD_LENGTH}")
jose jwk gen --input="${input}" --output=- | \
jose fmt --json=- --object --get k --unquote=-
}

# clevis_luks_token_id_by_slot() returns the token ID linked to a
Expand Down Expand Up @@ -993,8 +982,7 @@ clevis_luks_do_bind() {
fi

local newkey jwe
if ! newkey="$(clevis_luks_generate_key "${DEV}")" \
|| [ -z "${newkey}" ]; then
if ! newkey="$(clevis_luks_generate_key)" || [ -z "${newkey}" ]; then
echo "Unable to generate a new key" >&2
return 1
fi
Expand Down
3 changes: 1 addition & 2 deletions src/luks/meson.build
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@

luksmeta_data = configuration_data()
luksmeta = dependency('luksmeta', version: '>=8', required: false)
pwmake = find_program('pwmake', required: false)

libcryptsetup = dependency('libcryptsetup', version: '>=2.0.4', required: false)
if libcryptsetup.found()
Expand Down Expand Up @@ -33,7 +32,7 @@ clevis_luks_unbind = configure_file(input: 'clevis-luks-unbind.in',
output: 'clevis-luks-unbind',
configuration: luksmeta_data)

if libcryptsetup.found() and luksmeta.found() and pwmake.found()
if libcryptsetup.found() and luksmeta.found()
subdir('systemd')
subdir('udisks2')

Expand Down

0 comments on commit 4d23eda

Please sign in to comment.