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/11/22 05:17:49 UTC
svn commit: r597293 - in /labs/badca: BaDCA/ BaDCA/Keys.py Makefile openssl/
openssl/rsamodule.c tests/3dca6bb1d753cad120cd2ed448c425b16cce492f.key
tests/dev_check.py
Author: dreid
Date: Wed Nov 21 20:17:46 2007
New Revision: 597293
URL: http://svn.apache.org/viewvc?rev=597293&view=rev
Log:
Add more functionality and start to make the keys module actually
work as expected.
By default we generate the filenames of the keys using the SHA1
hash of their public keys, which is now done automatically when keys
are created/opened.
Added:
labs/badca/Makefile
labs/badca/tests/3dca6bb1d753cad120cd2ed448c425b16cce492f.key
Modified:
labs/badca/BaDCA/ (props changed)
labs/badca/BaDCA/Keys.py
labs/badca/openssl/ (props changed)
labs/badca/openssl/rsamodule.c
labs/badca/tests/dev_check.py
Propchange: labs/badca/BaDCA/
------------------------------------------------------------------------------
--- svn:ignore (added)
+++ svn:ignore Wed Nov 21 20:17:46 2007
@@ -0,0 +1 @@
+*.pyc
Modified: labs/badca/BaDCA/Keys.py
URL: http://svn.apache.org/viewvc/labs/badca/BaDCA/Keys.py?rev=597293&r1=597292&r2=597293&view=diff
==============================================================================
--- labs/badca/BaDCA/Keys.py (original)
+++ labs/badca/BaDCA/Keys.py Wed Nov 21 20:17:46 2007
@@ -1,6 +1,5 @@
-# TODO - figure out how to do this stuff correctly during development
-# and when deployed.
import os, sys
+import hashlib
try:
import rsa
@@ -30,6 +29,19 @@
privFn = None # private key filename
pubFn = None # public key filename
bits = 0 # number of bits for the key
+ pubKey = '' # stored copy of the public key
+ sha1 = ''
+
+ def __init__(self, filename = None):
+ #if filename given, attempt to read the key and determine which
+ # we were given
+ if filename is None:
+ return
+ self.rsa = rsa.read(filename)
+ if self.rsa:
+ # find out how many bits!
+ self.bits = 0
+ self.privFn = filename
def create(self, bits = 2048):
print "Trying to create a new key!\n"
@@ -37,11 +49,68 @@
if self.rsa is None:
return 0
self.bits = bits
+ self.pubKey = rsa.getPublicKeyString(self.rsa)
+ self.getSHA1()
return 1
def getPublicKeyAsString(self):
if self.rsa is None:
return None
- return ''
+ if self.pubKey == '':
+ self.pubKey = rsa.getPublicKeyString(self.rsa)
+ return self.pubKey
+
+
+ def writeToDirectory(self, dir = '.'):
+ if self.privFn is None:
+ self.privFn = os.path.join(dir, self.sha1 + '.key')
+ if self.pubFn is None:
+ self.pubFn = os.path.join(dir, self.sha1 + '.public.key')
+
+ print "Writing private file to " + self.privFn + "\n"
+ priv = rsa.getPrivateKeyString(self.rsa)
+ f = open(self.privFn, "w")
+ f.write(priv)
+ f.close()
+
+ if self.pubKey == '':
+ self.pubKey = rsa.getPublicKeyString(self.rsa)
+
+ f = open(self.pubFn, "w")
+ f.write(self.pubKey)
+ f.close()
+
+ def readPrivateKey(self, filename = None):
+ if filename is None:
+ return 0
+ if not os.path.exists(filename):
+ return 0
+ self.Reset()
+ self.privFn = filename
+ self.rsa = rsa.read(filename)
+ if self.rsa:
+ self.pubKey = rsa.getPublicKeyString(self.rsa)
+ self.getSHA1()
+ return 1
+
+ def printToStdout(self):
+ print "Private key: " + self.privFn + "\n"
+ print "SHA1 : " + self.sha1 + "\n"
+
+ def getSHA1(self):
+ if self.rsa is None or self.pubKey == '':
+ return 0
+ s = hashlib.sha1()
+ s.update(self.pubKey)
+ self.sha1 = s.hexdigest()
+ return 1
+ def Reset(self):
+ self.rsa = None
+ self.rsa = None
+ self.privFn = None
+ self.pubFn = None
+ self.bits = 0
+ self.pubKey = ''
+ self.sha1 = ''
Added: labs/badca/Makefile
URL: http://svn.apache.org/viewvc/labs/badca/Makefile?rev=597293&view=auto
==============================================================================
--- labs/badca/Makefile (added)
+++ labs/badca/Makefile Wed Nov 21 20:17:46 2007
@@ -0,0 +1,5 @@
+# Simple Makefile to build the Python extensions
+
+all:
+ (cd openssl && python setup.py build) || exit 1
+
Propchange: labs/badca/openssl/
------------------------------------------------------------------------------
--- svn:ignore (added)
+++ svn:ignore Wed Nov 21 20:17:46 2007
@@ -0,0 +1,2 @@
+build
+
Modified: labs/badca/openssl/rsamodule.c
URL: http://svn.apache.org/viewvc/labs/badca/openssl/rsamodule.c?rev=597293&r1=597292&r2=597293&view=diff
==============================================================================
--- labs/badca/openssl/rsamodule.c (original)
+++ labs/badca/openssl/rsamodule.c Wed Nov 21 20:17:46 2007
@@ -69,6 +69,40 @@
return rv;
}
+/* This function is used when writing an RSA key to either a file
+ * or, if fn is NULL, stdout. The key can be either the private or
+ * public version depending on the which value passed in.
+ */
+static long getRSAString(RSA *rsa, int which, char **strPtr)
+{
+ BIO *out = NULL;
+ EVP_CIPHER *enc = NULL;
+ long sz = 0;
+ int rv;
+
+ if (!rsa)
+ return 0;
+
+ out = BIO_new(BIO_s_mem());
+ if (!out)
+ return 0;
+
+ if (which == PRIVATE)
+ rv = PEM_write_bio_RSAPrivateKey(out, rsa, enc, NULL, 0, NULL,
+ NULL);
+ else
+ rv = PEM_write_bio_RSA_PUBKEY(out,rsa);
+
+ /* Flush the BIO to make sure it's all written. */
+ (void)BIO_flush(out);
+
+ sz = BIO_get_mem_data(out, strPtr);
+ (void)BIO_set_close(out, BIO_NOCLOSE);
+ BIO_free(out);
+
+ return sz;
+}
+
/* This fucntion is called when an RSA pointer is finally freed by
* Python. This is done via setting this as the 2nd argument in
* PyCObject_FromVoidPtr().
@@ -173,11 +207,49 @@
return Py_BuildValue("i", i);
}
+static PyObject *
+getRSAPublicString(PyObject *self, PyObject *args)
+{
+ RSA *rsa = NULL;
+ long i = 0;
+ PyObject *temp = NULL;
+ char *ptr = NULL;
+
+ if (! PyArg_ParseTuple(args, "O", &temp)) {
+ return NULL;
+ }
+
+ rsa = (RSA *)PyCObject_AsVoidPtr(temp);
+ i = getRSAString(rsa, PUBLIC, &ptr);
+
+ return PyString_FromStringAndSize(ptr, i);
+}
+
+static PyObject *
+getRSAPrivateString(PyObject *self, PyObject *args)
+{
+ RSA *rsa = NULL;
+ long i = 0;
+ PyObject *temp = NULL;
+ char *ptr = NULL;
+
+ if (! PyArg_ParseTuple(args, "O", &temp)) {
+ return NULL;
+ }
+
+ rsa = (RSA *)PyCObject_AsVoidPtr(temp);
+ i = getRSAString(rsa, PRIVATE, &ptr);
+
+ return PyString_FromStringAndSize(ptr, i);
+}
+
static PyMethodDef RSAMethods[] = {
{ "genrsa", genrsa, METH_VARARGS, "Generate an RSA Key" },
{ "read", readRSA, METH_VARARGS, "Read an RSA private key" },
{ "writePrivate", writeRSAPrivate, METH_VARARGS, "Write a private RSA key to a file" },
{ "writePublic", writeRSAPublic, METH_VARARGS, "Write public key to file" },
+ { "getPrivateKeyString", getRSAPrivateString, METH_VARARGS, "Get private key as a string" },
+ { "getPublicKeyString", getRSAPublicString, METH_VARARGS, "Get public key as a string" },
{ NULL, NULL, 0, NULL },
};
Added: labs/badca/tests/3dca6bb1d753cad120cd2ed448c425b16cce492f.key
URL: http://svn.apache.org/viewvc/labs/badca/tests/3dca6bb1d753cad120cd2ed448c425b16cce492f.key?rev=597293&view=auto
==============================================================================
--- labs/badca/tests/3dca6bb1d753cad120cd2ed448c425b16cce492f.key (added)
+++ labs/badca/tests/3dca6bb1d753cad120cd2ed448c425b16cce492f.key Wed Nov 21 20:17:46 2007
@@ -0,0 +1,15 @@
+-----BEGIN RSA PRIVATE KEY-----
+MIICXAIBAAKBgQCgyWllmJj+JW4dODLEhckIsyim6tO1hmaL/6Ub/n6Sh0MY3IMY
+9Wc++Uv1WIePG7wW4q7fVgF50HTcGNq7yMqdfmk+46yGv5h1zql5zILrWtFSzySn
+1Y0wKl/fWfTaGB4ALidbLxqCFMFZSvgzi5WRacb6gQPAn88HuK4xAOi9twIDAQAB
+AoGAWCv7W1ZiV8Ro7ScbV//Rra6p1VcjQAFzDMTlY0OdX3dDiUersK0qsDL/v4yL
+0iOwcpBeu2SrrIRvAR+0G5RR30uctJz8t6HOIdvdlVf0Qm2zhHQ7lI2hRlc1IAhG
+PRUgcQUOykPRuvIASxTDT9H9vC8XEq194EbW0Td6C3srp4ECQQDOl7lARaSZlNrR
+qo0hmXmu3E1VhvgeTHEk25OoQrRBhnn1zPuFmiuh5rWE700kAg0Q/I5uAK2YCZnw
+rvIQpafBAkEAxz1NIAp79KNLl+LfNUZ/oFUcq3/VrHvgO8cherP4AyEg/TJEk1AJ
+feEJKEvW6qkBoW5bUQeT7STi3OUEwxCDdwJBAJzaOT6BIc2xZMnXcbvW6LpIbTbI
+eM2YQDc5t0wDCRTO4v4qmbUALqyHm73IaosyO3VwKzt9rE9IyuYgDY1yGIECQFXR
+tNfZTD063UPGKEjlXfHs11WT0bDjBtoY0vXscSa/7VrCfUFK11vOQ8iEqkUXmSk0
+xMapUfFy0vk8+IkhIs8CQHZBT6C6WX1q9B2xI8Y+dB67Eju9tXEIjcX9HS7StTt3
+UZo8yWn4r9po7EREB7+HF26JhFK+CuWWHrdGCfdz0Sg=
+-----END RSA PRIVATE KEY-----
Modified: labs/badca/tests/dev_check.py
URL: http://svn.apache.org/viewvc/labs/badca/tests/dev_check.py?rev=597293&r1=597292&r2=597293&view=diff
==============================================================================
--- labs/badca/tests/dev_check.py (original)
+++ labs/badca/tests/dev_check.py Wed Nov 21 20:17:46 2007
@@ -125,9 +125,14 @@
# working.
from BaDCA import Keys
-
myKey = Keys.RSAKey()
-myKey.create(4096)
+myKey.create(1024)
+s = myKey.getPublicKeyAsString()
+print "public key = \n" + s + "\n"
+
+myKey.writeToDirectory(testPath)
+myKey.readPrivateKey('tests/3dca6bb1d753cad120cd2ed448c425b16cce492f.key')
+myKey.printToStdout()
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@labs.apache.org
For additional commands, e-mail: commits-help@labs.apache.org