Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for loading private keys into SSH agent #778

Open
omribahumi opened this issue Jul 20, 2016 · 3 comments
Open

Add support for loading private keys into SSH agent #778

omribahumi opened this issue Jul 20, 2016 · 3 comments

Comments

@omribahumi
Copy link

@omribahumi omribahumi commented Jul 20, 2016

I would like to load private keys from a key store into my ssh-agent.
This looks well documented on https://github.com/openssh/openssh-portable/blob/master/PROTOCOL.agent
This looks like it belongs in paramiko.agent.AgentSSH, perhaps a add_key or a load_key method that accepts a paramiko.pkey.PKey ?

@bitprophet
Copy link
Member

@bitprophet bitprophet commented Jul 20, 2016

Sounds like a good feature-add offhand, thanks for the report :)

yan12125 added a commit to yan12125/paramiko that referenced this issue Oct 18, 2017
@mmetelskiy
Copy link

@mmetelskiy mmetelskiy commented Mar 25, 2020

I failed to implement the poc but here is what I have
Documentation moved to new location - pay attention to version
Other useful docs could be found here
My code:

import paramiko
from paramiko.message import Message
from paramiko.py3compat import byte_chr

private_key_file_path = '<path_to_private_rsa_key>'

SSH_AGENT_FAILURE = 5
SSH_AGENT_SUCCESS = 6
cSSH2_AGENTC_ADD_IDENTITY = byte_chr(17)

key = paramiko.RSAKey.from_private_key_file(private_key_file_path)

key_type = key.get_name()  # not sure that this will work with other key types

content = Message()
content.add_byte(cSSH2_AGENTC_ADD_IDENTITY)
content.add_string(key_type)
content.add_mpint(key.public_numbers.n)
content.add_mpint(key.public_numbers.e)
content.add_mpint(key.key.private_numbers().d)
content.add_mpint(key.key.private_numbers().iqmp)
content.add_mpint(key.key.private_numbers().p)
content.add_mpint(key.key.private_numbers().q)

msg = Message()
msg.add_byte(cSSH2_AGENTC_ADD_IDENTITY)
msg.add_string(key_type)
msg.add_bytes(content.asbytes())

agent = paramiko.Agent()
ptype, result = agent._send_message(msg)

print(ptype)

ssh-agent is running with -d flag and here are the logs (only part that appears after running the above code):

debug2: fd 4 setting O_NONBLOCK
debug1: type 11
debug1: type 17
process_add_identity: decode private key: string is too large
debug1: XXX shrink: 3 < 4

I stopped at this point and moved to using ssh-add in bash
Python solution with subprocesses: link

@mmetelskiy
Copy link

@mmetelskiy mmetelskiy commented Mar 25, 2020

Upd: made it work in 5 minutes
Removing the duplicated cSSH2_AGENTC_ADD_IDENTITY and key_type params fixed the issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants