You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@labs.apache.org by dr...@apache.org on 2007/12/06 07:18:15 UTC

svn commit: r601628 - in /labs/badca: BaDCA/ tests/ tests/csr/ tests/keys/private/ tests/keys/public/

Author: dreid
Date: Wed Dec  5 22:18:13 2007
New Revision: 601628

URL: http://svn.apache.org/viewvc?rev=601628&view=rev
Log:
This commit changes how we find private keys when we have a public
key by allowing the key object to have a series of directories
specified to search for the matching filename.
Add test for new functionality

Add more csr tests

Change naming of tests in the test files to simply use numbers.
This means we can control the ordering and as we use the supplied
description when displaying progress we don't loose anything.

Added:
    labs/badca/tests/csr/test2.csr
    labs/badca/tests/keys/private/
    labs/badca/tests/keys/private/2b466cb0b0190cf996721f9ebf7ec30bf456d4ea.key
    labs/badca/tests/keys/public/
    labs/badca/tests/keys/public/test3.public.key
Modified:
    labs/badca/BaDCA/CSRs.py
    labs/badca/BaDCA/Keys.py
    labs/badca/tests/CSRTestCase.py
    labs/badca/tests/KeysTestCase.py

Modified: labs/badca/BaDCA/CSRs.py
URL: http://svn.apache.org/viewvc/labs/badca/BaDCA/CSRs.py?rev=601628&r1=601627&r2=601628&view=diff
==============================================================================
--- labs/badca/BaDCA/CSRs.py (original)
+++ labs/badca/BaDCA/CSRs.py Wed Dec  5 22:18:13 2007
@@ -139,5 +139,6 @@
         if self.rKey is None:
             print "no key object..."
             return 0
+        self.info = csr.parse(self.csr)
         return 1
 

Modified: labs/badca/BaDCA/Keys.py
URL: http://svn.apache.org/viewvc/labs/badca/BaDCA/Keys.py?rev=601628&r1=601627&r2=601628&view=diff
==============================================================================
--- labs/badca/BaDCA/Keys.py (original)
+++ labs/badca/BaDCA/Keys.py Wed Dec  5 22:18:13 2007
@@ -43,6 +43,7 @@
     
     avail = KEY_NONE
     directory = None
+    directories = []
 
     def __init__(self, public = None, private = None):
         # If we have been given either the public or private key object
@@ -62,6 +63,13 @@
     def setDirectory(self, thedir):
         self.directory = thedir
 
+    def addSearchDirectory(self, thedir):
+        if not thedir in self.directories:
+            self.directories.append(thedir)
+            if self.pubRSA is not None and self.privRSA is None:
+                self.searchPrivateKey()
+        return 1
+
     def isValid(self):
         if self.privRSA is None and self.pubRSA is None:
             return 0
@@ -133,21 +141,20 @@
             self.bits = rsa.getKeyStrength(self.pubRSA)
             self.sha1 = getSHA1(pubKey)
             self.avail = KEY_PUBLIC
-            if self.directory is not None:
-                fn = os.path.join(self.directory, self.sha1 + '.key')
-            else:
-                fn = self.sha1 + '.key'
-            self.readPrivateKey(filename = fn, internal = 1)
+            self.searchPrivateKey()
 
     def readPrivateKey(self, filename = None, internal = 0):
         if filename is None:
             return 0
         if not os.path.exists(filename):
             return 0
-        self.Reset()
+        self.addSearchDirectory(os.path.dirname(filename))
+        if internal == 0:
+            self.Reset()
         self.privRSA = rsa.read(filename)
         if self.privRSA:
             self.privFn = filename
+            self.avail |= KEY_PRIVATE
             if internal == 0:
                 self.processKeys()
             return 1
@@ -166,19 +173,15 @@
         if self.pubRSA is None:
             return 0
         self.pubFn = filename
-        self.avail = KEY_PUBLIC
+        self.avail |= KEY_PUBLIC
 
         # Do we have access to matching private key?
         f = open(filename, "r")
         txt = f.read()
         f.close()
         self.sha1 = getSHA1(txt)
-        dirn = os.path.dirname(filename)
-        if os.path.exists(os.path.join(dirn, self.sha1 + '.key')):
-            self.privFn = os.path.join(dirn, self.sha1 + '.key')
-            self.privRSA = rsa.read(self.privFn)
-            self.avail = KEY_PUBLIC | KEY_PRIVATE
-
+        self.addSearchDirectory(os.path.dirname(filename))
+        self.searchPrivateKey()
         return 1
 
     def printToStdout(self):
@@ -206,4 +209,14 @@
         self.pubKey = ''
         self.sha1 = ''
         self.avail = KEY_NONE
+
+    # assuming we have a public key, do we have a private key available
+    # in the same directory or in a configured directory to search?
+    def searchPrivateKey(self):
+        if self.sha1 is None:
+            return
+        for d in self.directories:
+            ckFn = os.path.join(d, self.sha1 + '.key')
+            if os.path.exists(ckFn):
+                self.readPrivateKey(ckFn, 1)
 

Modified: labs/badca/tests/CSRTestCase.py
URL: http://svn.apache.org/viewvc/labs/badca/tests/CSRTestCase.py?rev=601628&r1=601627&r2=601628&view=diff
==============================================================================
--- labs/badca/tests/CSRTestCase.py (original)
+++ labs/badca/tests/CSRTestCase.py Wed Dec  5 22:18:13 2007
@@ -5,6 +5,11 @@
 class BaDCAKeysTestCase(unittest.TestCase):
     obj = None
 
+    def subjectCheck(self, which, val):
+        ckVal = self.obj.getSubject(which)
+        assert ckVal == val, "Subject '%s' failed! Returned %s instead of %s" \
+                       % (which, str(ckVal), val)
+
     def setUp(self):
         """ Called prior to every test """
         if self.obj is None:
@@ -34,6 +39,30 @@
         assert key.hasPublic(), "No public key found"
         assert key.hasPrivate() == 0, "Private key found when none should exist"
         assert key.bits == 2048, "Incorrect strength key returned"
+
+    def test04Info(self):
+        """ Test extraction of information from a CSR """
+        assert self.obj.readFromFile('tests/csr/test1.csr') == 1, \
+                                                "Failed to read the CSR"
+        self.subjectCheck('C', 'GB')
+        self.subjectCheck('CN', "asylum.zones.apache.org")
+        self.subjectCheck('Email', "dreid@apache.org")
+
+    def test05(self):
+        """ CSR with a private key available """
+        assert self.obj.readFromFile('tests/csr/test2.csr') == 1, \
+                                                "Failed to read the CSR"
+        key = self.obj.getKey()
+        assert key is not None, "Unable to get key object from CSR"
+        assert key.hasPublic(), "No public key found!"
+        assert key.addSearchDirectory('tests/keys') == 1, \
+                                 "Unable to add search directory to key"
+        assert key.hasPrivate(), "Unable to find private key"
+        assert key.hasPublic(), "No public key found!"
+        self.subjectCheck('C', 'GB')
+        self.subjectCheck('CN', "david reid")
+        self.subjectCheck('Email', "dreid@apache.org")
+
 
 if __name__ == "__main__":
     unittest.main()

Modified: labs/badca/tests/KeysTestCase.py
URL: http://svn.apache.org/viewvc/labs/badca/tests/KeysTestCase.py?rev=601628&r1=601627&r2=601628&view=diff
==============================================================================
--- labs/badca/tests/KeysTestCase.py (original)
+++ labs/badca/tests/KeysTestCase.py Wed Dec  5 22:18:13 2007
@@ -12,21 +12,21 @@
         else:
             self.Reset()
 
-    def testCreation(self):
+    def test01(self):
         """ Testing creation of keys of different strengths """
-        for r in ( 1024, 2048, 4096, 8192 ):
+        for r in ( 1024, 2048, 4096 ):
             assert self.obj.create(r) == 1, \
                             "Unable to create key of strength " + str(r)
             assert self.obj.bits == r, "Incorrect strength key created"
 
-    def testInvalidCreation(self):
+    def test02(self):
         """ Checking we accept only keys of correct strengths """
         self.assertRaises(Keys.rsa.keySize, self.obj.create, 512)
         self.assertRaises(Keys.rsa.keySize, self.obj.create, 9216)
         self.assertRaises(Keys.rsa.keySize, self.obj.create, 1023)
         self.assertRaises(Keys.rsa.keySize, self.obj.create, 8193)
 
-    def testReadPublicKey(self):
+    def test03(self):
         """ Test reading in a public key we have a private key available for """
         assert self.obj.readPublicKey('tests/keys/test1.public.key') == 1, \
                                              "Failed to read public key"
@@ -35,19 +35,23 @@
         assert self.obj.hasPrivate(), "No private key available"
 
 
-    def testReadPrivateKey(self):
+    def test04(self):
         """ Test reading of private key """
         assert self.obj.readPrivateKey('tests/keys/test2.key') == 1, \
-               "Failed to read private key"
+                                            "Failed to read private key"
         assert self.obj.isValid(), "Invalid key object created"
         assert self.obj.hasPublic(), "Public key not available??"
         assert self.obj.hasPrivate(), "Private key not available??"
 
-    def suite():
-        suite = unittest.TestSuite()
-        suite.addTest(BaDCAKeysTestCase("testCreation"))
-        suite.addTest(BaDCAKeysTestCase("testInvalidCreation"))
-        return suite
+    def test06(self):
+        """ Test directory support """
+        assert self.obj.readPublicKey('tests/keys/public/test3.public.key') == 1, \
+                                             "Failed to read public key"
+        assert self.obj.hasPublic(), "No public key available"
+        assert self.obj.hasPrivate() == 0, "Private key available???"
+        assert self.obj.addSearchDirectory('tests/keys/private') == 1, \
+                                 "Unable to add search directory to key"
+        assert self.obj.hasPrivate(), "Private key not available"
 
 if __name__ == "__main__":
     unittest.main()

Added: labs/badca/tests/csr/test2.csr
URL: http://svn.apache.org/viewvc/labs/badca/tests/csr/test2.csr?rev=601628&view=auto
==============================================================================
--- labs/badca/tests/csr/test2.csr (added)
+++ labs/badca/tests/csr/test2.csr Wed Dec  5 22:18:13 2007
@@ -0,0 +1,12 @@
+-----BEGIN CERTIFICATE REQUEST-----
+MIIBpjCCAQ8CAQAwZjELMAkGA1UEBhMCR0IxEzARBgNVBAgTClNvbWUtU3RhdGUx
+DDAKBgNVBAoTA0FTRjETMBEGA1UEAxMKZGF2aWQgcmVpZDEfMB0GCSqGSIb3DQEJ
+ARYQZHJlaWRAYXBhY2hlLm9yZzCBnzANBgkqhkiG9w0BAQEFAAOBjQAwgYkCgYEA
+p9fk0Rx4h3Q8NmWZq8EYlOSAUrAXthB0fbiIPUzpiL92B7LMlTEY7BLZ1IGsHt/q
+VOtjqAbTmmiN/tfz9yRnWf9j06j+Eaj4sfquGQAonX2IyD50RNS6pfQYzBzG4Ujd
+mFLuWzZlPJBxdpttq6tzzm+rA2Us2DuDTVi1AziTjRMCAwEAAaAAMA0GCSqGSIb3
+DQEBBQUAA4GBAEilHCg63kuhWjyznwZVc7F5Gi/QDzOCls7HgAQrS9W4woGnAFOD
+UQ5wYNj7DtyWzicHrUbEX3NfiR1eqg+onHScFpIQokXNt0skDJWZslfTO/ELPlAq
+eLjc5RZR2zxLKNs9RUlitRe2nwGTiIkJgmgtaM2f/quxfbw4zKYOJbF6
+-----END CERTIFICATE REQUEST-----
+

Added: labs/badca/tests/keys/private/2b466cb0b0190cf996721f9ebf7ec30bf456d4ea.key
URL: http://svn.apache.org/viewvc/labs/badca/tests/keys/private/2b466cb0b0190cf996721f9ebf7ec30bf456d4ea.key?rev=601628&view=auto
==============================================================================
--- labs/badca/tests/keys/private/2b466cb0b0190cf996721f9ebf7ec30bf456d4ea.key (added)
+++ labs/badca/tests/keys/private/2b466cb0b0190cf996721f9ebf7ec30bf456d4ea.key Wed Dec  5 22:18:13 2007
@@ -0,0 +1,15 @@
+-----BEGIN RSA PRIVATE KEY-----
+MIICXQIBAAKBgQCZCnzWB7kZpl12IotutL3vJOETDaSAsEz8tr2jVjjY+JSXhZ88
+sjrseqrgFY9LYJ43GS36ZIbCNPmL62jG07wuuZ9azX0ePmnlCG/9E2QkJgh4/cma
+r325ZpIxLVc4j+n6JVFrHGzFpPHDZqwRCS7Iw5aq/bmR2qdw5t65f+4x7wIDAQAB
+AoGAThYS0iYIpPTE1UUU/byM+NtJWC5q1BqgfYVER3Xz9YkIUDOmpxKNjCGe1E1L
+2vsGXziixBLMy4S6G1FXGbPpXOfrx8fuyI/kiAjmjeEZtYKTthVc0b9QB+VJD2r/
+JjnX2si5+JftLcbb5tCtha9AYd2odwdHftApIK4Dzw9ZZ8ECQQDH2pciPc5TElps
+gPfpuW0GfaINeUVt5s5OYpKap1/n/pD5T4EVkzmQU5oo1T5nPOLitBJBjrozQHoo
+8E4quOrhAkEAxAkh3gQLngqUNMKx9oG6weswiH7DorJPF1oUk/f+AiBeTdEq7NhT
+FVUPG6077HE8HrXWAXxp26kiL+YBSscGzwJAe+zBlov4qm5Q/8Mg9x7xyfqRSAWQ
+1csu1lAtVBTBVZv2zYFNK9G0+he9GeDQ5zdMAQNj5p7YOvuaRFRuQ/OSQQJBAJLS
++C+tvfm/mKMvk1S6c2McQLxqnSI1BHHMAQ+WNQPotFj6KXk8yHV4wJkNFY2QfePO
+cxun0C06LyXK0frsuBcCQQC70qkmOhvU6Ax73ZQvCr0gQxukxS9W37k9h+4zRyxC
+Dg+tlJiwONWQyEofAWL5nNsoD/NwNAECOZpS286OmZ/A
+-----END RSA PRIVATE KEY-----

Added: labs/badca/tests/keys/public/test3.public.key
URL: http://svn.apache.org/viewvc/labs/badca/tests/keys/public/test3.public.key?rev=601628&view=auto
==============================================================================
--- labs/badca/tests/keys/public/test3.public.key (added)
+++ labs/badca/tests/keys/public/test3.public.key Wed Dec  5 22:18:13 2007
@@ -0,0 +1,6 @@
+-----BEGIN PUBLIC KEY-----
+MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCZCnzWB7kZpl12IotutL3vJOET
+DaSAsEz8tr2jVjjY+JSXhZ88sjrseqrgFY9LYJ43GS36ZIbCNPmL62jG07wuuZ9a
+zX0ePmnlCG/9E2QkJgh4/cmar325ZpIxLVc4j+n6JVFrHGzFpPHDZqwRCS7Iw5aq
+/bmR2qdw5t65f+4x7wIDAQAB
+-----END PUBLIC KEY-----



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@labs.apache.org
For additional commands, e-mail: commits-help@labs.apache.org