blob: 5402b3de21936f12e01f79afcc1bc33655b46c2b [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
Jean-Paul Calderone8b63d452008-03-21 18:31:12 -04005 * Copyright (C) Jean-Paul Calderone 2008, All rights reserved
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05006 *
7 * X.509 Name handling, mostly thin wrapping.
8 * See the file RATIONALE for a short explanation of why this module was written.
9 *
10 * Reviewed 2001-07-23
11 */
12#include <Python.h>
13#define crypto_MODULE
14#include "crypto.h"
15
Jean-Paul Calderone28ebb302008-12-29 16:25:30 -050016static PyMethodDef crypto_X509Name_methods[4];
Jean-Paul Calderone110cd092008-03-24 17:27:42 -040017
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050018/*
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 */
Jean-Paul Calderone28ebb302008-12-29 16:25:30 -0500105 if (!X509_NAME_add_entry_by_NID(name, nid, MBSTRING_UTF8,
106 (unsigned char *)utf8string,
107 -1, -1, 0))
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500108 {
109 exception_from_error_queue();
110 return -1;
111 }
112 return 0;
113}
114
115
116/*
117 * Find attribute. An X509Name object has the following attributes:
118 * countryName (alias C), stateOrProvince (alias ST), locality (alias L),
119 * organization (alias O), organizationalUnit (alias OU), commonName (alias
120 * CN) and more...
121 *
122 * Arguments: self - The X509Name object
123 * name - The attribute name
124 * Returns: A Python object for the attribute, or NULL if something went
125 * wrong
126 */
127static PyObject *
128crypto_X509Name_getattr(crypto_X509NameObj *self, char *name)
129{
130 int nid, len;
131 char *utf8string;
132
133 if ((nid = OBJ_txt2nid(name)) == NID_undef)
134 {
Jean-Paul Calderone110cd092008-03-24 17:27:42 -0400135 return Py_FindMethod(crypto_X509Name_methods, (PyObject *)self, name);
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500136 }
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 }
Jean-Paul Calderone5b8c5ee2008-02-19 00:43:02 -0500146 else {
147 PyObject* result = PyUnicode_Decode(utf8string, len, "utf-8", NULL);
148 OPENSSL_free(utf8string);
149 return result;
150 }
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500151}
152
153/*
154 * Set attribute
155 *
156 * Arguments: self - The X509Name object
157 * name - The attribute name
158 * value - The value to set
159 */
160static int
161crypto_X509Name_setattr(crypto_X509NameObj *self, char *name, PyObject *value)
162{
163 int nid;
Jean-Paul Calderone7b0443a2008-02-19 00:25:30 -0500164 int result;
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500165 char *buffer;
166
167 if ((nid = OBJ_txt2nid(name)) == NID_undef)
168 {
169 PyErr_SetString(PyExc_AttributeError, "No such attribute");
170 return -1;
171 }
172
173 /* Something of a hack to get nice unicode behaviour */
174 if (!PyArg_Parse(value, "es:setattr", "utf-8", &buffer))
175 return -1;
Jean-Paul Calderone5b8c5ee2008-02-19 00:43:02 -0500176
Jean-Paul Calderone7b0443a2008-02-19 00:25:30 -0500177 result = set_name_by_nid(self->x509_name, nid, buffer);
178 PyMem_Free(buffer);
179 return result;
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500180}
181
182/*
183 * Compare two X509Name structures.
184 *
185 * Arguments: n - The first X509Name
186 * m - The second X509Name
187 * Returns: <0 if n < m, 0 if n == m and >0 if n > m
188 */
189static int
190crypto_X509Name_compare(crypto_X509NameObj *n, crypto_X509NameObj *m)
191{
Jean-Paul Calderonee098dc72008-03-06 18:36:19 -0500192 int result = X509_NAME_cmp(n->x509_name, m->x509_name);
193 if (result < 0) {
194 return -1;
195 } else if (result > 0) {
196 return 1;
Jean-Paul Calderone28ebb302008-12-29 16:25:30 -0500197 } else
Jean-Paul Calderonee098dc72008-03-06 18:36:19 -0500198 return 0;
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500199}
200
201/*
202 * String representation of an X509Name
203 *
204 * Arguments: self - The X509Name object
205 * Returns: A string representation of the object
206 */
207static PyObject *
208crypto_X509Name_repr(crypto_X509NameObj *self)
209{
210 char tmpbuf[512] = "";
211 char realbuf[512+64];
212
213 if (X509_NAME_oneline(self->x509_name, tmpbuf, 512) == NULL)
214 {
215 exception_from_error_queue();
216 return NULL;
217 }
218 else
219 {
220 /* This is safe because tmpbuf is max 512 characters */
221 sprintf(realbuf, "<X509Name object '%s'>", tmpbuf);
222 return PyString_FromString(realbuf);
223 }
224}
225
Jean-Paul Calderone110cd092008-03-24 17:27:42 -0400226static char crypto_X509Name_hash_doc[] = "\n\
227Return the has value of this name\n\
228\n\
229Arguments: self - The X509 object\n\
230 args - The Python argument tuple, should be empty\n\
231Returns: None\n\
232";
233
234/*
235 * First four bytes of the MD5 digest of the DER form of an X509Name.
236 *
237 * Arguments: self - The X509Name object
238 * Returns: An integer giving the hash.
239 */
240static PyObject *
241crypto_X509Name_hash(crypto_X509NameObj *self, PyObject* args)
242{
243 unsigned long hash;
244
245 if (!PyArg_ParseTuple(args, ":hash")) {
246 return NULL;
247 }
248 hash = X509_NAME_hash(self->x509_name);
249 return PyInt_FromLong(hash);
250}
251
Jean-Paul Calderonee957a002008-03-25 15:16:51 -0400252static char crypto_X509Name_der_doc[] = "\n\
253Return the DER encodeing of this name\n\
254\n\
255Arguments: self - The X509 object\n\
256 args - The Python argument tuple, should be empty\n\
257Returns: None\n\
258";
259
260/*
261 * Arguments: self - The X509Name object
262 * Returns: The DER form of an X509Name.
263 */
264static PyObject *
265crypto_X509Name_der(crypto_X509NameObj *self, PyObject *args)
266{
267 if (!PyArg_ParseTuple(args, ":der")) {
268 return NULL;
269 }
270
271 i2d_X509_NAME(self->x509_name, 0);
272 return PyString_FromStringAndSize(self->x509_name->bytes->data,
273 self->x509_name->bytes->length);
274}
275
Jean-Paul Calderone110cd092008-03-24 17:27:42 -0400276
Jean-Paul Calderonec54cc182008-03-26 21:11:07 -0400277static char crypto_X509Name_get_components_doc[] = "\n\
278Returns the split-up components of this name.\n\
279\n\
280Arguments: self - The X509 object\n\
281 args - The Python argument tuple, should be empty\n\
282Returns: List of tuples (name, value).\n\
283";
284
285static PyObject *
286crypto_X509Name_get_components(crypto_X509NameObj *self, PyObject *args)
287{
288 int n, i;
289 X509_NAME *name = self->x509_name;
290 PyObject *list;
291
292 if (!PyArg_ParseTuple(args, ":get_components"))
293 return NULL;
294
295 n = X509_NAME_entry_count(name);
296 list = PyList_New(n);
297 for (i = 0; i < n; i++)
298 {
299 X509_NAME_ENTRY *ent;
300 ASN1_OBJECT *fname;
301 ASN1_STRING *fval;
302 int nid;
303 int l;
Jean-Paul Calderonec54cc182008-03-26 21:11:07 -0400304 unsigned char *str;
305 PyObject *tuple;
306
307 ent = X509_NAME_get_entry(name, i);
308
309 fname = X509_NAME_ENTRY_get_object(ent);
310 fval = X509_NAME_ENTRY_get_data(ent);
311
312 l = ASN1_STRING_length(fval);
313 str = ASN1_STRING_data(fval);
314
315 nid = OBJ_obj2nid(fname);
316
317 /* printf("fname is %s len=%d str=%s\n", OBJ_nid2sn(nid), l, str); */
318
319 tuple = PyTuple_New(2);
320 PyTuple_SetItem(tuple, 0, PyString_FromString(OBJ_nid2sn(nid)));
Jean-Paul Calderone28ebb302008-12-29 16:25:30 -0500321 PyTuple_SetItem(tuple, 1, PyString_FromStringAndSize((char *)str, l));
Jean-Paul Calderonec54cc182008-03-26 21:11:07 -0400322
323 PyList_SetItem(list, i, tuple);
324 }
325
326 return list;
327}
328
329
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500330/*
331 * Call the visitproc on all contained objects.
332 *
333 * Arguments: self - The Connection object
334 * visit - Function to call
335 * arg - Extra argument to visit
336 * Returns: 0 if all goes well, otherwise the return code from the first
337 * call that gave non-zero result.
338 */
339static int
340crypto_X509Name_traverse(crypto_X509NameObj *self, visitproc visit, void *arg)
341{
342 int ret = 0;
343
344 if (ret == 0 && self->parent_cert != NULL)
345 ret = visit(self->parent_cert, arg);
346 return ret;
347}
348
349/*
350 * Decref all contained objects and zero the pointers.
351 *
352 * Arguments: self - The Connection object
353 * Returns: Always 0.
354 */
355static int
356crypto_X509Name_clear(crypto_X509NameObj *self)
357{
358 Py_XDECREF(self->parent_cert);
359 self->parent_cert = NULL;
360 return 0;
361}
362
363/*
364 * Deallocate the memory used by the X509Name object
365 *
366 * Arguments: self - The X509Name object
367 * Returns: None
368 */
369static void
370crypto_X509Name_dealloc(crypto_X509NameObj *self)
371{
372 PyObject_GC_UnTrack(self);
373 /* Sometimes we don't have to dealloc this */
374 if (self->dealloc)
375 X509_NAME_free(self->x509_name);
376
377 crypto_X509Name_clear(self);
378
379 PyObject_GC_Del(self);
380}
381
Jean-Paul Calderone110cd092008-03-24 17:27:42 -0400382/*
383 * ADD_METHOD(name) expands to a correct PyMethodDef declaration
384 * { 'name', (PyCFunction)crypto_X509_name, METH_VARARGS }
385 * for convenience
386 */
387#define ADD_METHOD(name) \
388 { #name, (PyCFunction)crypto_X509Name_##name, METH_VARARGS, crypto_X509Name_##name##_doc }
389static PyMethodDef crypto_X509Name_methods[] =
390{
391 ADD_METHOD(hash),
Jean-Paul Calderonee957a002008-03-25 15:16:51 -0400392 ADD_METHOD(der),
Jean-Paul Calderonec54cc182008-03-26 21:11:07 -0400393 ADD_METHOD(get_components),
Jean-Paul Calderone110cd092008-03-24 17:27:42 -0400394 { NULL, NULL }
395};
396#undef ADD_METHOD
397
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500398PyTypeObject crypto_X509Name_Type = {
399 PyObject_HEAD_INIT(NULL)
400 0,
401 "X509Name",
402 sizeof(crypto_X509NameObj),
403 0,
404 (destructor)crypto_X509Name_dealloc,
405 NULL, /* print */
406 (getattrfunc)crypto_X509Name_getattr,
407 (setattrfunc)crypto_X509Name_setattr,
408 (cmpfunc)crypto_X509Name_compare,
409 (reprfunc)crypto_X509Name_repr,
410 NULL, /* as_number */
411 NULL, /* as_sequence */
412 NULL, /* as_mapping */
413 NULL, /* hash */
414 NULL, /* call */
415 NULL, /* str */
416 NULL, /* getattro */
417 NULL, /* setattro */
418 NULL, /* as_buffer */
Jean-Paul Calderone110cd092008-03-24 17:27:42 -0400419 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
420 NULL, /* tp_doc */
421 (traverseproc)crypto_X509Name_traverse, /* tp_traverse */
422 (inquiry)crypto_X509Name_clear, /* tp_clear */
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500423};
424
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500425/*
426 * Initialize the X509Name part of the crypto module
427 *
428 * Arguments: dict - The crypto module dictionary
429 * Returns: None
430 */
431int
432init_crypto_x509name(PyObject *dict)
433{
434 crypto_X509Name_Type.ob_type = &PyType_Type;
435 Py_INCREF(&crypto_X509Name_Type);
436 PyDict_SetItemString(dict, "X509NameType", (PyObject *)&crypto_X509Name_Type);
437 return 1;
438}