def testImportKey1(self):
     key_obj = DSA.importKey(self.der_public)
     self.assertFalse(key_obj.has_private())
     self.assertEqual(self.y, key_obj.y)
     self.assertEqual(self.p, key_obj.p)
     self.assertEqual(self.q, key_obj.q)
     self.assertEqual(self.g, key_obj.g)
 def testImportKey2(self):
     for pem in (self.pem_public, tostr(self.pem_public)):
         key_obj = DSA.importKey(pem)
         self.assertFalse(key_obj.has_private())
         self.assertEqual(self.y, key_obj.y)
         self.assertEqual(self.p, key_obj.p)
         self.assertEqual(self.q, key_obj.q)
         self.assertEqual(self.g, key_obj.g)
 def testImportKey10(self):
     key_obj = DSA.importKey(self.der_pkcs8_encrypted, "PWDTEST")
     self.assertTrue(key_obj.has_private())
     self.assertEqual(self.y, key_obj.y)
     self.assertEqual(self.p, key_obj.p)
     self.assertEqual(self.q, key_obj.q)
     self.assertEqual(self.g, key_obj.g)
     self.assertEqual(self.x, key_obj.x)
 def testImportKey7(self):
     for ssh in (self.ssh_pub, tostr(self.ssh_pub)):
         key_obj = DSA.importKey(ssh)
         self.assertFalse(key_obj.has_private())
         self.assertEqual(self.y, key_obj.y)
         self.assertEqual(self.p, key_obj.p)
         self.assertEqual(self.q, key_obj.q)
         self.assertEqual(self.g, key_obj.g)
 def testImportKey5(self):
     key_obj = DSA.importKey(self.der_pkcs8)
     self.assertTrue(key_obj.has_private())
     self.assertEqual(self.y, key_obj.y)
     self.assertEqual(self.p, key_obj.p)
     self.assertEqual(self.q, key_obj.q)
     self.assertEqual(self.g, key_obj.g)
     self.assertEqual(self.x, key_obj.x)
 def testImportKey9(self):
     for pem in (self.pem_pkcs8_encrypted, tostr(self.pem_pkcs8_encrypted)):
         key_obj = DSA.importKey(pem, "PWDTEST")
         self.assertTrue(key_obj.has_private())
         self.assertEqual(self.y, key_obj.y)
         self.assertEqual(self.p, key_obj.p)
         self.assertEqual(self.q, key_obj.q)
         self.assertEqual(self.g, key_obj.g)
         self.assertEqual(self.x, key_obj.x)
 def testImportKey6(self):
     for pem in (self.pem_pkcs8, tostr(self.pem_pkcs8)):
         key_obj = DSA.importKey(pem)
         self.assertTrue(key_obj.has_private())
         self.assertEqual(self.y, key_obj.y)
         self.assertEqual(self.p, key_obj.p)
         self.assertEqual(self.q, key_obj.q)
         self.assertEqual(self.g, key_obj.g)
         self.assertEqual(self.x, key_obj.x)
 def testExportKey8(self):
     tup = (self.y, self.g, self.p, self.q, self.x)
     key = DSA.construct(tup)
     encoded = key.export_key('PEM', pkcs8=False, passphrase="PWDTEST")
     key = DSA.importKey(encoded, "PWDTEST")
     self.assertEqual(self.y, key.y)
     self.assertEqual(self.p, key.p)
     self.assertEqual(self.q, key.q)
     self.assertEqual(self.g, key.g)
     self.assertEqual(self.x, key.x)
    def test_x509v3(self):

        # Sample V3 certificate with a 1024 bit DSA key
        x509_v3_cert = """
-----BEGIN CERTIFICATE-----
MIIFhjCCA26gAwIBAgIBAzANBgkqhkiG9w0BAQsFADBhMQswCQYDVQQGEwJVUzEL
MAkGA1UECAwCTUQxEjAQBgNVBAcMCUJhbHRpbW9yZTEQMA4GA1UEAwwHVGVzdCBD
QTEfMB0GCSqGSIb3DQEJARYQdGVzdEBleGFtcGxlLmNvbTAeFw0xNDA3MTMyMDUz
MjBaFw0xNzA0MDgyMDUzMjBaMEAxCzAJBgNVBAYTAlVTMQswCQYDVQQIDAJNRDES
MBAGA1UEBwwJQmFsdGltb3JlMRAwDgYDVQQDDAdhdXN0cmlhMIIBtjCCASsGByqG
SM44BAEwggEeAoGBALfd8gyEpVPA0ZI69Kp3nyJcu5N0ZZ3K1K9hleQLNqKEcZOh
7a/C2J1TPdmHTLJ0rAwBZ1nWxnARSgRphziGDFspKCYQwYcSMz8KoFgvXbXpuchy
oFACiQ2LqZnc5MakuLQtLcQciSYGYj3zmZdYMoa904F1aDWr+DxQI6DVC3/bAhUA
hqXMCJ6fQK3G2O9S3/CC/yVZXCsCgYBRXROl3R2khX7l10LQjDEgo3B1IzjXU/jP
McMBl6XO+nBJXxr/scbq8Ajiv7LTnG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aCIgzzT5NweJd/b71A2SyzHXJ14zBXsr1PMylMp2TpHIidhuuNuQL6I0HaollB4M
Z3FsVBMhVDw4Z76qnFPr8mZE2tar33hSlJI/3pS/bBiukuBk8U7VB0X8OqaUnP3C
7b2Z4G8GtqDVcKGMzkvMjT4n9rKd/Le+qHSsQOGO9W/0LB7UDAZSwUsfAPnoBgdS
5t9tIomLCOstByXi+gGZue1TcdCa3Ph4kO0=
-----END CERTIFICATE-----
        """.strip()

        # DSA public key as dumped by openssl
        y_str = """
72:95:68:d6:4a:72:87:13:c9:87:c7:50:39:9b:fd:
06:43:1e:b1:9e:15:88:7b:7f:8c:24:09:c4:85:11:
b1:03:62:78:48:2b:1f:59:4d:0e:c2:09:48:41:3a:
21:d7:3e:6e:51:4f:68:0e:2f:5c:62:03:4f:05:eb:
ce:15:19:c3:85:cd:b2:68:96:3a:54:06:27:23:e0:
fe:e8:9e:48:53:a6:1d:d2:26:86:fe:24:9c:e2:c5:
78:6c:5a:f4:6b:17:0f:a6:c7:b7:48:dd:17:69:0c:
62:29:e4:85:05:f1:67:a0:ca:8b:f9:d7:7d:19:bd:
d0:3d:94:a5:94:17:15:0f
        """
        p_str = """
00:b7:dd:f2:0c:84:a5:53:c0:d1:92:3a:f4:aa:77:
9f:22:5c:bb:93:74:65:9d:ca:d4:af:61:95:e4:0b:
36:a2:84:71:93:a1:ed:af:c2:d8:9d:53:3d:d9:87:
4c:b2:74:ac:0c:01:67:59:d6:c6:70:11:4a:04:69:
87:38:86:0c:5b:29:28:26:10:c1:87:12:33:3f:0a:
a0:58:2f:5d:b5:e9:b9:c8:72:a0:50:02:89:0d:8b:
a9:99:dc:e4:c6:a4:b8:b4:2d:2d:c4:1c:89:26:06:
62:3d:f3:99:97:58:32:86:bd:d3:81:75:68:35:ab:
f8:3c:50:23:a0:d5:0b:7f:db
        """
        q_str = """
00:86:a5:cc:08:9e:9f:40:ad:c6:d8:ef:52:df:f0:
82:ff:25:59:5c:2b
        """
        g_str = """
51:5d:13:a5:dd:1d:a4:85:7e:e5:d7:42:d0:8c:31:
20:a3:70:75:23:38:d7:53:f8:cf:31:c3:01:97:a5:
ce:fa:70:49:5f:1a:ff:b1:c6:ea:f0:08:e2:bf:b2:
d3:9c:6a:52:8e:0a:f2:1e:db:df:8f:cf:3b:91:fb:
e8:f1:06:d2:4b:79:29:de:fa:b9:b9:2a:88:ba:2e:
c4:ee:bd:e3:82:e5:9a:2e:3e:b5:e8:01:b5:1d:63:
9c:b5:72:54:8e:ab:22:69:b6:70:b2:9d:a1:d3:d8:
07:4f:39:87:11:70:4e:9a:20:11:bb:cc:45:c4:a0:
07:c6:cb:a8:ee:89:c0:6f
        """

        key = DSA.importKey(x509_v3_cert)
        for comp_name in ('y', 'p', 'q', 'g'):
            comp_str = locals()[comp_name + "_str"]
            comp = int(re.sub("[^0-9a-f]", "", comp_str), 16)
            self.assertEqual(getattr(key, comp_name), comp)
        self.assertFalse(key.has_private())
    def test_x509v1(self):

        # Sample V1 certificate with a 1024 bit DSA key
        x509_v1_cert = """
-----BEGIN CERTIFICATE-----
MIIDUjCCArsCAQIwDQYJKoZIhvcNAQEFBQAwfjENMAsGA1UEChMEQWNtZTELMAkG
A1UECxMCUkQxHDAaBgkqhkiG9w0BCQEWDXNwYW1AYWNtZS5vcmcxEzARBgNVBAcT
Ck1ldHJvcG9saXMxETAPBgNVBAgTCE5ldyBZb3JrMQswCQYDVQQGEwJVUzENMAsG
A1UEAxMEdGVzdDAeFw0xNDA3MTEyMDM4NDNaFw0xNzA0MDYyMDM4NDNaME0xCzAJ
BgNVBAYTAlVTMREwDwYDVQQIEwhOZXcgWW9yazENMAsGA1UEChMEQWNtZTELMAkG
A1UECxMCUkQxDzANBgNVBAMTBnBvbGFuZDCCAbYwggErBgcqhkjOOAQBMIIBHgKB
gQDOrN4Ox4+t3T6wKeHfhzArhcrNEFMQ4Ss+4PIKyimDy9Bn64WPkL1B/9dvYIga
23GLu6tVJmXo6EdJnVOHEMhr99EeOwuDWWeP7Awq7RSlKEejokr4BEzMTW/tExSD
cO6/GI7xzh0eTH+VTTPDfyrJMYCkh0rJAfCP+5xrmPNetwIVALtXYOV1yoRrzJ2Q
M5uEjidH6GiZAoGAfUqA1SAm5g5U68SILMVX9l5rq0OpB0waBMpJQ31/R/yXNDqo
c3gGWZTOJFU4IzwNpGhrGNADUByz/lc1SAOAdEJIr0JVrhbGewQjB4pWqoLGbBKz
RoavTNDc/zD7SYa12evWDHADwvlXoeQg+lWop1zS8OqaDC7aLGKpWN3/m8kDgYQA
AoGAKoirPAfcp1rbbl4y2FFAIktfW8f4+T7d2iKSg73aiVfujhNOt1Zz1lfC0NI2
eonLWO3tAM4XGKf1TLjb5UXngGn40okPsaA81YE6ZIKm20ywjlOY3QkAEdMaLVY3
9PJvM8RGB9m7pLKxyHfGMfF40MVN4222zKeGp7xhM0CNiCUwDQYJKoZIhvcNAQEF
BQADgYEAfbNZfpYa2KlALEM1FZnwvQDvJHntHz8LdeJ4WM7CXDlKi67wY2HKM30w
s2xej75imkVOFd1kF2d0A8sjfriXLVIt1Hwq9ANZomhu4Edx0xpH8tqdh/bDtnM2
TmduZNY9OWkb07h0CtWD6Zt8fhRllVsSSrlWd/2or7FXNC5weFQ=
-----END CERTIFICATE-----
        """.strip()

        # DSA public key as dumped by openssl
        y_str = """
2a:88:ab:3c:07:dc:a7:5a:db:6e:5e:32:d8:51:40:
22:4b:5f:5b:c7:f8:f9:3e:dd:da:22:92:83:bd:da:
89:57:ee:8e:13:4e:b7:56:73:d6:57:c2:d0:d2:36:
7a:89:cb:58:ed:ed:00:ce:17:18:a7:f5:4c:b8:db:
e5:45:e7:80:69:f8:d2:89:0f:b1:a0:3c:d5:81:3a:
64:82:a6:db:4c:b0:8e:53:98:dd:09:00:11:d3:1a:
2d:56:37:f4:f2:6f:33:c4:46:07:d9:bb:a4:b2:b1:
c8:77:c6:31:f1:78:d0:c5:4d:e3:6d:b6:cc:a7:86:
a7:bc:61:33:40:8d:88:25
        """
        p_str = """
00:ce:ac:de:0e:c7:8f:ad:dd:3e:b0:29:e1:df:87:
30:2b:85:ca:cd:10:53:10:e1:2b:3e:e0:f2:0a:ca:
29:83:cb:d0:67:eb:85:8f:90:bd:41:ff:d7:6f:60:
88:1a:db:71:8b:bb:ab:55:26:65:e8:e8:47:49:9d:
53:87:10:c8:6b:f7:d1:1e:3b:0b:83:59:67:8f:ec:
0c:2a:ed:14:a5:28:47:a3:a2:4a:f8:04:4c:cc:4d:
6f:ed:13:14:83:70:ee:bf:18:8e:f1:ce:1d:1e:4c:
7f:95:4d:33:c3:7f:2a:c9:31:80:a4:87:4a:c9:01:
f0:8f:fb:9c:6b:98:f3:5e:b7
        """
        q_str = """
00:bb:57:60:e5:75:ca:84:6b:cc:9d:90:33:9b:84:
8e:27:47:e8:68:99
        """
        g_str = """
7d:4a:80:d5:20:26:e6:0e:54:eb:c4:88:2c:c5:57:
f6:5e:6b:ab:43:a9:07:4c:1a:04:ca:49:43:7d:7f:
47:fc:97:34:3a:a8:73:78:06:59:94:ce:24:55:38:
23:3c:0d:a4:68:6b:18:d0:03:50:1c:b3:fe:57:35:
48:03:80:74:42:48:af:42:55:ae:16:c6:7b:04:23:
07:8a:56:aa:82:c6:6c:12:b3:46:86:af:4c:d0:dc:
ff:30:fb:49:86:b5:d9:eb:d6:0c:70:03:c2:f9:57:
a1:e4:20:fa:55:a8:a7:5c:d2:f0:ea:9a:0c:2e:da:
2c:62:a9:58:dd:ff:9b:c9
        """

        key = DSA.importKey(x509_v1_cert)
        for comp_name in ('y', 'p', 'q', 'g'):
            comp_str = locals()[comp_name + "_str"]
            comp = int(re.sub("[^0-9a-f]", "", comp_str), 16)
            self.assertEqual(getattr(key, comp_name), comp)
        self.assertFalse(key.has_private())