labs-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dr...@apache.org
Subject svn commit: r601691 - in /labs/badca: BaDCA/CSRs.py openssl/csrmodule.c tests/CSRTestCase.py tests/runTests.py
Date Thu, 06 Dec 2007 10:50:37 GMT
Author: dreid
Date: Thu Dec  6 02:50:36 2007
New Revision: 601691

URL: http://svn.apache.org/viewvc?rev=601691&view=rev
Log:
Fix an error in runTests.py pointed out by Mads Toftum
Add test to create a CSR
Fix a couple of small issues with the csr extension

This change gets us to a point whereby we can now create
CSR's. Next up is certificates.


Modified:
    labs/badca/BaDCA/CSRs.py
    labs/badca/openssl/csrmodule.c
    labs/badca/tests/CSRTestCase.py
    labs/badca/tests/runTests.py

Modified: labs/badca/BaDCA/CSRs.py
URL: http://svn.apache.org/viewvc/labs/badca/BaDCA/CSRs.py?rev=601691&r1=601690&r2=601691&view=diff
==============================================================================
--- labs/badca/BaDCA/CSRs.py (original)
+++ labs/badca/BaDCA/CSRs.py Thu Dec  6 02:50:36 2007
@@ -16,6 +16,7 @@
     csr = None
     sha1 = None
     rKey = None
+    fn = None
 
     def __init__(self, filename = None):
         if filename is not None:
@@ -24,8 +25,11 @@
     def getKey(self):
         return self.rKey
 
-    def setPrivateKey(self, thekey):
-        self.pKey = thekey
+    def setKey(self, thekey):
+        self.rKey = thekey
+
+    def getFilename(self):
+        return self.fn
 
     def readFromFile(self, filename):
         if filename is None or filename == '':
@@ -65,15 +69,17 @@
             self.info['subject'] = {}
             sub = self.info['subject']
         sub[which] = value
+        return 1
 
     def createCSR(self):
-        if self.pKey is None:
-            print "No key available, unable to create!"
+        # Should we set exceptions here???
+        if self.rKey is None:
+            return 0
+        if not self.rKey.hasPrivate():
             return 0
-        if not self.pKey.hasPrivateKey():
-            print "Private key required to create a CSR"
+        self.csr = csr.create(self.rKey.privRSA, self.info)
+        if self.csr is None:
             return 0
-        self.csr = csr.create(self.pKey.privRSA, self.info)
         return 1
 
     def asString(self):
@@ -85,16 +91,18 @@
     def writeToDirectory(self, dir):
         if self.csr is None:
             return 0
-        thestr = csr.asString(self.req)
+        thestr = csr.asString(self.csr)
         if self.sha1 is None:
             self.sha1 = getSHA1(thestr)
-        fn = os.path.join(dir, self.sha1 + '.csr')
+        self.fn = os.path.join(dir, self.sha1 + '.csr')
 
-        f = open(fn, "w")
-        f.write(thestr)
-        f.close()
+        f = open(self.fn, "w")
+        if f is not None:
+            f.write(thestr)
+            f.close()
+            return 1
 
-        return 1
+        return 0
 
     def printToStdout(self):
         print "CSR Details\n===========\n"

Modified: labs/badca/openssl/csrmodule.c
URL: http://svn.apache.org/viewvc/labs/badca/openssl/csrmodule.c?rev=601691&r1=601690&r2=601691&view=diff
==============================================================================
--- labs/badca/openssl/csrmodule.c (original)
+++ labs/badca/openssl/csrmodule.c Thu Dec  6 02:50:36 2007
@@ -31,10 +31,14 @@
         char *key = PyString_AsString(pKey);
         char *val = PyString_AsString(pValue);
 
-        if ((nid = OBJ_txt2nid(key)) == NID_undef &&
-            (nid = OBJ_sn2nid(key)) == NID_undef) {
-            printf("Failed to find key %s\n", key);
-            continue;
+        if (strstr(key, "Email")) {
+            nid = NID_pkcs9_emailAddress;
+        } else {
+            if ((nid = OBJ_txt2nid(key)) == NID_undef &&
+                (nid = OBJ_sn2nid(key)) == NID_undef) {
+                printf("Failed to find key %s\n", key);
+                continue;
+            }
         }
 
         X509_NAME_add_entry_by_NID(subj, nid, chtype, 
@@ -255,8 +259,10 @@
     if (! PyArg_ParseTuple(args, "OO", &tmp, &dict))
         return NULL;
     rsa = (RSA *)PyCObject_AsVoidPtr(tmp);
-    if (!rsa || !dict)
+    if (!rsa || !dict || RSA_check_key(rsa) != 1) {
+        PyErr_SetString(PyExc_ValueError, "Invalid RSA key supplied");
         return NULL;
+    }
 
 /*    if (tmp = PyDict_GetItem(dict, Py_BuildValue("s", "chtype"))
 */

Modified: labs/badca/tests/CSRTestCase.py
URL: http://svn.apache.org/viewvc/labs/badca/tests/CSRTestCase.py?rev=601691&r1=601690&r2=601691&view=diff
==============================================================================
--- labs/badca/tests/CSRTestCase.py (original)
+++ labs/badca/tests/CSRTestCase.py Thu Dec  6 02:50:36 2007
@@ -1,3 +1,4 @@
+import os
 import unittest
 
 from BaDCA import Keys, CSRs
@@ -10,6 +11,10 @@
         assert ckVal == val, "Subject '%s' failed! Returned %s instead of %s" \
                        % (which, str(ckVal), val)
 
+    def subjectSet(self, which, val):
+        assert self.obj.setSubject(which, val) == 1, \
+               "Failed to set subject '%s' to value '%s'" % (which, val)
+
     def setUp(self):
         """ Called prior to every test """
         if self.obj is None:
@@ -63,6 +68,24 @@
         self.subjectCheck('CN', "david reid")
         self.subjectCheck('Email', "dreid@apache.org")
 
+
+    def test06(self):
+        """ Create a CSR """
+        key = Keys.RSAKey()
+        key.addSearchDirectory('tests/keys/private')
+        key.readPublicKey('tests/keys/public/test3.public.key')
+        assert key.hasPrivate(), "Failed to get private key"
+        self.subjectSet('C', 'GB')
+        self.subjectSet('CN', 'test06')
+        self.subjectSet('Email', 'test@test.com')
+        self.obj.setKey(key)
+        assert self.obj.createCSR() == 1, "Error creating CSR"
+        assert self.obj.writeToDirectory('tests/csr') == 1, \
+                                "Unable to write to the temporary file!"
+        fn = self.obj.getFilename()
+        assert os.path.exists(fn), "File was not created"
+        os.unlink(fn)
+        assert os.path.exists(fn) == 0, "File was not removed cleanly"
 
 if __name__ == "__main__":
     unittest.main()

Modified: labs/badca/tests/runTests.py
URL: http://svn.apache.org/viewvc/labs/badca/tests/runTests.py?rev=601691&r1=601690&r2=601691&view=diff
==============================================================================
--- labs/badca/tests/runTests.py (original)
+++ labs/badca/tests/runTests.py Thu Dec  6 02:50:36 2007
@@ -7,7 +7,7 @@
 
 import unittest
 
-class badcaTester():
+class badcaTester:
     directory = None
     fileMatcher = None
     loader = unittest.defaultTestLoader



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


Mime
View raw message