Skip to content

Implement support for Yubikeys and potential other tokens via wireless NFC using smartcard readers#6766

Closed
StarGate01 wants to merge 9 commits intokeepassxreboot:refactor/internalize-ykcorefrom
StarGate01:feature/yubikey-pcsc
Closed

Implement support for Yubikeys and potential other tokens via wireless NFC using smartcard readers#6766
StarGate01 wants to merge 9 commits intokeepassxreboot:refactor/internalize-ykcorefrom
StarGate01:feature/yubikey-pcsc

Conversation

@StarGate01
Copy link
Copy Markdown
Contributor

@StarGate01 StarGate01 commented Jul 25, 2021

This fixes #4090 and arekinath/YkOtpApplet#3 , and might fix #6439 . It also potentially provides a base for #255 .

Rationale

I want to use my Yubikeys with KeePassXC not (only) by plugging them in via USB, but also by wirelessly presenting them to a NFC reader which is connected via USB or built-into my computer - just like I do on my phone.

Eventually, other hardware tokens which do not have a USB interface at all but implement the wireless protocol will be supported automatically as well. Examples include eg. the Vivokey or any smartcard running vk-ykhmac.

Screenshots

Screenshot_2021-07-26_01-49-21

Explanation of changes

This new interface uses the PC/SC protocol and API, instead of the USB protocol via the ykpers library. Both interfaces can be used in parallel and the same key can be used on both interfaces without reprogramming it.

Many YubiKeys expose their functionality as a smartcard / CCID device, which can be interfaced with using PC/SC. This is especially useful for NFC-only or NFC-capable Yubikeys, when they are used together with a PC/SC compliant NFC reader device.

Essentially, this implementation does the same as the ykdroid module for Keepass2Android:
It provides a way to wirelessly (depending on the reader) interface a YubiKey or any key which implements the Yubikey challenge-response HMAC-SHA1 protocol.

Although many (not all) Yubikeys expose their CCID functionality over their own USB connection as well, the HMAC-SHA1 functionality is often locked in this mode, as it requires eg. a touch on the gold button. When accessing the smartcard functionality wirelessly via NFC (like this code can do using a reader), then the user interaction is to present the key to the reader, just like you would tap the key against your phone when using ykdroid. A continuously presented key eventually times out due to security concerns.

This implementation has been tested on Linux using pcsc-lite, Windows using the native Winscard.dll library, and Mac OSX using the native PCSC-framework (which is a port of pcsc-lite by Apple) library.

Changes to the build system

Since this feature requires the PCSC API libraries for each OS, the cmake configuration has been changed to automatically find and link the correct one.

A new build flag (-DWITH_XC_YUBIKEY_PCSC=[ON|OFF], requires -DWITH_XC_YUBIKEY=ON, default: OFF, but included by -DWITH_XC_ALL=ON) can be used to control whether the new interface is compiled in. If this if OFF, the functionality remains unchanged, only the existing USB interface is enabled, and no PCSC library is required.

Package maintainers on Linux have to add a dependency on pcsc-lite if they want to include this feature.

Smaller changes

Since the Yubikey can now be a wireless token as well, the verb "plug in" was replaces with a more generic "interface with". This shall indicate that the user has to present their token to the reader, or plug it in via USB.

The hardware key selection menu in the UI now additionally displays how a token is connected (USB or PCSC) and its firmware version.

A sample "third-party" hardware token has been added. This is mainly for future developers to be used as a reference for other smartcards.

Testing strategy

The new interface has been compiled and tested successfully on Linux x64 5.4.131-1-MANJARO, Windows 10 x64 21H1, and Mac OSX x64 18.2.0 Mojave (using VirtualBox) using the documented build tools.

The hardware keys used for testing were:

In general, all PC/SC compliant CCID devices which implement the Yubikey HMAC-SHA1 challenge-response protocol should work.

The readers used for testing were:

In general, all PC/SC compliant NFC-capable smartcard readers with CCID / PC/SC driver support for the target OS should work.

The keys were programmed with the same HMAC-SHA1 secret, and verified to be able to generate a valid key to unlock the database in all modes and slot configurations.

Type of change

  • ✅ New feature (change that adds functionality)

Workflow note

I realize this is a rather large PR, and especially since this is my first one to this project I am happy to receive any feedback concerning conventions, methodology or anything else. If there are questions or concerns regarding the implementation, I am willing to adjust the code if needed.

If there are things that should be changed or any mistakes, please point them out. Although I did my best to verify and test on all systems using all the hardware I have available, some things might have slipped though.

If this is something you do not want to add at all, I would like to discuss why and what would have to be changed or implemented instead.

Sources and further reading

* Allow for multiple vendor ID's to be checked at once. This allows for the use of one tracking index, streamlining KPXC code.
* Remove support for libusb 0.x on Linux
* Better handling of USB errors during initial key query. Output warnings to console.
@StarGate01
Copy link
Copy Markdown
Contributor Author

The CI build on Linux probably fails due to the missing pcsc-lite package on the build system.

As for the code format check I have no idea. I ran make format as documented and integrated all changes.

@droidmonkey
Copy link
Copy Markdown
Member

Please rebase this onto my yubikey pr

@droidmonkey
Copy link
Copy Markdown
Member

Also, holy shit you are awesome!

@StarGate01
Copy link
Copy Markdown
Contributor Author

Please rebase this onto my yubikey pr

I assume you mean #6654 ?

Also, holy shit you are awesome!

I am glad you like it, it was quite a challenge to get this running smoothly on all three systems.

@droidmonkey
Copy link
Copy Markdown
Member

droidmonkey commented Jul 26, 2021

I assume you mean #6654 ?

Yes, that will make this easier to merge once we merge that one first.

it was quite a challenge to get this running smoothly on all three systems.

That is definitely a challenge for a lot of our integrations, very tough project to make this work without a hitch!

This requires a new library dependency: PCSC.
The PCSC library provides methods to access smartcards.
On Linux, the third-party pcsc-lite package is used.
On Windows, the native Windows API (Winscard.dll) is used.
On Mac OSX, the native OSX API (framework-PCSC) is used.
The main YubiKey file now acts as a gateway
to multiple interfaces, which each implement
YubiKeyInterface.
Since the Yubikey can now be a wireless token as well,
the verb "plug in" was replaces with a more
generic "interface with". This shall indicate
that the user has to present their token to the
reader, or plug it in via USB.
This new interface uses the PC/SC protocol and API
instead of the USB protocol via ykpers.
Many YubiKeys expose their functionality as a CCID
device, which can be interfaced with using PC/SC.
This is especially useful for NFC-only or NFC-capable Yubikeys,
when they are used together with a PC/SC
compliant NFC reader device.

Essentially, this implementation does the same
as the ykdroid module for Keepass2Android:
It provides a way to wirelessly (depending on the reader)
interface a YubiKey or any key which implements the
Yubikey challenge-response HMAC-SHA1 protocol.

Although many (not all) Yubikeys expose their CCID functionality
over their own USB connection as well, the HMAC-SHA1
functionality is often locked in this mode, as it requires eg. a
touch on the gold button. When accessing the CCID functionality
wirelessly via NFC (like this code can do using a reader), then
the user interaction is to present the key to the reader,
just like you would tap the key against your phone
when using ykdroid.

This implementation has been tested on Linux using pcsc-lite,
Windows using the native Winscard.dll library,
and Mac OSX using the native PCSC-framework library.
@StarGate01 StarGate01 force-pushed the feature/yubikey-pcsc branch 2 times, most recently from 4686430 to 9c44cf7 Compare July 26, 2021 15:46
@StarGate01
Copy link
Copy Markdown
Contributor Author

Alright, I rebased the feature branch onto your PR. Everything still works according to my tests.

develop has since gained one additional commit, but I chose to not merge or rebase on that too since your PR does not do so as well.

In addition, I added a small fix for Windows, which enables USB hotplugging for reader devices. I also clarified the YubiKey security timeout flow.

@StarGate01 StarGate01 changed the base branch from develop to refactor/internalize-ykcore July 26, 2021 15:54
@StarGate01
Copy link
Copy Markdown
Contributor Author

StarGate01 commented Jul 26, 2021

If you do not have a NFC Yubikey or a NFC PC/SC card reader, I added instructions on how to emulate the PC/SC HMAC-SHA1 functionality of a Yubikey completely in software on Linux: https://github.com/StarGate01/vk-ykhmac#readme .

@droidmonkey
Copy link
Copy Markdown
Member

Excellent, we'll merge my pr first then rebase yours onto develop

@droidmonkey droidmonkey force-pushed the refactor/internalize-ykcore branch from 9e88fc4 to 7cb8265 Compare August 22, 2021 21:17
@droidmonkey droidmonkey force-pushed the refactor/internalize-ykcore branch from 7cb8265 to 7db9e3a Compare September 5, 2021 12:51
@droidmonkey droidmonkey deleted the branch keepassxreboot:refactor/internalize-ykcore September 5, 2021 13:11
@droidmonkey droidmonkey closed this Sep 5, 2021
@StarGate01
Copy link
Copy Markdown
Contributor Author

Should I rebase this PR on the current state of develop ?

@droidmonkey
Copy link
Copy Markdown
Member

droidmonkey commented Sep 5, 2021

How'd this get closed, stupid github. Yes please rebase. I think this closed cause the branch was deleted, once you rebase and set this up to track develop then I can reopen it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature: Hardware Keys pr: new feature Pull request adds a new feature pr: refactoring Pull request refactors code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Yubikey Support via MS RDP HMAC-SHA1 challenge response via NFC

3 participants