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