Jean-Paul Calderone | 8b63d45 | 2008-03-21 18:31:12 -0400 | [diff] [blame] | 1 | # Copyright (C) Jean-Paul Calderone 2008, All rights reserved |
| 2 | |
Jean-Paul Calderone | d8782ad | 2008-03-04 23:39:59 -0500 | [diff] [blame] | 3 | """ |
| 4 | Unit tests for L{OpenSSL.crypto}. |
| 5 | """ |
| 6 | |
| 7 | from unittest import TestCase |
| 8 | |
| 9 | from OpenSSL.crypto import TYPE_RSA, TYPE_DSA, Error, PKey, PKeyType |
Jean-Paul Calderone | 78381d2 | 2008-03-06 23:35:22 -0500 | [diff] [blame] | 10 | from OpenSSL.crypto import X509, X509Type, X509Name, X509NameType |
Jean-Paul Calderone | 2aa2b33 | 2008-03-06 21:43:14 -0500 | [diff] [blame] | 11 | from OpenSSL.crypto import X509Req, X509ReqType |
Jean-Paul Calderone | e7db4b4 | 2008-12-31 13:39:24 -0500 | [diff] [blame^] | 12 | from OpenSSL.crypto import X509Extension, X509ExtensionType |
Jean-Paul Calderone | 828c9cb | 2008-04-26 18:06:54 -0400 | [diff] [blame] | 13 | from OpenSSL.crypto import FILETYPE_PEM, load_certificate, load_privatekey |
| 14 | from OpenSSL.crypto import dump_privatekey |
| 15 | |
| 16 | |
| 17 | cleartextPrivateKeyPEM = ( |
| 18 | "-----BEGIN RSA PRIVATE KEY-----\n" |
| 19 | "MIICXAIBAAKBgQDaemNe1syksAbFFpF3aoOrZ18vB/IQNZrAjFqXPv9iieJm7+Tc\n" |
| 20 | "g+lA/v0qmoEKrpT2xfwxXmvZwBNM4ZhyRC3DPIFEyJV7/3IA1p5iuMY/GJI1VIgn\n" |
| 21 | "aikQCnrsyxtaRpsMBeZRniaVzcUJ+XnEdFGEjlo+k0xlwfVclDEMwgpXAQIDAQAB\n" |
| 22 | "AoGBALi0a7pMQqqgnriVAdpBVJveQtxSDVWi2/gZMKVZfzNheuSnv4amhtaKPKJ+\n" |
| 23 | "CMZtHkcazsE2IFvxRN/kgato9H3gJqq8nq2CkdpdLNVKBoxiCtkLfutdY4SQLtoY\n" |
| 24 | "USN7exk131pchsAJXYlR6mCW+ZP+E523cNwpPgsyKxVbmXSBAkEA9470fy2W0jFM\n" |
| 25 | "taZFslpntKSzbvn6JmdtjtvWrM1bBaeeqFiGBuQFYg46VaCUaeRWYw02jmYAsDYh\n" |
| 26 | "ZQavmXThaQJBAOHtlAQ0IJJEiMZr6vtVPH32fmbthSv1AUSYPzKqdlQrUnOXPQXu\n" |
| 27 | "z70cFoLG1TvPF5rBxbOkbQ/s8/ka5ZjPfdkCQCeC7YsO36+UpsWnUCBzRXITh4AC\n" |
| 28 | "7eYLQ/U1KUJTVF/GrQ/5cQrQgftwgecAxi9Qfmk4xqhbp2h4e0QAmS5I9WECQH02\n" |
| 29 | "0QwrX8nxFeTytr8pFGezj4a4KVCdb2B3CL+p3f70K7RIo9d/7b6frJI6ZL/LHQf2\n" |
| 30 | "UP4pKRDkgKsVDx7MELECQGm072/Z7vmb03h/uE95IYJOgY4nfmYs0QKA9Is18wUz\n" |
| 31 | "DpjfE33p0Ha6GO1VZRIQoqE24F8o5oimy3BEjryFuw4=\n" |
| 32 | "-----END RSA PRIVATE KEY-----\n") |
| 33 | |
| 34 | |
Jean-Paul Calderone | 5ef8651 | 2008-04-26 19:06:28 -0400 | [diff] [blame] | 35 | cleartextCertificatePEM = ( |
| 36 | "-----BEGIN CERTIFICATE-----\n" |
| 37 | "MIICfTCCAeYCAQEwDQYJKoZIhvcNAQEEBQAwgYYxCzAJBgNVBAYTAlVTMRkwFwYD\n" |
| 38 | "VQQDExBweW9wZW5zc2wuc2YubmV0MREwDwYDVQQHEwhOZXcgWW9yazESMBAGA1UE\n" |
| 39 | "ChMJUHlPcGVuU1NMMREwDwYDVQQIEwhOZXcgWW9yazEQMA4GCSqGSIb3DQEJARYB\n" |
| 40 | "IDEQMA4GA1UECxMHVGVzdGluZzAeFw0wODAzMjUxOTA0MTNaFw0wOTAzMjUxOTA0\n" |
| 41 | "MTNaMIGGMQswCQYDVQQGEwJVUzEZMBcGA1UEAxMQcHlvcGVuc3NsLnNmLm5ldDER\n" |
| 42 | "MA8GA1UEBxMITmV3IFlvcmsxEjAQBgNVBAoTCVB5T3BlblNTTDERMA8GA1UECBMI\n" |
| 43 | "TmV3IFlvcmsxEDAOBgkqhkiG9w0BCQEWASAxEDAOBgNVBAsTB1Rlc3RpbmcwgZ8w\n" |
| 44 | "DQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBANp6Y17WzKSwBsUWkXdqg6tnXy8H8hA1\n" |
| 45 | "msCMWpc+/2KJ4mbv5NyD6UD+/SqagQqulPbF/DFea9nAE0zhmHJELcM8gUTIlXv/\n" |
| 46 | "cgDWnmK4xj8YkjVUiCdqKRAKeuzLG1pGmwwF5lGeJpXNxQn5ecR0UYSOWj6TTGXB\n" |
| 47 | "9VyUMQzCClcBAgMBAAEwDQYJKoZIhvcNAQEEBQADgYEAmm0Vzvv1O91WLl2LnF2P\n" |
| 48 | "q55LJdOnJbCCXIgxLdoVmvYAz1ZJq1eGKgKWI5QLgxiSzJLEU7KK//aVfiZzoCd5\n" |
| 49 | "RipBiEEMEV4eAY317bHPwPP+4Bj9t0l8AsDLseC5vLRHgxrLEu3bn08DYx6imB5Q\n" |
| 50 | "UBj849/xpszEM7BhwKE0GiQ=\n" |
| 51 | "-----END CERTIFICATE-----\n") |
| 52 | |
| 53 | |
Jean-Paul Calderone | 828c9cb | 2008-04-26 18:06:54 -0400 | [diff] [blame] | 54 | encryptedPrivateKeyPEM = ( |
| 55 | "-----BEGIN RSA PRIVATE KEY-----\n" |
| 56 | "Proc-Type: 4,ENCRYPTED\n" |
| 57 | "DEK-Info: BF-CBC,8306665233D056B1\n" |
| 58 | "\n" |
| 59 | "BwxghOcX1F+M108qRGBfpUBrfaeKOszDEV18OjEE55p0yGsiDxvdol3c4bwI5ITy\n" |
| 60 | "ltP8w9O33CDUCjr+Ymj8xLpPP60TTfr/aHq+2fEuG4TfkeHb5fVYm0mgVnaOhJs3\n" |
| 61 | "a2n5IL/KNCdP3zMZa0IaMJ0M+VK90SLpq5nzXOWkufLyZL1+n8srkk06gepmHS7L\n" |
| 62 | "rH3rALNboG8yTH1qjE8PwcMrJAQfRMd4/4RTQv+4pUuKj7I2en+YwSQ/gomy7qN1\n" |
| 63 | "3s/gMgV/2GUbEcTVch4thZ9l3WsX18V76rBQkiZ7yrJkxwNMv+Qc2GfHtBnsXAyA\n" |
| 64 | "0nIE4Mm/OQqX8h7EJ4c2s1DMGVS0YZGU+75HN0A3iD01h8C5utqSScWzBA45j/Vy\n" |
| 65 | "3aypQVqQeW7kBMQlpc6pHvJ1EsjiAJRCto7tZNLxRdjMKBV4w75JNLaAFSraqA+R\n" |
| 66 | "/WPcdcXAQuhmCeh31fzmVOHJGRF7/5pAR/b7AnFTD4YbYVcglNis/jpdiI9k2AYP\n" |
| 67 | "wZNwXOIh6Ibq5hMvyV4/pySyLbgDOrfrOGpi8N6lBbzewByYQKiXwUEZf+Y5499/\n" |
| 68 | "CckajBhgYynPpe6mgsSeklWGc845iIwAtzavBNZIkn1hKP1P+TFjbl2O75u/9JLJ\n" |
| 69 | "6i4IFYCyQmwiHX8nTR717SpCN2gyZ2HrX7z2mKP/KokkAX2yidwoKh9FMUV5lOGO\n" |
| 70 | "JPc4MfPo4lPB7SP30AtOh7y7zlS3x8Uo0+0wCg5Z5Fn/73x3W+p5nyI0G9n7RGzL\n" |
| 71 | "ZeCWLdG/Cm6ZyIpYZGbZ5m+U3Fr6/El9V6LSxrB1TB+8G1NTdLlbeA==\n" |
| 72 | "-----END RSA PRIVATE KEY-----\n") |
| 73 | encryptedPrivateKeyPEMPassphrase = "foobar" |
| 74 | |
| 75 | |
Jean-Paul Calderone | ac930e1 | 2008-03-06 18:50:51 -0500 | [diff] [blame] | 76 | class _Python23TestCaseHelper: |
Jean-Paul Calderone | 7da26a7 | 2008-03-06 00:35:20 -0500 | [diff] [blame] | 77 | # Python 2.3 compatibility. |
| 78 | def assertTrue(self, *a, **kw): |
| 79 | return self.failUnless(*a, **kw) |
| 80 | |
| 81 | |
Jean-Paul Calderone | 7535dab | 2008-03-06 18:53:11 -0500 | [diff] [blame] | 82 | def assertFalse(self, *a, **kw): |
| 83 | return self.failIf(*a, **kw) |
| 84 | |
| 85 | |
Jean-Paul Calderone | e7db4b4 | 2008-12-31 13:39:24 -0500 | [diff] [blame^] | 86 | class X509ExtTests(TestCase, _Python23TestCaseHelper): |
| 87 | def test_construction(self): |
| 88 | """ |
| 89 | L{X509Extension} accepts an extension type name, a critical flag, |
| 90 | and an extension value and returns an L{X509ExtensionType} instance. |
| 91 | """ |
| 92 | basic = X509Extension('basicConstraints', 1, 'CA:true') |
| 93 | self.assertTrue( |
| 94 | isinstance(basic, X509ExtensionType), |
| 95 | "%r is of type %r, should be %r" % ( |
| 96 | basic, type(basic), X509ExtensionType)) |
| 97 | |
| 98 | comment = X509Extension('nsComment', 0, 'pyOpenSSL unit test') |
| 99 | self.assertTrue( |
| 100 | isinstance(comment, X509ExtensionType), |
| 101 | "%r is of type %r, should be %r" % ( |
| 102 | comment, type(comment), X509ExtensionType)) |
| 103 | |
| 104 | |
| 105 | def test_get_critical(self): |
| 106 | """ |
| 107 | L{X509ExtensionType.get_critical} returns the value of the |
| 108 | extension's critical flag. |
| 109 | """ |
| 110 | ext = X509Extension('basicConstraints', 1, 'CA:true') |
| 111 | self.assertTrue(ext.get_critical()) |
| 112 | ext = X509Extension('basicConstraints', 0, 'CA:true') |
| 113 | self.assertFalse(ext.get_critical()) |
| 114 | |
Jean-Paul Calderone | 7535dab | 2008-03-06 18:53:11 -0500 | [diff] [blame] | 115 | |
Jean-Paul Calderone | ac930e1 | 2008-03-06 18:50:51 -0500 | [diff] [blame] | 116 | class PKeyTests(TestCase, _Python23TestCaseHelper): |
| 117 | """ |
| 118 | Unit tests for L{OpenSSL.crypto.PKey}. |
| 119 | """ |
Jean-Paul Calderone | d8782ad | 2008-03-04 23:39:59 -0500 | [diff] [blame] | 120 | def test_construction(self): |
| 121 | """ |
| 122 | L{PKey} takes no arguments and returns a new L{PKeyType} instance. |
| 123 | """ |
| 124 | self.assertRaises(TypeError, PKey, None) |
| 125 | key = PKey() |
| 126 | self.assertTrue( |
| 127 | isinstance(key, PKeyType), |
| 128 | "%r is of type %r, should be %r" % (key, type(key), PKeyType)) |
| 129 | |
| 130 | |
| 131 | def test_pregeneration(self): |
| 132 | """ |
| 133 | L{PKeyType.bits} and L{PKeyType.type} return C{0} before the key is |
| 134 | generated. |
| 135 | """ |
| 136 | key = PKey() |
| 137 | self.assertEqual(key.type(), 0) |
| 138 | self.assertEqual(key.bits(), 0) |
| 139 | |
| 140 | |
| 141 | def test_failedGeneration(self): |
| 142 | """ |
Jean-Paul Calderone | ab82db7 | 2008-03-06 00:09:31 -0500 | [diff] [blame] | 143 | L{PKeyType.generate_key} takes two arguments, the first giving the key |
| 144 | type as one of L{TYPE_RSA} or L{TYPE_DSA} and the second giving the |
| 145 | number of bits to generate. If an invalid type is specified or |
| 146 | generation fails, L{Error} is raised. If an invalid number of bits is |
| 147 | specified, L{ValueError} or L{Error} is raised. |
Jean-Paul Calderone | d8782ad | 2008-03-04 23:39:59 -0500 | [diff] [blame] | 148 | """ |
| 149 | key = PKey() |
| 150 | self.assertRaises(TypeError, key.generate_key) |
| 151 | self.assertRaises(TypeError, key.generate_key, 1, 2, 3) |
| 152 | self.assertRaises(TypeError, key.generate_key, "foo", "bar") |
| 153 | self.assertRaises(Error, key.generate_key, -1, 0) |
Jean-Paul Calderone | ab82db7 | 2008-03-06 00:09:31 -0500 | [diff] [blame] | 154 | |
Jean-Paul Calderone | ab82db7 | 2008-03-06 00:09:31 -0500 | [diff] [blame] | 155 | self.assertRaises(ValueError, key.generate_key, TYPE_RSA, -1) |
| 156 | self.assertRaises(ValueError, key.generate_key, TYPE_RSA, 0) |
Jean-Paul Calderone | d71fe98 | 2008-03-06 00:31:50 -0500 | [diff] [blame] | 157 | |
| 158 | # XXX RSA generation for small values of bits is fairly buggy in a wide |
| 159 | # range of OpenSSL versions. I need to figure out what the safe lower |
| 160 | # bound for a reasonable number of OpenSSL versions is and explicitly |
| 161 | # check for that in the wrapper. The failure behavior is typically an |
| 162 | # infinite loop inside OpenSSL. |
| 163 | |
| 164 | # self.assertRaises(Error, key.generate_key, TYPE_RSA, 2) |
Jean-Paul Calderone | d8782ad | 2008-03-04 23:39:59 -0500 | [diff] [blame] | 165 | |
| 166 | # XXX DSA generation seems happy with any number of bits. The DSS |
| 167 | # says bits must be between 512 and 1024 inclusive. OpenSSL's DSA |
| 168 | # generator doesn't seem to care about the upper limit at all. For |
Jean-Paul Calderone | eff3cd9 | 2008-03-05 22:35:26 -0500 | [diff] [blame] | 169 | # the lower limit, it uses 512 if anything smaller is specified. |
Jean-Paul Calderone | d8782ad | 2008-03-04 23:39:59 -0500 | [diff] [blame] | 170 | # So, it doesn't seem possible to make generate_key fail for |
| 171 | # TYPE_DSA with a bits argument which is at least an int. |
| 172 | |
| 173 | # self.assertRaises(Error, key.generate_key, TYPE_DSA, -7) |
| 174 | |
| 175 | |
| 176 | def test_rsaGeneration(self): |
| 177 | """ |
| 178 | L{PKeyType.generate_key} generates an RSA key when passed |
| 179 | L{TYPE_RSA} as a type and a reasonable number of bits. |
| 180 | """ |
| 181 | bits = 128 |
| 182 | key = PKey() |
| 183 | key.generate_key(TYPE_RSA, bits) |
| 184 | self.assertEqual(key.type(), TYPE_RSA) |
| 185 | self.assertEqual(key.bits(), bits) |
| 186 | |
| 187 | |
| 188 | def test_dsaGeneration(self): |
| 189 | """ |
| 190 | L{PKeyType.generate_key} generates a DSA key when passed |
| 191 | L{TYPE_DSA} as a type and a reasonable number of bits. |
| 192 | """ |
| 193 | # 512 is a magic number. The DSS (Digital Signature Standard) |
| 194 | # allows a minimum of 512 bits for DSA. DSA_generate_parameters |
| 195 | # will silently promote any value below 512 to 512. |
| 196 | bits = 512 |
| 197 | key = PKey() |
| 198 | key.generate_key(TYPE_DSA, bits) |
| 199 | self.assertEqual(key.type(), TYPE_DSA) |
| 200 | self.assertEqual(key.bits(), bits) |
| 201 | |
| 202 | |
| 203 | def test_regeneration(self): |
| 204 | """ |
| 205 | L{PKeyType.generate_key} can be called multiple times on the same |
| 206 | key to generate new keys. |
| 207 | """ |
| 208 | key = PKey() |
| 209 | for type, bits in [(TYPE_RSA, 512), (TYPE_DSA, 576)]: |
| 210 | key.generate_key(type, bits) |
| 211 | self.assertEqual(key.type(), type) |
| 212 | self.assertEqual(key.bits(), bits) |
Jean-Paul Calderone | eff3cd9 | 2008-03-05 22:35:26 -0500 | [diff] [blame] | 213 | |
| 214 | |
| 215 | |
Jean-Paul Calderone | ac930e1 | 2008-03-06 18:50:51 -0500 | [diff] [blame] | 216 | class X509NameTests(TestCase, _Python23TestCaseHelper): |
Jean-Paul Calderone | eff3cd9 | 2008-03-05 22:35:26 -0500 | [diff] [blame] | 217 | """ |
| 218 | Unit tests for L{OpenSSL.crypto.X509Name}. |
| 219 | """ |
Jean-Paul Calderone | e098dc7 | 2008-03-06 18:36:19 -0500 | [diff] [blame] | 220 | def _x509name(self, **attrs): |
| 221 | # XXX There's no other way to get a new X509Name yet. |
| 222 | name = X509().get_subject() |
| 223 | attrs = attrs.items() |
| 224 | # Make the order stable - order matters! |
| 225 | attrs.sort(lambda (k1, v1), (k2, v2): cmp(v1, v2)) |
| 226 | for k, v in attrs: |
| 227 | setattr(name, k, v) |
| 228 | return name |
| 229 | |
| 230 | |
Jean-Paul Calderone | eff3cd9 | 2008-03-05 22:35:26 -0500 | [diff] [blame] | 231 | def test_attributes(self): |
| 232 | """ |
| 233 | L{X509NameType} instances have attributes for each standard (?) |
| 234 | X509Name field. |
| 235 | """ |
Jean-Paul Calderone | e098dc7 | 2008-03-06 18:36:19 -0500 | [diff] [blame] | 236 | name = self._x509name() |
Jean-Paul Calderone | eff3cd9 | 2008-03-05 22:35:26 -0500 | [diff] [blame] | 237 | name.commonName = "foo" |
| 238 | self.assertEqual(name.commonName, "foo") |
| 239 | self.assertEqual(name.CN, "foo") |
| 240 | name.CN = "baz" |
| 241 | self.assertEqual(name.commonName, "baz") |
| 242 | self.assertEqual(name.CN, "baz") |
| 243 | name.commonName = "bar" |
| 244 | self.assertEqual(name.commonName, "bar") |
| 245 | self.assertEqual(name.CN, "bar") |
| 246 | name.CN = "quux" |
| 247 | self.assertEqual(name.commonName, "quux") |
| 248 | self.assertEqual(name.CN, "quux") |
| 249 | |
| 250 | |
Jean-Paul Calderone | eff3cd9 | 2008-03-05 22:35:26 -0500 | [diff] [blame] | 251 | def test_copy(self): |
| 252 | """ |
| 253 | L{X509Name} creates a new L{X509NameType} instance with all the same |
| 254 | attributes as an existing L{X509NameType} instance when called with |
| 255 | one. |
| 256 | """ |
Jean-Paul Calderone | e098dc7 | 2008-03-06 18:36:19 -0500 | [diff] [blame] | 257 | name = self._x509name(commonName="foo", emailAddress="bar@example.com") |
Jean-Paul Calderone | eff3cd9 | 2008-03-05 22:35:26 -0500 | [diff] [blame] | 258 | |
| 259 | copy = X509Name(name) |
| 260 | self.assertEqual(copy.commonName, "foo") |
| 261 | self.assertEqual(copy.emailAddress, "bar@example.com") |
Jean-Paul Calderone | e098dc7 | 2008-03-06 18:36:19 -0500 | [diff] [blame] | 262 | |
| 263 | # Mutate the copy and ensure the original is unmodified. |
Jean-Paul Calderone | eff3cd9 | 2008-03-05 22:35:26 -0500 | [diff] [blame] | 264 | copy.commonName = "baz" |
| 265 | self.assertEqual(name.commonName, "foo") |
Jean-Paul Calderone | e098dc7 | 2008-03-06 18:36:19 -0500 | [diff] [blame] | 266 | |
| 267 | # Mutate the original and ensure the copy is unmodified. |
Jean-Paul Calderone | eff3cd9 | 2008-03-05 22:35:26 -0500 | [diff] [blame] | 268 | name.emailAddress = "quux@example.com" |
| 269 | self.assertEqual(copy.emailAddress, "bar@example.com") |
| 270 | |
Jean-Paul Calderone | e098dc7 | 2008-03-06 18:36:19 -0500 | [diff] [blame] | 271 | |
| 272 | def test_repr(self): |
| 273 | """ |
| 274 | L{repr} passed an L{X509NameType} instance should return a string |
| 275 | containing a description of the type and the NIDs which have been set |
| 276 | on it. |
| 277 | """ |
| 278 | name = self._x509name(commonName="foo", emailAddress="bar") |
| 279 | self.assertEqual( |
| 280 | repr(name), |
| 281 | "<X509Name object '/emailAddress=bar/CN=foo'>") |
| 282 | |
| 283 | |
| 284 | def test_comparison(self): |
| 285 | """ |
| 286 | L{X509NameType} instances should compare based on their NIDs. |
| 287 | """ |
| 288 | def _equality(a, b, assertTrue, assertFalse): |
| 289 | assertTrue(a == b, "(%r == %r) --> False" % (a, b)) |
| 290 | assertFalse(a != b) |
| 291 | assertTrue(b == a) |
| 292 | assertFalse(b != a) |
| 293 | |
| 294 | def assertEqual(a, b): |
| 295 | _equality(a, b, self.assertTrue, self.assertFalse) |
| 296 | |
| 297 | # Instances compare equal to themselves. |
| 298 | name = self._x509name() |
| 299 | assertEqual(name, name) |
| 300 | |
| 301 | # Empty instances should compare equal to each other. |
| 302 | assertEqual(self._x509name(), self._x509name()) |
| 303 | |
| 304 | # Instances with equal NIDs should compare equal to each other. |
| 305 | assertEqual(self._x509name(commonName="foo"), |
| 306 | self._x509name(commonName="foo")) |
| 307 | |
| 308 | # Instance with equal NIDs set using different aliases should compare |
| 309 | # equal to each other. |
| 310 | assertEqual(self._x509name(commonName="foo"), |
| 311 | self._x509name(CN="foo")) |
| 312 | |
| 313 | # Instances with more than one NID with the same values should compare |
| 314 | # equal to each other. |
| 315 | assertEqual(self._x509name(CN="foo", organizationalUnitName="bar"), |
| 316 | self._x509name(commonName="foo", OU="bar")) |
| 317 | |
| 318 | def assertNotEqual(a, b): |
| 319 | _equality(a, b, self.assertFalse, self.assertTrue) |
| 320 | |
| 321 | # Instances with different values for the same NID should not compare |
| 322 | # equal to each other. |
| 323 | assertNotEqual(self._x509name(CN="foo"), |
| 324 | self._x509name(CN="bar")) |
| 325 | |
| 326 | # Instances with different NIDs should not compare equal to each other. |
| 327 | assertNotEqual(self._x509name(CN="foo"), |
| 328 | self._x509name(OU="foo")) |
| 329 | |
| 330 | def _inequality(a, b, assertTrue, assertFalse): |
| 331 | assertTrue(a < b) |
| 332 | assertTrue(a <= b) |
| 333 | assertTrue(b > a) |
| 334 | assertTrue(b >= a) |
| 335 | assertFalse(a > b) |
| 336 | assertFalse(a >= b) |
| 337 | assertFalse(b < a) |
| 338 | assertFalse(b <= a) |
| 339 | |
| 340 | def assertLessThan(a, b): |
| 341 | _inequality(a, b, self.assertTrue, self.assertFalse) |
| 342 | |
| 343 | # An X509Name with a NID with a value which sorts less than the value |
| 344 | # of the same NID on another X509Name compares less than the other |
| 345 | # X509Name. |
| 346 | assertLessThan(self._x509name(CN="abc"), |
| 347 | self._x509name(CN="def")) |
| 348 | |
| 349 | def assertGreaterThan(a, b): |
| 350 | _inequality(a, b, self.assertFalse, self.assertTrue) |
| 351 | |
| 352 | # An X509Name with a NID with a value which sorts greater than the |
| 353 | # value of the same NID on another X509Name compares greater than the |
| 354 | # other X509Name. |
| 355 | assertGreaterThan(self._x509name(CN="def"), |
| 356 | self._x509name(CN="abc")) |
Jean-Paul Calderone | 2aa2b33 | 2008-03-06 21:43:14 -0500 | [diff] [blame] | 357 | |
| 358 | |
Jean-Paul Calderone | 110cd09 | 2008-03-24 17:27:42 -0400 | [diff] [blame] | 359 | def test_hash(self): |
| 360 | """ |
| 361 | L{X509Name.hash} returns an integer hash based on the value of the |
| 362 | name. |
| 363 | """ |
| 364 | a = self._x509name(CN="foo") |
| 365 | b = self._x509name(CN="foo") |
| 366 | self.assertEqual(a.hash(), b.hash()) |
| 367 | a.CN = "bar" |
| 368 | self.assertNotEqual(a.hash(), b.hash()) |
| 369 | |
| 370 | |
Jean-Paul Calderone | e957a00 | 2008-03-25 15:16:51 -0400 | [diff] [blame] | 371 | def test_der(self): |
| 372 | """ |
| 373 | L{X509Name.der} returns the DER encoded form of the name. |
| 374 | """ |
| 375 | a = self._x509name(CN="foo", C="US") |
| 376 | self.assertEqual( |
| 377 | a.der(), |
| 378 | '0\x1b1\x0b0\t\x06\x03U\x04\x06\x13\x02US' |
| 379 | '1\x0c0\n\x06\x03U\x04\x03\x13\x03foo') |
| 380 | |
| 381 | |
Jean-Paul Calderone | c54cc18 | 2008-03-26 21:11:07 -0400 | [diff] [blame] | 382 | def test_get_components(self): |
| 383 | """ |
| 384 | L{X509Name.get_components} returns a C{list} of two-tuples of C{str} |
| 385 | giving the NIDs and associated values which make up the name. |
| 386 | """ |
| 387 | a = self._x509name() |
| 388 | self.assertEqual(a.get_components(), []) |
| 389 | a.CN = "foo" |
| 390 | self.assertEqual(a.get_components(), [("CN", "foo")]) |
| 391 | a.organizationalUnitName = "bar" |
| 392 | self.assertEqual( |
| 393 | a.get_components(), |
| 394 | [("CN", "foo"), ("OU", "bar")]) |
| 395 | |
Jean-Paul Calderone | 110cd09 | 2008-03-24 17:27:42 -0400 | [diff] [blame] | 396 | |
Jean-Paul Calderone | ac0d95f | 2008-03-10 00:00:42 -0400 | [diff] [blame] | 397 | class _PKeyInteractionTestsMixin: |
| 398 | """ |
| 399 | Tests which involve another thing and a PKey. |
| 400 | """ |
| 401 | def signable(self): |
| 402 | """ |
| 403 | Return something with a C{set_pubkey}, C{set_pubkey}, and C{sign} method. |
| 404 | """ |
| 405 | raise NotImplementedError() |
| 406 | |
| 407 | |
| 408 | def test_signWithUngenerated(self): |
| 409 | """ |
| 410 | L{X509Req.sign} raises L{ValueError} when pass a L{PKey} with no parts. |
| 411 | """ |
| 412 | request = self.signable() |
| 413 | key = PKey() |
| 414 | self.assertRaises(ValueError, request.sign, key, 'MD5') |
| 415 | |
| 416 | |
| 417 | def test_signWithPublicKey(self): |
| 418 | """ |
| 419 | L{X509Req.sign} raises L{ValueError} when pass a L{PKey} with no |
| 420 | private part as the signing key. |
| 421 | """ |
| 422 | request = self.signable() |
| 423 | key = PKey() |
| 424 | key.generate_key(TYPE_RSA, 512) |
| 425 | request.set_pubkey(key) |
| 426 | pub = request.get_pubkey() |
| 427 | self.assertRaises(ValueError, request.sign, pub, 'MD5') |
| 428 | |
| 429 | |
| 430 | |
| 431 | class X509ReqTests(TestCase, _PKeyInteractionTestsMixin, _Python23TestCaseHelper): |
Jean-Paul Calderone | 2aa2b33 | 2008-03-06 21:43:14 -0500 | [diff] [blame] | 432 | """ |
| 433 | Tests for L{OpenSSL.crypto.X509Req}. |
| 434 | """ |
Jean-Paul Calderone | ac0d95f | 2008-03-10 00:00:42 -0400 | [diff] [blame] | 435 | def signable(self): |
| 436 | """ |
| 437 | Create and return a new L{X509Req}. |
| 438 | """ |
| 439 | return X509Req() |
| 440 | |
| 441 | |
Jean-Paul Calderone | 2aa2b33 | 2008-03-06 21:43:14 -0500 | [diff] [blame] | 442 | def test_construction(self): |
| 443 | """ |
| 444 | L{X509Req} takes no arguments and returns an L{X509ReqType} instance. |
| 445 | """ |
| 446 | request = X509Req() |
| 447 | self.assertTrue( |
| 448 | isinstance(request, X509ReqType), |
| 449 | "%r is of type %r, should be %r" % (request, type(request), X509ReqType)) |
| 450 | |
| 451 | |
Jean-Paul Calderone | 8dd19b8 | 2008-12-28 20:41:16 -0500 | [diff] [blame] | 452 | def test_version(self): |
| 453 | """ |
| 454 | L{X509ReqType.set_version} sets the X.509 version of the certificate |
| 455 | request. L{X509ReqType.get_version} returns the X.509 version of |
| 456 | the certificate request. The initial value of the version is 0. |
| 457 | """ |
| 458 | request = X509Req() |
| 459 | self.assertEqual(request.get_version(), 0) |
| 460 | request.set_version(1) |
| 461 | self.assertEqual(request.get_version(), 1) |
| 462 | request.set_version(3) |
| 463 | self.assertEqual(request.get_version(), 3) |
| 464 | |
| 465 | |
Jean-Paul Calderone | 2aa2b33 | 2008-03-06 21:43:14 -0500 | [diff] [blame] | 466 | def test_get_subject(self): |
| 467 | """ |
| 468 | L{X509ReqType.get_subject} returns an L{X509Name} for the subject of |
| 469 | the request and which is valid even after the request object is |
| 470 | otherwise dead. |
| 471 | """ |
| 472 | request = X509Req() |
| 473 | subject = request.get_subject() |
| 474 | self.assertTrue( |
| 475 | isinstance(subject, X509NameType), |
| 476 | "%r is of type %r, should be %r" % (subject, type(subject), X509NameType)) |
| 477 | subject.commonName = "foo" |
| 478 | self.assertEqual(request.get_subject().commonName, "foo") |
| 479 | del request |
| 480 | subject.commonName = "bar" |
| 481 | self.assertEqual(subject.commonName, "bar") |
Jean-Paul Calderone | 78381d2 | 2008-03-06 23:35:22 -0500 | [diff] [blame] | 482 | |
| 483 | |
| 484 | |
Jean-Paul Calderone | ac0d95f | 2008-03-10 00:00:42 -0400 | [diff] [blame] | 485 | class X509Tests(TestCase, _PKeyInteractionTestsMixin, _Python23TestCaseHelper): |
Jean-Paul Calderone | 78381d2 | 2008-03-06 23:35:22 -0500 | [diff] [blame] | 486 | """ |
| 487 | Tests for L{OpenSSL.crypto.X509}. |
| 488 | """ |
Jean-Paul Calderone | 5ef8651 | 2008-04-26 19:06:28 -0400 | [diff] [blame] | 489 | pemData = cleartextCertificatePEM + cleartextPrivateKeyPEM |
Jean-Paul Calderone | 8114b45 | 2008-03-25 15:27:59 -0400 | [diff] [blame] | 490 | |
Jean-Paul Calderone | ac0d95f | 2008-03-10 00:00:42 -0400 | [diff] [blame] | 491 | def signable(self): |
| 492 | """ |
| 493 | Create and return a new L{X509}. |
| 494 | """ |
| 495 | return X509() |
| 496 | |
| 497 | |
Jean-Paul Calderone | 78381d2 | 2008-03-06 23:35:22 -0500 | [diff] [blame] | 498 | def test_construction(self): |
| 499 | """ |
| 500 | L{X509} takes no arguments and returns an instance of L{X509Type}. |
| 501 | """ |
| 502 | certificate = X509() |
| 503 | self.assertTrue( |
| 504 | isinstance(certificate, X509Type), |
| 505 | "%r is of type %r, should be %r" % (certificate, |
| 506 | type(certificate), |
| 507 | X509Type)) |
| 508 | |
| 509 | |
| 510 | def test_serial_number(self): |
| 511 | """ |
| 512 | The serial number of an L{X509Type} can be retrieved and modified with |
| 513 | L{X509Type.get_serial_number} and L{X509Type.set_serial_number}. |
| 514 | """ |
| 515 | certificate = X509() |
| 516 | self.assertRaises(TypeError, certificate.set_serial_number) |
| 517 | self.assertRaises(TypeError, certificate.set_serial_number, 1, 2) |
| 518 | self.assertRaises(TypeError, certificate.set_serial_number, "1") |
| 519 | self.assertRaises(TypeError, certificate.set_serial_number, 5.5) |
| 520 | self.assertEqual(certificate.get_serial_number(), 0) |
| 521 | certificate.set_serial_number(1) |
| 522 | self.assertEqual(certificate.get_serial_number(), 1) |
| 523 | certificate.set_serial_number(2 ** 32 + 1) |
| 524 | self.assertEqual(certificate.get_serial_number(), 2 ** 32 + 1) |
| 525 | certificate.set_serial_number(2 ** 64 + 1) |
| 526 | self.assertEqual(certificate.get_serial_number(), 2 ** 64 + 1) |
Jean-Paul Calderone | 525ef80 | 2008-03-09 20:39:42 -0400 | [diff] [blame] | 527 | certificate.set_serial_number(2 ** 128 + 1) |
| 528 | self.assertEqual(certificate.get_serial_number(), 2 ** 128 + 1) |
| 529 | |
| 530 | |
| 531 | def _setBoundTest(self, which): |
| 532 | """ |
| 533 | L{X509Type.set_notBefore} takes a string in the format of an ASN1 |
| 534 | GENERALIZEDTIME and sets the beginning of the certificate's validity |
| 535 | period to it. |
| 536 | """ |
| 537 | certificate = X509() |
| 538 | set = getattr(certificate, 'set_not' + which) |
| 539 | get = getattr(certificate, 'get_not' + which) |
| 540 | |
Jean-Paul Calderone | e0615b5 | 2008-03-09 21:44:46 -0400 | [diff] [blame] | 541 | # Starts with no value. |
| 542 | self.assertEqual(get(), None) |
| 543 | |
Jean-Paul Calderone | 525ef80 | 2008-03-09 20:39:42 -0400 | [diff] [blame] | 544 | # GMT (Or is it UTC?) -exarkun |
| 545 | when = "20040203040506Z" |
| 546 | set(when) |
| 547 | self.assertEqual(get(), when) |
| 548 | |
| 549 | # A plus two hours and thirty minutes offset |
| 550 | when = "20040203040506+0530" |
| 551 | set(when) |
| 552 | self.assertEqual(get(), when) |
| 553 | |
| 554 | # A minus one hour fifteen minutes offset |
| 555 | when = "20040203040506-0115" |
| 556 | set(when) |
| 557 | self.assertEqual(get(), when) |
| 558 | |
| 559 | # An invalid string results in a ValueError |
| 560 | self.assertRaises(ValueError, set, "foo bar") |
| 561 | |
| 562 | |
| 563 | def test_set_notBefore(self): |
| 564 | """ |
| 565 | L{X509Type.set_notBefore} takes a string in the format of an ASN1 |
| 566 | GENERALIZEDTIME and sets the beginning of the certificate's validity |
| 567 | period to it. |
| 568 | """ |
| 569 | self._setBoundTest("Before") |
| 570 | |
| 571 | |
| 572 | def test_set_notAfter(self): |
| 573 | """ |
| 574 | L{X509Type.set_notAfter} takes a string in the format of an ASN1 |
| 575 | GENERALIZEDTIME and sets the end of the certificate's validity period |
| 576 | to it. |
| 577 | """ |
| 578 | self._setBoundTest("After") |
Jean-Paul Calderone | 76576d5 | 2008-03-24 16:04:46 -0400 | [diff] [blame] | 579 | |
| 580 | |
Jean-Paul Calderone | 38a646d | 2008-03-25 15:16:18 -0400 | [diff] [blame] | 581 | def test_get_notBefore(self): |
| 582 | """ |
| 583 | L{X509Type.get_notBefore} returns a string in the format of an ASN1 |
| 584 | GENERALIZEDTIME even for certificates which store it as UTCTIME |
| 585 | internally. |
| 586 | """ |
Jean-Paul Calderone | 8114b45 | 2008-03-25 15:27:59 -0400 | [diff] [blame] | 587 | cert = load_certificate(FILETYPE_PEM, self.pemData) |
Jean-Paul Calderone | 38a646d | 2008-03-25 15:16:18 -0400 | [diff] [blame] | 588 | self.assertEqual(cert.get_notBefore(), "20080325190413Z") |
| 589 | |
| 590 | |
| 591 | def test_get_notAfter(self): |
| 592 | """ |
| 593 | L{X509Type.get_notAfter} returns a string in the format of an ASN1 |
| 594 | GENERALIZEDTIME even for certificates which store it as UTCTIME |
| 595 | internally. |
| 596 | """ |
Jean-Paul Calderone | 8114b45 | 2008-03-25 15:27:59 -0400 | [diff] [blame] | 597 | cert = load_certificate(FILETYPE_PEM, self.pemData) |
Jean-Paul Calderone | 38a646d | 2008-03-25 15:16:18 -0400 | [diff] [blame] | 598 | self.assertEqual(cert.get_notAfter(), "20090325190413Z") |
| 599 | |
| 600 | |
Jean-Paul Calderone | 76576d5 | 2008-03-24 16:04:46 -0400 | [diff] [blame] | 601 | def test_digest(self): |
| 602 | """ |
| 603 | L{X509.digest} returns a string giving ":"-separated hex-encoded words |
| 604 | of the digest of the certificate. |
| 605 | """ |
| 606 | cert = X509() |
| 607 | self.assertEqual( |
| 608 | cert.digest("md5"), |
| 609 | "A8:EB:07:F8:53:25:0A:F2:56:05:C5:A5:C4:C4:C7:15") |
Jean-Paul Calderone | 828c9cb | 2008-04-26 18:06:54 -0400 | [diff] [blame] | 610 | |
| 611 | |
| 612 | |
Jean-Paul Calderone | 6fe60c2 | 2008-04-26 20:04:53 -0400 | [diff] [blame] | 613 | class FunctionTests(TestCase, _Python23TestCaseHelper): |
Jean-Paul Calderone | 828c9cb | 2008-04-26 18:06:54 -0400 | [diff] [blame] | 614 | """ |
| 615 | Tests for free-functions in the L{OpenSSL.crypto} module. |
| 616 | """ |
| 617 | def test_load_privatekey_wrongPassphrase(self): |
| 618 | """ |
| 619 | L{load_privatekey} raises L{OpenSSL.crypto.Error} when it is passed an |
| 620 | encrypted PEM and an incorrect passphrase. |
| 621 | """ |
| 622 | self.assertRaises( |
| 623 | Error, |
| 624 | load_privatekey, FILETYPE_PEM, encryptedPrivateKeyPEM, "quack") |
| 625 | |
| 626 | |
| 627 | def test_load_privatekey_passphrase(self): |
| 628 | """ |
| 629 | L{load_privatekey} can create a L{PKey} object from an encrypted PEM |
| 630 | string if given the passphrase. |
| 631 | """ |
| 632 | key = load_privatekey( |
| 633 | FILETYPE_PEM, encryptedPrivateKeyPEM, |
| 634 | encryptedPrivateKeyPEMPassphrase) |
| 635 | self.assertTrue(isinstance(key, PKeyType)) |
| 636 | |
| 637 | |
| 638 | def test_load_privatekey_wrongPassphraseCallback(self): |
| 639 | """ |
| 640 | L{load_privatekey} raises L{OpenSSL.crypto.Error} when it is passed an |
| 641 | encrypted PEM and a passphrase callback which returns an incorrect |
| 642 | passphrase. |
| 643 | """ |
| 644 | called = [] |
| 645 | def cb(*a): |
| 646 | called.append(None) |
| 647 | return "quack" |
| 648 | self.assertRaises( |
| 649 | Error, |
| 650 | load_privatekey, FILETYPE_PEM, encryptedPrivateKeyPEM, cb) |
| 651 | self.assertTrue(called) |
| 652 | |
| 653 | def test_load_privatekey_passphraseCallback(self): |
| 654 | """ |
| 655 | L{load_privatekey} can create a L{PKey} object from an encrypted PEM |
| 656 | string if given a passphrase callback which returns the correct |
| 657 | password. |
| 658 | """ |
| 659 | called = [] |
| 660 | def cb(writing): |
| 661 | called.append(writing) |
| 662 | return encryptedPrivateKeyPEMPassphrase |
| 663 | key = load_privatekey(FILETYPE_PEM, encryptedPrivateKeyPEM, cb) |
| 664 | self.assertTrue(isinstance(key, PKeyType)) |
| 665 | self.assertEqual(called, [False]) |
| 666 | |
| 667 | |
| 668 | def test_dump_privatekey_passphrase(self): |
| 669 | """ |
| 670 | L{dump_privatekey} writes an encrypted PEM when given a passphrase. |
| 671 | """ |
| 672 | passphrase = "foo" |
| 673 | key = load_privatekey(FILETYPE_PEM, cleartextPrivateKeyPEM) |
| 674 | pem = dump_privatekey(FILETYPE_PEM, key, "blowfish", passphrase) |
| 675 | self.assertTrue(isinstance(pem, str)) |
| 676 | loadedKey = load_privatekey(FILETYPE_PEM, pem, passphrase) |
| 677 | self.assertTrue(isinstance(loadedKey, PKeyType)) |
| 678 | self.assertEqual(loadedKey.type(), key.type()) |
| 679 | self.assertEqual(loadedKey.bits(), key.bits()) |
| 680 | |
| 681 | |
| 682 | def test_dump_privatekey_passphraseCallback(self): |
| 683 | """ |
| 684 | L{dump_privatekey} writes an encrypted PEM when given a callback which |
| 685 | returns the correct passphrase. |
| 686 | """ |
| 687 | passphrase = "foo" |
| 688 | called = [] |
| 689 | def cb(writing): |
| 690 | called.append(writing) |
| 691 | return passphrase |
| 692 | key = load_privatekey(FILETYPE_PEM, cleartextPrivateKeyPEM) |
| 693 | pem = dump_privatekey(FILETYPE_PEM, key, "blowfish", cb) |
| 694 | self.assertTrue(isinstance(pem, str)) |
| 695 | self.assertEqual(called, [True]) |
| 696 | loadedKey = load_privatekey(FILETYPE_PEM, pem, passphrase) |
| 697 | self.assertTrue(isinstance(loadedKey, PKeyType)) |
| 698 | self.assertEqual(loadedKey.type(), key.type()) |
| 699 | self.assertEqual(loadedKey.bits(), key.bits()) |