blob: c55a9798b11bbb9fcba649d2ea09ba6031e9e4ef [file] [log] [blame]
Jean-Paul Calderoned8782ad2008-03-04 23:39:59 -05001"""
2Unit tests for L{OpenSSL.crypto}.
3"""
4
5from unittest import TestCase
6
7from OpenSSL.crypto import TYPE_RSA, TYPE_DSA, Error, PKey, PKeyType
Jean-Paul Calderoneeff3cd92008-03-05 22:35:26 -05008from OpenSSL.crypto import X509, X509Name, X509NameType
Jean-Paul Calderoned8782ad2008-03-04 23:39:59 -05009
10
Jean-Paul Calderoneac930e12008-03-06 18:50:51 -050011class _Python23TestCaseHelper:
Jean-Paul Calderone7da26a72008-03-06 00:35:20 -050012 # Python 2.3 compatibility.
13 def assertTrue(self, *a, **kw):
14 return self.failUnless(*a, **kw)
15
16
Jean-Paul Calderoneac930e12008-03-06 18:50:51 -050017class PKeyTests(TestCase, _Python23TestCaseHelper):
18 """
19 Unit tests for L{OpenSSL.crypto.PKey}.
20 """
Jean-Paul Calderoned8782ad2008-03-04 23:39:59 -050021 def test_construction(self):
22 """
23 L{PKey} takes no arguments and returns a new L{PKeyType} instance.
24 """
25 self.assertRaises(TypeError, PKey, None)
26 key = PKey()
27 self.assertTrue(
28 isinstance(key, PKeyType),
29 "%r is of type %r, should be %r" % (key, type(key), PKeyType))
30
31
32 def test_pregeneration(self):
33 """
34 L{PKeyType.bits} and L{PKeyType.type} return C{0} before the key is
35 generated.
36 """
37 key = PKey()
38 self.assertEqual(key.type(), 0)
39 self.assertEqual(key.bits(), 0)
40
41
42 def test_failedGeneration(self):
43 """
Jean-Paul Calderoneab82db72008-03-06 00:09:31 -050044 L{PKeyType.generate_key} takes two arguments, the first giving the key
45 type as one of L{TYPE_RSA} or L{TYPE_DSA} and the second giving the
46 number of bits to generate. If an invalid type is specified or
47 generation fails, L{Error} is raised. If an invalid number of bits is
48 specified, L{ValueError} or L{Error} is raised.
Jean-Paul Calderoned8782ad2008-03-04 23:39:59 -050049 """
50 key = PKey()
51 self.assertRaises(TypeError, key.generate_key)
52 self.assertRaises(TypeError, key.generate_key, 1, 2, 3)
53 self.assertRaises(TypeError, key.generate_key, "foo", "bar")
54 self.assertRaises(Error, key.generate_key, -1, 0)
Jean-Paul Calderoneab82db72008-03-06 00:09:31 -050055
Jean-Paul Calderoneab82db72008-03-06 00:09:31 -050056 self.assertRaises(ValueError, key.generate_key, TYPE_RSA, -1)
57 self.assertRaises(ValueError, key.generate_key, TYPE_RSA, 0)
Jean-Paul Calderoned71fe982008-03-06 00:31:50 -050058
59 # XXX RSA generation for small values of bits is fairly buggy in a wide
60 # range of OpenSSL versions. I need to figure out what the safe lower
61 # bound for a reasonable number of OpenSSL versions is and explicitly
62 # check for that in the wrapper. The failure behavior is typically an
63 # infinite loop inside OpenSSL.
64
65 # self.assertRaises(Error, key.generate_key, TYPE_RSA, 2)
Jean-Paul Calderoned8782ad2008-03-04 23:39:59 -050066
67 # XXX DSA generation seems happy with any number of bits. The DSS
68 # says bits must be between 512 and 1024 inclusive. OpenSSL's DSA
69 # generator doesn't seem to care about the upper limit at all. For
Jean-Paul Calderoneeff3cd92008-03-05 22:35:26 -050070 # the lower limit, it uses 512 if anything smaller is specified.
Jean-Paul Calderoned8782ad2008-03-04 23:39:59 -050071 # So, it doesn't seem possible to make generate_key fail for
72 # TYPE_DSA with a bits argument which is at least an int.
73
74 # self.assertRaises(Error, key.generate_key, TYPE_DSA, -7)
75
76
77 def test_rsaGeneration(self):
78 """
79 L{PKeyType.generate_key} generates an RSA key when passed
80 L{TYPE_RSA} as a type and a reasonable number of bits.
81 """
82 bits = 128
83 key = PKey()
84 key.generate_key(TYPE_RSA, bits)
85 self.assertEqual(key.type(), TYPE_RSA)
86 self.assertEqual(key.bits(), bits)
87
88
89 def test_dsaGeneration(self):
90 """
91 L{PKeyType.generate_key} generates a DSA key when passed
92 L{TYPE_DSA} as a type and a reasonable number of bits.
93 """
94 # 512 is a magic number. The DSS (Digital Signature Standard)
95 # allows a minimum of 512 bits for DSA. DSA_generate_parameters
96 # will silently promote any value below 512 to 512.
97 bits = 512
98 key = PKey()
99 key.generate_key(TYPE_DSA, bits)
100 self.assertEqual(key.type(), TYPE_DSA)
101 self.assertEqual(key.bits(), bits)
102
103
104 def test_regeneration(self):
105 """
106 L{PKeyType.generate_key} can be called multiple times on the same
107 key to generate new keys.
108 """
109 key = PKey()
110 for type, bits in [(TYPE_RSA, 512), (TYPE_DSA, 576)]:
111 key.generate_key(type, bits)
112 self.assertEqual(key.type(), type)
113 self.assertEqual(key.bits(), bits)
Jean-Paul Calderoneeff3cd92008-03-05 22:35:26 -0500114
115
116
Jean-Paul Calderoneac930e12008-03-06 18:50:51 -0500117class X509NameTests(TestCase, _Python23TestCaseHelper):
Jean-Paul Calderoneeff3cd92008-03-05 22:35:26 -0500118 """
119 Unit tests for L{OpenSSL.crypto.X509Name}.
120 """
Jean-Paul Calderonee098dc72008-03-06 18:36:19 -0500121 def _x509name(self, **attrs):
122 # XXX There's no other way to get a new X509Name yet.
123 name = X509().get_subject()
124 attrs = attrs.items()
125 # Make the order stable - order matters!
126 attrs.sort(lambda (k1, v1), (k2, v2): cmp(v1, v2))
127 for k, v in attrs:
128 setattr(name, k, v)
129 return name
130
131
Jean-Paul Calderoneeff3cd92008-03-05 22:35:26 -0500132 def test_attributes(self):
133 """
134 L{X509NameType} instances have attributes for each standard (?)
135 X509Name field.
136 """
Jean-Paul Calderonee098dc72008-03-06 18:36:19 -0500137 name = self._x509name()
Jean-Paul Calderoneeff3cd92008-03-05 22:35:26 -0500138 name.commonName = "foo"
139 self.assertEqual(name.commonName, "foo")
140 self.assertEqual(name.CN, "foo")
141 name.CN = "baz"
142 self.assertEqual(name.commonName, "baz")
143 self.assertEqual(name.CN, "baz")
144 name.commonName = "bar"
145 self.assertEqual(name.commonName, "bar")
146 self.assertEqual(name.CN, "bar")
147 name.CN = "quux"
148 self.assertEqual(name.commonName, "quux")
149 self.assertEqual(name.CN, "quux")
150
151
Jean-Paul Calderoneeff3cd92008-03-05 22:35:26 -0500152 def test_copy(self):
153 """
154 L{X509Name} creates a new L{X509NameType} instance with all the same
155 attributes as an existing L{X509NameType} instance when called with
156 one.
157 """
Jean-Paul Calderonee098dc72008-03-06 18:36:19 -0500158 name = self._x509name(commonName="foo", emailAddress="bar@example.com")
Jean-Paul Calderoneeff3cd92008-03-05 22:35:26 -0500159
160 copy = X509Name(name)
161 self.assertEqual(copy.commonName, "foo")
162 self.assertEqual(copy.emailAddress, "bar@example.com")
Jean-Paul Calderonee098dc72008-03-06 18:36:19 -0500163
164 # Mutate the copy and ensure the original is unmodified.
Jean-Paul Calderoneeff3cd92008-03-05 22:35:26 -0500165 copy.commonName = "baz"
166 self.assertEqual(name.commonName, "foo")
Jean-Paul Calderonee098dc72008-03-06 18:36:19 -0500167
168 # Mutate the original and ensure the copy is unmodified.
Jean-Paul Calderoneeff3cd92008-03-05 22:35:26 -0500169 name.emailAddress = "quux@example.com"
170 self.assertEqual(copy.emailAddress, "bar@example.com")
171
Jean-Paul Calderonee098dc72008-03-06 18:36:19 -0500172
173 def test_repr(self):
174 """
175 L{repr} passed an L{X509NameType} instance should return a string
176 containing a description of the type and the NIDs which have been set
177 on it.
178 """
179 name = self._x509name(commonName="foo", emailAddress="bar")
180 self.assertEqual(
181 repr(name),
182 "<X509Name object '/emailAddress=bar/CN=foo'>")
183
184
185 def test_comparison(self):
186 """
187 L{X509NameType} instances should compare based on their NIDs.
188 """
189 def _equality(a, b, assertTrue, assertFalse):
190 assertTrue(a == b, "(%r == %r) --> False" % (a, b))
191 assertFalse(a != b)
192 assertTrue(b == a)
193 assertFalse(b != a)
194
195 def assertEqual(a, b):
196 _equality(a, b, self.assertTrue, self.assertFalse)
197
198 # Instances compare equal to themselves.
199 name = self._x509name()
200 assertEqual(name, name)
201
202 # Empty instances should compare equal to each other.
203 assertEqual(self._x509name(), self._x509name())
204
205 # Instances with equal NIDs should compare equal to each other.
206 assertEqual(self._x509name(commonName="foo"),
207 self._x509name(commonName="foo"))
208
209 # Instance with equal NIDs set using different aliases should compare
210 # equal to each other.
211 assertEqual(self._x509name(commonName="foo"),
212 self._x509name(CN="foo"))
213
214 # Instances with more than one NID with the same values should compare
215 # equal to each other.
216 assertEqual(self._x509name(CN="foo", organizationalUnitName="bar"),
217 self._x509name(commonName="foo", OU="bar"))
218
219 def assertNotEqual(a, b):
220 _equality(a, b, self.assertFalse, self.assertTrue)
221
222 # Instances with different values for the same NID should not compare
223 # equal to each other.
224 assertNotEqual(self._x509name(CN="foo"),
225 self._x509name(CN="bar"))
226
227 # Instances with different NIDs should not compare equal to each other.
228 assertNotEqual(self._x509name(CN="foo"),
229 self._x509name(OU="foo"))
230
231 def _inequality(a, b, assertTrue, assertFalse):
232 assertTrue(a < b)
233 assertTrue(a <= b)
234 assertTrue(b > a)
235 assertTrue(b >= a)
236 assertFalse(a > b)
237 assertFalse(a >= b)
238 assertFalse(b < a)
239 assertFalse(b <= a)
240
241 def assertLessThan(a, b):
242 _inequality(a, b, self.assertTrue, self.assertFalse)
243
244 # An X509Name with a NID with a value which sorts less than the value
245 # of the same NID on another X509Name compares less than the other
246 # X509Name.
247 assertLessThan(self._x509name(CN="abc"),
248 self._x509name(CN="def"))
249
250 def assertGreaterThan(a, b):
251 _inequality(a, b, self.assertFalse, self.assertTrue)
252
253 # An X509Name with a NID with a value which sorts greater than the
254 # value of the same NID on another X509Name compares greater than the
255 # other X509Name.
256 assertGreaterThan(self._x509name(CN="def"),
257 self._x509name(CN="abc"))