blob: 259615e2fc878cf99b7b2d43797d3b683088b6c7 [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 Calderonee7db4b42008-12-31 13:39:24 -050067 /* There are other OpenSSL APIs which would let us pass in critical
68 * separately, but they're harder to use, and since value is already a pile
69 * of crappy junk smuggling a ton of utterly important structured data,
70 * what's the point of trying to avoid nasty stuff with strings? (However,
71 * X509V3_EXT_i2d in particular seems like it would be a better API to
72 * invoke. I do not know where to get the ext_struc it desires for its
73 * last parameter, though.) */
74 value_with_critical = malloc(strlen("critical,") + strlen(value) + 1);
75 if (critical) {
76 strcpy(value_with_critical, "critical,");
77 strcpy(value_with_critical + strlen("critical,"), value);
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050078 } else {
Jean-Paul Calderonee7db4b42008-12-31 13:39:24 -050079 strcpy(value_with_critical, value);
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050080 }
81
Jean-Paul Calderonee7db4b42008-12-31 13:39:24 -050082 self->x509_extension = X509V3_EXT_nconf(
83 NULL, NULL, type_name, value_with_critical);
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050084 self->dealloc = 1;
85
Jean-Paul Calderonee7db4b42008-12-31 13:39:24 -050086 free(value_with_critical);
87
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050088 return self;
89}
90
91/*
92 * Deallocate the memory used by the X509Extension object
93 *
94 * Arguments: self - The X509Extension object
95 * Returns: None
96 */
97static void
98crypto_X509Extension_dealloc(crypto_X509ExtensionObj *self)
99{
100 /* Sometimes we don't have to dealloc this */
101 if (self->dealloc)
102 X509_EXTENSION_free(self->x509_extension);
103
104 PyObject_Del(self);
105}
106
107/*
108 * Find attribute
109 *
110 * Arguments: self - The X509Extension object
111 * name - The attribute name
112 * Returns: A Python object for the attribute, or NULL if something
113 * went wrong.
114 */
115static PyObject *
116crypto_X509Extension_getattr(crypto_X509ExtensionObj *self, char *name)
117{
118 return Py_FindMethod(crypto_X509Extension_methods, (PyObject *)self, name);
119}
120
121/*
122 * Print a nice text representation of the certificate request.
123 */
124static PyObject *
125crypto_X509Extension_str(crypto_X509ExtensionObj *self)
126{
127 int str_len;
128 char *tmp_str;
129 PyObject *str;
130 BIO *bio = BIO_new(BIO_s_mem());
131
132 if (!X509V3_EXT_print(bio, self->x509_extension, 0, 0))
133 {
134 BIO_free(bio);
135 exception_from_error_queue();
136 return NULL;
137 }
138
139 str_len = BIO_get_mem_data(bio, &tmp_str);
140 str = PyString_FromStringAndSize(tmp_str, str_len);
141
142 BIO_free(bio);
143
144 return str;
145}
146
147PyTypeObject crypto_X509Extension_Type = {
148 PyObject_HEAD_INIT(NULL)
149 0,
150 "X509Extension",
151 sizeof(crypto_X509ExtensionObj),
152 0,
153 (destructor)crypto_X509Extension_dealloc,
154 NULL, /* print */
155 (getattrfunc)crypto_X509Extension_getattr,
156 NULL, /* setattr (setattrfunc)crypto_X509Name_setattr, */
157 NULL, /* compare */
158 NULL, /* repr */
159 NULL, /* as_number */
160 NULL, /* as_sequence */
161 NULL, /* as_mapping */
162 NULL, /* hash */
163 NULL, /* call */
164 (reprfunc)crypto_X509Extension_str /* str */
165};
166
167/*
168 * Initialize the X509Extension part of the crypto module
169 *
170 * Arguments: dict - The crypto module dictionary
171 * Returns: None
172 */
173int
174init_crypto_x509extension(PyObject *dict)
175{
176 crypto_X509Extension_Type.ob_type = &PyType_Type;
177 Py_INCREF(&crypto_X509Extension_Type);
178 PyDict_SetItemString(dict, "X509ExtensionType",
179 (PyObject *)&crypto_X509Extension_Type);
180 return 1;
181}
182