summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJean-Paul Calderone <exarkun@divmod.com>2009-05-13 12:32:49 -0400
committerJean-Paul Calderone <exarkun@divmod.com>2009-05-13 12:32:49 -0400
commit8e6ce97e2d8a7d142d03b5b877575e75fd2074ef (patch)
tree62c215c2c94538a87bd12ce1c056e67d37713649
parenteecb19801a9bd04f0993440711fa495eca07408a (diff)
downloadpyopenssl-8e6ce97e2d8a7d142d03b5b877575e75fd2074ef.tar.gz
Apply pyOpenSSL_rsa_check.diff
-rw-r--r--OpenSSL/crypto/pkey.c34
-rw-r--r--OpenSSL/test/test_crypto.py3
2 files changed, 37 insertions, 0 deletions
diff --git a/OpenSSL/crypto/pkey.c b/OpenSSL/crypto/pkey.c
index 0a13aa3..cfedfda 100644
--- a/OpenSSL/crypto/pkey.c
+++ b/OpenSSL/crypto/pkey.c
@@ -107,6 +107,39 @@ crypto_PKey_type(crypto_PKeyObj *self, PyObject *args)
return PyLong_FromLong(self->pkey->type);
}
+static char crypto_PKey_check_doc[] = "\n\
+Check the consistency of an RSA private key.\n\
+\n\
+Arguments: self - The PKey object\n\
+ args - The Python argument tuple, should be empty\n\
+Returns: True if key is consistent. False if not.\n\
+";
+
+static PyObject *
+crypto_PKey_check(crypto_PKeyObj *self, PyObject *args)
+{
+ int r;
+ EVP_PKEY *pkey;
+
+ if (!PyArg_ParseTuple(args, ":check"))
+ return NULL;
+ pkey = self->pkey;
+ if(pkey == NULL)
+ return NULL;
+
+ if(pkey->type == EVP_PKEY_RSA) {
+ RSA *rsa;
+ rsa = EVP_PKEY_get1_RSA(pkey);
+ r = RSA_check_key(rsa);
+ if (r == 1)
+ return PyInt_FromLong(1L);
+ else
+ return PyInt_FromLong(0L);
+ } else {
+ PyErr_SetString( PyExc_TypeError, "key type unsupported");
+ return NULL;
+ }
+}
/*
* ADD_METHOD(name) expands to a correct PyMethodDef declaration
@@ -120,6 +153,7 @@ static PyMethodDef crypto_PKey_methods[] =
ADD_METHOD(generate_key),
ADD_METHOD(bits),
ADD_METHOD(type),
+ ADD_METHOD(check),
{ NULL, NULL }
};
#undef ADD_METHOD
diff --git a/OpenSSL/test/test_crypto.py b/OpenSSL/test/test_crypto.py
index 71da5c3..6c0ee31 100644
--- a/OpenSSL/test/test_crypto.py
+++ b/OpenSSL/test/test_crypto.py
@@ -564,6 +564,7 @@ class PKeyTests(TestCase):
key.generate_key(TYPE_RSA, bits)
self.assertEqual(key.type(), TYPE_RSA)
self.assertEqual(key.bits(), bits)
+ self.assertTrue(key.check())
def test_dsaGeneration(self):
@@ -579,6 +580,7 @@ class PKeyTests(TestCase):
key.generate_key(TYPE_DSA, bits)
self.assertEqual(key.type(), TYPE_DSA)
self.assertEqual(key.bits(), bits)
+ self.assertRaises(TypeError, key.check)
def test_regeneration(self):
@@ -2077,6 +2079,7 @@ class FunctionTests(TestCase):
L{dump_privatekey} writes a PEM, DER, and text.
"""
key = load_privatekey(FILETYPE_PEM, cleartextPrivateKeyPEM)
+ self.assertTrue(key.check())
dumped_pem = dump_privatekey(FILETYPE_PEM, key)
self.assertEqual(dumped_pem, cleartextPrivateKeyPEM)
dumped_der = dump_privatekey(FILETYPE_ASN1, key)