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 | |
Rick Dean | 5b7b637 | 2009-04-01 11:34:06 -0500 | [diff] [blame] | 7 | from unittest import TestCase, main |
Jean-Paul Calderone | 653f558 | 2009-04-01 14:42:32 -0400 | [diff] [blame] | 8 | from os import popen2 |
Jean-Paul Calderone | d8782ad | 2008-03-04 23:39:59 -0500 | [diff] [blame] | 9 | |
| 10 | from OpenSSL.crypto import TYPE_RSA, TYPE_DSA, Error, PKey, PKeyType |
Jean-Paul Calderone | 78381d2 | 2008-03-06 23:35:22 -0500 | [diff] [blame] | 11 | from OpenSSL.crypto import X509, X509Type, X509Name, X509NameType |
Jean-Paul Calderone | 2aa2b33 | 2008-03-06 21:43:14 -0500 | [diff] [blame] | 12 | from OpenSSL.crypto import X509Req, X509ReqType |
Jean-Paul Calderone | e7db4b4 | 2008-12-31 13:39:24 -0500 | [diff] [blame] | 13 | from OpenSSL.crypto import X509Extension, X509ExtensionType |
Rick Dean | 5b7b637 | 2009-04-01 11:34:06 -0500 | [diff] [blame] | 14 | from OpenSSL.crypto import load_certificate, load_privatekey |
Jean-Paul Calderone | f17e421 | 2009-04-01 14:21:40 -0400 | [diff] [blame] | 15 | from OpenSSL.crypto import FILETYPE_PEM, FILETYPE_ASN1, FILETYPE_TEXT |
Jean-Paul Calderone | 7191986 | 2009-04-01 13:01:19 -0400 | [diff] [blame] | 16 | from OpenSSL.crypto import dump_certificate, load_certificate_request |
| 17 | from OpenSSL.crypto import dump_certificate_request, dump_privatekey |
Jean-Paul Calderone | dc138fa | 2009-06-27 14:32:07 -0400 | [diff] [blame] | 18 | from OpenSSL.crypto import PKCS7Type, load_pkcs7_data |
| 19 | from OpenSSL.crypto import PKCS12Type, load_pkcs12 |
| 20 | from OpenSSL.crypto import NetscapeSPKI, NetscapeSPKIType |
Jean-Paul Calderone | 828c9cb | 2008-04-26 18:06:54 -0400 | [diff] [blame] | 21 | |
| 22 | |
Jean-Paul Calderone | 20131f5 | 2009-04-01 12:05:45 -0400 | [diff] [blame] | 23 | cleartextCertificatePEM = """-----BEGIN CERTIFICATE----- |
| 24 | MIIC7TCCAlagAwIBAgIIPQzE4MbeufQwDQYJKoZIhvcNAQEFBQAwWDELMAkGA1UE |
| 25 | BhMCVVMxCzAJBgNVBAgTAklMMRAwDgYDVQQHEwdDaGljYWdvMRAwDgYDVQQKEwdU |
| 26 | ZXN0aW5nMRgwFgYDVQQDEw9UZXN0aW5nIFJvb3QgQ0EwIhgPMjAwOTAzMjUxMjM2 |
| 27 | NThaGA8yMDE3MDYxMTEyMzY1OFowWDELMAkGA1UEBhMCVVMxCzAJBgNVBAgTAklM |
| 28 | MRAwDgYDVQQHEwdDaGljYWdvMRAwDgYDVQQKEwdUZXN0aW5nMRgwFgYDVQQDEw9U |
| 29 | ZXN0aW5nIFJvb3QgQ0EwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBAPmaQumL |
| 30 | urpE527uSEHdL1pqcDRmWzu+98Y6YHzT/J7KWEamyMCNZ6fRW1JCR782UQ8a07fy |
| 31 | 2xXsKy4WdKaxyG8CcatwmXvpvRQ44dSANMihHELpANTdyVp6DCysED6wkQFurHlF |
| 32 | 1dshEaJw8b/ypDhmbVIo6Ci1xvCJqivbLFnbAgMBAAGjgbswgbgwHQYDVR0OBBYE |
| 33 | FINVdy1eIfFJDAkk51QJEo3IfgSuMIGIBgNVHSMEgYAwfoAUg1V3LV4h8UkMCSTn |
| 34 | VAkSjch+BK6hXKRaMFgxCzAJBgNVBAYTAlVTMQswCQYDVQQIEwJJTDEQMA4GA1UE |
| 35 | BxMHQ2hpY2FnbzEQMA4GA1UEChMHVGVzdGluZzEYMBYGA1UEAxMPVGVzdGluZyBS |
| 36 | b290IENBggg9DMTgxt659DAMBgNVHRMEBTADAQH/MA0GCSqGSIb3DQEBBQUAA4GB |
| 37 | AGGCDazMJGoWNBpc03u6+smc95dEead2KlZXBATOdFT1VesY3+nUOqZhEhTGlDMi |
| 38 | hkgaZnzoIq/Uamidegk4hirsCT/R+6vsKAAxNTcBjUeZjlykCJWy5ojShGftXIKY |
| 39 | w/njVbKMXrvc83qmTdGl3TAM0fxQIpqgcglFLveEBgzn |
| 40 | -----END CERTIFICATE----- |
| 41 | """ |
Jean-Paul Calderone | 828c9cb | 2008-04-26 18:06:54 -0400 | [diff] [blame] | 42 | |
Jean-Paul Calderone | 20131f5 | 2009-04-01 12:05:45 -0400 | [diff] [blame] | 43 | cleartextPrivateKeyPEM = """-----BEGIN RSA PRIVATE KEY----- |
| 44 | MIICXQIBAAKBgQD5mkLpi7q6ROdu7khB3S9aanA0Zls7vvfGOmB80/yeylhGpsjA |
| 45 | jWen0VtSQke/NlEPGtO38tsV7CsuFnSmschvAnGrcJl76b0UOOHUgDTIoRxC6QDU |
| 46 | 3claegwsrBA+sJEBbqx5RdXbIRGicPG/8qQ4Zm1SKOgotcbwiaor2yxZ2wIDAQAB |
| 47 | AoGBAPCgMpmLxzwDaUmcFbTJUvlLW1hoxNNYSu2jIZm1k/hRAcE60JYwvBkgz3UB |
| 48 | yMEh0AtLxYe0bFk6EHah11tMUPgscbCq73snJ++8koUw+csk22G65hOs51bVb7Aa |
| 49 | 6JBe67oLzdtvgCUFAA2qfrKzWRZzAdhUirQUZgySZk+Xq1pBAkEA/kZG0A6roTSM |
| 50 | BVnx7LnPfsycKUsTumorpXiylZJjTi9XtmzxhrYN6wgZlDOOwOLgSQhszGpxVoMD |
| 51 | u3gByT1b2QJBAPtL3mSKdvwRu/+40zaZLwvSJRxaj0mcE4BJOS6Oqs/hS1xRlrNk |
| 52 | PpQ7WJ4yM6ZOLnXzm2mKyxm50Mv64109FtMCQQDOqS2KkjHaLowTGVxwC0DijMfr |
| 53 | I9Lf8sSQk32J5VWCySWf5gGTfEnpmUa41gKTMJIbqZZLucNuDcOtzUaeWZlZAkA8 |
| 54 | ttXigLnCqR486JDPTi9ZscoZkZ+w7y6e/hH8t6d5Vjt48JVyfjPIaJY+km58LcN3 |
| 55 | 6AWSeGAdtRFHVzR7oHjVAkB4hutvxiOeiIVQNBhM6RSI9aBPMI21DoX2JRoxvNW2 |
| 56 | cbvAhow217X9V0dVerEOKxnNYspXRrh36h7k4mQA+sDq |
| 57 | -----END RSA PRIVATE KEY----- |
| 58 | """ |
Jean-Paul Calderone | 828c9cb | 2008-04-26 18:06:54 -0400 | [diff] [blame] | 59 | |
Rick Dean | 5b7b637 | 2009-04-01 11:34:06 -0500 | [diff] [blame] | 60 | cleartextCertificateRequestPEM = ( |
| 61 | "-----BEGIN CERTIFICATE REQUEST-----\n" |
| 62 | "MIIBnjCCAQcCAQAwXjELMAkGA1UEBhMCVVMxCzAJBgNVBAgTAklMMRAwDgYDVQQH\n" |
| 63 | "EwdDaGljYWdvMRcwFQYDVQQKEw5NeSBDb21wYW55IEx0ZDEXMBUGA1UEAxMORnJl\n" |
| 64 | "ZGVyaWNrIERlYW4wgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBANp6Y17WzKSw\n" |
| 65 | "BsUWkXdqg6tnXy8H8hA1msCMWpc+/2KJ4mbv5NyD6UD+/SqagQqulPbF/DFea9nA\n" |
| 66 | "E0zhmHJELcM8gUTIlXv/cgDWnmK4xj8YkjVUiCdqKRAKeuzLG1pGmwwF5lGeJpXN\n" |
| 67 | "xQn5ecR0UYSOWj6TTGXB9VyUMQzCClcBAgMBAAGgADANBgkqhkiG9w0BAQUFAAOB\n" |
| 68 | "gQAAJGuF/R/GGbeC7FbFW+aJgr9ee0Xbl6nlhu7pTe67k+iiKT2dsl2ti68MVTnu\n" |
| 69 | "Vrb3HUNqOkiwsJf6kCtq5oPn3QVYzTa76Dt2y3Rtzv6boRSlmlfrgS92GNma8JfR\n" |
| 70 | "oICQk3nAudi6zl1Dix3BCv1pUp5KMtGn3MeDEi6QFGy2rA==\n" |
| 71 | "-----END CERTIFICATE REQUEST-----\n") |
| 72 | |
Jean-Paul Calderone | 20131f5 | 2009-04-01 12:05:45 -0400 | [diff] [blame] | 73 | encryptedPrivateKeyPEM = """-----BEGIN RSA PRIVATE KEY----- |
| 74 | Proc-Type: 4,ENCRYPTED |
| 75 | DEK-Info: DES-EDE3-CBC,9573604A18579E9E |
Jean-Paul Calderone | 5ef8651 | 2008-04-26 19:06:28 -0400 | [diff] [blame] | 76 | |
Jean-Paul Calderone | 20131f5 | 2009-04-01 12:05:45 -0400 | [diff] [blame] | 77 | SHOho56WxDkT0ht10UTeKc0F5u8cqIa01kzFAmETw0MAs8ezYtK15NPdCXUm3X/2 |
| 78 | a17G7LSF5bkxOgZ7vpXyMzun/owrj7CzvLxyncyEFZWvtvzaAhPhvTJtTIB3kf8B |
| 79 | 8+qRcpTGK7NgXEgYBW5bj1y4qZkD4zCL9o9NQzsKI3Ie8i0239jsDOWR38AxjXBH |
| 80 | mGwAQ4Z6ZN5dnmM4fhMIWsmFf19sNyAML4gHenQCHhmXbjXeVq47aC2ProInJbrm |
| 81 | +00TcisbAQ40V9aehVbcDKtS4ZbMVDwncAjpXpcncC54G76N6j7F7wL7L/FuXa3A |
| 82 | fvSVy9n2VfF/pJ3kYSflLHH2G/DFxjF7dl0GxhKPxJjp3IJi9VtuvmN9R2jZWLQF |
| 83 | tfC8dXgy/P9CfFQhlinqBTEwgH0oZ/d4k4NVFDSdEMaSdmBAjlHpc+Vfdty3HVnV |
| 84 | rKXj//wslsFNm9kIwJGIgKUa/n2jsOiydrsk1mgH7SmNCb3YHgZhbbnq0qLat/HC |
| 85 | gHDt3FHpNQ31QzzL3yrenFB2L9osIsnRsDTPFNi4RX4SpDgNroxOQmyzCCV6H+d4 |
| 86 | o1mcnNiZSdxLZxVKccq0AfRpHqpPAFnJcQHP6xyT9MZp6fBa0XkxDnt9kNU8H3Qw |
| 87 | 7SJWZ69VXjBUzMlQViLuaWMgTnL+ZVyFZf9hTF7U/ef4HMLMAVNdiaGG+G+AjCV/ |
| 88 | MbzjS007Oe4qqBnCWaFPSnJX6uLApeTbqAxAeyCql56ULW5x6vDMNC3dwjvS/CEh |
| 89 | 11n8RkgFIQA0AhuKSIg3CbuartRsJnWOLwgLTzsrKYL4yRog1RJrtw== |
| 90 | -----END RSA PRIVATE KEY----- |
| 91 | """ |
Jean-Paul Calderone | 828c9cb | 2008-04-26 18:06:54 -0400 | [diff] [blame] | 92 | encryptedPrivateKeyPEMPassphrase = "foobar" |
| 93 | |
Jean-Paul Calderone | dc138fa | 2009-06-27 14:32:07 -0400 | [diff] [blame] | 94 | # Some PKCS12 data, base64 encoded. The data itself was constructed using the |
| 95 | # openssl command line: |
| 96 | # |
| 97 | # openssl pkcs12 -export -in s.pem -out o.p12 -inkey s.pem -certfile s.pem |
| 98 | # |
| 99 | # With s.pem containing a private key and certificate. The contents of the |
| 100 | # generated file, o.p12, were then base64 encoded to produce this value. |
| 101 | pkcs12Data = """\ |
| 102 | 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 |
| 143 | """.decode('base64') |
| 144 | |
| 145 | # Some PKCS#7 stuff. Generated with the openssl command line: |
| 146 | # |
| 147 | # openssl crl2pkcs7 -inform pem -outform pem -certfile s.pem -nocrl |
| 148 | # |
| 149 | # with a certificate and key (but the key should be irrelevant) in s.pem |
| 150 | pkcs7Data = """\ |
| 151 | -----BEGIN PKCS7----- |
| 152 | MIIDNwYJKoZIhvcNAQcCoIIDKDCCAyQCAQExADALBgkqhkiG9w0BBwGgggMKMIID |
| 153 | BjCCAm+gAwIBAgIBATANBgkqhkiG9w0BAQQFADB7MQswCQYDVQQGEwJTRzERMA8G |
| 154 | A1UEChMITTJDcnlwdG8xFDASBgNVBAsTC00yQ3J5cHRvIENBMSQwIgYDVQQDExtN |
| 155 | MkNyeXB0byBDZXJ0aWZpY2F0ZSBNYXN0ZXIxHTAbBgkqhkiG9w0BCQEWDm5ncHNA |
| 156 | cG9zdDEuY29tMB4XDTAwMDkxMDA5NTEzMFoXDTAyMDkxMDA5NTEzMFowUzELMAkG |
| 157 | A1UEBhMCU0cxETAPBgNVBAoTCE0yQ3J5cHRvMRIwEAYDVQQDEwlsb2NhbGhvc3Qx |
| 158 | HTAbBgkqhkiG9w0BCQEWDm5ncHNAcG9zdDEuY29tMFwwDQYJKoZIhvcNAQEBBQAD |
| 159 | SwAwSAJBAKy+e3dulvXzV7zoTZWc5TzgApr8DmeQHTYC8ydfzH7EECe4R1Xh5kwI |
| 160 | zOuuFfn178FBiS84gngaNcrFi0Z5fAkCAwEAAaOCAQQwggEAMAkGA1UdEwQCMAAw |
| 161 | LAYJYIZIAYb4QgENBB8WHU9wZW5TU0wgR2VuZXJhdGVkIENlcnRpZmljYXRlMB0G |
| 162 | A1UdDgQWBBTPhIKSvnsmYsBVNWjj0m3M2z0qVTCBpQYDVR0jBIGdMIGagBT7hyNp |
| 163 | 65w6kxXlxb8pUU/+7Sg4AaF/pH0wezELMAkGA1UEBhMCU0cxETAPBgNVBAoTCE0y |
| 164 | Q3J5cHRvMRQwEgYDVQQLEwtNMkNyeXB0byBDQTEkMCIGA1UEAxMbTTJDcnlwdG8g |
| 165 | Q2VydGlmaWNhdGUgTWFzdGVyMR0wGwYJKoZIhvcNAQkBFg5uZ3BzQHBvc3QxLmNv |
| 166 | bYIBADANBgkqhkiG9w0BAQQFAAOBgQA7/CqT6PoHycTdhEStWNZde7M/2Yc6BoJu |
| 167 | VwnW8YxGO8Sn6UJ4FeffZNcYZddSDKosw8LtPOeWoK3JINjAk5jiPQ2cww++7QGG |
| 168 | /g5NDjxFZNDJP1dGiLAxPW6JXwov4v0FmdzfLOZ01jDcgQQZqEpYlgpuI5JEWUQ9 |
| 169 | Ho4EzbYCOaEAMQA= |
| 170 | -----END PKCS7----- |
| 171 | """ |
| 172 | |
Jean-Paul Calderone | 828c9cb | 2008-04-26 18:06:54 -0400 | [diff] [blame] | 173 | |
Jean-Paul Calderone | ac930e1 | 2008-03-06 18:50:51 -0500 | [diff] [blame] | 174 | class _Python23TestCaseHelper: |
Jean-Paul Calderone | 7da26a7 | 2008-03-06 00:35:20 -0500 | [diff] [blame] | 175 | # Python 2.3 compatibility. |
| 176 | def assertTrue(self, *a, **kw): |
| 177 | return self.failUnless(*a, **kw) |
| 178 | |
| 179 | |
Jean-Paul Calderone | 7535dab | 2008-03-06 18:53:11 -0500 | [diff] [blame] | 180 | def assertFalse(self, *a, **kw): |
| 181 | return self.failIf(*a, **kw) |
| 182 | |
| 183 | |
Jean-Paul Calderone | 391585f | 2008-12-31 14:36:31 -0500 | [diff] [blame] | 184 | |
Jean-Paul Calderone | e7db4b4 | 2008-12-31 13:39:24 -0500 | [diff] [blame] | 185 | class X509ExtTests(TestCase, _Python23TestCaseHelper): |
| 186 | def test_construction(self): |
| 187 | """ |
| 188 | L{X509Extension} accepts an extension type name, a critical flag, |
| 189 | and an extension value and returns an L{X509ExtensionType} instance. |
| 190 | """ |
Jean-Paul Calderone | 2ee1e7c | 2008-12-31 14:58:38 -0500 | [diff] [blame] | 191 | basic = X509Extension('basicConstraints', True, 'CA:true') |
Jean-Paul Calderone | e7db4b4 | 2008-12-31 13:39:24 -0500 | [diff] [blame] | 192 | self.assertTrue( |
| 193 | isinstance(basic, X509ExtensionType), |
| 194 | "%r is of type %r, should be %r" % ( |
| 195 | basic, type(basic), X509ExtensionType)) |
| 196 | |
Jean-Paul Calderone | 2ee1e7c | 2008-12-31 14:58:38 -0500 | [diff] [blame] | 197 | comment = X509Extension('nsComment', False, 'pyOpenSSL unit test') |
Jean-Paul Calderone | e7db4b4 | 2008-12-31 13:39:24 -0500 | [diff] [blame] | 198 | self.assertTrue( |
| 199 | isinstance(comment, X509ExtensionType), |
| 200 | "%r is of type %r, should be %r" % ( |
| 201 | comment, type(comment), X509ExtensionType)) |
| 202 | |
| 203 | |
Jean-Paul Calderone | 391585f | 2008-12-31 14:36:31 -0500 | [diff] [blame] | 204 | def test_invalid_extension(self): |
| 205 | """ |
| 206 | L{X509Extension} raises something if it is passed a bad extension |
| 207 | name or value. |
| 208 | """ |
| 209 | self.assertRaises( |
| 210 | Error, X509Extension, 'thisIsMadeUp', False, 'hi') |
| 211 | self.assertRaises( |
| 212 | Error, X509Extension, 'basicConstraints', False, 'blah blah') |
| 213 | |
Jean-Paul Calderone | 2ee1e7c | 2008-12-31 14:58:38 -0500 | [diff] [blame] | 214 | # Exercise a weird one (an extension which uses the r2i method). This |
| 215 | # exercises the codepath that requires a non-NULL ctx to be passed to |
| 216 | # X509V3_EXT_nconf. It can't work now because we provide no |
| 217 | # configuration database. It might be made to work in the future. |
| 218 | self.assertRaises( |
| 219 | Error, X509Extension, 'proxyCertInfo', True, |
| 220 | 'language:id-ppl-anyLanguage,pathlen:1,policy:text:AB') |
| 221 | |
Jean-Paul Calderone | 391585f | 2008-12-31 14:36:31 -0500 | [diff] [blame] | 222 | |
Jean-Paul Calderone | e7db4b4 | 2008-12-31 13:39:24 -0500 | [diff] [blame] | 223 | def test_get_critical(self): |
| 224 | """ |
| 225 | L{X509ExtensionType.get_critical} returns the value of the |
| 226 | extension's critical flag. |
| 227 | """ |
Jean-Paul Calderone | 2ee1e7c | 2008-12-31 14:58:38 -0500 | [diff] [blame] | 228 | ext = X509Extension('basicConstraints', True, 'CA:true') |
Jean-Paul Calderone | e7db4b4 | 2008-12-31 13:39:24 -0500 | [diff] [blame] | 229 | self.assertTrue(ext.get_critical()) |
Jean-Paul Calderone | 2ee1e7c | 2008-12-31 14:58:38 -0500 | [diff] [blame] | 230 | ext = X509Extension('basicConstraints', False, 'CA:true') |
Jean-Paul Calderone | e7db4b4 | 2008-12-31 13:39:24 -0500 | [diff] [blame] | 231 | self.assertFalse(ext.get_critical()) |
| 232 | |
Jean-Paul Calderone | 7535dab | 2008-03-06 18:53:11 -0500 | [diff] [blame] | 233 | |
Jean-Paul Calderone | f8c5fab | 2008-12-31 15:53:48 -0500 | [diff] [blame] | 234 | def test_get_short_name(self): |
| 235 | """ |
| 236 | L{X509ExtensionType.get_short_name} returns a string giving the short |
| 237 | type name of the extension. |
| 238 | """ |
| 239 | ext = X509Extension('basicConstraints', True, 'CA:true') |
| 240 | self.assertEqual(ext.get_short_name(), 'basicConstraints') |
| 241 | ext = X509Extension('nsComment', True, 'foo bar') |
| 242 | self.assertEqual(ext.get_short_name(), 'nsComment') |
| 243 | |
| 244 | |
Jean-Paul Calderone | 391585f | 2008-12-31 14:36:31 -0500 | [diff] [blame] | 245 | |
Jean-Paul Calderone | ac930e1 | 2008-03-06 18:50:51 -0500 | [diff] [blame] | 246 | class PKeyTests(TestCase, _Python23TestCaseHelper): |
| 247 | """ |
| 248 | Unit tests for L{OpenSSL.crypto.PKey}. |
| 249 | """ |
Jean-Paul Calderone | d8782ad | 2008-03-04 23:39:59 -0500 | [diff] [blame] | 250 | def test_construction(self): |
| 251 | """ |
| 252 | L{PKey} takes no arguments and returns a new L{PKeyType} instance. |
| 253 | """ |
| 254 | self.assertRaises(TypeError, PKey, None) |
| 255 | key = PKey() |
| 256 | self.assertTrue( |
| 257 | isinstance(key, PKeyType), |
| 258 | "%r is of type %r, should be %r" % (key, type(key), PKeyType)) |
| 259 | |
| 260 | |
| 261 | def test_pregeneration(self): |
| 262 | """ |
| 263 | L{PKeyType.bits} and L{PKeyType.type} return C{0} before the key is |
| 264 | generated. |
| 265 | """ |
| 266 | key = PKey() |
| 267 | self.assertEqual(key.type(), 0) |
| 268 | self.assertEqual(key.bits(), 0) |
| 269 | |
| 270 | |
| 271 | def test_failedGeneration(self): |
| 272 | """ |
Jean-Paul Calderone | ab82db7 | 2008-03-06 00:09:31 -0500 | [diff] [blame] | 273 | L{PKeyType.generate_key} takes two arguments, the first giving the key |
| 274 | type as one of L{TYPE_RSA} or L{TYPE_DSA} and the second giving the |
| 275 | number of bits to generate. If an invalid type is specified or |
| 276 | generation fails, L{Error} is raised. If an invalid number of bits is |
| 277 | specified, L{ValueError} or L{Error} is raised. |
Jean-Paul Calderone | d8782ad | 2008-03-04 23:39:59 -0500 | [diff] [blame] | 278 | """ |
| 279 | key = PKey() |
| 280 | self.assertRaises(TypeError, key.generate_key) |
| 281 | self.assertRaises(TypeError, key.generate_key, 1, 2, 3) |
| 282 | self.assertRaises(TypeError, key.generate_key, "foo", "bar") |
| 283 | self.assertRaises(Error, key.generate_key, -1, 0) |
Jean-Paul Calderone | ab82db7 | 2008-03-06 00:09:31 -0500 | [diff] [blame] | 284 | |
Jean-Paul Calderone | ab82db7 | 2008-03-06 00:09:31 -0500 | [diff] [blame] | 285 | self.assertRaises(ValueError, key.generate_key, TYPE_RSA, -1) |
| 286 | self.assertRaises(ValueError, key.generate_key, TYPE_RSA, 0) |
Jean-Paul Calderone | d71fe98 | 2008-03-06 00:31:50 -0500 | [diff] [blame] | 287 | |
| 288 | # XXX RSA generation for small values of bits is fairly buggy in a wide |
| 289 | # range of OpenSSL versions. I need to figure out what the safe lower |
| 290 | # bound for a reasonable number of OpenSSL versions is and explicitly |
| 291 | # check for that in the wrapper. The failure behavior is typically an |
| 292 | # infinite loop inside OpenSSL. |
| 293 | |
| 294 | # self.assertRaises(Error, key.generate_key, TYPE_RSA, 2) |
Jean-Paul Calderone | d8782ad | 2008-03-04 23:39:59 -0500 | [diff] [blame] | 295 | |
| 296 | # XXX DSA generation seems happy with any number of bits. The DSS |
| 297 | # says bits must be between 512 and 1024 inclusive. OpenSSL's DSA |
| 298 | # 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] | 299 | # the lower limit, it uses 512 if anything smaller is specified. |
Jean-Paul Calderone | d8782ad | 2008-03-04 23:39:59 -0500 | [diff] [blame] | 300 | # So, it doesn't seem possible to make generate_key fail for |
| 301 | # TYPE_DSA with a bits argument which is at least an int. |
| 302 | |
| 303 | # self.assertRaises(Error, key.generate_key, TYPE_DSA, -7) |
| 304 | |
| 305 | |
| 306 | def test_rsaGeneration(self): |
| 307 | """ |
| 308 | L{PKeyType.generate_key} generates an RSA key when passed |
| 309 | L{TYPE_RSA} as a type and a reasonable number of bits. |
| 310 | """ |
| 311 | bits = 128 |
| 312 | key = PKey() |
| 313 | key.generate_key(TYPE_RSA, bits) |
| 314 | self.assertEqual(key.type(), TYPE_RSA) |
| 315 | self.assertEqual(key.bits(), bits) |
| 316 | |
| 317 | |
| 318 | def test_dsaGeneration(self): |
| 319 | """ |
| 320 | L{PKeyType.generate_key} generates a DSA key when passed |
| 321 | L{TYPE_DSA} as a type and a reasonable number of bits. |
| 322 | """ |
| 323 | # 512 is a magic number. The DSS (Digital Signature Standard) |
| 324 | # allows a minimum of 512 bits for DSA. DSA_generate_parameters |
| 325 | # will silently promote any value below 512 to 512. |
| 326 | bits = 512 |
| 327 | key = PKey() |
| 328 | key.generate_key(TYPE_DSA, bits) |
| 329 | self.assertEqual(key.type(), TYPE_DSA) |
| 330 | self.assertEqual(key.bits(), bits) |
| 331 | |
| 332 | |
| 333 | def test_regeneration(self): |
| 334 | """ |
| 335 | L{PKeyType.generate_key} can be called multiple times on the same |
| 336 | key to generate new keys. |
| 337 | """ |
| 338 | key = PKey() |
| 339 | for type, bits in [(TYPE_RSA, 512), (TYPE_DSA, 576)]: |
| 340 | key.generate_key(type, bits) |
| 341 | self.assertEqual(key.type(), type) |
| 342 | self.assertEqual(key.bits(), bits) |
Jean-Paul Calderone | eff3cd9 | 2008-03-05 22:35:26 -0500 | [diff] [blame] | 343 | |
| 344 | |
| 345 | |
Jean-Paul Calderone | ac930e1 | 2008-03-06 18:50:51 -0500 | [diff] [blame] | 346 | class X509NameTests(TestCase, _Python23TestCaseHelper): |
Jean-Paul Calderone | eff3cd9 | 2008-03-05 22:35:26 -0500 | [diff] [blame] | 347 | """ |
| 348 | Unit tests for L{OpenSSL.crypto.X509Name}. |
| 349 | """ |
Jean-Paul Calderone | e098dc7 | 2008-03-06 18:36:19 -0500 | [diff] [blame] | 350 | def _x509name(self, **attrs): |
| 351 | # XXX There's no other way to get a new X509Name yet. |
| 352 | name = X509().get_subject() |
| 353 | attrs = attrs.items() |
| 354 | # Make the order stable - order matters! |
| 355 | attrs.sort(lambda (k1, v1), (k2, v2): cmp(v1, v2)) |
| 356 | for k, v in attrs: |
| 357 | setattr(name, k, v) |
| 358 | return name |
| 359 | |
| 360 | |
Jean-Paul Calderone | eff3cd9 | 2008-03-05 22:35:26 -0500 | [diff] [blame] | 361 | def test_attributes(self): |
| 362 | """ |
| 363 | L{X509NameType} instances have attributes for each standard (?) |
| 364 | X509Name field. |
| 365 | """ |
Jean-Paul Calderone | e098dc7 | 2008-03-06 18:36:19 -0500 | [diff] [blame] | 366 | name = self._x509name() |
Jean-Paul Calderone | eff3cd9 | 2008-03-05 22:35:26 -0500 | [diff] [blame] | 367 | name.commonName = "foo" |
| 368 | self.assertEqual(name.commonName, "foo") |
| 369 | self.assertEqual(name.CN, "foo") |
| 370 | name.CN = "baz" |
| 371 | self.assertEqual(name.commonName, "baz") |
| 372 | self.assertEqual(name.CN, "baz") |
| 373 | name.commonName = "bar" |
| 374 | self.assertEqual(name.commonName, "bar") |
| 375 | self.assertEqual(name.CN, "bar") |
| 376 | name.CN = "quux" |
| 377 | self.assertEqual(name.commonName, "quux") |
| 378 | self.assertEqual(name.CN, "quux") |
| 379 | |
| 380 | |
Jean-Paul Calderone | eff3cd9 | 2008-03-05 22:35:26 -0500 | [diff] [blame] | 381 | def test_copy(self): |
| 382 | """ |
| 383 | L{X509Name} creates a new L{X509NameType} instance with all the same |
| 384 | attributes as an existing L{X509NameType} instance when called with |
| 385 | one. |
| 386 | """ |
Jean-Paul Calderone | e098dc7 | 2008-03-06 18:36:19 -0500 | [diff] [blame] | 387 | name = self._x509name(commonName="foo", emailAddress="bar@example.com") |
Jean-Paul Calderone | eff3cd9 | 2008-03-05 22:35:26 -0500 | [diff] [blame] | 388 | |
| 389 | copy = X509Name(name) |
| 390 | self.assertEqual(copy.commonName, "foo") |
| 391 | self.assertEqual(copy.emailAddress, "bar@example.com") |
Jean-Paul Calderone | e098dc7 | 2008-03-06 18:36:19 -0500 | [diff] [blame] | 392 | |
| 393 | # Mutate the copy and ensure the original is unmodified. |
Jean-Paul Calderone | eff3cd9 | 2008-03-05 22:35:26 -0500 | [diff] [blame] | 394 | copy.commonName = "baz" |
| 395 | self.assertEqual(name.commonName, "foo") |
Jean-Paul Calderone | e098dc7 | 2008-03-06 18:36:19 -0500 | [diff] [blame] | 396 | |
| 397 | # Mutate the original and ensure the copy is unmodified. |
Jean-Paul Calderone | eff3cd9 | 2008-03-05 22:35:26 -0500 | [diff] [blame] | 398 | name.emailAddress = "quux@example.com" |
| 399 | self.assertEqual(copy.emailAddress, "bar@example.com") |
| 400 | |
Jean-Paul Calderone | e098dc7 | 2008-03-06 18:36:19 -0500 | [diff] [blame] | 401 | |
| 402 | def test_repr(self): |
| 403 | """ |
| 404 | L{repr} passed an L{X509NameType} instance should return a string |
| 405 | containing a description of the type and the NIDs which have been set |
| 406 | on it. |
| 407 | """ |
| 408 | name = self._x509name(commonName="foo", emailAddress="bar") |
| 409 | self.assertEqual( |
| 410 | repr(name), |
| 411 | "<X509Name object '/emailAddress=bar/CN=foo'>") |
| 412 | |
| 413 | |
| 414 | def test_comparison(self): |
| 415 | """ |
| 416 | L{X509NameType} instances should compare based on their NIDs. |
| 417 | """ |
| 418 | def _equality(a, b, assertTrue, assertFalse): |
| 419 | assertTrue(a == b, "(%r == %r) --> False" % (a, b)) |
| 420 | assertFalse(a != b) |
| 421 | assertTrue(b == a) |
| 422 | assertFalse(b != a) |
| 423 | |
| 424 | def assertEqual(a, b): |
| 425 | _equality(a, b, self.assertTrue, self.assertFalse) |
| 426 | |
| 427 | # Instances compare equal to themselves. |
| 428 | name = self._x509name() |
| 429 | assertEqual(name, name) |
| 430 | |
| 431 | # Empty instances should compare equal to each other. |
| 432 | assertEqual(self._x509name(), self._x509name()) |
| 433 | |
| 434 | # Instances with equal NIDs should compare equal to each other. |
| 435 | assertEqual(self._x509name(commonName="foo"), |
| 436 | self._x509name(commonName="foo")) |
| 437 | |
| 438 | # Instance with equal NIDs set using different aliases should compare |
| 439 | # equal to each other. |
| 440 | assertEqual(self._x509name(commonName="foo"), |
| 441 | self._x509name(CN="foo")) |
| 442 | |
| 443 | # Instances with more than one NID with the same values should compare |
| 444 | # equal to each other. |
| 445 | assertEqual(self._x509name(CN="foo", organizationalUnitName="bar"), |
| 446 | self._x509name(commonName="foo", OU="bar")) |
| 447 | |
| 448 | def assertNotEqual(a, b): |
| 449 | _equality(a, b, self.assertFalse, self.assertTrue) |
| 450 | |
| 451 | # Instances with different values for the same NID should not compare |
| 452 | # equal to each other. |
| 453 | assertNotEqual(self._x509name(CN="foo"), |
| 454 | self._x509name(CN="bar")) |
| 455 | |
| 456 | # Instances with different NIDs should not compare equal to each other. |
| 457 | assertNotEqual(self._x509name(CN="foo"), |
| 458 | self._x509name(OU="foo")) |
| 459 | |
| 460 | def _inequality(a, b, assertTrue, assertFalse): |
| 461 | assertTrue(a < b) |
| 462 | assertTrue(a <= b) |
| 463 | assertTrue(b > a) |
| 464 | assertTrue(b >= a) |
| 465 | assertFalse(a > b) |
| 466 | assertFalse(a >= b) |
| 467 | assertFalse(b < a) |
| 468 | assertFalse(b <= a) |
| 469 | |
| 470 | def assertLessThan(a, b): |
| 471 | _inequality(a, b, self.assertTrue, self.assertFalse) |
| 472 | |
| 473 | # An X509Name with a NID with a value which sorts less than the value |
| 474 | # of the same NID on another X509Name compares less than the other |
| 475 | # X509Name. |
| 476 | assertLessThan(self._x509name(CN="abc"), |
| 477 | self._x509name(CN="def")) |
| 478 | |
| 479 | def assertGreaterThan(a, b): |
| 480 | _inequality(a, b, self.assertFalse, self.assertTrue) |
| 481 | |
| 482 | # An X509Name with a NID with a value which sorts greater than the |
| 483 | # value of the same NID on another X509Name compares greater than the |
| 484 | # other X509Name. |
| 485 | assertGreaterThan(self._x509name(CN="def"), |
| 486 | self._x509name(CN="abc")) |
Jean-Paul Calderone | 2aa2b33 | 2008-03-06 21:43:14 -0500 | [diff] [blame] | 487 | |
| 488 | |
Jean-Paul Calderone | 110cd09 | 2008-03-24 17:27:42 -0400 | [diff] [blame] | 489 | def test_hash(self): |
| 490 | """ |
| 491 | L{X509Name.hash} returns an integer hash based on the value of the |
| 492 | name. |
| 493 | """ |
| 494 | a = self._x509name(CN="foo") |
| 495 | b = self._x509name(CN="foo") |
| 496 | self.assertEqual(a.hash(), b.hash()) |
| 497 | a.CN = "bar" |
| 498 | self.assertNotEqual(a.hash(), b.hash()) |
| 499 | |
| 500 | |
Jean-Paul Calderone | e957a00 | 2008-03-25 15:16:51 -0400 | [diff] [blame] | 501 | def test_der(self): |
| 502 | """ |
| 503 | L{X509Name.der} returns the DER encoded form of the name. |
| 504 | """ |
| 505 | a = self._x509name(CN="foo", C="US") |
| 506 | self.assertEqual( |
| 507 | a.der(), |
| 508 | '0\x1b1\x0b0\t\x06\x03U\x04\x06\x13\x02US' |
| 509 | '1\x0c0\n\x06\x03U\x04\x03\x13\x03foo') |
| 510 | |
| 511 | |
Jean-Paul Calderone | c54cc18 | 2008-03-26 21:11:07 -0400 | [diff] [blame] | 512 | def test_get_components(self): |
| 513 | """ |
| 514 | L{X509Name.get_components} returns a C{list} of two-tuples of C{str} |
| 515 | giving the NIDs and associated values which make up the name. |
| 516 | """ |
| 517 | a = self._x509name() |
| 518 | self.assertEqual(a.get_components(), []) |
| 519 | a.CN = "foo" |
| 520 | self.assertEqual(a.get_components(), [("CN", "foo")]) |
| 521 | a.organizationalUnitName = "bar" |
| 522 | self.assertEqual( |
| 523 | a.get_components(), |
| 524 | [("CN", "foo"), ("OU", "bar")]) |
| 525 | |
Jean-Paul Calderone | 110cd09 | 2008-03-24 17:27:42 -0400 | [diff] [blame] | 526 | |
Jean-Paul Calderone | ac0d95f | 2008-03-10 00:00:42 -0400 | [diff] [blame] | 527 | class _PKeyInteractionTestsMixin: |
| 528 | """ |
| 529 | Tests which involve another thing and a PKey. |
| 530 | """ |
| 531 | def signable(self): |
| 532 | """ |
| 533 | Return something with a C{set_pubkey}, C{set_pubkey}, and C{sign} method. |
| 534 | """ |
| 535 | raise NotImplementedError() |
| 536 | |
| 537 | |
| 538 | def test_signWithUngenerated(self): |
| 539 | """ |
| 540 | L{X509Req.sign} raises L{ValueError} when pass a L{PKey} with no parts. |
| 541 | """ |
| 542 | request = self.signable() |
| 543 | key = PKey() |
| 544 | self.assertRaises(ValueError, request.sign, key, 'MD5') |
| 545 | |
| 546 | |
| 547 | def test_signWithPublicKey(self): |
| 548 | """ |
| 549 | L{X509Req.sign} raises L{ValueError} when pass a L{PKey} with no |
| 550 | private part as the signing key. |
| 551 | """ |
| 552 | request = self.signable() |
| 553 | key = PKey() |
| 554 | key.generate_key(TYPE_RSA, 512) |
| 555 | request.set_pubkey(key) |
| 556 | pub = request.get_pubkey() |
| 557 | self.assertRaises(ValueError, request.sign, pub, 'MD5') |
| 558 | |
| 559 | |
| 560 | |
| 561 | class X509ReqTests(TestCase, _PKeyInteractionTestsMixin, _Python23TestCaseHelper): |
Jean-Paul Calderone | 2aa2b33 | 2008-03-06 21:43:14 -0500 | [diff] [blame] | 562 | """ |
| 563 | Tests for L{OpenSSL.crypto.X509Req}. |
| 564 | """ |
Jean-Paul Calderone | ac0d95f | 2008-03-10 00:00:42 -0400 | [diff] [blame] | 565 | def signable(self): |
| 566 | """ |
| 567 | Create and return a new L{X509Req}. |
| 568 | """ |
| 569 | return X509Req() |
| 570 | |
| 571 | |
Jean-Paul Calderone | 2aa2b33 | 2008-03-06 21:43:14 -0500 | [diff] [blame] | 572 | def test_construction(self): |
| 573 | """ |
| 574 | L{X509Req} takes no arguments and returns an L{X509ReqType} instance. |
| 575 | """ |
| 576 | request = X509Req() |
| 577 | self.assertTrue( |
| 578 | isinstance(request, X509ReqType), |
| 579 | "%r is of type %r, should be %r" % (request, type(request), X509ReqType)) |
| 580 | |
| 581 | |
Jean-Paul Calderone | 8dd19b8 | 2008-12-28 20:41:16 -0500 | [diff] [blame] | 582 | def test_version(self): |
| 583 | """ |
| 584 | L{X509ReqType.set_version} sets the X.509 version of the certificate |
| 585 | request. L{X509ReqType.get_version} returns the X.509 version of |
| 586 | the certificate request. The initial value of the version is 0. |
| 587 | """ |
| 588 | request = X509Req() |
| 589 | self.assertEqual(request.get_version(), 0) |
| 590 | request.set_version(1) |
| 591 | self.assertEqual(request.get_version(), 1) |
| 592 | request.set_version(3) |
| 593 | self.assertEqual(request.get_version(), 3) |
| 594 | |
| 595 | |
Jean-Paul Calderone | 2aa2b33 | 2008-03-06 21:43:14 -0500 | [diff] [blame] | 596 | def test_get_subject(self): |
| 597 | """ |
| 598 | L{X509ReqType.get_subject} returns an L{X509Name} for the subject of |
| 599 | the request and which is valid even after the request object is |
| 600 | otherwise dead. |
| 601 | """ |
| 602 | request = X509Req() |
| 603 | subject = request.get_subject() |
| 604 | self.assertTrue( |
| 605 | isinstance(subject, X509NameType), |
| 606 | "%r is of type %r, should be %r" % (subject, type(subject), X509NameType)) |
| 607 | subject.commonName = "foo" |
| 608 | self.assertEqual(request.get_subject().commonName, "foo") |
| 609 | del request |
| 610 | subject.commonName = "bar" |
| 611 | self.assertEqual(subject.commonName, "bar") |
Jean-Paul Calderone | 78381d2 | 2008-03-06 23:35:22 -0500 | [diff] [blame] | 612 | |
| 613 | |
| 614 | |
Jean-Paul Calderone | ac0d95f | 2008-03-10 00:00:42 -0400 | [diff] [blame] | 615 | class X509Tests(TestCase, _PKeyInteractionTestsMixin, _Python23TestCaseHelper): |
Jean-Paul Calderone | 78381d2 | 2008-03-06 23:35:22 -0500 | [diff] [blame] | 616 | """ |
| 617 | Tests for L{OpenSSL.crypto.X509}. |
| 618 | """ |
Jean-Paul Calderone | 5ef8651 | 2008-04-26 19:06:28 -0400 | [diff] [blame] | 619 | pemData = cleartextCertificatePEM + cleartextPrivateKeyPEM |
Jean-Paul Calderone | 8114b45 | 2008-03-25 15:27:59 -0400 | [diff] [blame] | 620 | |
Jean-Paul Calderone | ac0d95f | 2008-03-10 00:00:42 -0400 | [diff] [blame] | 621 | def signable(self): |
| 622 | """ |
| 623 | Create and return a new L{X509}. |
| 624 | """ |
| 625 | return X509() |
| 626 | |
| 627 | |
Jean-Paul Calderone | 78381d2 | 2008-03-06 23:35:22 -0500 | [diff] [blame] | 628 | def test_construction(self): |
| 629 | """ |
| 630 | L{X509} takes no arguments and returns an instance of L{X509Type}. |
| 631 | """ |
| 632 | certificate = X509() |
| 633 | self.assertTrue( |
| 634 | isinstance(certificate, X509Type), |
| 635 | "%r is of type %r, should be %r" % (certificate, |
| 636 | type(certificate), |
| 637 | X509Type)) |
| 638 | |
| 639 | |
| 640 | def test_serial_number(self): |
| 641 | """ |
| 642 | The serial number of an L{X509Type} can be retrieved and modified with |
| 643 | L{X509Type.get_serial_number} and L{X509Type.set_serial_number}. |
| 644 | """ |
| 645 | certificate = X509() |
| 646 | self.assertRaises(TypeError, certificate.set_serial_number) |
| 647 | self.assertRaises(TypeError, certificate.set_serial_number, 1, 2) |
| 648 | self.assertRaises(TypeError, certificate.set_serial_number, "1") |
| 649 | self.assertRaises(TypeError, certificate.set_serial_number, 5.5) |
| 650 | self.assertEqual(certificate.get_serial_number(), 0) |
| 651 | certificate.set_serial_number(1) |
| 652 | self.assertEqual(certificate.get_serial_number(), 1) |
| 653 | certificate.set_serial_number(2 ** 32 + 1) |
| 654 | self.assertEqual(certificate.get_serial_number(), 2 ** 32 + 1) |
| 655 | certificate.set_serial_number(2 ** 64 + 1) |
| 656 | self.assertEqual(certificate.get_serial_number(), 2 ** 64 + 1) |
Jean-Paul Calderone | 525ef80 | 2008-03-09 20:39:42 -0400 | [diff] [blame] | 657 | certificate.set_serial_number(2 ** 128 + 1) |
| 658 | self.assertEqual(certificate.get_serial_number(), 2 ** 128 + 1) |
| 659 | |
| 660 | |
| 661 | def _setBoundTest(self, which): |
| 662 | """ |
| 663 | L{X509Type.set_notBefore} takes a string in the format of an ASN1 |
| 664 | GENERALIZEDTIME and sets the beginning of the certificate's validity |
| 665 | period to it. |
| 666 | """ |
| 667 | certificate = X509() |
| 668 | set = getattr(certificate, 'set_not' + which) |
| 669 | get = getattr(certificate, 'get_not' + which) |
| 670 | |
Jean-Paul Calderone | e0615b5 | 2008-03-09 21:44:46 -0400 | [diff] [blame] | 671 | # Starts with no value. |
| 672 | self.assertEqual(get(), None) |
| 673 | |
Jean-Paul Calderone | 525ef80 | 2008-03-09 20:39:42 -0400 | [diff] [blame] | 674 | # GMT (Or is it UTC?) -exarkun |
| 675 | when = "20040203040506Z" |
| 676 | set(when) |
| 677 | self.assertEqual(get(), when) |
| 678 | |
| 679 | # A plus two hours and thirty minutes offset |
| 680 | when = "20040203040506+0530" |
| 681 | set(when) |
| 682 | self.assertEqual(get(), when) |
| 683 | |
| 684 | # A minus one hour fifteen minutes offset |
| 685 | when = "20040203040506-0115" |
| 686 | set(when) |
| 687 | self.assertEqual(get(), when) |
| 688 | |
| 689 | # An invalid string results in a ValueError |
| 690 | self.assertRaises(ValueError, set, "foo bar") |
| 691 | |
| 692 | |
| 693 | def test_set_notBefore(self): |
| 694 | """ |
| 695 | L{X509Type.set_notBefore} takes a string in the format of an ASN1 |
| 696 | GENERALIZEDTIME and sets the beginning of the certificate's validity |
| 697 | period to it. |
| 698 | """ |
| 699 | self._setBoundTest("Before") |
| 700 | |
| 701 | |
| 702 | def test_set_notAfter(self): |
| 703 | """ |
| 704 | L{X509Type.set_notAfter} takes a string in the format of an ASN1 |
| 705 | GENERALIZEDTIME and sets the end of the certificate's validity period |
| 706 | to it. |
| 707 | """ |
| 708 | self._setBoundTest("After") |
Jean-Paul Calderone | 76576d5 | 2008-03-24 16:04:46 -0400 | [diff] [blame] | 709 | |
| 710 | |
Jean-Paul Calderone | 38a646d | 2008-03-25 15:16:18 -0400 | [diff] [blame] | 711 | def test_get_notBefore(self): |
| 712 | """ |
| 713 | L{X509Type.get_notBefore} returns a string in the format of an ASN1 |
| 714 | GENERALIZEDTIME even for certificates which store it as UTCTIME |
| 715 | internally. |
| 716 | """ |
Jean-Paul Calderone | 8114b45 | 2008-03-25 15:27:59 -0400 | [diff] [blame] | 717 | cert = load_certificate(FILETYPE_PEM, self.pemData) |
Jean-Paul Calderone | 20131f5 | 2009-04-01 12:05:45 -0400 | [diff] [blame] | 718 | self.assertEqual(cert.get_notBefore(), "20090325123658Z") |
Jean-Paul Calderone | 38a646d | 2008-03-25 15:16:18 -0400 | [diff] [blame] | 719 | |
| 720 | |
| 721 | def test_get_notAfter(self): |
| 722 | """ |
| 723 | L{X509Type.get_notAfter} returns a string in the format of an ASN1 |
| 724 | GENERALIZEDTIME even for certificates which store it as UTCTIME |
| 725 | internally. |
| 726 | """ |
Jean-Paul Calderone | 8114b45 | 2008-03-25 15:27:59 -0400 | [diff] [blame] | 727 | cert = load_certificate(FILETYPE_PEM, self.pemData) |
Jean-Paul Calderone | 20131f5 | 2009-04-01 12:05:45 -0400 | [diff] [blame] | 728 | self.assertEqual(cert.get_notAfter(), "20170611123658Z") |
Jean-Paul Calderone | 38a646d | 2008-03-25 15:16:18 -0400 | [diff] [blame] | 729 | |
| 730 | |
Jean-Paul Calderone | 76576d5 | 2008-03-24 16:04:46 -0400 | [diff] [blame] | 731 | def test_digest(self): |
| 732 | """ |
| 733 | L{X509.digest} returns a string giving ":"-separated hex-encoded words |
| 734 | of the digest of the certificate. |
| 735 | """ |
| 736 | cert = X509() |
| 737 | self.assertEqual( |
| 738 | cert.digest("md5"), |
| 739 | "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] | 740 | |
| 741 | |
| 742 | |
Jean-Paul Calderone | 6fe60c2 | 2008-04-26 20:04:53 -0400 | [diff] [blame] | 743 | class FunctionTests(TestCase, _Python23TestCaseHelper): |
Jean-Paul Calderone | 828c9cb | 2008-04-26 18:06:54 -0400 | [diff] [blame] | 744 | """ |
| 745 | Tests for free-functions in the L{OpenSSL.crypto} module. |
| 746 | """ |
| 747 | def test_load_privatekey_wrongPassphrase(self): |
| 748 | """ |
| 749 | L{load_privatekey} raises L{OpenSSL.crypto.Error} when it is passed an |
| 750 | encrypted PEM and an incorrect passphrase. |
| 751 | """ |
| 752 | self.assertRaises( |
| 753 | Error, |
| 754 | load_privatekey, FILETYPE_PEM, encryptedPrivateKeyPEM, "quack") |
| 755 | |
| 756 | |
| 757 | def test_load_privatekey_passphrase(self): |
| 758 | """ |
| 759 | L{load_privatekey} can create a L{PKey} object from an encrypted PEM |
| 760 | string if given the passphrase. |
| 761 | """ |
| 762 | key = load_privatekey( |
| 763 | FILETYPE_PEM, encryptedPrivateKeyPEM, |
| 764 | encryptedPrivateKeyPEMPassphrase) |
| 765 | self.assertTrue(isinstance(key, PKeyType)) |
| 766 | |
| 767 | |
| 768 | def test_load_privatekey_wrongPassphraseCallback(self): |
| 769 | """ |
| 770 | L{load_privatekey} raises L{OpenSSL.crypto.Error} when it is passed an |
| 771 | encrypted PEM and a passphrase callback which returns an incorrect |
| 772 | passphrase. |
| 773 | """ |
| 774 | called = [] |
| 775 | def cb(*a): |
| 776 | called.append(None) |
| 777 | return "quack" |
| 778 | self.assertRaises( |
| 779 | Error, |
| 780 | load_privatekey, FILETYPE_PEM, encryptedPrivateKeyPEM, cb) |
| 781 | self.assertTrue(called) |
| 782 | |
| 783 | def test_load_privatekey_passphraseCallback(self): |
| 784 | """ |
| 785 | L{load_privatekey} can create a L{PKey} object from an encrypted PEM |
| 786 | string if given a passphrase callback which returns the correct |
| 787 | password. |
| 788 | """ |
| 789 | called = [] |
| 790 | def cb(writing): |
| 791 | called.append(writing) |
| 792 | return encryptedPrivateKeyPEMPassphrase |
| 793 | key = load_privatekey(FILETYPE_PEM, encryptedPrivateKeyPEM, cb) |
| 794 | self.assertTrue(isinstance(key, PKeyType)) |
| 795 | self.assertEqual(called, [False]) |
| 796 | |
| 797 | |
| 798 | def test_dump_privatekey_passphrase(self): |
| 799 | """ |
| 800 | L{dump_privatekey} writes an encrypted PEM when given a passphrase. |
| 801 | """ |
| 802 | passphrase = "foo" |
| 803 | key = load_privatekey(FILETYPE_PEM, cleartextPrivateKeyPEM) |
| 804 | pem = dump_privatekey(FILETYPE_PEM, key, "blowfish", passphrase) |
| 805 | self.assertTrue(isinstance(pem, str)) |
| 806 | loadedKey = load_privatekey(FILETYPE_PEM, pem, passphrase) |
| 807 | self.assertTrue(isinstance(loadedKey, PKeyType)) |
| 808 | self.assertEqual(loadedKey.type(), key.type()) |
| 809 | self.assertEqual(loadedKey.bits(), key.bits()) |
| 810 | |
Jean-Paul Calderone | f17e421 | 2009-04-01 14:21:40 -0400 | [diff] [blame] | 811 | |
| 812 | def _runopenssl(self, pem, *args): |
| 813 | """ |
| 814 | Run the command line openssl tool with the given arguments and write |
| 815 | the given PEM to its stdin. |
| 816 | """ |
Jean-Paul Calderone | 16c4d5e | 2009-05-04 18:27:52 -0400 | [diff] [blame] | 817 | write, read = popen2(" ".join(("openssl",) + args), "b") |
Jean-Paul Calderone | 653f558 | 2009-04-01 14:42:32 -0400 | [diff] [blame] | 818 | write.write(pem) |
| 819 | write.close() |
| 820 | return read.read() |
Jean-Paul Calderone | f17e421 | 2009-04-01 14:21:40 -0400 | [diff] [blame] | 821 | |
| 822 | |
Rick Dean | 5b7b637 | 2009-04-01 11:34:06 -0500 | [diff] [blame] | 823 | def test_dump_certificate(self): |
| 824 | """ |
| 825 | L{dump_certificate} writes PEM, DER, and text. |
| 826 | """ |
Jean-Paul Calderone | f17e421 | 2009-04-01 14:21:40 -0400 | [diff] [blame] | 827 | pemData = cleartextCertificatePEM + cleartextPrivateKeyPEM |
Rick Dean | 5b7b637 | 2009-04-01 11:34:06 -0500 | [diff] [blame] | 828 | cert = load_certificate(FILETYPE_PEM, pemData) |
| 829 | dumped_pem = dump_certificate(FILETYPE_PEM, cert) |
| 830 | self.assertEqual(dumped_pem, cleartextCertificatePEM) |
| 831 | dumped_der = dump_certificate(FILETYPE_ASN1, cert) |
Jean-Paul Calderone | f17e421 | 2009-04-01 14:21:40 -0400 | [diff] [blame] | 832 | good_der = self._runopenssl(dumped_pem, "x509", "-outform", "DER") |
Rick Dean | 5b7b637 | 2009-04-01 11:34:06 -0500 | [diff] [blame] | 833 | self.assertEqual(dumped_der, good_der) |
| 834 | cert2 = load_certificate(FILETYPE_ASN1, dumped_der) |
| 835 | dumped_pem2 = dump_certificate(FILETYPE_PEM, cert2) |
| 836 | self.assertEqual(dumped_pem2, cleartextCertificatePEM) |
| 837 | dumped_text = dump_certificate(FILETYPE_TEXT, cert) |
Jean-Paul Calderone | f17e421 | 2009-04-01 14:21:40 -0400 | [diff] [blame] | 838 | good_text = self._runopenssl(dumped_pem, "x509", "-noout", "-text") |
Rick Dean | 5b7b637 | 2009-04-01 11:34:06 -0500 | [diff] [blame] | 839 | self.assertEqual(dumped_text, good_text) |
| 840 | |
| 841 | |
| 842 | def test_dump_privatekey(self): |
| 843 | """ |
| 844 | L{dump_privatekey} writes a PEM, DER, and text. |
| 845 | """ |
| 846 | key = load_privatekey(FILETYPE_PEM, cleartextPrivateKeyPEM) |
| 847 | dumped_pem = dump_privatekey(FILETYPE_PEM, key) |
| 848 | self.assertEqual(dumped_pem, cleartextPrivateKeyPEM) |
| 849 | dumped_der = dump_privatekey(FILETYPE_ASN1, key) |
Jean-Paul Calderone | f17e421 | 2009-04-01 14:21:40 -0400 | [diff] [blame] | 850 | # XXX This OpenSSL call writes "writing RSA key" to standard out. Sad. |
| 851 | good_der = self._runopenssl(dumped_pem, "rsa", "-outform", "DER") |
Rick Dean | 5b7b637 | 2009-04-01 11:34:06 -0500 | [diff] [blame] | 852 | self.assertEqual(dumped_der, good_der) |
| 853 | key2 = load_privatekey(FILETYPE_ASN1, dumped_der) |
| 854 | dumped_pem2 = dump_privatekey(FILETYPE_PEM, key2) |
| 855 | self.assertEqual(dumped_pem2, cleartextPrivateKeyPEM) |
| 856 | dumped_text = dump_privatekey(FILETYPE_TEXT, key) |
Jean-Paul Calderone | f17e421 | 2009-04-01 14:21:40 -0400 | [diff] [blame] | 857 | good_text = self._runopenssl(dumped_pem, "rsa", "-noout", "-text") |
Rick Dean | 5b7b637 | 2009-04-01 11:34:06 -0500 | [diff] [blame] | 858 | self.assertEqual(dumped_text, good_text) |
| 859 | |
Jean-Paul Calderone | f17e421 | 2009-04-01 14:21:40 -0400 | [diff] [blame] | 860 | |
Rick Dean | 5b7b637 | 2009-04-01 11:34:06 -0500 | [diff] [blame] | 861 | def test_dump_certificate_request(self): |
| 862 | """ |
| 863 | L{dump_certificate_request} writes a PEM, DER, and text. |
| 864 | """ |
| 865 | req = load_certificate_request(FILETYPE_PEM, cleartextCertificateRequestPEM) |
| 866 | dumped_pem = dump_certificate_request(FILETYPE_PEM, req) |
| 867 | self.assertEqual(dumped_pem, cleartextCertificateRequestPEM) |
| 868 | dumped_der = dump_certificate_request(FILETYPE_ASN1, req) |
Jean-Paul Calderone | f17e421 | 2009-04-01 14:21:40 -0400 | [diff] [blame] | 869 | good_der = self._runopenssl(dumped_pem, "req", "-outform", "DER") |
Rick Dean | 5b7b637 | 2009-04-01 11:34:06 -0500 | [diff] [blame] | 870 | self.assertEqual(dumped_der, good_der) |
| 871 | req2 = load_certificate_request(FILETYPE_ASN1, dumped_der) |
| 872 | dumped_pem2 = dump_certificate_request(FILETYPE_PEM, req2) |
| 873 | self.assertEqual(dumped_pem2, cleartextCertificateRequestPEM) |
| 874 | dumped_text = dump_certificate_request(FILETYPE_TEXT, req) |
Jean-Paul Calderone | f17e421 | 2009-04-01 14:21:40 -0400 | [diff] [blame] | 875 | good_text = self._runopenssl(dumped_pem, "req", "-noout", "-text") |
Rick Dean | 5b7b637 | 2009-04-01 11:34:06 -0500 | [diff] [blame] | 876 | self.assertEqual(dumped_text, good_text) |
| 877 | |
Jean-Paul Calderone | 828c9cb | 2008-04-26 18:06:54 -0400 | [diff] [blame] | 878 | |
| 879 | def test_dump_privatekey_passphraseCallback(self): |
| 880 | """ |
| 881 | L{dump_privatekey} writes an encrypted PEM when given a callback which |
| 882 | returns the correct passphrase. |
| 883 | """ |
| 884 | passphrase = "foo" |
| 885 | called = [] |
| 886 | def cb(writing): |
| 887 | called.append(writing) |
| 888 | return passphrase |
| 889 | key = load_privatekey(FILETYPE_PEM, cleartextPrivateKeyPEM) |
| 890 | pem = dump_privatekey(FILETYPE_PEM, key, "blowfish", cb) |
| 891 | self.assertTrue(isinstance(pem, str)) |
| 892 | self.assertEqual(called, [True]) |
| 893 | loadedKey = load_privatekey(FILETYPE_PEM, pem, passphrase) |
| 894 | self.assertTrue(isinstance(loadedKey, PKeyType)) |
| 895 | self.assertEqual(loadedKey.type(), key.type()) |
| 896 | self.assertEqual(loadedKey.bits(), key.bits()) |
Rick Dean | 5b7b637 | 2009-04-01 11:34:06 -0500 | [diff] [blame] | 897 | |
| 898 | |
Jean-Paul Calderone | dc138fa | 2009-06-27 14:32:07 -0400 | [diff] [blame] | 899 | def test_load_pkcs7_data(self): |
| 900 | """ |
| 901 | L{load_pkcs7_data} accepts a PKCS#7 string and returns an instance of |
| 902 | L{PKCS7Type}. |
| 903 | """ |
| 904 | pkcs7 = load_pkcs7_data(FILETYPE_PEM, pkcs7Data) |
| 905 | self.assertTrue(isinstance(pkcs7, PKCS7Type)) |
| 906 | |
| 907 | |
| 908 | def test_load_pkcs12(self): |
| 909 | """ |
| 910 | L{load_pkcs12} accepts a PKCS#12 string and returns an instance of |
| 911 | L{PKCS12Type}. |
| 912 | """ |
| 913 | pkcs12 = load_pkcs12(pkcs12Data) |
| 914 | self.assertTrue(isinstance(pkcs12, PKCS12Type)) |
| 915 | |
| 916 | |
| 917 | |
| 918 | class NetscapeSPKITests(TestCase): |
| 919 | """ |
| 920 | Tests for L{OpenSSL.crypto.NetscapeSPKI}. |
| 921 | """ |
| 922 | def test_construction(self): |
| 923 | """ |
| 924 | L{NetscapeSPKI} returns an instance of L{NetscapeSPKIType}. |
| 925 | """ |
| 926 | nspki = NetscapeSPKI() |
| 927 | self.assertTrue(isinstance(nspki, NetscapeSPKIType)) |
| 928 | |
| 929 | |
| 930 | |
Rick Dean | 5b7b637 | 2009-04-01 11:34:06 -0500 | [diff] [blame] | 931 | if __name__ == '__main__': |
| 932 | main() |