Class SshAgent
- Namespace
- SshSharp
- Assembly
- SshSharp.dll
An SSH agent (ssh-agent, the Windows OpenSSH agent service, or any agent speaking the same protocol) that holds private keys and
signs with them, so the keys never enter this process. List its keys with GetKeysAsync(CancellationToken) and pass them in
PrivateKeys; every signature is then made by the agent.
public sealed class SshAgent
- Inheritance
-
SshAgent
- Inherited Members
Remarks
Each request opens a new connection to the agent, so an agent restarted between connections is picked up. Signatures for
authentication are made on a connection bound to the SSH session (session-bind@openssh.com), so keys added with destination
constraints (ssh-add -h) work as they do with ssh.
Constructors
- SshAgent(string)
An agent at
path: a Unix domain socket, or on Windows a named pipe such as\.\pipe\openssh-ssh-agent.
Properties
- IsInProcess
Whether this agent runs in this process (FromKeys(params SshPrivateKey[])) rather than being reached through a socket or pipe.
- Path
The socket or pipe the agent listens on. For an in-process agent (FromKeys(params SshPrivateKey[])), which listens nowhere, it is
(in-process).
Methods
- AddKeyAsync(SshPrivateKey, SshAgentKeyConstraints?, CancellationToken)
Adds
keyto the agent, asssh-adddoes, optionally withconstraints(a lifetime, or a confirmation for each use). A key paired with a certificate is added with it. Adding a key the agent already holds replaces its constraints.
- FromEnvironment()
The user's agent, as ssh finds it:
SSH_AUTH_SOCK; on Windows, when that is not set, the OpenSSH agent service's pipe, or PuTTY's Pageant (0.75 and later) when only Pageant is running. Null when there is none to try. Whether an agent is actually running shows on the first request. Asynchronous code should call FromEnvironmentAsync(CancellationToken) instead, which does not block a thread.
- FromEnvironmentAsync(CancellationToken)
The user's agent, as FromEnvironment() finds it, without blocking a thread. On Windows, finding the agent service's pipe or Pageant's means listing the named pipes, which can be slow (each listing is bounded at 2 seconds); here that wait is awaited. Null when there is no agent to try. Nothing is sent to the agent: whether it is running shows on the first request, such as GetKeysAsync(CancellationToken).
- FromKeys(params SshPrivateKey[])
An agent served in this process from
keys: key files, generated keys, keys in a Windows key store (FromRsa(RSA, string?)), keys behind an SshKeySigner such as a cloud KMS or an HSM (FromSigner(SshKeySigner, string?)), and keys paired with certificates (WithCertificate(SshCertificate)), which it lists asssh-addadds them: the key, then its certificate. Forward it with ForwardAgent where no agent process runs (a container, a service), or to forward a key that is not in any agent.
- FromPageant()
PuTTY's Pageant (0.75 and later), on the named pipe it creates for the current user (
\.\pipe\pageant.<user>.<hash>), or null when Pageant is not running or this is not Windows. Its keys work like any agent's.
- GetKeysAsync(CancellationToken)
The keys the agent holds, ready for PrivateKeys. Keys of types SshSharp cannot authenticate with (DSA, for example) are left out.
- LockAsync(string, CancellationToken)
Locks the agent with
passphrase(ssh-add -x): until UnlockAsync(string, CancellationToken), it lists no keys and signs nothing.
- QueryExtensionsAsync(CancellationToken)
The protocol extensions the agent supports (RFC 9987's
queryextension), for examplesession-bind@openssh.com. An agent that does not implementqueryyields an empty list.
- RemoveAllKeysAsync(CancellationToken)
Removes every key from the agent (
ssh-add -D).
- RemoveKeyAsync(SshPrivateKey, CancellationToken)
Removes one key from the agent (
ssh-add -d): one from GetKeysAsync(CancellationToken), or the key a file holds.
- UnlockAsync(string, CancellationToken)
Unlocks an agent locked with LockAsync(string, CancellationToken) (
ssh-add -X).