1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
|
#!/usr/bin/env python
"""Unit tests for M2Crypto.RSA.
Copyright (c) 2000 Ng Pheng Siong. All rights reserved."""
import unittest
import sha, md5, os
from M2Crypto import RSA, BIO, Rand, m2, EVP, X509
class RSATestCase(unittest.TestCase):
errkey = 'tests/dsa.priv.pem'
privkey = 'tests/rsa.priv.pem'
privkey2 = 'tests/rsa.priv2.pem'
pubkey = 'tests/rsa.pub.pem'
data = sha.sha('The magic words are squeamish ossifrage.').digest()
e_padding_ok = ('pkcs1_padding', 'pkcs1_oaep_padding')
s_padding_ok = ('pkcs1_padding',)
s_padding_nok = ('no_padding', 'sslv23_padding', 'pkcs1_oaep_padding')
def gen_callback(self, *args):
pass
def gen2_callback(self):
pass
def pp_callback(self, *args):
# The passphrase for rsa.priv2.pem is 'qwerty'.
return 'qwerty'
def pp2_callback(self, *args):
# Misbehaving passphrase callback.
pass
def test_loadkey_junk(self):
self.assertRaises(RSA.RSAError, RSA.load_key, self.errkey)
def test_loadkey_pp(self):
rsa = RSA.load_key(self.privkey2, self.pp_callback)
assert len(rsa) == 512
assert rsa.e == '\000\000\000\003\001\000\001' # aka 65537 aka 0xf4
assert rsa.check_key() == 1
def test_loadkey_pp_bad_cb(self):
self.assertRaises(RSA.RSAError, RSA.load_key, self.privkey2, self.pp2_callback)
def test_loadkey(self):
rsa = RSA.load_key(self.privkey)
assert len(rsa) == 512
assert rsa.e == '\000\000\000\003\001\000\001' # aka 65537 aka 0xf4
assert rsa.check_key() == 1
def test_loadkey_bio(self):
keybio = BIO.MemoryBuffer(open(self.privkey).read())
rsa = RSA.load_key_bio(keybio)
assert len(rsa) == 512
assert rsa.e == '\000\000\000\003\001\000\001' # aka 65537 aka 0xf4
assert rsa.check_key() == 1
def test_keygen(self):
rsa = RSA.gen_key(256, 65537, self.gen_callback)
assert len(rsa) == 256
assert rsa.e == '\000\000\000\003\001\000\001' # aka 65537 aka 0xf4
assert rsa.check_key() == 1
def test_keygen_bad_cb(self):
rsa = RSA.gen_key(256, 65537, self.gen2_callback)
assert len(rsa) == 256
assert rsa.e == '\000\000\000\003\001\000\001' # aka 65537 aka 0xf4
assert rsa.check_key() == 1
def test_private_encrypt(self):
priv = RSA.load_key(self.privkey)
# pkcs1_padding
for padding in self.s_padding_ok:
p = getattr(RSA, padding)
ctxt = priv.private_encrypt(self.data, p)
ptxt = priv.public_decrypt(ctxt, p)
assert ptxt == self.data
# The other paddings.
for padding in self.s_padding_nok:
p = getattr(RSA, padding)
self.assertRaises(RSA.RSAError, priv.private_encrypt, self.data, p)
# Type-check the data to be encrypted.
self.assertRaises(TypeError, priv.private_encrypt, self.gen_callback, RSA.pkcs1_padding)
def test_public_encrypt(self):
priv = RSA.load_key(self.privkey)
# pkcs1_padding, pkcs1_oaep_padding
for padding in self.e_padding_ok:
p = getattr(RSA, padding)
ctxt = priv.public_encrypt(self.data, p)
ptxt = priv.private_decrypt(ctxt, p)
assert ptxt == self.data
# sslv23_padding
ctxt = priv.public_encrypt(self.data, RSA.sslv23_padding)
self.assertRaises(RSA.RSAError, priv.private_decrypt, ctxt, RSA.sslv23_padding)
# no_padding
self.assertRaises(RSA.RSAError, priv.public_encrypt, self.data, RSA.no_padding)
# Type-check the data to be encrypted.
self.assertRaises(TypeError, priv.public_encrypt, self.gen_callback, RSA.pkcs1_padding)
def test_x509_public_encrypt(self):
x509 = X509.load_cert("tests/recipient.pem")
rsa = x509.get_pubkey().get_rsa()
rsa.public_encrypt("data", RSA.pkcs1_padding)
def test_loadpub(self):
rsa = RSA.load_pub_key(self.pubkey)
assert len(rsa) == 512
assert rsa.e == '\000\000\000\003\001\000\001' # aka 65537 aka 0xf4
assert rsa.check_key()
def test_loadpub_bad(self):
self.assertRaises(RSA.RSAError, RSA.load_pub_key, self.errkey)
def test_savepub(self):
rsa = RSA.load_pub_key(self.pubkey)
assert rsa.as_pem() # calls save_key_bio
f = 'tests/rsa_test.pub'
try:
self.assertEquals(rsa.save_key(f), 1)
finally:
try:
os.remove(f)
except IOError:
pass
def test_set_bn(self):
rsa = RSA.load_pub_key(self.pubkey)
assert m2.rsa_set_e(rsa.rsa, '\000\000\000\003\001\000\001') is None
self.assertRaises(RSA.RSAError, m2.rsa_set_e, rsa.rsa, '\000\000\000\003\001')
def test_newpub(self):
old = RSA.load_pub_key(self.pubkey)
new = RSA.new_pub_key(old.pub())
assert new.check_key()
assert len(new) == 512
assert new.e == '\000\000\000\003\001\000\001' # aka 65537 aka 0xf4
def test_sign_and_verify(self):
"""
Testing signing and verifying digests
"""
algos = {'sha1':'',
'ripemd160':'',
'md5':''}
if m2.OPENSSL_VERSION_NUMBER >= 0x90800F:
algos['sha224'] = ''
algos['sha256'] = ''
algos['sha384'] = ''
algos['sha512'] = ''
message = "This is the message string"
digest = sha.sha(message).digest()
rsa = RSA.load_key(self.privkey)
rsa2 = RSA.load_pub_key(self.pubkey)
for algo in algos.keys():
signature = rsa.sign(digest, algo)
#assert signature == algos[algo], 'mismatched signature with algorithm %s: signature=%s' % (algo, signature)
verify = rsa2.verify(digest, signature, algo)
assert verify == 1, 'verification failed with algorithm %s' % algo
def test_sign_bad_method(self):
"""
Testing calling sign with an unsupported message digest algorithm
"""
rsa = RSA.load_key(self.privkey)
message = "This is the message string"
digest = md5.md5(message).digest()
self.assertRaises(ValueError, rsa.sign,
digest, 'bad_digest_method')
def test_verify_bad_method(self):
"""
Testing calling verify with an unsupported message digest algorithm
"""
rsa = RSA.load_key(self.privkey)
message = "This is the message string"
digest = md5.md5(message).digest()
signature = rsa.sign(digest, 'sha1')
self.assertRaises(ValueError, rsa.verify,
digest, signature, 'bad_digest_method')
def test_verify_mismatched_algo(self):
"""
Testing verify to make sure it fails when we use a different
message digest algorithm
"""
rsa = RSA.load_key(self.privkey)
message = "This is the message string"
digest = sha.sha(message).digest()
signature = rsa.sign(digest, 'sha1')
rsa2 = RSA.load_pub_key(self.pubkey)
self.assertRaises(RSA.RSAError, rsa.verify,
digest, signature, 'md5')
def test_sign_fail(self):
"""
Testing sign to make sure it fails when I give it
a bogus digest. Looking at the RSA sign method
I discovered that with the digest methods we use
it has to be longer than a certain length.
"""
rsa = RSA.load_key(self.privkey)
digest = """This string should be long enough to warrant an error in
RSA_sign"""
self.assertRaises(RSA.RSAError, rsa.sign, digest)
def test_verify_bad_signature(self):
"""
Testing verify to make sure it fails when we use a bad signature
"""
rsa = RSA.load_key(self.privkey)
message = "This is the message string"
digest = sha.sha(message).digest()
otherMessage = "Abracadabra"
otherDigest = sha.sha(otherMessage).digest()
otherSignature = rsa.sign(otherDigest)
self.assertRaises(RSA.RSAError, rsa.verify,
digest, otherSignature)
def suite():
return unittest.makeSuite(RSATestCase)
if __name__ == '__main__':
Rand.load_file('randpool.dat', -1)
unittest.TextTestRunner().run(suite())
Rand.save_file('randpool.dat')
|