在Python中验证ssh-agent签名

时间:2013-05-26 04:16:30

标签: python paramiko ssh-agent

Python的Paramiko library允许您请求ssh-agent为您签署一些数据。那很棒!但是,当我只能访问公钥而不是代理时,如何验证签名?

from base64 import b64encode
from paramiko.agent import Agent
agent = Agent()
key = agent.get_keys()[0]
sig = key.sign_ssh_data(None, "Hello World!")
print b64encode(sig)
# AAAAB3NzaC1yc2EAAAEAqZFIGxm1Zz3ALeTa2YiiYX2rbr9Cnb3gj4tJj1Fl19sNsRy6UbbauYnmahYWl6mWqhOHJaj0sAtVqPab8U1RiyeZyQB+Bwp0MUal4ZGCmrJxJ7ykVE+BeJnpOxXyutHHV376TXC5l+tx0PjFsylL1vVbBm6J927Nyc/cq1uk0q2QpcRexg0iJ51i1WGyISVvu1PC+g0LmW1dGh2mXTo/fpt+Cu45/hjWpMlcBUg8O60+nkj2jYxxGVh/z7U6zTYrDSJU4hTEveuKG5I38gsaMRBw/YkSIaMtkGNJX36Ybc+/EVolL4Z/NOOCV+kd7WPHjSTf1hVZ02ulTTrnMN4HfA==

我已经尝试了我能提出的每一个解决方案。 OpenSSL甚至无法查看我正在使用的密钥(通过ssh-keygen生成):

$ ssh-keygen -f ~/.ssh/testssh.pub -e -m pem > test.pub
$ cat test.pub
-----BEGIN RSA PUBLIC KEY-----
MIIBCgKCAQEA7Zs6ZyrHk3RpDKv7Kzd7NCU2Qk5k5ajVJvntRPiEukWOIe4CkcBo
B76HSDyPvEDI50DUyebj2Qj77NDX/lh5AhDrVTEmX/Lfzx2iCwgpWwQcu8dgAaN5
qhsCMYOc77d2YykcnbDUhgxus/KiieLnruKmviVdSHoLBs6ygnVEa+8nx+DhJwSB
B3+6gClM89QnKrPYOonj9z4zi+UeWPX9TVwJVOAADnXEC6LCmDCgHDb+FrCaiNT9
d4yam3iZJ2RMQ+ajBj6JWvoig2LdUBI7eSgmrREjjEEskBCNRkD4YmjeT8mfZg9d
FhbGz0CQjnF31jiohIvhABTwcUQnDdrPEQIDAQAB
-----END RSA PUBLIC KEY-----
$ openssl rsa -inform PEM -in test.pub -pubin -text
unable to load Public Key
140735078689212:error:0906D06C:PEM routines:PEM_read_bio:no start line:pem_lib.c:696:Expecting: PUBLIC KEY

PyCrypto为相同的数据提供了完全不同的签名:

from base64 import b64encode
from Crypto.Hash import SHA
from Crypto.PublicKey import RSA
from Crypto.Signature import PKCS1_v1_5

# PyCrypto
key = RSA.importKey(open('.ssh/testssh').read())
signer = PKCS1_v1_5.new(key)
h = SHA.new()
h.update("Hello World!")
sig = signer.sign(h)
print "CRYPTO: %s" % b64encode(sig)
# CRYPTO: qZFIGxm1Zz3ALeTa2YiiYX2rbr9Cnb3gj4tJj1Fl19sNsRy6UbbauYnmahYWl6mWqhOHJaj0sAtVqPab8U1RiyeZyQB+Bwp0MUal4ZGCmrJxJ7ykVE+BeJnpOxXyutHHV376TXC5l+tx0PjFsylL1vVbBm6J927Nyc/cq1uk0q2QpcRexg0iJ51i1WGyISVvu1PC+g0LmW1dGh2mXTo/fpt+Cu45/hjWpMlcBUg8O60+nkj2jYxxGVh/z7U6zTYrDSJU4hTEveuKG5I38gsaMRBw/YkSIaMtkGNJX36Ybc+/EVolL4Z/NOOCV+kd7WPHjSTf1hVZ02ulTTrnMN4HfA==

显然,我不是一个哭泣学家。我一直在阅读过去12小时的代码,试图弄清楚如何验证ssh-agent签名。我有什么痛苦明显的遗失吗?什么是“正确”的方式?

更新

在M2Crypto验证流程概述here之后,我收到了针对ssh-agent签名的错误:

Traceback (most recent call last):
  File "test.py", line 36, in <module>
    print key.verify(datasha.digest(), agentsig)
  File "/Users/shbeta/virtualenvs/dev/lib/python2.7/site-packages/M2Crypto-0.21.1-py2.7-macosx-10.7-intel.egg/M2Crypto/RSA.py", line 259, in verify
M2Crypto.RSA.RSAError: wrong signature length

然而,对Crypto生成的签名进行测试是成功的。 ssh-agent是否使用非标准签名算法?

更新2:编辑。我误读了。

1 个答案:

答案 0 :(得分:1)

似乎从公共SSH密钥构建RSA密钥对象的一般想法是......不太准确。再说一遍,我不是密码学家。但这有效:

from paramiko import Message
from paramiko.rsakey import RSAKey
from sshkey import load_rsa_pub_key
with open('.ssh/testssh.pub') as fh:
  ketype, b64key, _ = fh.next().strip().split(None, 2)
decoded_key = base64.b64decode(b64key)
pkey = RSAKey(data=decoded_key)
psig = Message(agentsig)
print pkey.verify_ssh_sig(plaintext, psig)
# True
print pkey.verify_ssh_sig("bogus", psig)
# False

我很想知道为什么使用转换的PEM格式不起作用。 ssh-keygen是否错误地转储它?