Index: src/crypto/x509.c =================================================================== --- src/crypto/x509.c (revision 58) +++ src/crypto/x509.c (working copy) @@ -688,6 +688,48 @@ return Py_None; } +static char crypto_X509_modulus_doc[] = "\n\ +Print the RSA / DSA key modulus.\n\ +\n\ +@return: modulus\n\ +"; + +static PyObject * +crypto_X509_modulus(crypto_X509Obj *self, PyObject *args) +{ + EVP_PKEY *pkey; + PyObject *str; + char *tmp_str; + int str_len; + BIO *bio = BIO_new(BIO_s_mem()); + + if (!PyArg_ParseTuple(args, ":modulus")) + { + BIO_free(bio); + return NULL; + } + + if ((pkey = X509_get_pubkey(self->x509)) == NULL) + { + BIO_free(bio); + exception_from_error_queue(crypto_Error); + return NULL; + } + + if (pkey->type == EVP_PKEY_RSA) + BN_print(bio, pkey->pkey.rsa->n); + else + if (pkey->type == EVP_PKEY_DSA) + BN_print(bio, pkey->pkey.dsa->pub_key); + + EVP_PKEY_free(pkey); + str_len = BIO_get_mem_data(bio, &tmp_str); + str = PyString_FromStringAndSize(tmp_str, str_len); + BIO_free(bio); + + return str; +} + /* * ADD_METHOD(name) expands to a correct PyMethodDef declaration * { 'name', (PyCFunction)crypto_X509_name, METH_VARARGS } @@ -718,6 +760,7 @@ ADD_METHOD(subject_name_hash), ADD_METHOD(digest), ADD_METHOD(add_extensions), + ADD_METHOD(modulus), { NULL, NULL } }; #undef ADD_METHOD Index: src/crypto/pkey.c =================================================================== --- src/crypto/pkey.c (revision 58) +++ src/crypto/pkey.c (working copy) @@ -106,7 +106,50 @@ return PyInt_FromLong(self->pkey->type); } +static char crypto_PKey_modulus_doc[] = "\n\ +Print the RSA / DSA key modulus.\n\ +\n\ +@return: modulus\n\ +"; +static PyObject * +crypto_PKey_modulus(crypto_PKeyObj *self, PyObject *args) +{ + EVP_PKEY *pkey; + PyObject *str; + char *tmp_str; + int str_len; + BIO *bio = BIO_new(BIO_s_mem()); + RSA *rsa=NULL; + DSA *dsa=NULL; + + if (!PyArg_ParseTuple(args, ":modulus")) + { + BIO_free(bio); + return NULL; + } + + if (self->pkey->type == EVP_PKEY_RSA) + { + rsa = EVP_PKEY_get1_RSA(self->pkey); + BN_print(bio, rsa->n); + } + else + if (self->pkey->type == EVP_PKEY_DSA) + { + dsa = EVP_PKEY_get1_DSA(pkey); + BN_print(bio, dsa->pub_key); + } + + str_len = BIO_get_mem_data(bio, &tmp_str); + str = PyString_FromStringAndSize(tmp_str, str_len); + BIO_free(bio); + + return str; +} + + + /* * ADD_METHOD(name) expands to a correct PyMethodDef declaration * { 'name', (PyCFunction)crypto_PKey_name, METH_VARARGS } @@ -119,6 +162,7 @@ ADD_METHOD(generate_key), ADD_METHOD(bits), ADD_METHOD(type), + ADD_METHOD(modulus), { NULL, NULL } }; #undef ADD_METHOD