blob: 99a62c336d9d06849090c71d6fed9209578f70a7 [file] [log] [blame]
Jean-Paul Calderone8b63d452008-03-21 18:31:12 -04001# Copyright (C) Jean-Paul Calderone 2008, All rights reserved
2
Jean-Paul Calderoned8782ad2008-03-04 23:39:59 -05003"""
4Unit tests for L{OpenSSL.crypto}.
5"""
6
Jean-Paul Calderone38a646d2008-03-25 15:16:18 -04007import os
8
Jean-Paul Calderoned8782ad2008-03-04 23:39:59 -05009from unittest import TestCase
10
11from OpenSSL.crypto import TYPE_RSA, TYPE_DSA, Error, PKey, PKeyType
Jean-Paul Calderone78381d22008-03-06 23:35:22 -050012from OpenSSL.crypto import X509, X509Type, X509Name, X509NameType
Jean-Paul Calderone2aa2b332008-03-06 21:43:14 -050013from OpenSSL.crypto import X509Req, X509ReqType
Jean-Paul Calderone38a646d2008-03-25 15:16:18 -040014from OpenSSL.crypto import FILETYPE_PEM, load_certificate
Jean-Paul Calderoned8782ad2008-03-04 23:39:59 -050015
Jean-Paul Calderoneac930e12008-03-06 18:50:51 -050016class _Python23TestCaseHelper:
Jean-Paul Calderone7da26a72008-03-06 00:35:20 -050017 # Python 2.3 compatibility.
18 def assertTrue(self, *a, **kw):
19 return self.failUnless(*a, **kw)
20
21
Jean-Paul Calderone7535dab2008-03-06 18:53:11 -050022 def assertFalse(self, *a, **kw):
23 return self.failIf(*a, **kw)
24
25
26
Jean-Paul Calderoneac930e12008-03-06 18:50:51 -050027class PKeyTests(TestCase, _Python23TestCaseHelper):
28 """
29 Unit tests for L{OpenSSL.crypto.PKey}.
30 """
Jean-Paul Calderoned8782ad2008-03-04 23:39:59 -050031 def test_construction(self):
32 """
33 L{PKey} takes no arguments and returns a new L{PKeyType} instance.
34 """
35 self.assertRaises(TypeError, PKey, None)
36 key = PKey()
37 self.assertTrue(
38 isinstance(key, PKeyType),
39 "%r is of type %r, should be %r" % (key, type(key), PKeyType))
40
41
42 def test_pregeneration(self):
43 """
44 L{PKeyType.bits} and L{PKeyType.type} return C{0} before the key is
45 generated.
46 """
47 key = PKey()
48 self.assertEqual(key.type(), 0)
49 self.assertEqual(key.bits(), 0)
50
51
52 def test_failedGeneration(self):
53 """
Jean-Paul Calderoneab82db72008-03-06 00:09:31 -050054 L{PKeyType.generate_key} takes two arguments, the first giving the key
55 type as one of L{TYPE_RSA} or L{TYPE_DSA} and the second giving the
56 number of bits to generate. If an invalid type is specified or
57 generation fails, L{Error} is raised. If an invalid number of bits is
58 specified, L{ValueError} or L{Error} is raised.
Jean-Paul Calderoned8782ad2008-03-04 23:39:59 -050059 """
60 key = PKey()
61 self.assertRaises(TypeError, key.generate_key)
62 self.assertRaises(TypeError, key.generate_key, 1, 2, 3)
63 self.assertRaises(TypeError, key.generate_key, "foo", "bar")
64 self.assertRaises(Error, key.generate_key, -1, 0)
Jean-Paul Calderoneab82db72008-03-06 00:09:31 -050065
Jean-Paul Calderoneab82db72008-03-06 00:09:31 -050066 self.assertRaises(ValueError, key.generate_key, TYPE_RSA, -1)
67 self.assertRaises(ValueError, key.generate_key, TYPE_RSA, 0)
Jean-Paul Calderoned71fe982008-03-06 00:31:50 -050068
69 # XXX RSA generation for small values of bits is fairly buggy in a wide
70 # range of OpenSSL versions. I need to figure out what the safe lower
71 # bound for a reasonable number of OpenSSL versions is and explicitly
72 # check for that in the wrapper. The failure behavior is typically an
73 # infinite loop inside OpenSSL.
74
75 # self.assertRaises(Error, key.generate_key, TYPE_RSA, 2)
Jean-Paul Calderoned8782ad2008-03-04 23:39:59 -050076
77 # XXX DSA generation seems happy with any number of bits. The DSS
78 # says bits must be between 512 and 1024 inclusive. OpenSSL's DSA
79 # generator doesn't seem to care about the upper limit at all. For
Jean-Paul Calderoneeff3cd92008-03-05 22:35:26 -050080 # the lower limit, it uses 512 if anything smaller is specified.
Jean-Paul Calderoned8782ad2008-03-04 23:39:59 -050081 # So, it doesn't seem possible to make generate_key fail for
82 # TYPE_DSA with a bits argument which is at least an int.
83
84 # self.assertRaises(Error, key.generate_key, TYPE_DSA, -7)
85
86
87 def test_rsaGeneration(self):
88 """
89 L{PKeyType.generate_key} generates an RSA key when passed
90 L{TYPE_RSA} as a type and a reasonable number of bits.
91 """
92 bits = 128
93 key = PKey()
94 key.generate_key(TYPE_RSA, bits)
95 self.assertEqual(key.type(), TYPE_RSA)
96 self.assertEqual(key.bits(), bits)
97
98
99 def test_dsaGeneration(self):
100 """
101 L{PKeyType.generate_key} generates a DSA key when passed
102 L{TYPE_DSA} as a type and a reasonable number of bits.
103 """
104 # 512 is a magic number. The DSS (Digital Signature Standard)
105 # allows a minimum of 512 bits for DSA. DSA_generate_parameters
106 # will silently promote any value below 512 to 512.
107 bits = 512
108 key = PKey()
109 key.generate_key(TYPE_DSA, bits)
110 self.assertEqual(key.type(), TYPE_DSA)
111 self.assertEqual(key.bits(), bits)
112
113
114 def test_regeneration(self):
115 """
116 L{PKeyType.generate_key} can be called multiple times on the same
117 key to generate new keys.
118 """
119 key = PKey()
120 for type, bits in [(TYPE_RSA, 512), (TYPE_DSA, 576)]:
121 key.generate_key(type, bits)
122 self.assertEqual(key.type(), type)
123 self.assertEqual(key.bits(), bits)
Jean-Paul Calderoneeff3cd92008-03-05 22:35:26 -0500124
125
126
Jean-Paul Calderoneac930e12008-03-06 18:50:51 -0500127class X509NameTests(TestCase, _Python23TestCaseHelper):
Jean-Paul Calderoneeff3cd92008-03-05 22:35:26 -0500128 """
129 Unit tests for L{OpenSSL.crypto.X509Name}.
130 """
Jean-Paul Calderonee098dc72008-03-06 18:36:19 -0500131 def _x509name(self, **attrs):
132 # XXX There's no other way to get a new X509Name yet.
133 name = X509().get_subject()
134 attrs = attrs.items()
135 # Make the order stable - order matters!
136 attrs.sort(lambda (k1, v1), (k2, v2): cmp(v1, v2))
137 for k, v in attrs:
138 setattr(name, k, v)
139 return name
140
141
Jean-Paul Calderoneeff3cd92008-03-05 22:35:26 -0500142 def test_attributes(self):
143 """
144 L{X509NameType} instances have attributes for each standard (?)
145 X509Name field.
146 """
Jean-Paul Calderonee098dc72008-03-06 18:36:19 -0500147 name = self._x509name()
Jean-Paul Calderoneeff3cd92008-03-05 22:35:26 -0500148 name.commonName = "foo"
149 self.assertEqual(name.commonName, "foo")
150 self.assertEqual(name.CN, "foo")
151 name.CN = "baz"
152 self.assertEqual(name.commonName, "baz")
153 self.assertEqual(name.CN, "baz")
154 name.commonName = "bar"
155 self.assertEqual(name.commonName, "bar")
156 self.assertEqual(name.CN, "bar")
157 name.CN = "quux"
158 self.assertEqual(name.commonName, "quux")
159 self.assertEqual(name.CN, "quux")
160
161
Jean-Paul Calderoneeff3cd92008-03-05 22:35:26 -0500162 def test_copy(self):
163 """
164 L{X509Name} creates a new L{X509NameType} instance with all the same
165 attributes as an existing L{X509NameType} instance when called with
166 one.
167 """
Jean-Paul Calderonee098dc72008-03-06 18:36:19 -0500168 name = self._x509name(commonName="foo", emailAddress="bar@example.com")
Jean-Paul Calderoneeff3cd92008-03-05 22:35:26 -0500169
170 copy = X509Name(name)
171 self.assertEqual(copy.commonName, "foo")
172 self.assertEqual(copy.emailAddress, "bar@example.com")
Jean-Paul Calderonee098dc72008-03-06 18:36:19 -0500173
174 # Mutate the copy and ensure the original is unmodified.
Jean-Paul Calderoneeff3cd92008-03-05 22:35:26 -0500175 copy.commonName = "baz"
176 self.assertEqual(name.commonName, "foo")
Jean-Paul Calderonee098dc72008-03-06 18:36:19 -0500177
178 # Mutate the original and ensure the copy is unmodified.
Jean-Paul Calderoneeff3cd92008-03-05 22:35:26 -0500179 name.emailAddress = "quux@example.com"
180 self.assertEqual(copy.emailAddress, "bar@example.com")
181
Jean-Paul Calderonee098dc72008-03-06 18:36:19 -0500182
183 def test_repr(self):
184 """
185 L{repr} passed an L{X509NameType} instance should return a string
186 containing a description of the type and the NIDs which have been set
187 on it.
188 """
189 name = self._x509name(commonName="foo", emailAddress="bar")
190 self.assertEqual(
191 repr(name),
192 "<X509Name object '/emailAddress=bar/CN=foo'>")
193
194
195 def test_comparison(self):
196 """
197 L{X509NameType} instances should compare based on their NIDs.
198 """
199 def _equality(a, b, assertTrue, assertFalse):
200 assertTrue(a == b, "(%r == %r) --> False" % (a, b))
201 assertFalse(a != b)
202 assertTrue(b == a)
203 assertFalse(b != a)
204
205 def assertEqual(a, b):
206 _equality(a, b, self.assertTrue, self.assertFalse)
207
208 # Instances compare equal to themselves.
209 name = self._x509name()
210 assertEqual(name, name)
211
212 # Empty instances should compare equal to each other.
213 assertEqual(self._x509name(), self._x509name())
214
215 # Instances with equal NIDs should compare equal to each other.
216 assertEqual(self._x509name(commonName="foo"),
217 self._x509name(commonName="foo"))
218
219 # Instance with equal NIDs set using different aliases should compare
220 # equal to each other.
221 assertEqual(self._x509name(commonName="foo"),
222 self._x509name(CN="foo"))
223
224 # Instances with more than one NID with the same values should compare
225 # equal to each other.
226 assertEqual(self._x509name(CN="foo", organizationalUnitName="bar"),
227 self._x509name(commonName="foo", OU="bar"))
228
229 def assertNotEqual(a, b):
230 _equality(a, b, self.assertFalse, self.assertTrue)
231
232 # Instances with different values for the same NID should not compare
233 # equal to each other.
234 assertNotEqual(self._x509name(CN="foo"),
235 self._x509name(CN="bar"))
236
237 # Instances with different NIDs should not compare equal to each other.
238 assertNotEqual(self._x509name(CN="foo"),
239 self._x509name(OU="foo"))
240
241 def _inequality(a, b, assertTrue, assertFalse):
242 assertTrue(a < b)
243 assertTrue(a <= b)
244 assertTrue(b > a)
245 assertTrue(b >= a)
246 assertFalse(a > b)
247 assertFalse(a >= b)
248 assertFalse(b < a)
249 assertFalse(b <= a)
250
251 def assertLessThan(a, b):
252 _inequality(a, b, self.assertTrue, self.assertFalse)
253
254 # An X509Name with a NID with a value which sorts less than the value
255 # of the same NID on another X509Name compares less than the other
256 # X509Name.
257 assertLessThan(self._x509name(CN="abc"),
258 self._x509name(CN="def"))
259
260 def assertGreaterThan(a, b):
261 _inequality(a, b, self.assertFalse, self.assertTrue)
262
263 # An X509Name with a NID with a value which sorts greater than the
264 # value of the same NID on another X509Name compares greater than the
265 # other X509Name.
266 assertGreaterThan(self._x509name(CN="def"),
267 self._x509name(CN="abc"))
Jean-Paul Calderone2aa2b332008-03-06 21:43:14 -0500268
269
Jean-Paul Calderone110cd092008-03-24 17:27:42 -0400270 def test_hash(self):
271 """
272 L{X509Name.hash} returns an integer hash based on the value of the
273 name.
274 """
275 a = self._x509name(CN="foo")
276 b = self._x509name(CN="foo")
277 self.assertEqual(a.hash(), b.hash())
278 a.CN = "bar"
279 self.assertNotEqual(a.hash(), b.hash())
280
281
Jean-Paul Calderonee957a002008-03-25 15:16:51 -0400282 def test_der(self):
283 """
284 L{X509Name.der} returns the DER encoded form of the name.
285 """
286 a = self._x509name(CN="foo", C="US")
287 self.assertEqual(
288 a.der(),
289 '0\x1b1\x0b0\t\x06\x03U\x04\x06\x13\x02US'
290 '1\x0c0\n\x06\x03U\x04\x03\x13\x03foo')
291
292
Jean-Paul Calderone110cd092008-03-24 17:27:42 -0400293
Jean-Paul Calderoneac0d95f2008-03-10 00:00:42 -0400294class _PKeyInteractionTestsMixin:
295 """
296 Tests which involve another thing and a PKey.
297 """
298 def signable(self):
299 """
300 Return something with a C{set_pubkey}, C{set_pubkey}, and C{sign} method.
301 """
302 raise NotImplementedError()
303
304
305 def test_signWithUngenerated(self):
306 """
307 L{X509Req.sign} raises L{ValueError} when pass a L{PKey} with no parts.
308 """
309 request = self.signable()
310 key = PKey()
311 self.assertRaises(ValueError, request.sign, key, 'MD5')
312
313
314 def test_signWithPublicKey(self):
315 """
316 L{X509Req.sign} raises L{ValueError} when pass a L{PKey} with no
317 private part as the signing key.
318 """
319 request = self.signable()
320 key = PKey()
321 key.generate_key(TYPE_RSA, 512)
322 request.set_pubkey(key)
323 pub = request.get_pubkey()
324 self.assertRaises(ValueError, request.sign, pub, 'MD5')
325
326
327
328class X509ReqTests(TestCase, _PKeyInteractionTestsMixin, _Python23TestCaseHelper):
Jean-Paul Calderone2aa2b332008-03-06 21:43:14 -0500329 """
330 Tests for L{OpenSSL.crypto.X509Req}.
331 """
Jean-Paul Calderoneac0d95f2008-03-10 00:00:42 -0400332 def signable(self):
333 """
334 Create and return a new L{X509Req}.
335 """
336 return X509Req()
337
338
Jean-Paul Calderone2aa2b332008-03-06 21:43:14 -0500339 def test_construction(self):
340 """
341 L{X509Req} takes no arguments and returns an L{X509ReqType} instance.
342 """
343 request = X509Req()
344 self.assertTrue(
345 isinstance(request, X509ReqType),
346 "%r is of type %r, should be %r" % (request, type(request), X509ReqType))
347
348
349 def test_get_subject(self):
350 """
351 L{X509ReqType.get_subject} returns an L{X509Name} for the subject of
352 the request and which is valid even after the request object is
353 otherwise dead.
354 """
355 request = X509Req()
356 subject = request.get_subject()
357 self.assertTrue(
358 isinstance(subject, X509NameType),
359 "%r is of type %r, should be %r" % (subject, type(subject), X509NameType))
360 subject.commonName = "foo"
361 self.assertEqual(request.get_subject().commonName, "foo")
362 del request
363 subject.commonName = "bar"
364 self.assertEqual(subject.commonName, "bar")
Jean-Paul Calderone78381d22008-03-06 23:35:22 -0500365
366
367
Jean-Paul Calderoneac0d95f2008-03-10 00:00:42 -0400368class X509Tests(TestCase, _PKeyInteractionTestsMixin, _Python23TestCaseHelper):
Jean-Paul Calderone78381d22008-03-06 23:35:22 -0500369 """
370 Tests for L{OpenSSL.crypto.X509}.
371 """
Jean-Paul Calderoneac0d95f2008-03-10 00:00:42 -0400372 def signable(self):
373 """
374 Create and return a new L{X509}.
375 """
376 return X509()
377
378
Jean-Paul Calderone78381d22008-03-06 23:35:22 -0500379 def test_construction(self):
380 """
381 L{X509} takes no arguments and returns an instance of L{X509Type}.
382 """
383 certificate = X509()
384 self.assertTrue(
385 isinstance(certificate, X509Type),
386 "%r is of type %r, should be %r" % (certificate,
387 type(certificate),
388 X509Type))
389
390
391 def test_serial_number(self):
392 """
393 The serial number of an L{X509Type} can be retrieved and modified with
394 L{X509Type.get_serial_number} and L{X509Type.set_serial_number}.
395 """
396 certificate = X509()
397 self.assertRaises(TypeError, certificate.set_serial_number)
398 self.assertRaises(TypeError, certificate.set_serial_number, 1, 2)
399 self.assertRaises(TypeError, certificate.set_serial_number, "1")
400 self.assertRaises(TypeError, certificate.set_serial_number, 5.5)
401 self.assertEqual(certificate.get_serial_number(), 0)
402 certificate.set_serial_number(1)
403 self.assertEqual(certificate.get_serial_number(), 1)
404 certificate.set_serial_number(2 ** 32 + 1)
405 self.assertEqual(certificate.get_serial_number(), 2 ** 32 + 1)
406 certificate.set_serial_number(2 ** 64 + 1)
407 self.assertEqual(certificate.get_serial_number(), 2 ** 64 + 1)
Jean-Paul Calderone525ef802008-03-09 20:39:42 -0400408 certificate.set_serial_number(2 ** 128 + 1)
409 self.assertEqual(certificate.get_serial_number(), 2 ** 128 + 1)
410
411
412 def _setBoundTest(self, which):
413 """
414 L{X509Type.set_notBefore} takes a string in the format of an ASN1
415 GENERALIZEDTIME and sets the beginning of the certificate's validity
416 period to it.
417 """
418 certificate = X509()
419 set = getattr(certificate, 'set_not' + which)
420 get = getattr(certificate, 'get_not' + which)
421
Jean-Paul Calderonee0615b52008-03-09 21:44:46 -0400422 # Starts with no value.
423 self.assertEqual(get(), None)
424
Jean-Paul Calderone525ef802008-03-09 20:39:42 -0400425 # GMT (Or is it UTC?) -exarkun
426 when = "20040203040506Z"
427 set(when)
428 self.assertEqual(get(), when)
429
430 # A plus two hours and thirty minutes offset
431 when = "20040203040506+0530"
432 set(when)
433 self.assertEqual(get(), when)
434
435 # A minus one hour fifteen minutes offset
436 when = "20040203040506-0115"
437 set(when)
438 self.assertEqual(get(), when)
439
440 # An invalid string results in a ValueError
441 self.assertRaises(ValueError, set, "foo bar")
442
443
444 def test_set_notBefore(self):
445 """
446 L{X509Type.set_notBefore} takes a string in the format of an ASN1
447 GENERALIZEDTIME and sets the beginning of the certificate's validity
448 period to it.
449 """
450 self._setBoundTest("Before")
451
452
453 def test_set_notAfter(self):
454 """
455 L{X509Type.set_notAfter} takes a string in the format of an ASN1
456 GENERALIZEDTIME and sets the end of the certificate's validity period
457 to it.
458 """
459 self._setBoundTest("After")
Jean-Paul Calderone76576d52008-03-24 16:04:46 -0400460
461
Jean-Paul Calderone38a646d2008-03-25 15:16:18 -0400462 def test_get_notBefore(self):
463 """
464 L{X509Type.get_notBefore} returns a string in the format of an ASN1
465 GENERALIZEDTIME even for certificates which store it as UTCTIME
466 internally.
467 """
468 pem = os.path.join(os.path.split(__file__)[0], "server.pem")
469 cert = load_certificate(FILETYPE_PEM, file(pem, "r").read())
470 self.assertEqual(cert.get_notBefore(), "20080325190413Z")
471
472
473 def test_get_notAfter(self):
474 """
475 L{X509Type.get_notAfter} returns a string in the format of an ASN1
476 GENERALIZEDTIME even for certificates which store it as UTCTIME
477 internally.
478 """
479 pem = os.path.join(os.path.split(__file__)[0], "server.pem")
480 cert = load_certificate(FILETYPE_PEM, file(pem, "r").read())
481 self.assertEqual(cert.get_notAfter(), "20090325190413Z")
482
483
Jean-Paul Calderone76576d52008-03-24 16:04:46 -0400484 def test_digest(self):
485 """
486 L{X509.digest} returns a string giving ":"-separated hex-encoded words
487 of the digest of the certificate.
488 """
489 cert = X509()
490 self.assertEqual(
491 cert.digest("md5"),
492 "A8:EB:07:F8:53:25:0A:F2:56:05:C5:A5:C4:C4:C7:15")