Python 的Paramiko 库允许您请求 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 验证过程,我收到了针对 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:已编辑。我看错了。