1
require 'jruby_pageant'
3
module Net; module SSH; module Authentication
5
# This class implements an agent for JRuby + Pageant.
7
# Written by Artūras Šlajus <arturas.slajus@gmail.com>
12
# A simple module for extending keys, to allow blobs and comments to be
15
# :blob is used by OpenSSL::PKey::RSA#to_blob
16
attr_accessor :java_blob
17
attr_accessor :comment
20
# Instantiates a new agent object, connects to a running SSH agent,
21
# negotiates the agent protocol version, and returns the agent object.
22
def self.connect(logger=nil)
28
# Creates a new Agent object, using the optional logger instance to
30
def initialize(logger=nil)
34
# Connect to the agent process using the socket factory and socket name
35
# given by the attribute writers. If the agent on the other end of the
36
# socket reports that it is an SSH2-compatible agent, this will fail
37
# (it only supports the ssh-agent distributed by OpenSSH).
39
debug { "connecting to Pageant ssh-agent (via java connector)" }
40
@agent_proxy = JRubyPageant.create
41
unless @agent_proxy.is_running
42
raise AgentNotAvailable, "Pageant is not running!"
44
debug { "connection to Pageant ssh-agent (via java connector) succeeded" }
45
rescue AgentProxyException => e
46
error { "could not connect to Pageant ssh-agent (via java connector)" }
47
raise AgentNotAvailable, e.message, e.backtrace
50
# Return an array of all identities (public keys) known to the agent.
51
# Each key returned is augmented with a +comment+ property which is set
52
# to the comment returned by the agent for that key.
54
debug { "getting identities from Pageant" }
55
@agent_proxy.get_identities.map do |identity|
56
blob = identity.get_blob
57
key = Buffer.new(String.from_java_bytes(blob)).read_key
60
key.comment = String.from_java_bytes(identity.get_comment)
63
rescue AgentProxyException => e
64
raise AgentError, "Cannot get identities: #{e.message}", e.backtrace
67
# Simulate agent close. This agent reference is no longer able to
73
# Using the agent and the given public key, sign the given data. The
74
# signature is returned in SSH2 format.
76
signed = @agent_proxy.sign(key.java_blob, data.to_java_bytes)
77
String.from_java_bytes(signed)
78
rescue AgentProxyException => e
80
"agent could not sign data with requested identity: #{e.message}",