Add X509ExtensionType.get_short_name
diff --git a/ChangeLog b/ChangeLog
index bd2e7f1..75a9745 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,10 @@
2008-12-31 Jean-Paul Calderone <exarkun@twistedmatrix.com>
+ * src/crypto/x509ext.c, test/test_crypto.py: Add the get_short_name
+ method to X509Extension based on patch from Alex Stapleton.
+
+2008-12-31 Jean-Paul Calderone <exarkun@twistedmatrix.com>
+
* src/crypto/x509ext.c, test/test_crypto.py: Fix X509Extension so
that it is possible to instantiate extensions which use s2i or r2i
instead of v2i (an extremely obscure extension implementation
diff --git a/doc/pyOpenSSL.tex b/doc/pyOpenSSL.tex
index 46318ea..ebd08ff 100644
--- a/doc/pyOpenSSL.tex
+++ b/doc/pyOpenSSL.tex
@@ -534,12 +534,16 @@
\subsubsection{X509Extension objects \label{openssl-509ext}}
-X509Extension objects currently only have one method:
+X509Extension objects have several methods:
\begin{methoddesc}[X509Extension]{get_critical}{}
Return the critical field of the extension object.
\end{methoddesc}
+\begin{methoddesc}[X509Extension]{get_short_name}{}
+Return the short type name of the extension object.
+\end{methoddesc}
+
\subsubsection{NetscapeSPKI objects \label{openssl-netscape-spki}}
NetscapeSPKI objects have the following methods:
diff --git a/src/crypto/x509ext.c b/src/crypto/x509ext.c
index 53dc48a..3b3c814 100644
--- a/src/crypto/x509ext.c
+++ b/src/crypto/x509ext.c
@@ -30,6 +30,31 @@
return PyInt_FromLong(X509_EXTENSION_get_critical(self->x509_extension));
}
+static char crypto_X509Extension_get_short_name_doc[] = "\n\
+Returns the short version of the type name of the X509Extension\n\
+\n\
+Arguments: self - The X509Extension object\n\
+ args - The argument tuple, should be empty\n\
+Returns: The short type name.\n\
+";
+
+static PyObject *
+crypto_X509Extension_get_short_name(crypto_X509ExtensionObj *self, PyObject *args) {
+ ASN1_OBJECT *obj;
+ const char *extname;
+
+ if (!PyArg_ParseTuple(args, ":get_short_name")) {
+ return NULL;
+ }
+
+ /* Returns an internal pointer to x509_extension, not a copy */
+ obj = X509_EXTENSION_get_object(self->x509_extension);
+
+ extname = OBJ_nid2sn(OBJ_obj2nid(obj));
+ return PyString_FromString(extname);
+}
+
+
/*
* ADD_METHOD(name) expands to a correct PyMethodDef declaration
* { 'name', (PyCFunction)crypto_X509Extension_name, METH_VARARGS }
@@ -40,6 +65,7 @@
static PyMethodDef crypto_X509Extension_methods[] =
{
ADD_METHOD(get_critical),
+ ADD_METHOD(get_short_name),
{ NULL, NULL }
};
#undef ADD_METHOD
diff --git a/test/test_crypto.py b/test/test_crypto.py
index 2ddb84d..902bde1 100644
--- a/test/test_crypto.py
+++ b/test/test_crypto.py
@@ -133,6 +133,17 @@
self.assertFalse(ext.get_critical())
+ def test_get_short_name(self):
+ """
+ L{X509ExtensionType.get_short_name} returns a string giving the short
+ type name of the extension.
+ """
+ ext = X509Extension('basicConstraints', True, 'CA:true')
+ self.assertEqual(ext.get_short_name(), 'basicConstraints')
+ ext = X509Extension('nsComment', True, 'foo bar')
+ self.assertEqual(ext.get_short_name(), 'nsComment')
+
+
class PKeyTests(TestCase, _Python23TestCaseHelper):
"""