1

我想使用 Python根据 OAuth 1 RFC 部分https://www.rfc-editor.org/rfc/rfc5849#section-3.4.3中的 RSA-SHA1 签名方法对消息进行签名。我试图通过将结果与 openssl 的结果进行比较来验证我是否走在正确的轨道上。

现在我突然发现自己多了一个八位字节,由于我的密码学知识充其量是有限的,我需要一些帮助。通过查看加密的源代码和 openssl 的手册页以及输出非常相似的事实,我认为我至少使用了正确的算法。

然而,当使用 openssl rsautl 时,我什至没有关闭......

$ openssl genrsa -out private.pem 1024

$ cat message
Lorem ipsum

$ cat sign.py
from Crypto.PublicKey import RSA
from Crypto.Signature import PKCS1_v1_5
from Crypto.Hash import SHA
key = RSA.importKey(open("private.pem").read())
message = open("message").read()[:-1] # skip last newline
h = SHA.new(message)
p = PKCS1_v1_5.new(key)
signature = p.sign(h)
signature_trim = p.sign(h)[:-1] # will give same output as openssl dgst -sign
print signature    # remove print when not using hexdump

来自 Python 的输出

$ python sign.py | hexdump
0000000 1346 38af 89a8 d203 ee26 0cfa a4bc 3a6c
0000010 44fd a436 2c50 03ba 7c84 333a 910a 843e
0000020 f71b 5731 1d2a 8895 9f5c 86b1 1838 7de9
0000030 5c13 d7e5 a019 6ad1 e5a5 d4d5 bd6f 0032
0000040 f320 c5ad fc41 da2c a9c3 2d9a cdce f6d6
0000050 4ef4 6dbd 1ba2 edc1 648e 184a 2e6c e746
0000060 fd92 ba61 b4da f607 d7a4 fbef 8230 378d
0000070 a143 b444 c711 7121 6e08 9d88 bb05 0d25
0000080 000a                                   
0000081

使用 openssl dgst 签名的输出(不确定这是否真的是 rsa pkcs #1 v1.5)

$ echo -n $(cat message) | openssl dgst -sign private.pem | hexdump
0000000 1346 38af 89a8 d203 ee26 0cfa a4bc 3a6c
0000010 44fd a436 2c50 03ba 7c84 333a 910a 843e
0000020 f71b 5731 1d2a 8895 9f5c 86b1 1838 7de9
0000030 5c13 d7e5 a019 6ad1 e5a5 d4d5 bd6f 0032
0000040 f320 c5ad fc41 da2c a9c3 2d9a cdce f6d6
0000050 4ef4 6dbd 1ba2 edc1 648e 184a 2e6c e746
0000060 fd92 ba61 b4da f607 d7a4 fbef 8230 378d
0000070 a143 b444 c711 7121 6e08 9d88 bb05 0d25
0000080

然后在 sign.py 中删除签名打印并根据其公钥验证签名。由于修剪签名将无法通过验证,因此我认为不应该这样做,但我以前错了。

pubkey = key.publickey()
pp = PKCS1_v1_5.new(pubkey)
print pp.verify(h, signature)         # True
print pp.verify(h, signature_trim)    # False

openssl rsautl 的输出(也不确定这是否是 pkcs #1 v1.5)

$ echo -n $(cat message) | openssl dgst -sha1 | openssl rsautl -sign -inkey private.pem  | hexdump
0000000 14a4 f02c 527f 26f9 29f6 281c 3185 4a1a
0000010 def8 052b b620 cca2 38d9 a389 0b44 112a
0000020 283c ebff 4228 6f77 7a65 9d53 4b98 a073
0000030 bbd9 1aca 3447 a917 d7c3 0968 63c4 6806
0000040 6112 6f36 2d38 a770 5afa a8e0 adf3 4bef
0000050 120c cc10 5194 75ad bdda 91e6 fd79 8f4c
0000060 b864 efb8 cc88 a4da e977 b488 6241 15fb
0000070 e105 1d11 8627 75bd 345b 34da 538f a8db
0000080

我显然做错了什么,现在我想知道有多少。除了base64编码和“Lorem ipsum”不是有效的签名基本字符串的面孔......

...我需要修改什么才能使其成为有效的 RSA-SHA1 签名?

4

2 回答 2

4

Pythonprint将始终附加一个换行符,这是您看到的额外字节。

据我所知,没有避免这种情况的干净方法,print signature,也不会奏效。

另一种方法是使用较低的级别sys.stdout.write(signature)

于 2012-01-17T22:39:24.187 回答
0

找到了答案。没有什么。

使用创建证书+密钥

$ openssl req -x509 -nodes -days 365 -newkey rsa:1024 -sha1 -subj \ 
  '/C=US/ST=CA/L=Mountain View/CN=www.example.com' -keyout myrsakey.pem \
  -out myrsacert.pem
  1. 在 Google Auth Playground http://googlecodesamples.com/oauth_playground/上注册了一个域
  2. 验证我的域并上传证书文件
  3. 使用 Playground 制作请求
  4. 用我的代码中的输出确认了 Playground 的输出 =)

RSA-SHA1 签名方法

def sign_rsa(method, url, params, private_rsa):
   """Sign a request using RSASSA-PKCS #1 v1.5.

   Per `section 3.4.3`_ of the spec.

   .. _`section 3.4.3`: http://tools.ietf.org/html/rfc5849#section-3.4.3

   """
   from Crypto.PublicKey import RSA
   from Crypto.Signature import PKCS1_v1_5
   from Crypto.Hash import SHA
   key = RSA.importKey(private_rsa)
   message = prepare_base_string(method, url, params)
   h = SHA.new(message)    
   p = PKCS1_v1_5.new(key)
   return escape(binascii.b2a_base64(p.sign(h))[:-1])

RSA-SHA1 验证

def verify_rsa(method, url, params, public_rsa, signature):
   """Verify a RSASSA-PKCS #1 v1.5 base64 encoded signature.

   Per `section 3.4.3`_ of the spec.

   .. _`section 3.4.3`: http://tools.ietf.org/html/rfc5849#section-3.4.3

   """
   from Crypto.PublicKey import RSA
   from Crypto.Signature import PKCS1_v1_5
   from Crypto.Hash import SHA
   key = RSA.importKey(public_rsa)
   message = prepare_base_string(method, url, params)
   h = SHA.new(message)
   p = PKCS1_v1_5.new(key)
   signature = binascii.a2b_base64(urllib.unquote(signature))
   return p.verify(h, signature)

好奇的人可以在https://github.com/ib-lundgren/oauthlib/blob/master/oauthlib/oauth.py找到它们以及 prepare_base_string 和逃逸

仍然不知道 hexdumps 但如果我弄明白了会更新。

于 2012-01-17T21:33:57.723 回答