| Method from com.sshtools.j2ssh.agent.SshAgentClient Detail: |
public void addKey(SshPrivateKey prvkey,
SshPublicKey pubkey,
String description,
KeyConstraints constraints) throws IOException {
SubsystemMessage msg = new SshAgentAddKey(prvkey, pubkey, description,
constraints);
sendMessage(msg);
msg = readMessage();
if (!(msg instanceof SshAgentSuccess)) {
throw new IOException("The key could not be added");
}
}
|
public void close() {
log.info("Closing agent client");
try {
in.close();
} catch (IOException ex) {
}
try {
out.close();
} catch (IOException ex1) {
}
try {
if (socket != null) {
socket.close();
}
} catch (IOException ex2) {
}
}
|
public static Socket connectAgentSocket(String location) throws IOException, AgentNotAvailableException {
try {
if (location == null) {
throw new AgentNotAvailableException();
}
int idx = location.indexOf(":");
if (idx == -1) {
throw new AgentNotAvailableException();
}
String host = location.substring(0, idx);
int port = Integer.parseInt(location.substring(idx + 1));
Socket socket = new Socket(host, port);
return socket;
} catch (IOException ex) {
throw new AgentNotAvailableException();
}
}
Connect a socket to the agent at the location specified. |
public static SshAgentClient connectLocalAgent(String application,
String location) throws IOException, AgentNotAvailableException {
try {
Socket socket = connectAgentSocket(location);
return new SshAgentClient(false, application, socket);
} catch (IOException ex) {
throw new AgentNotAvailableException();
}
}
Connect to the local agent. |
public void deleteAllKeys() throws IOException {
SubsystemMessage msg = new SshAgentDeleteAllKeys();
sendMessage(msg);
msg = readMessage();
if (!(msg instanceof SshAgentSuccess)) {
throw new IOException("The agent failed to delete all keys");
}
}
Delete all the keys held by the agent. |
public void deleteKey(SshPublicKey key,
String description) throws IOException {
SubsystemMessage msg = new SshAgentDeleteKey(key, description);
sendMessage(msg);
msg = readMessage();
if (!(msg instanceof SshAgentSuccess)) {
throw new IOException("The agent failed to delete the key");
}
}
Delete a key held by the agent |
public byte[] getRandomData(int count) throws IOException {
SubsystemMessage msg = new SshAgentRandom(count);
sendMessage(msg);
msg = readMessage();
if (msg instanceof SshAgentRandomData) {
return ((SshAgentRandomData) msg).getRandomData();
} else {
throw new IOException(
"Agent failed to provide the request random data");
}
}
Request some random data from the remote side |
public byte[] hashAndSign(SshPublicKey key,
byte[] data) throws IOException {
SubsystemMessage msg = new SshAgentPrivateKeyOp(key, HASH_AND_SIGN, data);
sendMessage(msg);
msg = readMessage();
if (msg instanceof SshAgentOperationComplete) {
return ((SshAgentOperationComplete) msg).getData();
} else {
throw new IOException("The operation failed");
}
}
Request a hash and sign operation be performed for a given public key. |
public Map listKeys() throws IOException {
SubsystemMessage msg = new SshAgentListKeys();
sendMessage(msg);
msg = readMessage();
if (msg instanceof SshAgentKeyList) {
return ((SshAgentKeyList) msg).getKeys();
} else {
throw new IOException("The agent responsed with an invalid message");
}
}
List all the keys on the agent. |
public boolean lockAgent(String password) throws IOException {
SubsystemMessage msg = new SshAgentLock(password);
sendMessage(msg);
msg = readMessage();
return (msg instanceof SshAgentSuccess);
}
|
public void ping(byte[] padding) throws IOException {
SubsystemMessage msg = new SshAgentPing(padding);
sendMessage(msg);
msg = readMessage();
if (msg instanceof SshAgentAlive) {
if (!Arrays.equals(padding, ((SshAgentAlive) msg).getPadding())) {
throw new IOException(
"Agent failed to reply with expected data");
}
} else {
throw new IOException(
"Agent failed to provide the request random data");
}
}
Ping the remote side with some random padding data |
protected SubsystemMessage readMessage() throws InvalidMessageException {
try {
byte[] lendata = new byte[4];
byte[] msgdata;
int len;
// Read the first 4 bytes to determine the length of the message
len = 0;
while (len < 3) {
len += in.read(lendata, len, lendata.length - len);
}
len = (int) ByteArrayReader.readInt(lendata, 0);
msgdata = new byte[len];
len = 0;
while (len < msgdata.length) {
len += in.read(msgdata, len, msgdata.length - len);
}
Integer id = new Integer((int) msgdata[0] & 0xFF);
if (messages.containsKey(id)) {
Class cls = (Class) messages.get(id);
SubsystemMessage msg = (SubsystemMessage) cls.newInstance();
msg.fromByteArray(msgdata);
log.info("Received message " + msg.getMessageName());
return msg;
} else {
throw new InvalidMessageException("Unrecognised message id " +
id.toString());
}
} catch (Exception ex) {
throw new InvalidMessageException(ex.getMessage());
}
}
Read a single message from the inputstream and convert into a valid
subsystem message |
protected void registerMessages() {
messages.put(new Integer(
SshAgentVersionResponse.SSH_AGENT_VERSION_RESPONSE),
SshAgentVersionResponse.class);
messages.put(new Integer(SshAgentSuccess.SSH_AGENT_SUCCESS),
SshAgentSuccess.class);
messages.put(new Integer(SshAgentFailure.SSH_AGENT_FAILURE),
SshAgentFailure.class);
messages.put(new Integer(SshAgentKeyList.SSH_AGENT_KEY_LIST),
SshAgentKeyList.class);
messages.put(new Integer(SshAgentRandomData.SSH_AGENT_RANDOM_DATA),
SshAgentRandomData.class);
messages.put(new Integer(SshAgentAlive.SSH_AGENT_ALIVE),
SshAgentAlive.class);
messages.put(new Integer(
SshAgentOperationComplete.SSH_AGENT_OPERATION_COMPLETE),
SshAgentOperationComplete.class);
}
Register the subsystem messages |
protected void sendForwardingNotice() throws IOException {
InetAddress addr = InetAddress.getLocalHost();
SshAgentForwardingNotice msg = new SshAgentForwardingNotice(addr.getHostName(),
addr.getHostAddress(), 22);
sendMessage(msg);
}
Send a forwarding notice. |
protected void sendMessage(SubsystemMessage msg) throws IOException {
log.info("Sending message " + msg.getMessageName());
byte[] msgdata = msg.toByteArray();
out.write(ByteArrayWriter.encodeInt(msgdata.length));
out.write(msgdata);
out.flush();
}
|
protected void sendVersionRequest(String application) throws IOException {
SubsystemMessage msg = new SshAgentRequestVersion(application);
sendMessage(msg);
msg = readMessage();
if (msg instanceof SshAgentVersionResponse) {
SshAgentVersionResponse reply = (SshAgentVersionResponse) msg;
if (reply.getVersion() != 2) {
throw new IOException(
"The agent verison is not compatible with verison 2");
}
} else {
throw new IOException(
"The agent did not respond with the appropriate version");
}
}
Request the agent version. |
public boolean unlockAgent(String password) throws IOException {
SubsystemMessage msg = new SshAgentUnlock(password);
sendMessage(msg);
msg = readMessage();
return (msg instanceof SshAgentSuccess);
}
|