GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - lkingland/smartsocket: Use a YubiKey (or local keys) for GPG auth and signing when either local or connected remotely

BranchesTags
Open more actions menu

Latest commit

 

History

16 Commits
16 Commits

Folders and files

NameName
Last commit message
Last commit date
assets
assets
 
 
cmd
cmd
 
 
docs
docs
 
 
.gitignore
.gitignore
 
 
ARCHITECTURE.md
ARCHITECTURE.md
 
 
LICENSE
LICENSE
 
 
Makefile
Makefile
 
 
README.md
README.md
 
 
go.mod
go.mod
 
 
gpg-agent-local.service
gpg-agent-local.service
 
 
gpg-agent-local.socket
gpg-agent-local.socket
 
 
gpg-agent-ssh-local.socket
gpg-agent-ssh-local.socket
 
 
smartsocket-gpg.socket
smartsocket-gpg.socket
 
 
smartsocket-ssh.socket
smartsocket-ssh.socket
 
 
smartsocket.service
smartsocket.service
 
 

Repository files navigation

smartsocket: a YubiKey plugged into a circuit-traced brain

Carry your YubiKey anywhere, and every machine you SSH into signs and authenticates with it as if it were plugged in.

smartsocket is a small router that sits behind the standard gpg-agent and ssh-agent sockets. For every request it finds the key that can actually do the work, whether that's in the machine in front of you or forwarded from the one you're holding, and sends the request there. Your tools never know it exists.

New to YubiKeys? Start with the YubiKey Setup Guide. It takes you from a blank key to signing and authenticating, then hands back here for forwarding.

What it does

  • Your key follows you. SSH into your desktop from a laptop, and commits you make there are signed by the key in your hand.
  • Or it stays put. Sit at the desk with the key plugged in, and everything works locally, session or no session.
  • Two keys, no thought. A forwarded key wins. Pull it, and the key in the machine takes over on the very next request.
  • It routes on keys, not sockets. It asks each agent whether it can actually sign. An empty forward never hijacks you.
  • PIN prompts land on your screen. When the server's own card needs a PIN, your laptop's native dialog asks for it.
  • It's a drop-in. It owns the standard socket paths, so ssh, git and gpg need no changes and no per-host config.
  • One router for both. GPG signing and SSH authentication go through the same logic.

Why not just forward the agent?

Forwarding a hardware key has always meant picking one of two half-solutions:

  • ssh-agent-switcher gives you a stable socket path that proxies to whichever forwarded agent is live. It's great for surviving tmux, but it's SSH-only and can't tell whether the key behind the socket can do anything.
  • The gpg-agent forwarding recipe (RemoteForward … S.gpg-agent.extra plus StreamLocalBindUnlink yes) handles GPG signing, but it's a hand-rolled setup, and a remote gpg-agent will happily autostart and steal your forwarded socket.

smartsocket combines the stable path with GPG forwarding, then adds the two things neither has: routing on whether a usable key is present, and automatic local fallback, with PIN prompts sent to your own screen.

smartsocket ssh-agent-switcher manual gpg-agent forward 1Password SSH agent
SSH auth routing ✅ ✅ ✅ ✅
GPG / Assuan signing ✅ ❌ SSH only ✅ ❌ SSH only
Routes on key presence (not just an open socket) ✅ ❌ first socket that opens ❌ whatever's forwarded ⚠️ partial
Automatic local fallback when the remote is keyless ✅ ❌ ❌ manual ❌
Dual-key precedence (remote › local) ✅ ❌ ❌ via ssh-config conditionals
Pinentry forwarded to the client's native prompt ✅ pinentry-smart ❌ ❌ ✅ (app-native)
Survives tmux / stale SSH_AUTH_SOCK ✅ ✅ ❌ ✅
Transparent — no per-host config ✅ systemd socket activation ⚠️ minimal ❌ fiddly per-host ⚠️ low, but closed
Open source ✅ Go ✅ Go ✅ built-in tooling ❌ commercial

Comparison reflects each tool's primary design goal; the adjacent projects are excellent at what they set out to do — smartsocket just targets a different sweet spot.

Quick start

  1. On the server, the machine you SSH into: make install enable.
  2. On the client, the machine you SSH from: forward both agent sockets in ~/.ssh/config (details).
  3. On the server, point SSH_AUTH_SOCK at /run/user/1000/gnupg/S.gpg-agent.ssh (details).

That's the whole setup. To have the server's own PIN prompts appear on your laptop too, add pinentry forwarding.

How it works

Everything that needs your key goes through two sockets: the Assuan socket (S.gpg-agent) for signing and decryption, and the ssh-agent socket (S.gpg-agent.ssh) for authentication. smartsocket owns both and backs them with whichever YubiKey is usable right now.

flowchart LR
    u1["git commit -S<br/>(sign)"] --> gpg
    u2["gpg --decrypt<br/>(decrypt)"] --> gpg
    u3["gpg --sign / --encrypt"] --> gpg
    u4["ssh · scp · git push<br/>(auth)"] --> ssh

    gpg["S.gpg-agent<br/>Assuan protocol"] --> smart(("smartsocket"))
    ssh["S.gpg-agent.ssh<br/>ssh-agent protocol"] --> smart
    smart --> key["🔑 whichever YubiKey<br/>is usable — local or remote"]

    classDef router fill:#cfe8ff,stroke:#0969da,stroke-width:2px,color:#082b52;
    classDef sock fill:#efe6ff,stroke:#8250df,color:#2a1152;
    classDef key fill:#fff3bf,stroke:#bf8700,color:#3d2f00;
    class smart router;
    class gpg,ssh sock;
    class key key;
Loading

For each connection it asks one question: does the forwarded agent hold a usable key? For SSH, that means the agent offers at least one identity. For GPG, it means the agent can reach a card. If yes, the request goes to the remote. Otherwise, including on any error or timeout, it goes to the local agent. The internals are in ARCHITECTURE.md.

Every case, one question

In the diagrams, green is the path that lights up, grey is dormant, and 🔑 amber is a key that's physically present.

At the desk. No session, so nothing is forwarded. The probe finds nothing and the local agent signs with the key in the machine.

flowchart LR
    t["tools on server"] --> s(("smart"))
    s -.->|"remote empty"| r["remote"]
    s ==>|"routes local"| l["local gpg-agent"]
    l --> ks["🔑 server key"]
    kc["client key — none"]

    classDef on fill:#d7f5dd,stroke:#1a7f37,stroke-width:2px,color:#08260f;
    classDef off fill:#ececec,stroke:#b0b0b0,color:#6b6b6b;
    classDef key fill:#fff3bf,stroke:#bf8700,color:#3d2f00;
    class t,s,l on;
    class r,kc off;
    class ks key;
Loading

Away, one key. The key rides your SSH session onto the server. The probe sees it and routes remote. The server needs no key of its own, and the same key still works locally on the laptop.

flowchart LR
    kc["🔑 client key"] ==>|"SSH forward"| r["remote"]
    t["tools on server"] --> s(("smart"))
    s ==>|"remote has key"| r
    s -.->|"local: no card"| l["local gpg-agent"]
    ks["server key — none"]

    classDef on fill:#d7f5dd,stroke:#1a7f37,stroke-width:2px,color:#08260f;
    classDef off fill:#ececec,stroke:#b0b0b0,color:#6b6b6b;
    classDef key fill:#fff3bf,stroke:#bf8700,color:#3d2f00;
    class t,s,r on;
    class l,ks off;
    class kc key;
Loading

Away, two keys. A duplicate key stays in the server. The forwarded key wins by precedence and the server's key waits on standby. Pull the client key, even while the session lingers, and the next request falls back to the server's key without a hitch.

flowchart LR
    kc["🔑 client key"] ==>|"SSH forward"| r["remote"]
    t["tools on server"] --> s(("smart"))
    s ==>|"remote wins (precedence)"| r
    s -.->|"standby"| l["local gpg-agent"]
    l -.- ks["🔑 server key (idle)"]

    classDef on fill:#d7f5dd,stroke:#1a7f37,stroke-width:2px,color:#08260f;
    classDef off fill:#ececec,stroke:#b0b0b0,color:#6b6b6b;
    classDef key fill:#fff3bf,stroke:#bf8700,color:#3d2f00;
    classDef idle fill:#fff8dd,stroke:#c9a200,color:#5a4a00,stroke-dasharray:4 3;
    class t,s,r on;
    class kc key;
    class l off;
    class ks idle;
Loading

No key anywhere. Nothing is forwarded and nothing is plugged in. The router still routes local, the local agent has no card, and the operation fails, as it should. Nothing is quietly handed to a keyless agent.

flowchart LR
    t["tools on server"] --> s(("smart"))
    s -.->|"remote empty"| r["remote"]
    s ==>|"fails toward local"| l["local gpg-agent"]
    l --> x["❌ no card —<br/>operation fails"]
    kc["client key — none"]
    ks["server key — none"]

    classDef on fill:#d7f5dd,stroke:#1a7f37,stroke-width:2px,color:#08260f;
    classDef off fill:#ececec,stroke:#b0b0b0,color:#6b6b6b;
    classDef fail fill:#ffd7d5,stroke:#cf222e,color:#5a0a0a;
    class t,s,l on;
    class r,kc,ks off;
    class x fail;
Loading

To force local while a keyed remote is connected, disconnect the session.


Setup in depth

Installation

On the server:

make install
make enable

This builds and installs smartsocket and pinentry-smart to ~/.local/bin and the systemd user units to ~/.local/share/systemd/user. make enable masks the stock gpg-agent socket units and enables smartsocket's sockets and a dedicated local gpg-agent.

Socket paths

Standard paths, owned by smartsocket:

  • /run/user/1000/gnupg/S.gpg-agent for GPG operations
  • /run/user/1000/gnupg/S.gpg-agent.ssh for SSH authentication

Remote sockets, forwarded from the client over SSH:

  • /run/user/1000/gnupg/S.gpg-agent.remote
  • /run/user/1000/gnupg/S.gpg-agent.ssh.remote

Local sockets, the local gpg-agent fallback:

  • /run/user/1000/gnupg/S.gpg-agent.local
  • /run/user/1000/gnupg/S.gpg-agent.ssh.local

Client configuration

On the machine you SSH from, the one holding the YubiKey, run gpg-agent with SSH support:

# ~/.gnupg/gpg-agent.conf
enable-ssh-support

Then forward both agent sockets in ~/.ssh/config:

Host myserver
    # Forward GPG agent socket (for signing)
    RemoteForward /run/user/1000/gnupg/S.gpg-agent.remote /path/to/local/S.gpg-agent

    # Forward SSH agent socket
    RemoteForward /run/user/1000/gnupg/S.gpg-agent.ssh.remote /path/to/local/S.gpg-agent.ssh

    # Allow SSH to overwrite stale sockets on reconnect
    StreamLocalBindUnlink yes

To find your local socket paths:

# GPG socket
gpgconf --list-dirs agent-socket
# Linux: /run/user/1000/gnupg/S.gpg-agent
# macOS: /Users/username/.gnupg/S.gpg-agent

# SSH socket
gpgconf --list-dirs agent-ssh-socket
# Linux: /run/user/1000/gnupg/S.gpg-agent.ssh
# macOS: /Users/username/.gnupg/S.gpg-agent.ssh

Server sshd configuration

On the server, make sure /etc/ssh/sshd_config includes the following, so SSH can clean up stale forwarded sockets on reconnect:

StreamLocalBindUnlink yes

Shell configuration

On the server, point SSH_AUTH_SOCK at the standard path:

# .bashrc / .zshrc
export SSH_AUTH_SOCK=/run/user/1000/gnupg/S.gpg-agent.ssh
# config.nu
$env.SSH_AUTH_SOCK = "/run/user/1000/gnupg/S.gpg-agent.ssh"

Pinentry forwarding

When the server's local key serves, because no remote is connected or the remote is keyless, gpg-agent needs that card's PIN. On a headless server the default curses pinentry is awkward, and an unattended agent can't use it at all. pinentry-smart sends the prompt to the client's native pinentry, such as pinentry-mac, over a reverse-forwarded socket. With no client connected, it falls back to a local pinentry. It rides your existing outbound SSH session, so the server never connects back to the client and stores no credential. How it decides safely is in ARCHITECTURE.md.

make install already put pinentry-smart in ~/.local/bin/.

On the server, point gpg-agent at it and reload. The path must be absolute: gpg-agent runs pinentry-program with no PATH search, so a bare pinentry-smart fails with can't connect to the PIN entry module …: IPC connect call failed or No pinentry.

# ~/.gnupg/gpg-agent.conf
pinentry-program /home/<you>/.local/bin/pinentry-smart
systemctl --user restart gpg-agent-local.service   # re-read the config

pinentry-smart forwards to the client only if /run/user/<uid>/gnupg/S.pinentry is a live pinentry, meaning the client is connected and its responder is up. Otherwise it runs the local fallback, /usr/bin/pinentry-curses by default. PINENTRY_SMART_SOCKET and PINENTRY_SMART_FALLBACK override both.

On the client, reverse-forward a pinentry socket to the server, alongside the agent forwards, under the server's Host block:

RemoteForward /run/user/1000/gnupg/S.pinentry /Users/<you>/.hamr/pinentry.sock
StreamLocalBindUnlink yes

Then run a small responder in your GUI login session, so the native dialog can draw. Test it from a GUI terminal first:

socat UNIX-LISTEN:$HOME/.hamr/pinentry.sock,fork,unlink-early EXEC:$(which pinentry-mac)

The SSH forward delivers to a socket, but pinentry-mac speaks Assuan on stdin and stdout. socat bridges the two: UNIX-LISTEN gives the forward a target, and EXEC starts a fresh pinentry-mac for each connection with the socket wired to its stdio. fork means one per PIN request. It runs silently; the popup is the output.

Once it works, make it permanent with a user LaunchAgent (~/Library/LaunchAgents/io.kingland.hamr.pinentry.plist, with RunAtLoad and KeepAlive, running the same socat command). It stays in the GUI session and survives reconnects and reboots.

With the session up, the server's PIN prompt pops up as your native pinentry. With it down, the server falls back to its local pinentry automatically, since StreamLocalBindUnlink removes the forwarded socket on disconnect.

Management

make status    # Check all socket and service status
make disable   # Stop smartsocket and restore original gpg-agent
make enable    # Enable smartsocket (masks original gpg-agent)
make uninstall # Remove everything

Standalone mode

For testing, or on systems without systemd, smartsocket can run on its own and listen on .smart sockets instead:

./smartsocket

Clients then have to be pointed at the .smart paths.

systemd units

  • smartsocket-gpg.socket listens on S.gpg-agent.
  • smartsocket-ssh.socket listens on S.gpg-agent.ssh.
  • smartsocket.service is the router.
  • gpg-agent-local.socket listens on S.gpg-agent.local.
  • gpg-agent-ssh-local.socket listens on S.gpg-agent.ssh.local.
  • gpg-agent-local.service is the local gpg-agent.

Known issues

A free-running gpg-agent steals smartsocket's sockets

Symptom. Connections to S.gpg-agent or S.gpg-agent.ssh bypass smartsocket and reach a local gpg-agent instead. For example, ssh-add -L returns "no identities", or only the local key, when a remote key should be in play.

Check first. Run ss -lxn | grep S.gpg-agent. Two listeners on the same path mean a rogue agent stole it: smartsocket's has a backlog of 4096, the rogue's 64. A single listener means smartsocket is working and chose local on purpose, because the remote held no usable key or answered too slowly. In that case, check the remote.

Cause. Anything that starts gpg-agent --daemon replaces the socket file with its own: gpgconf --launch gpg-agent, or gpg-connect-agent without --no-autostart. The details are in ARCHITECTURE.md.

Recovery. Restart the socket units so they re-bind the paths:

pkill -f 'gpg-agent.*--daemon'
systemctl --user restart smartsocket-gpg.socket smartsocket-ssh.socket

Prevention. Keep shell startup and SSH config from launching gpg-agent:

  • Remove gpgconf --launch gpg-agent from .bashrc, config.nu and the like. smartsocket and gpg-agent-local.service are socket-activated, so no launch is needed.
  • If ~/.ssh/config has a Match host * exec "gpg-connect-agent UPDATESTARTUPTTY /bye" block for pinentry-tty, add --no-autostart:
    Match host * exec "gpg-connect-agent --no-autostart UPDATESTARTUPTTY /bye"
    

About

Use a YubiKey (or local keys) for GPG auth and signing when either local or connected remotely

Resources

Readme
MIT license
Activity

Stars

1 star

Watchers

0 watching

Forks

0 forks
Report repository

Releases

Packages

Contributors

Languages

You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.