Index: test/test_crypto.py =================================================================== --- test/test_crypto.py (revision 353) +++ test/test_crypto.py (working copy) @@ -234,6 +234,7 @@ class PKeyTests(TestCase, _Python23TestC 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): @@ -249,6 +250,7 @@ class PKeyTests(TestCase, _Python23TestC 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): @@ -775,6 +777,7 @@ class FunctionTests(TestCase, _Python23T 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) Index: src/crypto/pkey.c =================================================================== --- src/crypto/pkey.c (revision 353) +++ src/crypto/pkey.c (working copy) @@ -112,6 +112,39 @@ crypto_PKey_type(crypto_PKeyObj *self, P return PyInt_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 @@ -125,6 +158,7 @@ static PyMethodDef crypto_PKey_methods[] ADD_METHOD(generate_key), ADD_METHOD(bits), ADD_METHOD(type), + ADD_METHOD(check), { NULL, NULL } }; #undef ADD_METHOD