blob: 4f38175b507eed0f6fae3970b39c4f499a26865b [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 *
Jean-Paul Calderone5ae32fb2009-07-17 15:03:31 -040078crypto_X509Extension_New(char *type_name, int critical, char *value,
Rick Dean47262da2009-07-08 16:17:17 -050079 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 Calderone5ae32fb2009-07-17 15:03:31 -040085 /*
86 * Initialize most of the fields to NULL.
87 */
88 X509V3_set_ctx(&ctx, NULL, NULL, NULL, NULL, 0);
89
Jean-Paul Calderone2ee1e7c2008-12-31 14:58:38 -050090 /* We have no configuration database - but perhaps we should. Anyhow, the
91 * context is necessary for any extension which uses the r2i conversion
Jean-Paul Calderone5ae32fb2009-07-17 15:03:31 -040092 * method. That is, X509V3_EXT_nconf may segfault if passed a NULL ctx.
93 */
Jean-Paul Calderone2ee1e7c2008-12-31 14:58:38 -050094 X509V3_set_ctx_nodb(&ctx);
Jean-Paul Calderone5ae32fb2009-07-17 15:03:31 -040095
96 /*
97 * Initialize the subject and issuer, if appropriate. ctx is a local, and
98 * as far as I can tell none of the X509V3_* APIs invoked here steal any
99 * references, so no need to incref subject or issuer.
100 */
101 if (subject) {
Rick Dean47262da2009-07-08 16:17:17 -0500102 ctx.subject_cert = subject->x509;
Jean-Paul Calderone5ae32fb2009-07-17 15:03:31 -0400103 }
104
105 if (issuer) {
Rick Dean47262da2009-07-08 16:17:17 -0500106 ctx.issuer_cert = issuer->x509;
Jean-Paul Calderone5ae32fb2009-07-17 15:03:31 -0400107 }
Jean-Paul Calderone2ee1e7c2008-12-31 14:58:38 -0500108
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500109 self = PyObject_New(crypto_X509ExtensionObj, &crypto_X509Extension_Type);
110
Jean-Paul Calderonee7db4b42008-12-31 13:39:24 -0500111 if (self == NULL) {
Jean-Paul Calderone2ee1e7c2008-12-31 14:58:38 -0500112 goto error;
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500113 }
114
Jean-Paul Calderone391585f2008-12-31 14:36:31 -0500115 self->dealloc = 0;
116
Jean-Paul Calderonee7db4b42008-12-31 13:39:24 -0500117 /* There are other OpenSSL APIs which would let us pass in critical
118 * separately, but they're harder to use, and since value is already a pile
119 * of crappy junk smuggling a ton of utterly important structured data,
120 * what's the point of trying to avoid nasty stuff with strings? (However,
121 * X509V3_EXT_i2d in particular seems like it would be a better API to
122 * invoke. I do not know where to get the ext_struc it desires for its
123 * last parameter, though.) */
124 value_with_critical = malloc(strlen("critical,") + strlen(value) + 1);
Jean-Paul Calderone2ee1e7c2008-12-31 14:58:38 -0500125 if (!value_with_critical) {
126 goto critical_malloc_error;
127 }
128
Jean-Paul Calderonee7db4b42008-12-31 13:39:24 -0500129 if (critical) {
130 strcpy(value_with_critical, "critical,");
131 strcpy(value_with_critical + strlen("critical,"), value);
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500132 } else {
Jean-Paul Calderonee7db4b42008-12-31 13:39:24 -0500133 strcpy(value_with_critical, value);
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500134 }
135
Jean-Paul Calderonee7db4b42008-12-31 13:39:24 -0500136 self->x509_extension = X509V3_EXT_nconf(
Jean-Paul Calderone2ee1e7c2008-12-31 14:58:38 -0500137 NULL, &ctx, type_name, value_with_critical);
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500138
Jean-Paul Calderonee7db4b42008-12-31 13:39:24 -0500139 free(value_with_critical);
140
Jean-Paul Calderone391585f2008-12-31 14:36:31 -0500141 if (!self->x509_extension) {
Jean-Paul Calderone2ee1e7c2008-12-31 14:58:38 -0500142 goto nconf_error;
Jean-Paul Calderone391585f2008-12-31 14:36:31 -0500143 }
144
145 self->dealloc = 1;
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500146 return self;
Jean-Paul Calderone2ee1e7c2008-12-31 14:58:38 -0500147
148 nconf_error:
149 exception_from_error_queue();
150
151 critical_malloc_error:
Jean-Paul Calderonea96bfed2009-05-27 08:47:34 -0400152 Py_XDECREF(self);
Jean-Paul Calderone2ee1e7c2008-12-31 14:58:38 -0500153
154 error:
155 return NULL;
156
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500157}
158
Jean-Paul Calderone51e90662009-06-27 11:17:28 -0400159static char crypto_X509Extension_doc[] = "\n\
Rick Dean47262da2009-07-08 16:17:17 -0500160X509Extension(typename, critical, value[, subject][, issuer]) -> \n\
161 X509Extension instance\n\
Jean-Paul Calderone51e90662009-06-27 11:17:28 -0400162\n\
163@param typename: The name of the extension to create.\n\
164@type typename: C{str}\n\
165@param critical: A flag indicating whether this is a critical extension.\n\
166@param value: The value of the extension.\n\
167@type value: C{str}\n\
Rick Dean47262da2009-07-08 16:17:17 -0500168@param subject: Optional X509 cert to use as subject.\n\
169@type subject: C{X509}\n\
170@param issuer: Optional X509 cert to use as issuer.\n\
171@type issuer: C{X509}\n\
Jean-Paul Calderone51e90662009-06-27 11:17:28 -0400172@return: The X509Extension object\n\
173";
174
175static PyObject *
Rick Dean47262da2009-07-08 16:17:17 -0500176crypto_X509Extension_new(PyTypeObject *subtype, PyObject *args,
177 PyObject *kwargs) {
Jean-Paul Calderone51e90662009-06-27 11:17:28 -0400178 char *type_name, *value;
Rick Dean47262da2009-07-08 16:17:17 -0500179 int critical = 0;
180 crypto_X509Obj * subject = NULL;
181 crypto_X509Obj * issuer = NULL;
182 static char *kwlist[] = {"type_name", "critical", "value", "subject",
183 "issuer", NULL};
Jean-Paul Calderone51e90662009-06-27 11:17:28 -0400184
Rick Dean47262da2009-07-08 16:17:17 -0500185 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "sis|O!O!:X509Extension",
186 kwlist, &type_name, &critical, &value,
187 &crypto_X509_Type, &subject,
188 &crypto_X509_Type, &issuer )) {
Jean-Paul Calderone51e90662009-06-27 11:17:28 -0400189 return NULL;
190 }
191
Rick Dean47262da2009-07-08 16:17:17 -0500192 return (PyObject *)crypto_X509Extension_New(type_name, critical, value,
193 subject, issuer);
Jean-Paul Calderone51e90662009-06-27 11:17:28 -0400194}
195
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500196/*
197 * Deallocate the memory used by the X509Extension object
198 *
199 * Arguments: self - The X509Extension object
200 * Returns: None
201 */
202static void
203crypto_X509Extension_dealloc(crypto_X509ExtensionObj *self)
204{
205 /* Sometimes we don't have to dealloc this */
206 if (self->dealloc)
207 X509_EXTENSION_free(self->x509_extension);
208
209 PyObject_Del(self);
210}
211
212/*
213 * Find attribute
214 *
215 * Arguments: self - The X509Extension object
216 * name - The attribute name
217 * Returns: A Python object for the attribute, or NULL if something
218 * went wrong.
219 */
220static PyObject *
221crypto_X509Extension_getattr(crypto_X509ExtensionObj *self, char *name)
222{
223 return Py_FindMethod(crypto_X509Extension_methods, (PyObject *)self, name);
224}
225
226/*
227 * Print a nice text representation of the certificate request.
228 */
229static PyObject *
230crypto_X509Extension_str(crypto_X509ExtensionObj *self)
231{
232 int str_len;
233 char *tmp_str;
234 PyObject *str;
235 BIO *bio = BIO_new(BIO_s_mem());
236
237 if (!X509V3_EXT_print(bio, self->x509_extension, 0, 0))
238 {
239 BIO_free(bio);
240 exception_from_error_queue();
241 return NULL;
242 }
243
244 str_len = BIO_get_mem_data(bio, &tmp_str);
245 str = PyString_FromStringAndSize(tmp_str, str_len);
246
247 BIO_free(bio);
248
249 return str;
250}
251
252PyTypeObject crypto_X509Extension_Type = {
253 PyObject_HEAD_INIT(NULL)
254 0,
255 "X509Extension",
256 sizeof(crypto_X509ExtensionObj),
257 0,
258 (destructor)crypto_X509Extension_dealloc,
259 NULL, /* print */
260 (getattrfunc)crypto_X509Extension_getattr,
261 NULL, /* setattr (setattrfunc)crypto_X509Name_setattr, */
262 NULL, /* compare */
263 NULL, /* repr */
264 NULL, /* as_number */
265 NULL, /* as_sequence */
266 NULL, /* as_mapping */
267 NULL, /* hash */
268 NULL, /* call */
Jean-Paul Calderone51e90662009-06-27 11:17:28 -0400269 (reprfunc)crypto_X509Extension_str, /* str */
270 NULL, /* getattro */
271 NULL, /* setattro */
272 NULL, /* as_buffer */
273 Py_TPFLAGS_DEFAULT,
274 crypto_X509Extension_doc, /* doc */
275 NULL, /* traverse */
276 NULL, /* clear */
277 NULL, /* tp_richcompare */
278 0, /* tp_weaklistoffset */
279 NULL, /* tp_iter */
280 NULL, /* tp_iternext */
281 crypto_X509Extension_methods, /* tp_methods */
282 NULL, /* tp_members */
283 NULL, /* tp_getset */
284 NULL, /* tp_base */
285 NULL, /* tp_dict */
286 NULL, /* tp_descr_get */
287 NULL, /* tp_descr_set */
288 0, /* tp_dictoffset */
289 NULL, /* tp_init */
290 NULL, /* tp_alloc */
291 crypto_X509Extension_new, /* tp_new */
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500292};
293
294/*
295 * Initialize the X509Extension part of the crypto module
296 *
Jean-Paul Calderone51e90662009-06-27 11:17:28 -0400297 * Arguments: dict - The crypto module
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500298 * Returns: None
299 */
300int
Jean-Paul Calderone51e90662009-06-27 11:17:28 -0400301init_crypto_x509extension(PyObject *module)
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500302{
Jean-Paul Calderone51e90662009-06-27 11:17:28 -0400303 if (PyType_Ready(&crypto_X509Extension_Type) < 0) {
304 return 0;
305 }
306
307 if (PyModule_AddObject(module, "X509Extension",
308 (PyObject *)&crypto_X509Extension_Type) != 0) {
309 return 0;
310 }
311
312 if (PyModule_AddObject(module, "X509ExtensionType",
313 (PyObject *)&crypto_X509Extension_Type) != 0) {
314 return 0;
315 }
316
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500317 return 1;
318}