yubico-piv-tool: ykcs11: 'agent refused operation' OS X 10.13.6

Hi,

I am aware of #88 featuring @paul-pearce & co, but that does not help and is also partly obsolete because PIV functionality is now built-in to OS X. I have also seen #118, but not much of interest there, partly for the same reasons.

To summarise where I am at:

Basic setup has been done:

yubico-piv-tool -achange-pin
yubico-piv-tool -achange-puk
yubico-piv-tool -k -agenerate -s9c -AECCP384

This: ssh-add -s/Users/foobar/Work/yubico-piv-tool-1.6.0-mac/lib/libykpiv.dylib

Does not work, it yields agent refused operation.

So then I compiled the latest versions of libressl (2.8.0) and openssh (7.7p1) from scratch, thinking it might be something with the Apple binaries (OS X 10.13.6 ships with OpenSSH_7.6p1 compiled against LibreSSL 2.6.2), but that yields the same agent refused operation, e.g.:

libre/bin/ssh-add -s/Users/foobar/Work/yubico-piv-tool-1.6.0-mac/lib/libykcs11.dylib

I then read about the whitelist, and so I tried this:

libre/bin/ssh-agent -P/Users/foobar/Work/yubico-piv-tool-1.6.0-mac/lib/*

SSH_AUTH_SOCK=/path/to/sock/agent.96574 libre/bin/ssh-add -s/Users/foobar/Work/yubico-piv-tool-1.6.0-mac/lib/libykcs11.dylib

That yields the same errorL

Could not add card “/Users/foobar/Work/yubico-piv-tool-1.6.0-mac/lib/libykcs11.dylib”: agent refused operation

And running ssh-agent in debug (-d) mode doesn’t yield much of interest:

debug2: fd 3 setting O_NONBLOCK
debug3: fd 4 is O_NONBLOCK
debug1: process_message: socket 1 (fd=4) type 20
debug1: process_add_smartcard_key: add /Users/foobar/Work/yubico-piv-tool-1.6.0-mac/lib/libykcs11.dylib

Finally, I should add that for all the tests above, I have also tried the /usr/local/lib trick as mentioned i the previous issues, and that made no difference.

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 15 (4 by maintainers)

Most upvoted comments

I would like to report that getting PIV-based SSH authentication working on macOS Monterey has been an absolutely horrendous experience. It took me hours and hours to figure everything out. Here are some of the issues I encountered (as far as I can remember them):

  • Following the instructions in Using PIV for SSH through PKCS #11, one of the first steps failed. After googling the error, I read that yubico-piv-tool is on the way out to be replaced by ykman, which was able to generate keys and certificates. EDIT: Ok, I misunderstood that, but yubico-piv-tool did fail, requiring me to look for alternatives.
  • Since the official documentation was outdated, I somehow ended up following guides that suggested installing the OpenSC cask through Homebrew. I was able to successfully log into an SSH server with an ECCP256 key on my Yubikey 5 NFC.
  • After looking for the cause for hours, I finally discovered that OpenSC broke PIV authentication for logging into macOS! So I uninstalled OpenSC, but I’m left to configure PIV SSH Authentication in another way.
  • Again, after a long search, I found that Apple ships a PKCS11 provides with the OS: ssh-keychain.dylib. But that doesn’t work with the Yubikey however, for whatever reason. No mention of it in Yubico’s docs.
  • Other articles and bugtracker threads pointed me to libykcs11.so. But it appears this ships with yubico-piv-tool, so I’m not very confident this will work (since I read earlier it was deprecated in favor of ykman).
  • Unfortunately, ssh-agent shipped with macOS will refuse to load libykcs11.dylib from the location where Homebrew installs it. Because it can’t simply say that in plain language, I was in for several more hours of hunting for the reason why this wasn’t working. More information: SSH PIV authentication on MacOS without errors
  • Additionally, the combination of Monterey’s OpenSSH and libykcs11.so do not support EC keys. This is not mentioned in the Yubico docs either. ssh-keygen -D /usr/local/lib/libykcs11.dylib does list an RSA key, but that turned out to be the key in slot f9, which is not useable for SSH authentication (why does ssh-keygen list it then?).

After all this, I can’t say that I am a satisfied customer. I am aware that Apple likes to move stuff around with every OS upgrade, which is part of the problem here. But Yubico can make the experience infinitely more smooth for their customers by making sure their documentation is up to date. Please!

I chose to generate an RSA key in slot 9c instead of overwriting my ECCP256 key in slot 9a:

ykman piv keys generate --pin-policy NEVER --touch-policy ALWAYS 9c 9c.pem
ykman piv certificates generate -s "SSH Key" -d 10950 9c 9c.pem

I recommend using the CACHED pin policy instead if you’re running remote VSCode sessions over SSH, because it seems to make several connections requiring you to touch the key every time.

After telling ssh-keygen to accept /usr/local/lib/libykcs11.dylib run with the -P argument (see this gist and that gist, but see NOTE below), these work:

ssh-keygen -D /usr/local/lib/libykcs11.dylib
ssh-add -s /usr/local/lib/libykcs11.dylib

NOTE: It’s better to disable the existing com.openssh.ssh-agent launch agent and make your custom one use SSH_AUTH_SOCK directly. This will avoid having to override SSH_AUTH_SOCK with SSH_AUTH_SOCK_LOCAL in various places.

I need to add libykcs11.so to the agent because I wanted to avoid having to enter the PIN every time I connect (ykman’s --pin-policy doesn’t seem to have any effect).

But then there’s the issue that you need to remove and add libykcs11.so from/to the agent if the Yubikey was unplugged (#88#issuecomment-279102884). The script below, which I named yubissh, helps with that (not tested thoroughly!).

Note: you need to create the ~/.local/share/yubissh directory first.

#!/bin/bash

# Wrapper for 'ssh' that only asks for your Yubikey's PIN when needed
#
# Adds the PKCS11 provider to the SSH agent if needed, but only if the Yubikey
# is plugged in. Also handles the fact that the PKCS11 provider needs to be
# removed and added again if the Yubikey was unplugged in the meantime.
# (https://github.com/Yubico/yubico-piv-tool/issues/88#issuecomment-279102884)

SESSION_ID_FILE=$HOME/.local/share/yubissh/last_session_id

# https://gist.github.com/vancluever/de1c3985c8f9e2a3c4bdc42a057e075e
if [ -n "${SSH_AUTH_SOCK_LOCAL}" ]; then
  export SSH_AUTH_SOCK="${SSH_AUTH_SOCK_LOCAL}"
fi

if [[ $(sc_auth identities) ]]; then
    PREV_SESSION_ID=$(cat $SESSION_ID_FILE 2>/dev/null)
    SESSION_ID=$(ioreg -r -p IOUSB -n "YubiKey OTP+FIDO+CCID" -a | plutil -extract "0.sessionID" raw -)
    if [[ "$SESSION_ID" != "$PREV_SESSION_ID" ]]; then
        ssh-add -e /usr/local/lib/libykcs11.dylib
        ssh-add -s /usr/local/lib/libykcs11.dylib
    fi
    echo $SESSION_ID > $SESSION_ID_FILE
fi

ssh "$@"

Both will be supported. Even though there’s overlap when it comes to the YubiKey’s PIV application, the two products have different use cases. The ykman is a tool to manage the YubiKey with its different applications (PIV, FIDO, OTP…etc). yubico-piv-tool is mainly a C library and a PKCS11 module that manages only the PIV application. While yubico-piv-tool as a command line tool can fully be used to manage the YubiKey PIV application just as well as ykman, there are few edge cases where the functionality is available through yubico-piv-tool but not ykman. See https://developers.yubico.com/PIV/Tools.html for some more details

Thanks @brechtm and others. Needless to say this kind of feedback is significantly more useful. There is a longer conversation to be had about documentation and constantly having to play catch-up with a lot of other moving parts like OS upgrades and third-party project changes, but this is not the place for it.

Ultimately the goal is to make the user experience as good as it can be. I hear you (all) when you say that we’re not there, but there’s a lot of things at play. I’ll pass the feedback along and take the conversation internally.

Finally, regarding YubiHSM, that has a different set of challenges, but please open issues in the relevant projects or a support ticket if nothing applies. Feature requests for hardware devices are a separate topic, but bug reports, contributions and documentation change requests and suggestions are always welcome.