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