blob: b9c02333ec5e78db896d4d476de1114fdaa53724 [file] [log] [blame]
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001/*
2 * x509name.c
3 *
4 * Copyright (C) AB Strakt 2001, All rights reserved
5 *
6 * X.509 Name handling, mostly thin wrapping.
7 * See the file RATIONALE for a short explanation of why this module was written.
8 *
9 * Reviewed 2001-07-23
10 */
11#include <Python.h>
12#define crypto_MODULE
13#include "crypto.h"
14
15static char *CVSid = "@(#) $Id: x509name.c,v 1.16 2003/01/09 17:08:32 martin Exp $";
16
17
18/*
19 * Constructor for X509Name, never called by Python code directly
20 *
21 * Arguments: name - A "real" X509_NAME object
22 * dealloc - Boolean value to specify whether the destructor should
23 * free the "real" X509_NAME object
24 * Returns: The newly created X509Name object
25 */
26crypto_X509NameObj *
27crypto_X509Name_New(X509_NAME *name, int dealloc)
28{
29 crypto_X509NameObj *self;
30
31 self = PyObject_GC_New(crypto_X509NameObj, &crypto_X509Name_Type);
32
33 if (self == NULL)
34 return NULL;
35
36 self->x509_name = name;
37 self->dealloc = dealloc;
38 self->parent_cert = NULL;
39
40 PyObject_GC_Track(self);
41 return self;
42}
43
44/*
45 * Return a name string given a X509_NAME object and a name identifier. Used
46 * by the getattr function.
47 *
48 * Arguments: name - The X509_NAME object
49 * nid - The name identifier
50 * Returns: The name as a Python string object
51 */
52static int
53get_name_by_nid(X509_NAME *name, int nid, char **utf8string)
54{
55 int entry_idx;
56 X509_NAME_ENTRY *entry;
57 ASN1_STRING *data;
58 int len;
59
60 if ((entry_idx = X509_NAME_get_index_by_NID(name, nid, -1)) == -1)
61 {
62 return 0;
63 }
64 entry = X509_NAME_get_entry(name, entry_idx);
65 data = X509_NAME_ENTRY_get_data(entry);
66 if ((len = ASN1_STRING_to_UTF8((unsigned char **)utf8string, data)) < 0)
67 {
68 exception_from_error_queue();
69 return -1;
70 }
71
72 return len;
73}
74
75/*
76 * Given a X509_NAME object and a name identifier, set the corresponding
77 * attribute to the given string. Used by the setattr function.
78 *
79 * Arguments: name - The X509_NAME object
80 * nid - The name identifier
81 * value - The string to set
82 * Returns: 0 for success, -1 on failure
83 */
84static int
85set_name_by_nid(X509_NAME *name, int nid, char *utf8string)
86{
87 X509_NAME_ENTRY *ne;
88 int i, entry_count, temp_nid;
89
90 /* If there's an old entry for this NID, remove it */
91 entry_count = X509_NAME_entry_count(name);
92 for (i = 0; i < entry_count; i++)
93 {
94 ne = X509_NAME_get_entry(name, i);
95 temp_nid = OBJ_obj2nid(X509_NAME_ENTRY_get_object(ne));
96 if (temp_nid == nid)
97 {
98 ne = X509_NAME_delete_entry(name, i);
99 X509_NAME_ENTRY_free(ne);
100 break;
101 }
102 }
103
104 /* Add the new entry */
105 if (!X509_NAME_add_entry_by_NID(name, nid, MBSTRING_UTF8, utf8string,
106 -1, -1, 0))
107 {
108 exception_from_error_queue();
109 return -1;
110 }
111 return 0;
112}
113
114
115/*
116 * Find attribute. An X509Name object has the following attributes:
117 * countryName (alias C), stateOrProvince (alias ST), locality (alias L),
118 * organization (alias O), organizationalUnit (alias OU), commonName (alias
119 * CN) and more...
120 *
121 * Arguments: self - The X509Name object
122 * name - The attribute name
123 * Returns: A Python object for the attribute, or NULL if something went
124 * wrong
125 */
126static PyObject *
127crypto_X509Name_getattr(crypto_X509NameObj *self, char *name)
128{
129 int nid, len;
130 char *utf8string;
131
132 if ((nid = OBJ_txt2nid(name)) == NID_undef)
133 {
134 PyErr_SetString(PyExc_AttributeError, "No such attribute");
135 return NULL;
136 }
137
138 len = get_name_by_nid(self->x509_name, nid, &utf8string);
139 if (len < 0)
140 return NULL;
141 else if (len == 0)
142 {
143 Py_INCREF(Py_None);
144 return Py_None;
145 }
146 else
147 return PyUnicode_Decode(utf8string, len, "utf-8", NULL);
148}
149
150/*
151 * Set attribute
152 *
153 * Arguments: self - The X509Name object
154 * name - The attribute name
155 * value - The value to set
156 */
157static int
158crypto_X509Name_setattr(crypto_X509NameObj *self, char *name, PyObject *value)
159{
160 int nid;
161 char *buffer;
162
163 if ((nid = OBJ_txt2nid(name)) == NID_undef)
164 {
165 PyErr_SetString(PyExc_AttributeError, "No such attribute");
166 return -1;
167 }
168
169 /* Something of a hack to get nice unicode behaviour */
170 if (!PyArg_Parse(value, "es:setattr", "utf-8", &buffer))
171 return -1;
172
173 return set_name_by_nid(self->x509_name, nid, buffer);
174}
175
176/*
177 * Compare two X509Name structures.
178 *
179 * Arguments: n - The first X509Name
180 * m - The second X509Name
181 * Returns: <0 if n < m, 0 if n == m and >0 if n > m
182 */
183static int
184crypto_X509Name_compare(crypto_X509NameObj *n, crypto_X509NameObj *m)
185{
186 return X509_NAME_cmp(n->x509_name, m->x509_name);
187}
188
189/*
190 * String representation of an X509Name
191 *
192 * Arguments: self - The X509Name object
193 * Returns: A string representation of the object
194 */
195static PyObject *
196crypto_X509Name_repr(crypto_X509NameObj *self)
197{
198 char tmpbuf[512] = "";
199 char realbuf[512+64];
200
201 if (X509_NAME_oneline(self->x509_name, tmpbuf, 512) == NULL)
202 {
203 exception_from_error_queue();
204 return NULL;
205 }
206 else
207 {
208 /* This is safe because tmpbuf is max 512 characters */
209 sprintf(realbuf, "<X509Name object '%s'>", tmpbuf);
210 return PyString_FromString(realbuf);
211 }
212}
213
214/*
215 * Call the visitproc on all contained objects.
216 *
217 * Arguments: self - The Connection object
218 * visit - Function to call
219 * arg - Extra argument to visit
220 * Returns: 0 if all goes well, otherwise the return code from the first
221 * call that gave non-zero result.
222 */
223static int
224crypto_X509Name_traverse(crypto_X509NameObj *self, visitproc visit, void *arg)
225{
226 int ret = 0;
227
228 if (ret == 0 && self->parent_cert != NULL)
229 ret = visit(self->parent_cert, arg);
230 return ret;
231}
232
233/*
234 * Decref all contained objects and zero the pointers.
235 *
236 * Arguments: self - The Connection object
237 * Returns: Always 0.
238 */
239static int
240crypto_X509Name_clear(crypto_X509NameObj *self)
241{
242 Py_XDECREF(self->parent_cert);
243 self->parent_cert = NULL;
244 return 0;
245}
246
247/*
248 * Deallocate the memory used by the X509Name object
249 *
250 * Arguments: self - The X509Name object
251 * Returns: None
252 */
253static void
254crypto_X509Name_dealloc(crypto_X509NameObj *self)
255{
256 PyObject_GC_UnTrack(self);
257 /* Sometimes we don't have to dealloc this */
258 if (self->dealloc)
259 X509_NAME_free(self->x509_name);
260
261 crypto_X509Name_clear(self);
262
263 PyObject_GC_Del(self);
264}
265
266PyTypeObject crypto_X509Name_Type = {
267 PyObject_HEAD_INIT(NULL)
268 0,
269 "X509Name",
270 sizeof(crypto_X509NameObj),
271 0,
272 (destructor)crypto_X509Name_dealloc,
273 NULL, /* print */
274 (getattrfunc)crypto_X509Name_getattr,
275 (setattrfunc)crypto_X509Name_setattr,
276 (cmpfunc)crypto_X509Name_compare,
277 (reprfunc)crypto_X509Name_repr,
278 NULL, /* as_number */
279 NULL, /* as_sequence */
280 NULL, /* as_mapping */
281 NULL, /* hash */
282 NULL, /* call */
283 NULL, /* str */
284 NULL, /* getattro */
285 NULL, /* setattro */
286 NULL, /* as_buffer */
287 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
288 NULL, /* doc */
289 (traverseproc)crypto_X509Name_traverse,
290 (inquiry)crypto_X509Name_clear,
291};
292
293
294/*
295 * Initialize the X509Name part of the crypto module
296 *
297 * Arguments: dict - The crypto module dictionary
298 * Returns: None
299 */
300int
301init_crypto_x509name(PyObject *dict)
302{
303 crypto_X509Name_Type.ob_type = &PyType_Type;
304 Py_INCREF(&crypto_X509Name_Type);
305 PyDict_SetItemString(dict, "X509NameType", (PyObject *)&crypto_X509Name_Type);
306 return 1;
307}