blob: ce3d95d1366e9995101ee3f276dd45276e48d41f [file] [log] [blame]
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001/*
2 * x509ext.c
3 *
Jean-Paul Calderone8b63d452008-03-21 18:31:12 -04004 * Copyright (C) Jean-Paul Calderone 2008, All rights reserved
5 *
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05006 * Export X.509 extension functions and data structures.
7 * See the file RATIONALE for a short explanation of why this module was written.
8 *
9 * @(#) $Id: x509ext.c,v 1.1 2002/07/09 13:34:46 martin Exp $
10 */
11
12#include <Python.h>
13#define crypto_MODULE
14#include "crypto.h"
15
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050016static char crypto_X509Extension_get_critical_doc[] = "\n\
17Returns the critical field of the X509Extension\n\
18\n\
Jean-Paul Calderone54bcc832009-05-27 14:06:48 -040019@return: The critical field.\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050020";
21
22static PyObject *
23crypto_X509Extension_get_critical(crypto_X509ExtensionObj *self, PyObject *args)
24{
25 if (!PyArg_ParseTuple(args, ":get_critical"))
26 return NULL;
27
28 return PyInt_FromLong(X509_EXTENSION_get_critical(self->x509_extension));
29}
30
Jean-Paul Calderonef8c5fab2008-12-31 15:53:48 -050031static char crypto_X509Extension_get_short_name_doc[] = "\n\
32Returns the short version of the type name of the X509Extension\n\
33\n\
Jean-Paul Calderone54bcc832009-05-27 14:06:48 -040034@return: The short type name.\n\
Jean-Paul Calderonef8c5fab2008-12-31 15:53:48 -050035";
36
37static PyObject *
38crypto_X509Extension_get_short_name(crypto_X509ExtensionObj *self, PyObject *args) {
39 ASN1_OBJECT *obj;
40 const char *extname;
41
42 if (!PyArg_ParseTuple(args, ":get_short_name")) {
43 return NULL;
44 }
45
46 /* Returns an internal pointer to x509_extension, not a copy */
47 obj = X509_EXTENSION_get_object(self->x509_extension);
48
49 extname = OBJ_nid2sn(OBJ_obj2nid(obj));
50 return PyString_FromString(extname);
51}
52
53
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050054/*
55 * ADD_METHOD(name) expands to a correct PyMethodDef declaration
56 * { 'name', (PyCFunction)crypto_X509Extension_name, METH_VARARGS }
57 * for convenience
58 */
59#define ADD_METHOD(name) \
60{ #name, (PyCFunction)crypto_X509Extension_##name, METH_VARARGS, crypto_X509Extension_##name##_doc }
61static PyMethodDef crypto_X509Extension_methods[] =
62{
63 ADD_METHOD(get_critical),
Jean-Paul Calderonef8c5fab2008-12-31 15:53:48 -050064 ADD_METHOD(get_short_name),
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050065 { NULL, NULL }
66};
67#undef ADD_METHOD
68
69/*
70 * Constructor for X509Extension, never called by Python code directly
71 *
72 * Arguments: type_name - ???
73 * critical - ???
74 * value - ???
75 * Returns: The newly created X509Extension object
76 */
77crypto_X509ExtensionObj *
Rick Dean47262da2009-07-08 16:17:17 -050078crypto_X509Extension_New(char *type_name, int critical, char *value,
79 crypto_X509Obj *subject, crypto_X509Obj *issuer)
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050080{
Jean-Paul Calderone2ee1e7c2008-12-31 14:58:38 -050081 X509V3_CTX ctx;
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050082 crypto_X509ExtensionObj *self;
Jean-Paul Calderonee7db4b42008-12-31 13:39:24 -050083 char* value_with_critical = NULL;
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050084
Jean-Paul Calderone2ee1e7c2008-12-31 14:58:38 -050085 /* We have no configuration database - but perhaps we should. Anyhow, the
86 * context is necessary for any extension which uses the r2i conversion
87 * method. That is, X509V3_EXT_nconf may segfault if passed a NULL ctx. */
Rick Dean47262da2009-07-08 16:17:17 -050088 X509V3_set_ctx(&ctx, NULL, NULL, NULL, NULL, 0);
Jean-Paul Calderone2ee1e7c2008-12-31 14:58:38 -050089 X509V3_set_ctx_nodb(&ctx);
Rick Dean47262da2009-07-08 16:17:17 -050090 if(subject)
91 ctx.subject_cert = subject->x509;
92 if(issuer)
93 ctx.issuer_cert = issuer->x509;
Jean-Paul Calderone2ee1e7c2008-12-31 14:58:38 -050094
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050095 self = PyObject_New(crypto_X509ExtensionObj, &crypto_X509Extension_Type);
96
Jean-Paul Calderonee7db4b42008-12-31 13:39:24 -050097 if (self == NULL) {
Jean-Paul Calderone2ee1e7c2008-12-31 14:58:38 -050098 goto error;
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050099 }
100
Jean-Paul Calderone391585f2008-12-31 14:36:31 -0500101 self->dealloc = 0;
102
Jean-Paul Calderonee7db4b42008-12-31 13:39:24 -0500103 /* There are other OpenSSL APIs which would let us pass in critical
104 * separately, but they're harder to use, and since value is already a pile
105 * of crappy junk smuggling a ton of utterly important structured data,
106 * what's the point of trying to avoid nasty stuff with strings? (However,
107 * X509V3_EXT_i2d in particular seems like it would be a better API to
108 * invoke. I do not know where to get the ext_struc it desires for its
109 * last parameter, though.) */
110 value_with_critical = malloc(strlen("critical,") + strlen(value) + 1);
Jean-Paul Calderone2ee1e7c2008-12-31 14:58:38 -0500111 if (!value_with_critical) {
112 goto critical_malloc_error;
113 }
114
Jean-Paul Calderonee7db4b42008-12-31 13:39:24 -0500115 if (critical) {
116 strcpy(value_with_critical, "critical,");
117 strcpy(value_with_critical + strlen("critical,"), value);
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500118 } else {
Jean-Paul Calderonee7db4b42008-12-31 13:39:24 -0500119 strcpy(value_with_critical, value);
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500120 }
121
Jean-Paul Calderonee7db4b42008-12-31 13:39:24 -0500122 self->x509_extension = X509V3_EXT_nconf(
Jean-Paul Calderone2ee1e7c2008-12-31 14:58:38 -0500123 NULL, &ctx, type_name, value_with_critical);
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500124
Jean-Paul Calderonee7db4b42008-12-31 13:39:24 -0500125 free(value_with_critical);
126
Jean-Paul Calderone391585f2008-12-31 14:36:31 -0500127 if (!self->x509_extension) {
Jean-Paul Calderone2ee1e7c2008-12-31 14:58:38 -0500128 goto nconf_error;
Jean-Paul Calderone391585f2008-12-31 14:36:31 -0500129 }
130
131 self->dealloc = 1;
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500132 return self;
Jean-Paul Calderone2ee1e7c2008-12-31 14:58:38 -0500133
134 nconf_error:
135 exception_from_error_queue();
136
137 critical_malloc_error:
Jean-Paul Calderonea96bfed2009-05-27 08:47:34 -0400138 Py_XDECREF(self);
Jean-Paul Calderone2ee1e7c2008-12-31 14:58:38 -0500139
140 error:
141 return NULL;
142
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500143}
144
Jean-Paul Calderone51e90662009-06-27 11:17:28 -0400145static char crypto_X509Extension_doc[] = "\n\
Rick Dean47262da2009-07-08 16:17:17 -0500146X509Extension(typename, critical, value[, subject][, issuer]) -> \n\
147 X509Extension instance\n\
Jean-Paul Calderone51e90662009-06-27 11:17:28 -0400148\n\
149@param typename: The name of the extension to create.\n\
150@type typename: C{str}\n\
151@param critical: A flag indicating whether this is a critical extension.\n\
152@param value: The value of the extension.\n\
153@type value: C{str}\n\
Rick Dean47262da2009-07-08 16:17:17 -0500154@param subject: Optional X509 cert to use as subject.\n\
155@type subject: C{X509}\n\
156@param issuer: Optional X509 cert to use as issuer.\n\
157@type issuer: C{X509}\n\
Jean-Paul Calderone51e90662009-06-27 11:17:28 -0400158@return: The X509Extension object\n\
159";
160
161static PyObject *
Rick Dean47262da2009-07-08 16:17:17 -0500162crypto_X509Extension_new(PyTypeObject *subtype, PyObject *args,
163 PyObject *kwargs) {
Jean-Paul Calderone51e90662009-06-27 11:17:28 -0400164 char *type_name, *value;
Rick Dean47262da2009-07-08 16:17:17 -0500165 int critical = 0;
166 crypto_X509Obj * subject = NULL;
167 crypto_X509Obj * issuer = NULL;
168 static char *kwlist[] = {"type_name", "critical", "value", "subject",
169 "issuer", NULL};
Jean-Paul Calderone51e90662009-06-27 11:17:28 -0400170
Rick Dean47262da2009-07-08 16:17:17 -0500171 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "sis|O!O!:X509Extension",
172 kwlist, &type_name, &critical, &value,
173 &crypto_X509_Type, &subject,
174 &crypto_X509_Type, &issuer )) {
Jean-Paul Calderone51e90662009-06-27 11:17:28 -0400175 return NULL;
176 }
177
Rick Dean47262da2009-07-08 16:17:17 -0500178 return (PyObject *)crypto_X509Extension_New(type_name, critical, value,
179 subject, issuer);
Jean-Paul Calderone51e90662009-06-27 11:17:28 -0400180}
181
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500182/*
183 * Deallocate the memory used by the X509Extension object
184 *
185 * Arguments: self - The X509Extension object
186 * Returns: None
187 */
188static void
189crypto_X509Extension_dealloc(crypto_X509ExtensionObj *self)
190{
191 /* Sometimes we don't have to dealloc this */
192 if (self->dealloc)
193 X509_EXTENSION_free(self->x509_extension);
194
195 PyObject_Del(self);
196}
197
198/*
199 * Find attribute
200 *
201 * Arguments: self - The X509Extension object
202 * name - The attribute name
203 * Returns: A Python object for the attribute, or NULL if something
204 * went wrong.
205 */
206static PyObject *
207crypto_X509Extension_getattr(crypto_X509ExtensionObj *self, char *name)
208{
209 return Py_FindMethod(crypto_X509Extension_methods, (PyObject *)self, name);
210}
211
212/*
213 * Print a nice text representation of the certificate request.
214 */
215static PyObject *
216crypto_X509Extension_str(crypto_X509ExtensionObj *self)
217{
218 int str_len;
219 char *tmp_str;
220 PyObject *str;
221 BIO *bio = BIO_new(BIO_s_mem());
222
223 if (!X509V3_EXT_print(bio, self->x509_extension, 0, 0))
224 {
225 BIO_free(bio);
226 exception_from_error_queue();
227 return NULL;
228 }
229
230 str_len = BIO_get_mem_data(bio, &tmp_str);
231 str = PyString_FromStringAndSize(tmp_str, str_len);
232
233 BIO_free(bio);
234
235 return str;
236}
237
238PyTypeObject crypto_X509Extension_Type = {
239 PyObject_HEAD_INIT(NULL)
240 0,
241 "X509Extension",
242 sizeof(crypto_X509ExtensionObj),
243 0,
244 (destructor)crypto_X509Extension_dealloc,
245 NULL, /* print */
246 (getattrfunc)crypto_X509Extension_getattr,
247 NULL, /* setattr (setattrfunc)crypto_X509Name_setattr, */
248 NULL, /* compare */
249 NULL, /* repr */
250 NULL, /* as_number */
251 NULL, /* as_sequence */
252 NULL, /* as_mapping */
253 NULL, /* hash */
254 NULL, /* call */
Jean-Paul Calderone51e90662009-06-27 11:17:28 -0400255 (reprfunc)crypto_X509Extension_str, /* str */
256 NULL, /* getattro */
257 NULL, /* setattro */
258 NULL, /* as_buffer */
259 Py_TPFLAGS_DEFAULT,
260 crypto_X509Extension_doc, /* doc */
261 NULL, /* traverse */
262 NULL, /* clear */
263 NULL, /* tp_richcompare */
264 0, /* tp_weaklistoffset */
265 NULL, /* tp_iter */
266 NULL, /* tp_iternext */
267 crypto_X509Extension_methods, /* tp_methods */
268 NULL, /* tp_members */
269 NULL, /* tp_getset */
270 NULL, /* tp_base */
271 NULL, /* tp_dict */
272 NULL, /* tp_descr_get */
273 NULL, /* tp_descr_set */
274 0, /* tp_dictoffset */
275 NULL, /* tp_init */
276 NULL, /* tp_alloc */
277 crypto_X509Extension_new, /* tp_new */
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500278};
279
280/*
281 * Initialize the X509Extension part of the crypto module
282 *
Jean-Paul Calderone51e90662009-06-27 11:17:28 -0400283 * Arguments: dict - The crypto module
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500284 * Returns: None
285 */
286int
Jean-Paul Calderone51e90662009-06-27 11:17:28 -0400287init_crypto_x509extension(PyObject *module)
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500288{
Jean-Paul Calderone51e90662009-06-27 11:17:28 -0400289 if (PyType_Ready(&crypto_X509Extension_Type) < 0) {
290 return 0;
291 }
292
293 if (PyModule_AddObject(module, "X509Extension",
294 (PyObject *)&crypto_X509Extension_Type) != 0) {
295 return 0;
296 }
297
298 if (PyModule_AddObject(module, "X509ExtensionType",
299 (PyObject *)&crypto_X509Extension_Type) != 0) {
300 return 0;
301 }
302
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500303 return 1;
304}