Top Description Fields Constructors Methods
org.eclipse.jgit.internal.transport.sshd.agent

public Class SshAgentClient

extends Object
implements SshAgent
Class Inheritance
All Implemented Interfaces
org.apache.sshd.agent.SshAgent, java.nio.channels.Channel, java.io.Closeable, java.lang.AutoCloseable
Imports
java.io.IOException, java.security.KeyPair, .PrivateKey, .PublicKey, java.text.MessageFormat, java.util.AbstractMap, .ArrayList, .Arrays, .Collections, .List, .Map, java.util.concurrent.atomic.AtomicBoolean, org.apache.sshd.agent.SshAgent, .SshAgentConstants, .SshAgentKeyConstraint, org.apache.sshd.common.SshException, org.apache.sshd.common.config.keys.KeyUtils, org.apache.sshd.common.keyprovider.KeyPairProvider, org.apache.sshd.common.session.SessionContext, org.apache.sshd.common.util.buffer.Buffer, .BufferException, .BufferUtils, .ByteArrayBuffer, org.apache.sshd.common.util.buffer.keys.BufferPublicKeyParser, org.apache.sshd.common.util.io.der.DERParser, org.eclipse.jgit.internal.transport.sshd.SshdText, org.eclipse.jgit.transport.sshd.agent.Connector, org.slf4j.Logger, .LoggerFactory

A client for an SSH2 agent. This client supports querying identities, signature requests, and adding keys to an agent (with or without constraints). Removing keys is not supported, and the older SSH1 protocol is not supported.
See Also
SSH Agent Protocol, RFC draft

Field Summary

Modifier and TypeField and Description
private final AtomicBoolean
private final Connector
private static final Logger
private static final int

Constructor Summary

AccessConstructor and Description
public
SshAgentClient(Connector
Connector to use
connector
)

Creates a new SshAgentClient implementing the SSH2 ssh agent protocol, using the given Connector to connect to the SSH agent and to exchange messages.

Method Summary

Modifier and TypeMethod and Description
public void
addIdentity(KeyPair
KeyPair to add
key
,
String
to associate with the key
comment
,
SshAgentKeyConstraint...
SshAgentKeyConstraints for this key to pass on to the agent
constraints
)

Implements org.apache.sshd.agent.SshAgent.addIdentity.

Adds a key to the agent.

private static byte[]

Returns:

the extracted private key bytes; of length n
asn1Parse
(byte[]
encoded private key to extract the private key bytes from
encoded
,
int
number of bytes expected
n
)

Extracts the private key bytes from an encoded ed25519 private key by parsing the bytes as ASN.1 according to RFC 5958 (PKCS #8 encoding):

OneAsymmetricKey ::= SEQUENCE {
  version Version,
  privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
  privateKey PrivateKey,
  ...
public void
close()

Implements java.nio.channels.Channel.close.

Closes this channel.

public Iterable<? extends Map.Entry<PublicKey, String>>
public boolean
isOpen()

Implements java.nio.channels.Channel.isOpen.

Tells whether or not this channel is open.

private boolean
open(boolean debugging)

private static void
putEd25519Key(Buffer
Buffer to write to
msg
,
KeyPair
KeyPair to write
key
)

Writes an ed25519 KeyPair to a Buffer.

private static PublicKey

Returns:

the PublicKey, or null if the key could not be read
readKey
(Buffer
to read the key from
buffer
)

A safe version of Buffer#getPublicKey().

public void
public void
private Buffer
rpc(byte command, byte[] message)

private Buffer
rpc(byte command)

public Map.Entry<String, byte[]>
sign(SessionContext
The current SessionContext
session
,
PublicKey
The PublicKey to use for signing
key
,
String
Recommended signature algorithm - if null/empty then one will be selected based on the key type and/or signature factories

Note

even if specific algorithm specified, the implementation may disregard and choose another

algorithm
,
byte[]
Data to sign
data
)

Implements org.apache.sshd.agent.SshAgent.sign.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

closedback to summary
private final AtomicBoolean closed
connectorback to summary
private final Connector connector
LOGback to summary
private static final Logger LOG
MAX_NUMBER_OF_KEYSback to summary
private static final int MAX_NUMBER_OF_KEYS

Constructor Detail

SshAgentClientback to summary
public SshAgentClient(Connector connector)

Creates a new SshAgentClient implementing the SSH2 ssh agent protocol, using the given Connector to connect to the SSH agent and to exchange messages.

Parameters
connector:Connector

Connector to use

Method Detail

addIdentityback to summary
public void addIdentity(KeyPair key, String comment, SshAgentKeyConstraint... constraints) throws IOException

Implements org.apache.sshd.agent.SshAgent.addIdentity.

Doc from org.apache.sshd.agent.SshAgent.addIdentity.

Adds a key to the agent.

Parameters
key:KeyPair

KeyPair to add

comment:String

to associate with the key

constraints:SshAgentKeyConstraint[]

SshAgentKeyConstraints for this key to pass on to the agent

Annotations
@Override
Exceptions
IOException:
if an error in the communication with the agent occurred, or the agent did not return a reply indicating successful addition of the key
asn1Parseback to summary
private static byte[] asn1Parse(byte[] encoded, int n) throws IOException

Extracts the private key bytes from an encoded ed25519 private key by parsing the bytes as ASN.1 according to RFC 5958 (PKCS #8 encoding):

OneAsymmetricKey ::= SEQUENCE {
  version Version,
  privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
  privateKey PrivateKey,
  ...
}

Version ::= INTEGER
PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
PrivateKey ::= OCTET STRING

AlgorithmIdentifier  ::=  SEQUENCE  {
  algorithm   OBJECT IDENTIFIER,
  parameters  ANY DEFINED BY algorithm OPTIONAL
}

and RFC 8410: "... when encoding a OneAsymmetricKey object, the private key is wrapped in a CurvePrivateKey object and wrapped by the OCTET STRING of the 'privateKey' field."

CurvePrivateKey ::= OCTET STRING
Parameters
encoded:byte[]

encoded private key to extract the private key bytes from

n:int

number of bytes expected

Returns:byte[]

the extracted private key bytes; of length n

Exceptions
IOException:
if the private key cannot be extracted
See Also
RFC 5958, RFC 8410
closeback to summary
public void close() throws IOException

Implements java.nio.channels.Channel.close.

Doc from java.nio.channels.Channel.close.

Closes this channel.

After a channel is closed, any further attempt to invoke I/O operations upon it will cause a ClosedChannelException to be thrown.

If this channel is already closed then invoking this method has no effect.

This method may be invoked at any time. If some other thread has already invoked it, however, then another invocation will block until the first invocation is complete, after which it will return without effect.

Annotations
@Override
Exceptions
IOException:
If an I/O error occurs
getIdentitiesback to summary
public Iterable<? extends Map.Entry<PublicKey, String>> getIdentities() throws IOException

Implements org.apache.sshd.agent.SshAgent.getIdentities.

Annotations
@Override
isOpenback to summary
public boolean isOpen()

Implements java.nio.channels.Channel.isOpen.

Doc from java.nio.channels.Channel.isOpen.

Tells whether or not this channel is open.

Returns:boolean

true if, and only if, this channel is open

Annotations
@Override
openback to summary
private boolean open(boolean debugging) throws IOException
putEd25519Keyback to summary
private static void putEd25519Key(Buffer msg, KeyPair key) throws IOException

Writes an ed25519 KeyPair to a Buffer. OpenSSH specifies that it expects the 32 public key bytes, followed by 64 bytes formed by concatenating the 32 private key bytes with the 32 public key bytes.

Parameters
msg:Buffer

Buffer to write to

key:KeyPair

KeyPair to write

Exceptions
IOException:
if the private key cannot be written
readKeyback to summary
private static PublicKey readKey(Buffer buffer) throws BufferException

A safe version of Buffer#getPublicKey(). Upon return the buffers's read position is always after the key blob; any exceptions thrown by trying to read the key are logged and not propagated.

This is needed because an SSH agent might contain and deliver keys that we cannot handle (for instance ed448 keys).

Parameters
buffer:Buffer

to read the key from

Returns:PublicKey

the PublicKey, or null if the key could not be read

Exceptions
BufferException:
if the length of the key blob cannot be read or is corrupted
removeAllIdentitiesback to summary
public void removeAllIdentities() throws IOException

Implements org.apache.sshd.agent.SshAgent.removeAllIdentities.

Annotations
@Override
removeIdentityback to summary
public void removeIdentity(PublicKey key) throws IOException

Implements org.apache.sshd.agent.SshAgent.removeIdentity.

Annotations
@Override
rpcback to summary
private Buffer rpc(byte command, byte[] message) throws IOException
rpcback to summary
private Buffer rpc(byte command) throws IOException
signback to summary
public Map.Entry<String, byte[]> sign(SessionContext session, PublicKey key, String algorithm, byte[] data) throws IOException

Implements org.apache.sshd.agent.SshAgent.sign.

Parameters
session:SessionContext

Doc from org.apache.sshd.agent.SshAgent.sign.

The current SessionContext

key:PublicKey

Doc from org.apache.sshd.agent.SshAgent.sign.

The PublicKey to use for signing

algorithm:String

Doc from org.apache.sshd.agent.SshAgent.sign.

Recommended signature algorithm - if null/empty then one will be selected based on the key type and/or signature factories

Note

even if specific algorithm specified, the implementation may disregard and choose another

data:byte[]

Doc from org.apache.sshd.agent.SshAgent.sign.

Data to sign

Returns:Map.Entry<String, byte[]>

Doc from org.apache.sshd.agent.SshAgent.sign.

used algorithm + signed data - using the identity

Annotations
@Override
Exceptions
IOException:

Doc from org.apache.sshd.agent.SshAgent.sign.

If failed to sign