From 2ccf0ff6e8cdeca63ae25e6714bc14defc4df5a2 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Tue, 17 Dec 2019 18:39:12 +0100 Subject: [PATCH 1/4] man: tweaks to the crypttab(5) man page --- man/crypttab.xml | 11 ++++++----- man/yubikey-crypttab.sh | 16 +++++++++------- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/man/crypttab.xml b/man/crypttab.xml index d84a914a5e..9b6fffd154 100644 --- a/man/crypttab.xml +++ b/man/crypttab.xml @@ -425,10 +425,10 @@ line. This is useful for unlocking encrypted volumes through security tokens or smartcards. See below for an example how to set up this mechanism for unlocking a LUKS volume with a YubiKey security token. The specified URI can refer directly to a private RSA key stored on a token or alternatively - just to a slot or token in which case a suitable private RSA key object is automatically searched on - it. In this case if multiple suitable objects are found the token is refused. The key configured in - the third column is passed as is to RSA decryption. The resulting decrypted key is then base64 - encoded before it is used to unlock the LUKS volume. + just to a slot or token, in which case a search for a suitable private RSA key will be performed. In + this case if multiple suitable objects are found the token is refused. The key configured in the + third column is passed as is to RSA decryption. The resulting decrypted key is then base64 encoded + before it is used to unlock the LUKS volume. @@ -489,7 +489,8 @@ external /dev/sda3 keyfile:LABEL=keydev keyfile-timeout=10sYubikey-based Volume Unlocking Example The PKCS#11 logic allows hooking up any compatible security token that is capable of storing RSA - decryption keys. Here's an example how to set up a Yubikey security token for this purpose: + decryption keys. Here's an example how to set up a Yubikey security token for this purpose, using + ykman from the yubikey-manager project: diff --git a/man/yubikey-crypttab.sh b/man/yubikey-crypttab.sh index b7e8ee686f..9e65bae621 100644 --- a/man/yubikey-crypttab.sh +++ b/man/yubikey-crypttab.sh @@ -7,7 +7,9 @@ ykman piv reset # Generate a new private/public key pair on the device, store the public key in 'pubkey.pem'. ykman piv generate-key -a RSA2048 9d pubkey.pem -# Create a self-signed certificate from this public key, and store it on the device. +# Create a self-signed certificate from this public key, and store it on the +# device. The "subject" should be an arbitrary string to identify the token in +# the p11tool output below. ykman piv generate-certificate --subject "Knobelei" 9d pubkey.pem # Check if the newly create key on the Yubikey shows up as token in PKCS#11. Have a look at the output, and @@ -18,16 +20,16 @@ p11tool --list-tokens dd if=/dev/urandom of=plaintext.bin bs=128 count=1 # Encode the secret key also as base64 text (with all whitespace removed) -base64 < plaintext.bin | tr -d '\n\r\t ' > plaintext.base64 +base64 < plaintext.bin | tr -d '\n\r\t ' > plaintext.base64 # Encrypt this newly generated (binary) LUKS decryption key using the public key whose private key is on the # Yubikey, store the result in /etc/encrypted-luks-key.bin, where we'll look for it during boot. -openssl rsautl -encrypt -pubin -inkey pubkey.pem -in plaintext.bin -out /etc/encrypted-luks-key.bin +sudo openssl rsautl -encrypt -pubin -inkey pubkey.pem -in plaintext.bin -out /etc/encrypted-luks-key.bin # Configure the LUKS decryption key on the LUKS device. We use very low pbkdf settings since the key already # has quite a high quality (it comes directly from /dev/urandom after all), and thus we don't need to do much -# key derivation. -cryptsetup luksAddKey /dev/sda1 plaintext.base64 --pbkdf=pbkdf2 --pbkdf-force-iterations=1000 +# key derivation. Replace /dev/sdXn by the partition to use (e.g. sda1) +sudo cryptsetup luksAddKey /dev/sdXn plaintext.base64 --pbkdf=pbkdf2 --pbkdf-force-iterations=1000 # Now securely delete the plain text LUKS key, we don't need it anymore, and since it contains secret key # material it should be removed from disk thoroughly. @@ -39,7 +41,7 @@ rm pubkey.pem # Test: Let's run systemd-cryptsetup to test if this all worked. The option string should contain the full # PKCS#11 URI we have in the clipboard, it tells the tool how to decypher the encrypted LUKS key. -systemd-cryptsetup attach mytest /dev/sda1 /etc/encrypted-luks-key.bin 'pkcs11-uri=pkcs11:…' +sudo systemd-cryptsetup attach mytest /dev/sdXn /etc/encrypted-luks-key.bin 'pkcs11-uri=pkcs11:…' # If that worked, let's now add the same line persistently to /etc/crypttab, for the future. -echo "mytest /dev/sda1 /etc/encrypted-luks-key 'pkcs11-uri=pkcs11:…' >> /etc/crypttab +sudo bash -c 'echo "mytest /dev/sdXn /etc/encrypted-luks-key \'pkcs11-uri=pkcs11:…\'" >> /etc/crypttab' From 12f69587e97361b5eedaff4ad0a68843dc45219c Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Tue, 17 Dec 2019 18:39:53 +0100 Subject: [PATCH 2/4] cryptsetup-pkcs11: refuse keys above 16MiB size --- src/cryptsetup/cryptsetup-pkcs11.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/cryptsetup/cryptsetup-pkcs11.c b/src/cryptsetup/cryptsetup-pkcs11.c index c259a766d7..5c4d3acc97 100644 --- a/src/cryptsetup/cryptsetup-pkcs11.c +++ b/src/cryptsetup/cryptsetup-pkcs11.c @@ -12,12 +12,15 @@ #include "cryptsetup-pkcs11.h" #include "escape.h" #include "fd-util.h" +#include "format-util.h" #include "macro.h" #include "memory-util.h" #include "pkcs11-util.h" #include "stat-util.h" #include "strv.h" +#define KEY_FILE_SIZE_MAX (16U*1024U*1024U) /* 16 MiB */ + static int load_key_file( const char *key_file, size_t key_file_size, @@ -50,8 +53,13 @@ static int load_key_file( if (st.st_size == 0) return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Key file is empty, refusing."); - if ((uint64_t) st.st_size > SIZE_MAX) - return log_error_errno(SYNTHETIC_ERRNO(ERANGE), "Key file too large, refusing."); + if ((uint64_t) st.st_size > KEY_FILE_SIZE_MAX) { + char buf1[FORMAT_BYTES_MAX], buf2[FORMAT_BYTES_MAX]; + return log_error_errno(SYNTHETIC_ERRNO(ERANGE), + "Key file larger (%s) than allowed maximum size (%s), refusing.", + format_bytes(buf1, sizeof(buf1), st.st_size), + format_bytes(buf2, sizeof(buf2), KEY_FILE_SIZE_MAX)); + } if (key_file_offset >= (uint64_t) st.st_size) return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Key file offset too large for file, refusing."); From 3ded1d616a50d4b5c1dfe6cbe9536bb93d117597 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Tue, 17 Dec 2019 18:40:26 +0100 Subject: [PATCH 3/4] cryptsetup-pkcs11: line break some overly long lines --- src/cryptsetup/cryptsetup-pkcs11.c | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/cryptsetup/cryptsetup-pkcs11.c b/src/cryptsetup/cryptsetup-pkcs11.c index 5c4d3acc97..fab705708b 100644 --- a/src/cryptsetup/cryptsetup-pkcs11.c +++ b/src/cryptsetup/cryptsetup-pkcs11.c @@ -121,7 +121,16 @@ static int pkcs11_callback( /* Called for every token matching our URI */ - r = pkcs11_token_login(m, session, slot_id, token_info, data->friendly_name, "drive-harddisk", "pkcs11-pin", data->until, NULL); + r = pkcs11_token_login( + m, + session, + slot_id, + token_info, + data->friendly_name, + "drive-harddisk", + "pkcs11-pin", + data->until, + NULL); if (r < 0) return r; @@ -134,7 +143,14 @@ static int pkcs11_callback( if (r < 0) return r; - r = pkcs11_token_decrypt_data(m, session, object, data->encrypted_key, data->encrypted_key_size, &data->decrypted_key, &data->decrypted_key_size); + r = pkcs11_token_decrypt_data( + m, + session, + object, + data->encrypted_key, + data->encrypted_key_size, + &data->decrypted_key, + &data->decrypted_key_size); if (r < 0) return r; From 6789dd57f0a0b6caf764ce43b9233745c7e040ae Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Tue, 17 Dec 2019 18:40:46 +0100 Subject: [PATCH 4/4] cryptsetup-pkcs11: just return zero on success, no need to return anything else --- src/cryptsetup/cryptsetup-pkcs11.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cryptsetup/cryptsetup-pkcs11.c b/src/cryptsetup/cryptsetup-pkcs11.c index fab705708b..ec9186a6aa 100644 --- a/src/cryptsetup/cryptsetup-pkcs11.c +++ b/src/cryptsetup/cryptsetup-pkcs11.c @@ -154,7 +154,7 @@ static int pkcs11_callback( if (r < 0) return r; - return 1; + return 0; } int decrypt_pkcs11_key(