quick-docs/modules/ROOT/pages/using-yubikeys.adoc

301 lines
14 KiB
Text
Raw Normal View History

2023-01-03 14:30:27 +00:00
= Using Yubikeys with Fedora
The Fedora docs team
:revnumber: unknown
:revdate: 2022-06-16
:category: Using
// :tags: From Source
[CAUTION]
====
This page has not been thoroughly reviewed for technical accuracy for quite some time. This means any information on this page may be outdated or inaccurate. Reviews for technical accuracy are greatly appreciated. If you want to help, either edit it or open a ticket using the appropriate button on the right side below the banner.
====
== What is a YubiKey?
2023-01-03 14:30:27 +00:00
A YubiKey is a small USB and NFC based device, a so called https://developers.yubico.com/Developer_Program/Guides/YubiKey_Hardware.html[hardware security token], with modules for many security related use-cases. It generates one time passwords (OTPs), stores private keys and in general implements different authentication protocols. They are created and sold via a company called http://yubico.com/[Yubico].
2023-01-03 14:30:27 +00:00
For more information about YubiKey features, see their https://yubico.com/products/[product page].
2023-01-03 14:30:27 +00:00
== How do I get a yubikey?
You can purchase a yubikey from http://store.yubico.com/[Yubico's website].
== Consider a backup YubiKey
2023-01-03 14:30:27 +00:00
As soon as you start working with security tokens you have to account for the potential to lock yourself out of accounts tied to these tokens. As hardware security tokens are unique and designed to be extremely hard to copy you can't just make a backup of it like you can with software vaults like Keepass or AndOTP. Because of this all registrations you do with your primary key you should immediately do with a second backup key that you store in a secure location like a safe or at least always leave at home.
2023-01-03 14:30:27 +00:00
In practice this means to register both hardware tokens with your linux and web accounts, generate private keys twice and configure both public keys at e.g. github.
2023-01-03 14:30:27 +00:00
== Storage limitations
2023-01-03 14:30:27 +00:00
For some features private keys and other secrets are stored on the YubiKey. Each feature has it's own storage space and hence https://support.yubico.com/hc/en-us/articles/360013790319-How-many-accounts-can-I-register-my-YubiKey-with-[maximum number of credential slots]:
2023-01-03 14:30:27 +00:00
- OTP - Unlimited, as only one secret per key is required
- FIDO U2F - Unlimited, as only one secret per key is required
- FIDO2 - 25 credentials
- OATH - 32 credentials
- PIV - 24 x509 certificates and their respective private keys
- OpenPGP - 3 keys; one for encryption, signing and authentication each
2023-01-03 14:30:27 +00:00
== Using a YubiKey to authenticate to a machine running Fedora
2023-01-03 14:30:27 +00:00
Local system authentication uses https://www.redhat.com/sysadmin/pluggable-authentication-modules-pam[Pluggable Authentication Modules (PAM)]. There are two ways to configure the YubiKey PAM module to authenticate users. Either via the YubiCloud or using challenge-response. The YubiCloud is the standard method but depends on Yubico's cloud to validate your OTPs and hence requires constant internet access.
2023-01-03 14:30:27 +00:00
The setup is as follows: install the PAM module, register a YubiKey with your user account, create base configuration for either of the two authentication options and then choose the PAM configuration you want to use the YubiKey.
2023-01-03 14:30:27 +00:00
Install the PAM yubico module from the official repositories:
2023-01-03 14:30:27 +00:00
[source, bash]
[…]$ sudo dnf install pam_yubico
2023-01-03 14:30:27 +00:00
Register a currently connected YubiKey with your user account with
2023-01-03 14:30:27 +00:00
[source, bash]
[…]$ ykpamcfg -2 -v
2023-01-03 14:30:27 +00:00
Or for any other system user using sudo with
2023-01-03 14:30:27 +00:00
[source, bash]
[…]$ sudo -u someuser ykpamcfg -2 -v
2023-01-03 14:30:27 +00:00
Create two base configuration files in /etc/pam.d. yubikey-required and yubikey-sufficient.
2023-01-03 14:30:27 +00:00
For YubiCloud use the following:
2023-01-03 14:30:27 +00:00
[source]
#%PAM-1.0
auth required pam_yubico.so id=[Your API Client ID] key=[Your API Client Key]
2023-01-03 14:30:27 +00:00
[source]
#%PAM-1.0
auth sufficient pam_yubico.so id=[Your API Client ID] key=[Your API Client Key]
2023-01-03 14:30:27 +00:00
[CAUTION]
====
Note that the key is optional but without it there is no TLS verification which makes this susceptible to MitM attacks by default. Obtain a key at https://upgrade.yubico.com/getapikey[Yubico].
====
2023-01-03 14:30:27 +00:00
[NOTE]
====
If you have SELinux on the enforcing mode (the default mode), you should flip on the allow_ypbind boolean first, because pam_yubico needs to be able to connect to Yubico's online authentication. servers.
2023-01-03 14:30:27 +00:00
[source, bash]
[…]$ sudo setsebool -P allow_ypbind=1
Also, in order to allow sshd to access /root/.yubico/authorized_yubikeys, you should change its context:
2023-01-03 14:30:27 +00:00
[source, bash]
[…]$ chcon -R system_u:object_r:ssh_home_t:s0 /root/.yubico
====
For challenge-response use the following:
2023-01-03 14:30:27 +00:00
[source]
#%PAM-1.0
auth required pam_yubico.so mode=challenge-response
2023-01-03 14:30:27 +00:00
[source]
#%PAM-1.0
auth sufficient pam_yubico.so mode=challenge-response
2023-01-03 14:30:27 +00:00
[NOTE]
====
You may add the debug option at the end of these lines right after the mode option to get troubleshooting information in journald.
====
Next configure PAM to accept a YubiKey as a means of authentication. There are many options in /etc/pam.d to modify and add a YubiKey, but the most common use-cases are:
- /etc/pam.d/login
- /etc/pam.d/gdm
- /etc/pam.d/sudo
- /etc/pam.d/sshd
In a PAM configuration file if using yubikey-sufficient add an include line before or if using yubikey-required add it after a line that reads "auth substack system-auth" or "auth include system-auth". An include of yubikey-sufficient looks like this:
[source]
auth include yubikey-sufficient
2023-01-03 14:30:27 +00:00
The following example sets a YubiKey OTP as 'sufficient' factor for terminal login. This means that a YubiKey alone is enough to authenticate a user when logging in on a terminal.
2023-01-03 14:30:27 +00:00
Open /etc/pam.d/login with your editor of choice. Find the line that reads "auth substack system-auth". Above that, insert the following:
2023-01-03 14:30:27 +00:00
[source]
auth include yubikey-sufficient
2023-01-03 14:30:27 +00:00
The result looks similar to this:
2023-01-03 14:30:27 +00:00
[source]
#%PAM-1.0
auth substack system-auth
auth include postlogin
account required pam_nologin.so
account include system-auth
password include system-auth
# pam_selinux.so close should be the first session rule
session required pam_selinux.so close
session required pam_loginuid.so
# pam_selinux.so open should only be followed by sessions to be executed in the user context
session required pam_selinux.so open
session required pam_namespace.so
session optional pam_keyinit.so force revoke
session include system-auth
session include postlogin
-session optional pam_ck_connector.so
2023-01-03 14:30:27 +00:00
Next time you open a console (local, not ssh session) and attempt to login you should be prompted `YubiKey for '<user>':`. Tap your YubiKey to input an OTP and you will be logged without entering a password.
2023-01-03 14:30:27 +00:00
[CAUTION]
====
When using the yubikey-required option make sure to test this thoroughly in another session without closing your current one to mitigate locking yourself out of the system.
====
To add a YubiKey to more than terminal login, like local sshd servers, sudo or GDM login, add the respective auth include to one of the other configuration files in /etc/pam.d.
2023-01-03 14:30:27 +00:00
== Customizing a YubiKey with Fedora
2023-01-03 14:30:27 +00:00
A YubiKey comes pre-configured for Yubico OTP, but apart from that it uses default PINs for every other feature which you'll most likely want to change before use. There is software for customizing the YubiKey in the official repositories.
2023-01-03 14:30:27 +00:00
There are essentially two tools to use together with their respective GUI variants. 'yubikey-manager' and 'ykpersonalize'. The former is newer but supports less options than the latter. For all available options install both.
2023-01-03 14:30:27 +00:00
[source, bash]
[…]$ sudo dnf install ykpers
2023-01-03 14:30:27 +00:00
There is a gui for this command:
2023-01-03 14:30:27 +00:00
[source, bash]
[…]$ sudo dnf install yubikey-personalization-gui
2023-01-03 14:30:27 +00:00
There is a more recent, simpler tool, ykman:
[source, bash]
[…]$ sudo dnf install yubikey-manager
2023-01-03 14:30:27 +00:00
YubiKey manager also has a gui:
2023-01-03 14:30:27 +00:00
[source, bash]
[…]$ sudo dnf install yubikey-manager-gui
2023-01-03 14:30:27 +00:00
=== Writing a new static password to the second slot of the key
Newer YubiKeys (YubiKey 2+) have the ability to store two separate configurations. The first is generally used for OTPs, the second for a strong, static password. If the button is pressed shortly, something up to 1.5 seconds, the first configuration is triggered. If the button is pressed longer, in the range of 2.5 to 5 seconds, the second configuration is triggered.
2023-01-03 14:30:27 +00:00
Write a static key using ykman otp static.
2023-01-03 14:30:27 +00:00
[source, bash]
[…]$ ykman otp static 2 cbdefghijklnrtuv
2023-01-03 14:30:27 +00:00
A more elaborate example: write a new static key to the second configuration slot using a specific AES key.
[source, bash]
[…]$ ykpersonalize -oappend-cr -a123456deadcafebeef65432112345678 -2 -o-man-update
This writes a static key to the YubiKey based on the 32-byte AES key specified with the -a option. The -2 option sets the second slot as target. The other two options are a matter of personal taste. The append-cr option sends a carriage return as the last character of the key. That way I do not have to press <ENTER> myself. The -man-update option disables easy updating of the static key in the YubiKey. Enabling this will allow for altering the static password without the use of ykpersonalize.
2023-01-03 14:30:27 +00:00
=== Writing a new AES key to the first slot of the key
If we want to write a new configuration to the first slot of the key, we need to specify some more options. If you want to be able to upload you key to Yubico, in order to authenticate against their servers, remember what the values are that you use below. You will need them later on.
[source, bash]
[…]$ sudo ykpersonalize -1 -ofixed=vvhhhrhkhgidic -ouid=deadbeefcafe -a123456deadcfaebeef65432112345678 -oappend-cr
The -1 option tells ykpersonalize to use the first configuration. The fixed option specifies the public ID of the Yubikey. This is referred to as the 'prefix' later on, when we go uploading it. The value you use here has to start with 'ff' in hex or 'vv' in modhex (xref:#_what_is_modhex[see below]). Yubico enforces this when you try to upload your key to their servers. The value for the fixed option can be up to 16 characters in length.
As part of the OTP, you can specify an internal identifier for your key. This is what the uid option does. The value is in plain hex, not modhex and ''exactly'' 12 character long.
The -a option, again, is the 32-byte AES key and append-cr appends a carriage return to my key as the last character.
When I hit the <ENTER> key, the ykpersonalize program will present me with my options and ask for
confirmation before continuing:
====
Firmware version 2.1.1 Touch level 1795 Program sequence 3
Configuration data to be written to key configuration 1:
fixed: m:vvhhhrhkhgidic
uid: h:deadbeefcafe
key: h:123456deadcfaebeef65432112345678
acc_code: h:000000000000
ticket_flags: APPEND_CR
config_flags:
Commit? (y/n) [n]:
====
After pressing 'y', I am able to generate OTPs with my new key!
==== What is modhex?
When plugged in, the operating system treats the Yubikey as a USB keyboard. USB keyboards send scancodes to the operating system, which the operating system then interprets as keystrokes. The Yubikey has to make sure no ambiguity arises: there are many different kinds of keyboard layouts and the scancodes have to be interpreted as the same character on machines using every random keyboard layout out there. To fix this, the people of Yubico have created 'modhex',
which is a modified representation of hexadecimal characters that uses only 'safe' characters. 'Safe' characters are basically characters which have the same scancode on all keyboard layouts.
=== Uploading the generated AES key to Yubico
If you want to customize your YubiKey's AES key but still want to use it to authenticate through Yubico's servers, you can upload the key through https://upgrade.yubico.com/getapikey/. You will need to enter your email address and YubiKey's OTP.
=== Update the PINs of the PIV module
The https://www.yubico.com/authentication-standards/smart-card/[Personal Identitiy Verification (PIV)] module stores private keys and corresponding certificate files for purposes such as encryption, authentication and signatures. If your YubiKey supports this you want to change the PIN and PUK as well as the Management Key.
Set the PIN.
[source, bash]
[…]$ ykman piv access change-pin
Enter the current PIN: 123456
Enter the new PIN: ********
Repeat for confirmation: ********
New PIN set.
Set the PUK.
[source, bash]
[…]$ ykman piv access change-puk
Enter the current PUK: 12345678
Enter the new PUK: ********
Repeat for confirmation: ********
New PUK set.
Update the Management Key.
[source, bash]
[…]$ ykman piv access change-management-key --generate --protect
Enter the current management key [blank to use default key]:
Enter PIN: ********
You can now safely use the PIV module to generate private keys and store certificates.
=== Change the PIN of the FIDO2 module
https://www.yubico.com/authentication-standards/fido2/[FIDO2] is an open authentication standard and encompasses sub-standards and protocols to either provide two-factor or even passwordless authentication methods.
One interesting use case of the FIDO module to note is storing OpenSSH public-key identities, which modern OpenSSH agents can pick up right away and use. This makes ssh keys quite portable.
If your key supports FIDO change it's pin with ykman fido access like this:
[source, bash]
[…]$ ykman piv access change-pin
Enter the current PIN: 123456
Enter the new PIN: ********
Repeat for confirmation: ********
New PIN set.
=== Configure a password for OATH
The OATH feature provides TOTP and HOTP authentication protocols. It can be protected with a passphrase to access and generate OTP codes. This is different from the Yubico OTP feature, which uses a single stored secret on the YubiKey for challenge-response.
Change the OATH password with:
[source, bash]
[…]$ ykman oath access change
Enter the new password:
Repeat for confirmation:
Configure your device to remember this password so you don't have to re-enter it anymore.
[source, bash]
[…]$ ykman oath access remember
2023-01-03 14:30:27 +00:00
== Using the Yubikey to authenticate to websites
As of 2019, there is work in place to attempt to standardize using a yubikey on the web. The new standard is called WebAuthn, and you can learn more about it here: https://www.yubico.com/solutions/webauthn/. For now, the easiest way to see which platforms support the yubikey is by browsing https://www.yubico.com/works-with-yubikey/catalog/[yubico's catalog].