#include <Python.h>
#define crypto_MODULE
#include "crypto.h"
static char *CVSid = "@(#) $Id: x509ext.c,v 1.2 2004/09/23 14:25:28 murata Exp $";
static char crypto_X509Extension_get_critical_doc[] = "\n\
Returns the critical field of the X509Extension\n\
\n\
Arguments: self - The X509Extension object\n\
args - The argument tuple, should be empty\n\
Returns: The critical field.\n\
";
static PyObject *
crypto_X509Extension_get_critical(crypto_X509ExtensionObj *self, PyObject *args)
{
if (!PyArg_ParseTuple(args, ":get_critical"))
return NULL;
return PyInt_FromLong(X509_EXTENSION_get_critical(self->x509_extension));
}
#define ADD_METHOD(name) \
{ #name, (PyCFunction)crypto_X509Extension_##name, METH_VARARGS, crypto_X509Extension_##name##_doc }
static PyMethodDef crypto_X509Extension_methods[] =
{
ADD_METHOD(get_critical),
{ NULL, NULL }
};
#undef ADD_METHOD
crypto_X509ExtensionObj *
crypto_X509Extension_New(char *type_name, int critical, char *value)
{
crypto_X509ExtensionObj *self;
int ext_len, ext_nid;
unsigned char *ext_der, *p;
X509V3_EXT_METHOD *ext_method = NULL;
ASN1_OCTET_STRING *ext_oct;
STACK_OF(CONF_VALUE) *nval;
void * ext_struct;
X509_EXTENSION *extension = NULL;
self = PyObject_New(crypto_X509ExtensionObj, &crypto_X509Extension_Type);
if (self == NULL)
return NULL;
if ((ext_nid = OBJ_sn2nid(type_name)) == NID_undef)
{
PyErr_SetString(PyExc_ValueError, "Unknown extension name");
return NULL;
}
if (!(ext_method = X509V3_EXT_get_nid(ext_nid)))
{
PyErr_SetString(PyExc_ValueError, "Unknown extension");
return NULL;
}
if (!ext_method->v2i)
{
PyErr_SetString(PyExc_ValueError, "Can't initialize exception");
return NULL;
}
nval = X509V3_parse_list(value);
if (!nval)
{
PyErr_SetString(PyExc_ValueError, "Invalid extension string");
return NULL;
}
if(!(ext_struct = ext_method->v2i(ext_method, NULL, nval))) {
exception_from_error_queue();
return NULL;
}
sk_CONF_VALUE_pop_free(nval, X509V3_conf_free);
ext_len = ext_method->i2d(ext_struct, NULL);
if(!(ext_der = malloc(ext_len))) {
PyErr_SetString(PyExc_MemoryError, "Could not allocate memory");
return NULL;
}
p = ext_der;
ext_method->i2d(ext_struct, &p);
if(!(ext_oct = M_ASN1_OCTET_STRING_new())) {
exception_from_error_queue();
return NULL;
}
ext_oct->data = ext_der;
ext_oct->length = ext_len;
extension = X509_EXTENSION_create_by_NID(NULL, ext_nid, critical, ext_oct);
if (extension == NULL)
{
exception_from_error_queue();
M_ASN1_OCTET_STRING_free(ext_oct);
ext_method->ext_free(ext_struct);
return NULL;
}
M_ASN1_OCTET_STRING_free(ext_oct);
ext_method->ext_free(ext_struct);
self->x509_extension = extension;
self->dealloc = 1;
return self;
}
static void
crypto_X509Extension_dealloc(crypto_X509ExtensionObj *self)
{
if (self->dealloc)
X509_EXTENSION_free(self->x509_extension);
PyObject_Del(self);
}
static PyObject *
crypto_X509Extension_getattr(crypto_X509ExtensionObj *self, char *name)
{
return Py_FindMethod(crypto_X509Extension_methods, (PyObject *)self, name);
}
static PyObject *
crypto_X509Extension_str(crypto_X509ExtensionObj *self)
{
int str_len;
char *tmp_str;
PyObject *str;
BIO *bio = BIO_new(BIO_s_mem());
if (!X509V3_EXT_print(bio, self->x509_extension, 0, 0))
{
BIO_free(bio);
exception_from_error_queue();
return NULL;
}
str_len = BIO_get_mem_data(bio, &tmp_str);
str = PyString_FromStringAndSize(tmp_str, str_len);
BIO_free(bio);
return str;
}
PyTypeObject crypto_X509Extension_Type = {
PyObject_HEAD_INIT(NULL)
0,
"X509Extension",
sizeof(crypto_X509ExtensionObj),
0,
(destructor)crypto_X509Extension_dealloc,
NULL,
(getattrfunc)crypto_X509Extension_getattr,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
(reprfunc)crypto_X509Extension_str
};
int
init_crypto_x509extension(PyObject *dict)
{
crypto_X509Extension_Type.ob_type = &PyType_Type;
Py_INCREF(&crypto_X509Extension_Type);
PyDict_SetItemString(dict, "X509ExtensionType",
(PyObject *)&crypto_X509Extension_Type);
return 1;
}