blob: 8871ab199a3e40ac7d71ecd8c4495dec0b56da78 [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 Calderone138a3122008-12-30 15:05:38 -0500197 } else {
Jean-Paul Calderonee098dc72008-03-06 18:36:19 -0500198 return 0;
Jean-Paul Calderone138a3122008-12-30 15:05:38 -0500199 }
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500200}
201
202/*
203 * String representation of an X509Name
204 *
205 * Arguments: self - The X509Name object
206 * Returns: A string representation of the object
207 */
208static PyObject *
209crypto_X509Name_repr(crypto_X509NameObj *self)
210{
211 char tmpbuf[512] = "";
212 char realbuf[512+64];
213
214 if (X509_NAME_oneline(self->x509_name, tmpbuf, 512) == NULL)
215 {
216 exception_from_error_queue();
217 return NULL;
218 }
219 else
220 {
221 /* This is safe because tmpbuf is max 512 characters */
222 sprintf(realbuf, "<X509Name object '%s'>", tmpbuf);
223 return PyString_FromString(realbuf);
224 }
225}
226
Jean-Paul Calderone110cd092008-03-24 17:27:42 -0400227static char crypto_X509Name_hash_doc[] = "\n\
228Return the has value of this name\n\
229\n\
230Arguments: self - The X509 object\n\
231 args - The Python argument tuple, should be empty\n\
232Returns: None\n\
233";
234
235/*
236 * First four bytes of the MD5 digest of the DER form of an X509Name.
237 *
238 * Arguments: self - The X509Name object
239 * Returns: An integer giving the hash.
240 */
241static PyObject *
242crypto_X509Name_hash(crypto_X509NameObj *self, PyObject* args)
243{
244 unsigned long hash;
245
246 if (!PyArg_ParseTuple(args, ":hash")) {
247 return NULL;
248 }
249 hash = X509_NAME_hash(self->x509_name);
250 return PyInt_FromLong(hash);
251}
252
Jean-Paul Calderonee957a002008-03-25 15:16:51 -0400253static char crypto_X509Name_der_doc[] = "\n\
254Return the DER encodeing of this name\n\
255\n\
256Arguments: self - The X509 object\n\
257 args - The Python argument tuple, should be empty\n\
258Returns: None\n\
259";
260
261/*
262 * Arguments: self - The X509Name object
263 * Returns: The DER form of an X509Name.
264 */
265static PyObject *
266crypto_X509Name_der(crypto_X509NameObj *self, PyObject *args)
267{
268 if (!PyArg_ParseTuple(args, ":der")) {
269 return NULL;
270 }
271
272 i2d_X509_NAME(self->x509_name, 0);
273 return PyString_FromStringAndSize(self->x509_name->bytes->data,
274 self->x509_name->bytes->length);
275}
276
Jean-Paul Calderone110cd092008-03-24 17:27:42 -0400277
Jean-Paul Calderonec54cc182008-03-26 21:11:07 -0400278static char crypto_X509Name_get_components_doc[] = "\n\
279Returns the split-up components of this name.\n\
280\n\
281Arguments: self - The X509 object\n\
282 args - The Python argument tuple, should be empty\n\
283Returns: List of tuples (name, value).\n\
284";
285
286static PyObject *
287crypto_X509Name_get_components(crypto_X509NameObj *self, PyObject *args)
288{
289 int n, i;
290 X509_NAME *name = self->x509_name;
291 PyObject *list;
292
293 if (!PyArg_ParseTuple(args, ":get_components"))
294 return NULL;
295
296 n = X509_NAME_entry_count(name);
297 list = PyList_New(n);
298 for (i = 0; i < n; i++)
299 {
300 X509_NAME_ENTRY *ent;
301 ASN1_OBJECT *fname;
302 ASN1_STRING *fval;
303 int nid;
304 int l;
Jean-Paul Calderonec54cc182008-03-26 21:11:07 -0400305 unsigned char *str;
306 PyObject *tuple;
307
308 ent = X509_NAME_get_entry(name, i);
309
310 fname = X509_NAME_ENTRY_get_object(ent);
311 fval = X509_NAME_ENTRY_get_data(ent);
312
313 l = ASN1_STRING_length(fval);
314 str = ASN1_STRING_data(fval);
315
316 nid = OBJ_obj2nid(fname);
317
318 /* printf("fname is %s len=%d str=%s\n", OBJ_nid2sn(nid), l, str); */
319
320 tuple = PyTuple_New(2);
321 PyTuple_SetItem(tuple, 0, PyString_FromString(OBJ_nid2sn(nid)));
Jean-Paul Calderone28ebb302008-12-29 16:25:30 -0500322 PyTuple_SetItem(tuple, 1, PyString_FromStringAndSize((char *)str, l));
Jean-Paul Calderonec54cc182008-03-26 21:11:07 -0400323
324 PyList_SetItem(list, i, tuple);
325 }
326
327 return list;
328}
329
330
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500331/*
332 * Call the visitproc on all contained objects.
333 *
334 * Arguments: self - The Connection object
335 * visit - Function to call
336 * arg - Extra argument to visit
337 * Returns: 0 if all goes well, otherwise the return code from the first
338 * call that gave non-zero result.
339 */
340static int
341crypto_X509Name_traverse(crypto_X509NameObj *self, visitproc visit, void *arg)
342{
343 int ret = 0;
344
345 if (ret == 0 && self->parent_cert != NULL)
346 ret = visit(self->parent_cert, arg);
347 return ret;
348}
349
350/*
351 * Decref all contained objects and zero the pointers.
352 *
353 * Arguments: self - The Connection object
354 * Returns: Always 0.
355 */
356static int
357crypto_X509Name_clear(crypto_X509NameObj *self)
358{
359 Py_XDECREF(self->parent_cert);
360 self->parent_cert = NULL;
361 return 0;
362}
363
364/*
365 * Deallocate the memory used by the X509Name object
366 *
367 * Arguments: self - The X509Name object
368 * Returns: None
369 */
370static void
371crypto_X509Name_dealloc(crypto_X509NameObj *self)
372{
373 PyObject_GC_UnTrack(self);
374 /* Sometimes we don't have to dealloc this */
375 if (self->dealloc)
376 X509_NAME_free(self->x509_name);
377
378 crypto_X509Name_clear(self);
379
380 PyObject_GC_Del(self);
381}
382
Jean-Paul Calderone110cd092008-03-24 17:27:42 -0400383/*
384 * ADD_METHOD(name) expands to a correct PyMethodDef declaration
385 * { 'name', (PyCFunction)crypto_X509_name, METH_VARARGS }
386 * for convenience
387 */
388#define ADD_METHOD(name) \
389 { #name, (PyCFunction)crypto_X509Name_##name, METH_VARARGS, crypto_X509Name_##name##_doc }
390static PyMethodDef crypto_X509Name_methods[] =
391{
392 ADD_METHOD(hash),
Jean-Paul Calderonee957a002008-03-25 15:16:51 -0400393 ADD_METHOD(der),
Jean-Paul Calderonec54cc182008-03-26 21:11:07 -0400394 ADD_METHOD(get_components),
Jean-Paul Calderone110cd092008-03-24 17:27:42 -0400395 { NULL, NULL }
396};
397#undef ADD_METHOD
398
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500399PyTypeObject crypto_X509Name_Type = {
400 PyObject_HEAD_INIT(NULL)
401 0,
402 "X509Name",
403 sizeof(crypto_X509NameObj),
404 0,
405 (destructor)crypto_X509Name_dealloc,
406 NULL, /* print */
407 (getattrfunc)crypto_X509Name_getattr,
408 (setattrfunc)crypto_X509Name_setattr,
409 (cmpfunc)crypto_X509Name_compare,
410 (reprfunc)crypto_X509Name_repr,
411 NULL, /* as_number */
412 NULL, /* as_sequence */
413 NULL, /* as_mapping */
414 NULL, /* hash */
415 NULL, /* call */
416 NULL, /* str */
417 NULL, /* getattro */
418 NULL, /* setattro */
419 NULL, /* as_buffer */
Jean-Paul Calderone110cd092008-03-24 17:27:42 -0400420 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
421 NULL, /* tp_doc */
422 (traverseproc)crypto_X509Name_traverse, /* tp_traverse */
423 (inquiry)crypto_X509Name_clear, /* tp_clear */
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500424};
425
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500426/*
427 * Initialize the X509Name part of the crypto module
428 *
429 * Arguments: dict - The crypto module dictionary
430 * Returns: None
431 */
432int
433init_crypto_x509name(PyObject *dict)
434{
435 crypto_X509Name_Type.ob_type = &PyType_Type;
436 Py_INCREF(&crypto_X509Name_Type);
437 PyDict_SetItemString(dict, "X509NameType", (PyObject *)&crypto_X509Name_Type);
438 return 1;
439}