blob: 1d412daa3fecc30439ff46d9c22a45df43b777ac [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\
19Arguments: self - The X509Extension object\n\
20 args - The argument tuple, should be empty\n\
21Returns: The critical field.\n\
22";
23
24static PyObject *
25crypto_X509Extension_get_critical(crypto_X509ExtensionObj *self, PyObject *args)
26{
27 if (!PyArg_ParseTuple(args, ":get_critical"))
28 return NULL;
29
30 return PyInt_FromLong(X509_EXTENSION_get_critical(self->x509_extension));
31}
32
33/*
34 * ADD_METHOD(name) expands to a correct PyMethodDef declaration
35 * { 'name', (PyCFunction)crypto_X509Extension_name, METH_VARARGS }
36 * for convenience
37 */
38#define ADD_METHOD(name) \
39{ #name, (PyCFunction)crypto_X509Extension_##name, METH_VARARGS, crypto_X509Extension_##name##_doc }
40static PyMethodDef crypto_X509Extension_methods[] =
41{
42 ADD_METHOD(get_critical),
43 { NULL, NULL }
44};
45#undef ADD_METHOD
46
47/*
48 * Constructor for X509Extension, never called by Python code directly
49 *
50 * Arguments: type_name - ???
51 * critical - ???
52 * value - ???
53 * Returns: The newly created X509Extension object
54 */
55crypto_X509ExtensionObj *
56crypto_X509Extension_New(char *type_name, int critical, char *value)
57{
58 crypto_X509ExtensionObj *self;
Jean-Paul Calderonee7db4b42008-12-31 13:39:24 -050059 char* value_with_critical = NULL;
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050060
61 self = PyObject_New(crypto_X509ExtensionObj, &crypto_X509Extension_Type);
62
Jean-Paul Calderonee7db4b42008-12-31 13:39:24 -050063 if (self == NULL) {
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050064 return NULL;
65 }
66
Jean-Paul Calderone391585f2008-12-31 14:36:31 -050067 self->dealloc = 0;
68
Jean-Paul Calderonee7db4b42008-12-31 13:39:24 -050069 /* There are other OpenSSL APIs which would let us pass in critical
70 * separately, but they're harder to use, and since value is already a pile
71 * of crappy junk smuggling a ton of utterly important structured data,
72 * what's the point of trying to avoid nasty stuff with strings? (However,
73 * X509V3_EXT_i2d in particular seems like it would be a better API to
74 * invoke. I do not know where to get the ext_struc it desires for its
75 * last parameter, though.) */
76 value_with_critical = malloc(strlen("critical,") + strlen(value) + 1);
77 if (critical) {
78 strcpy(value_with_critical, "critical,");
79 strcpy(value_with_critical + strlen("critical,"), value);
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050080 } else {
Jean-Paul Calderonee7db4b42008-12-31 13:39:24 -050081 strcpy(value_with_critical, value);
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050082 }
83
Jean-Paul Calderonee7db4b42008-12-31 13:39:24 -050084 self->x509_extension = X509V3_EXT_nconf(
85 NULL, NULL, type_name, value_with_critical);
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050086
Jean-Paul Calderonee7db4b42008-12-31 13:39:24 -050087 free(value_with_critical);
88
Jean-Paul Calderone391585f2008-12-31 14:36:31 -050089 if (!self->x509_extension) {
90 PyObject_Free(self);
91 exception_from_error_queue();
92 return NULL;
93 }
94
95 self->dealloc = 1;
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050096 return self;
97}
98
99/*
100 * Deallocate the memory used by the X509Extension object
101 *
102 * Arguments: self - The X509Extension object
103 * Returns: None
104 */
105static void
106crypto_X509Extension_dealloc(crypto_X509ExtensionObj *self)
107{
108 /* Sometimes we don't have to dealloc this */
109 if (self->dealloc)
110 X509_EXTENSION_free(self->x509_extension);
111
112 PyObject_Del(self);
113}
114
115/*
116 * Find attribute
117 *
118 * Arguments: self - The X509Extension object
119 * name - The attribute name
120 * Returns: A Python object for the attribute, or NULL if something
121 * went wrong.
122 */
123static PyObject *
124crypto_X509Extension_getattr(crypto_X509ExtensionObj *self, char *name)
125{
126 return Py_FindMethod(crypto_X509Extension_methods, (PyObject *)self, name);
127}
128
129/*
130 * Print a nice text representation of the certificate request.
131 */
132static PyObject *
133crypto_X509Extension_str(crypto_X509ExtensionObj *self)
134{
135 int str_len;
136 char *tmp_str;
137 PyObject *str;
138 BIO *bio = BIO_new(BIO_s_mem());
139
140 if (!X509V3_EXT_print(bio, self->x509_extension, 0, 0))
141 {
142 BIO_free(bio);
143 exception_from_error_queue();
144 return NULL;
145 }
146
147 str_len = BIO_get_mem_data(bio, &tmp_str);
148 str = PyString_FromStringAndSize(tmp_str, str_len);
149
150 BIO_free(bio);
151
152 return str;
153}
154
155PyTypeObject crypto_X509Extension_Type = {
156 PyObject_HEAD_INIT(NULL)
157 0,
158 "X509Extension",
159 sizeof(crypto_X509ExtensionObj),
160 0,
161 (destructor)crypto_X509Extension_dealloc,
162 NULL, /* print */
163 (getattrfunc)crypto_X509Extension_getattr,
164 NULL, /* setattr (setattrfunc)crypto_X509Name_setattr, */
165 NULL, /* compare */
166 NULL, /* repr */
167 NULL, /* as_number */
168 NULL, /* as_sequence */
169 NULL, /* as_mapping */
170 NULL, /* hash */
171 NULL, /* call */
172 (reprfunc)crypto_X509Extension_str /* str */
173};
174
175/*
176 * Initialize the X509Extension part of the crypto module
177 *
178 * Arguments: dict - The crypto module dictionary
179 * Returns: None
180 */
181int
182init_crypto_x509extension(PyObject *dict)
183{
184 crypto_X509Extension_Type.ob_type = &PyType_Type;
185 Py_INCREF(&crypto_X509Extension_Type);
186 PyDict_SetItemString(dict, "X509ExtensionType",
187 (PyObject *)&crypto_X509Extension_Type);
188 return 1;
189}
190