blob: e84d5bc88c56aaa7528b4c0327922e1ab167a838 [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
Jean-Paul Calderone2cd7a922009-06-27 11:02:46 -040044
45static char crypto_X509Name_doc[] = "\n\
46X509Name(name) -> New X509Name object\n\
47\n\
48Create a new X509Name, copying the given X509Name instance.\n\
49\n\
50@param name: An X509Name object to copy\n\
51@return: The X509Name object\n\
52";
53
54static PyObject *
55crypto_X509Name_new(PyTypeObject *subtype, PyObject *args, PyObject *kwargs)
56{
57 crypto_X509NameObj *name;
58
59 if (!PyArg_ParseTuple(args, "O!:X509Name", &crypto_X509Name_Type, &name)) {
60 return NULL;
61 }
62
63 return (PyObject *)crypto_X509Name_New(X509_NAME_dup(name->x509_name), 1);
64}
65
66
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050067/*
68 * Return a name string given a X509_NAME object and a name identifier. Used
69 * by the getattr function.
70 *
71 * Arguments: name - The X509_NAME object
72 * nid - The name identifier
73 * Returns: The name as a Python string object
74 */
75static int
76get_name_by_nid(X509_NAME *name, int nid, char **utf8string)
77{
78 int entry_idx;
79 X509_NAME_ENTRY *entry;
80 ASN1_STRING *data;
81 int len;
82
83 if ((entry_idx = X509_NAME_get_index_by_NID(name, nid, -1)) == -1)
84 {
85 return 0;
86 }
87 entry = X509_NAME_get_entry(name, entry_idx);
88 data = X509_NAME_ENTRY_get_data(entry);
89 if ((len = ASN1_STRING_to_UTF8((unsigned char **)utf8string, data)) < 0)
90 {
Rick Deand369c932009-07-08 11:48:33 -050091 exception_from_error_queue(crypto_Error);
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050092 return -1;
93 }
94
95 return len;
96}
97
98/*
99 * Given a X509_NAME object and a name identifier, set the corresponding
100 * attribute to the given string. Used by the setattr function.
101 *
102 * Arguments: name - The X509_NAME object
103 * nid - The name identifier
104 * value - The string to set
105 * Returns: 0 for success, -1 on failure
106 */
107static int
108set_name_by_nid(X509_NAME *name, int nid, char *utf8string)
109{
110 X509_NAME_ENTRY *ne;
111 int i, entry_count, temp_nid;
112
113 /* If there's an old entry for this NID, remove it */
114 entry_count = X509_NAME_entry_count(name);
115 for (i = 0; i < entry_count; i++)
116 {
117 ne = X509_NAME_get_entry(name, i);
118 temp_nid = OBJ_obj2nid(X509_NAME_ENTRY_get_object(ne));
119 if (temp_nid == nid)
120 {
121 ne = X509_NAME_delete_entry(name, i);
122 X509_NAME_ENTRY_free(ne);
123 break;
124 }
125 }
126
127 /* Add the new entry */
Jean-Paul Calderone28ebb302008-12-29 16:25:30 -0500128 if (!X509_NAME_add_entry_by_NID(name, nid, MBSTRING_UTF8,
129 (unsigned char *)utf8string,
130 -1, -1, 0))
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500131 {
Rick Deand369c932009-07-08 11:48:33 -0500132 exception_from_error_queue(crypto_Error);
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500133 return -1;
134 }
135 return 0;
136}
137
138
139/*
140 * Find attribute. An X509Name object has the following attributes:
141 * countryName (alias C), stateOrProvince (alias ST), locality (alias L),
142 * organization (alias O), organizationalUnit (alias OU), commonName (alias
143 * CN) and more...
144 *
145 * Arguments: self - The X509Name object
146 * name - The attribute name
147 * Returns: A Python object for the attribute, or NULL if something went
148 * wrong
149 */
150static PyObject *
151crypto_X509Name_getattr(crypto_X509NameObj *self, char *name)
152{
153 int nid, len;
154 char *utf8string;
155
Jean-Paul Calderoneef414d52009-07-16 16:21:04 -0400156 if ((nid = OBJ_txt2nid(name)) == NID_undef) {
157 /*
158 * This is a bit weird. OBJ_txt2nid indicated failure, but it seems
159 * a lower level function, a2d_ASN1_OBJECT, also feels the need to
160 * push something onto the error queue. If we don't clean that up
161 * now, someone else will bump into it later and be quite confused.
162 * See lp#314814.
163 */
164 flush_error_queue();
Jean-Paul Calderone110cd092008-03-24 17:27:42 -0400165 return Py_FindMethod(crypto_X509Name_methods, (PyObject *)self, name);
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500166 }
167
168 len = get_name_by_nid(self->x509_name, nid, &utf8string);
169 if (len < 0)
170 return NULL;
171 else if (len == 0)
172 {
173 Py_INCREF(Py_None);
174 return Py_None;
175 }
Jean-Paul Calderone5b8c5ee2008-02-19 00:43:02 -0500176 else {
177 PyObject* result = PyUnicode_Decode(utf8string, len, "utf-8", NULL);
178 OPENSSL_free(utf8string);
179 return result;
180 }
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500181}
182
183/*
184 * Set attribute
185 *
186 * Arguments: self - The X509Name object
187 * name - The attribute name
188 * value - The value to set
189 */
190static int
191crypto_X509Name_setattr(crypto_X509NameObj *self, char *name, PyObject *value)
192{
193 int nid;
Jean-Paul Calderone7b0443a2008-02-19 00:25:30 -0500194 int result;
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500195 char *buffer;
196
197 if ((nid = OBJ_txt2nid(name)) == NID_undef)
198 {
199 PyErr_SetString(PyExc_AttributeError, "No such attribute");
200 return -1;
201 }
202
203 /* Something of a hack to get nice unicode behaviour */
204 if (!PyArg_Parse(value, "es:setattr", "utf-8", &buffer))
205 return -1;
Jean-Paul Calderone5b8c5ee2008-02-19 00:43:02 -0500206
Jean-Paul Calderone7b0443a2008-02-19 00:25:30 -0500207 result = set_name_by_nid(self->x509_name, nid, buffer);
208 PyMem_Free(buffer);
209 return result;
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500210}
211
212/*
213 * Compare two X509Name structures.
214 *
215 * Arguments: n - The first X509Name
216 * m - The second X509Name
217 * Returns: <0 if n < m, 0 if n == m and >0 if n > m
218 */
219static int
220crypto_X509Name_compare(crypto_X509NameObj *n, crypto_X509NameObj *m)
221{
Jean-Paul Calderonee098dc72008-03-06 18:36:19 -0500222 int result = X509_NAME_cmp(n->x509_name, m->x509_name);
223 if (result < 0) {
224 return -1;
225 } else if (result > 0) {
226 return 1;
Jean-Paul Calderone138a3122008-12-30 15:05:38 -0500227 } else {
Jean-Paul Calderonee098dc72008-03-06 18:36:19 -0500228 return 0;
Jean-Paul Calderone138a3122008-12-30 15:05:38 -0500229 }
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500230}
231
232/*
233 * String representation of an X509Name
234 *
235 * Arguments: self - The X509Name object
236 * Returns: A string representation of the object
237 */
238static PyObject *
239crypto_X509Name_repr(crypto_X509NameObj *self)
240{
241 char tmpbuf[512] = "";
242 char realbuf[512+64];
243
244 if (X509_NAME_oneline(self->x509_name, tmpbuf, 512) == NULL)
245 {
Rick Deand369c932009-07-08 11:48:33 -0500246 exception_from_error_queue(crypto_Error);
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500247 return NULL;
248 }
249 else
250 {
251 /* This is safe because tmpbuf is max 512 characters */
252 sprintf(realbuf, "<X509Name object '%s'>", tmpbuf);
253 return PyString_FromString(realbuf);
254 }
255}
256
Jean-Paul Calderone110cd092008-03-24 17:27:42 -0400257static char crypto_X509Name_hash_doc[] = "\n\
Jean-Paul Calderone54bcc832009-05-27 14:06:48 -0400258Return the hash value of this name\n\
Jean-Paul Calderone110cd092008-03-24 17:27:42 -0400259\n\
Jean-Paul Calderone54bcc832009-05-27 14:06:48 -0400260@return: None\n\
Jean-Paul Calderone110cd092008-03-24 17:27:42 -0400261";
262
263/*
264 * First four bytes of the MD5 digest of the DER form of an X509Name.
265 *
266 * Arguments: self - The X509Name object
267 * Returns: An integer giving the hash.
268 */
269static PyObject *
270crypto_X509Name_hash(crypto_X509NameObj *self, PyObject* args)
271{
272 unsigned long hash;
273
274 if (!PyArg_ParseTuple(args, ":hash")) {
275 return NULL;
276 }
277 hash = X509_NAME_hash(self->x509_name);
278 return PyInt_FromLong(hash);
279}
280
Jean-Paul Calderonee957a002008-03-25 15:16:51 -0400281static char crypto_X509Name_der_doc[] = "\n\
Jean-Paul Calderone54bcc832009-05-27 14:06:48 -0400282Return the DER encoding of this name\n\
Jean-Paul Calderonee957a002008-03-25 15:16:51 -0400283\n\
Jean-Paul Calderone54bcc832009-05-27 14:06:48 -0400284@return: None\n\
Jean-Paul Calderonee957a002008-03-25 15:16:51 -0400285";
286
287/*
288 * Arguments: self - The X509Name object
289 * Returns: The DER form of an X509Name.
290 */
291static PyObject *
292crypto_X509Name_der(crypto_X509NameObj *self, PyObject *args)
293{
294 if (!PyArg_ParseTuple(args, ":der")) {
295 return NULL;
296 }
297
298 i2d_X509_NAME(self->x509_name, 0);
299 return PyString_FromStringAndSize(self->x509_name->bytes->data,
300 self->x509_name->bytes->length);
301}
302
Jean-Paul Calderone110cd092008-03-24 17:27:42 -0400303
Jean-Paul Calderonec54cc182008-03-26 21:11:07 -0400304static char crypto_X509Name_get_components_doc[] = "\n\
305Returns the split-up components of this name.\n\
306\n\
Jean-Paul Calderone54bcc832009-05-27 14:06:48 -0400307@return: List of tuples (name, value).\n\
Jean-Paul Calderonec54cc182008-03-26 21:11:07 -0400308";
309
310static PyObject *
311crypto_X509Name_get_components(crypto_X509NameObj *self, PyObject *args)
312{
313 int n, i;
314 X509_NAME *name = self->x509_name;
315 PyObject *list;
316
317 if (!PyArg_ParseTuple(args, ":get_components"))
318 return NULL;
319
320 n = X509_NAME_entry_count(name);
321 list = PyList_New(n);
322 for (i = 0; i < n; i++)
323 {
324 X509_NAME_ENTRY *ent;
325 ASN1_OBJECT *fname;
326 ASN1_STRING *fval;
327 int nid;
328 int l;
Jean-Paul Calderonec54cc182008-03-26 21:11:07 -0400329 unsigned char *str;
330 PyObject *tuple;
331
332 ent = X509_NAME_get_entry(name, i);
333
334 fname = X509_NAME_ENTRY_get_object(ent);
335 fval = X509_NAME_ENTRY_get_data(ent);
336
337 l = ASN1_STRING_length(fval);
338 str = ASN1_STRING_data(fval);
339
340 nid = OBJ_obj2nid(fname);
341
342 /* printf("fname is %s len=%d str=%s\n", OBJ_nid2sn(nid), l, str); */
343
344 tuple = PyTuple_New(2);
345 PyTuple_SetItem(tuple, 0, PyString_FromString(OBJ_nid2sn(nid)));
Jean-Paul Calderone28ebb302008-12-29 16:25:30 -0500346 PyTuple_SetItem(tuple, 1, PyString_FromStringAndSize((char *)str, l));
Jean-Paul Calderonec54cc182008-03-26 21:11:07 -0400347
348 PyList_SetItem(list, i, tuple);
349 }
350
351 return list;
352}
353
354
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500355/*
356 * Call the visitproc on all contained objects.
357 *
358 * Arguments: self - The Connection object
359 * visit - Function to call
360 * arg - Extra argument to visit
361 * Returns: 0 if all goes well, otherwise the return code from the first
362 * call that gave non-zero result.
363 */
364static int
365crypto_X509Name_traverse(crypto_X509NameObj *self, visitproc visit, void *arg)
366{
367 int ret = 0;
368
369 if (ret == 0 && self->parent_cert != NULL)
370 ret = visit(self->parent_cert, arg);
371 return ret;
372}
373
374/*
375 * Decref all contained objects and zero the pointers.
376 *
377 * Arguments: self - The Connection object
378 * Returns: Always 0.
379 */
380static int
381crypto_X509Name_clear(crypto_X509NameObj *self)
382{
383 Py_XDECREF(self->parent_cert);
384 self->parent_cert = NULL;
385 return 0;
386}
387
388/*
389 * Deallocate the memory used by the X509Name object
390 *
391 * Arguments: self - The X509Name object
392 * Returns: None
393 */
394static void
395crypto_X509Name_dealloc(crypto_X509NameObj *self)
396{
397 PyObject_GC_UnTrack(self);
398 /* Sometimes we don't have to dealloc this */
399 if (self->dealloc)
400 X509_NAME_free(self->x509_name);
401
402 crypto_X509Name_clear(self);
403
404 PyObject_GC_Del(self);
405}
406
Jean-Paul Calderone110cd092008-03-24 17:27:42 -0400407/*
408 * ADD_METHOD(name) expands to a correct PyMethodDef declaration
409 * { 'name', (PyCFunction)crypto_X509_name, METH_VARARGS }
410 * for convenience
411 */
412#define ADD_METHOD(name) \
413 { #name, (PyCFunction)crypto_X509Name_##name, METH_VARARGS, crypto_X509Name_##name##_doc }
414static PyMethodDef crypto_X509Name_methods[] =
415{
416 ADD_METHOD(hash),
Jean-Paul Calderonee957a002008-03-25 15:16:51 -0400417 ADD_METHOD(der),
Jean-Paul Calderonec54cc182008-03-26 21:11:07 -0400418 ADD_METHOD(get_components),
Jean-Paul Calderone110cd092008-03-24 17:27:42 -0400419 { NULL, NULL }
420};
421#undef ADD_METHOD
422
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500423PyTypeObject crypto_X509Name_Type = {
424 PyObject_HEAD_INIT(NULL)
425 0,
426 "X509Name",
427 sizeof(crypto_X509NameObj),
428 0,
429 (destructor)crypto_X509Name_dealloc,
430 NULL, /* print */
431 (getattrfunc)crypto_X509Name_getattr,
432 (setattrfunc)crypto_X509Name_setattr,
433 (cmpfunc)crypto_X509Name_compare,
434 (reprfunc)crypto_X509Name_repr,
435 NULL, /* as_number */
436 NULL, /* as_sequence */
437 NULL, /* as_mapping */
438 NULL, /* hash */
439 NULL, /* call */
440 NULL, /* str */
441 NULL, /* getattro */
442 NULL, /* setattro */
443 NULL, /* as_buffer */
Jean-Paul Calderone110cd092008-03-24 17:27:42 -0400444 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
Jean-Paul Calderone2cd7a922009-06-27 11:02:46 -0400445 crypto_X509Name_doc, /* tp_doc */
Jean-Paul Calderone110cd092008-03-24 17:27:42 -0400446 (traverseproc)crypto_X509Name_traverse, /* tp_traverse */
447 (inquiry)crypto_X509Name_clear, /* tp_clear */
Jean-Paul Calderone2cd7a922009-06-27 11:02:46 -0400448 NULL, /* tp_richcompare */
449 0, /* tp_weaklistoffset */
450 NULL, /* tp_iter */
451 NULL, /* tp_iternext */
452 crypto_X509Name_methods, /* tp_methods */
453 NULL, /* tp_members */
454 NULL, /* tp_getset */
455 NULL, /* tp_base */
456 NULL, /* tp_dict */
457 NULL, /* tp_descr_get */
458 NULL, /* tp_descr_set */
459 0, /* tp_dictoffset */
460 NULL, /* tp_init */
461 NULL, /* tp_alloc */
462 crypto_X509Name_new, /* tp_new */
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500463};
464
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500465/*
466 * Initialize the X509Name part of the crypto module
467 *
Jean-Paul Calderone2cd7a922009-06-27 11:02:46 -0400468 * Arguments: module - The crypto module
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500469 * Returns: None
470 */
471int
Jean-Paul Calderone2cd7a922009-06-27 11:02:46 -0400472init_crypto_x509name(PyObject *module)
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500473{
Jean-Paul Calderone2cd7a922009-06-27 11:02:46 -0400474 if (PyType_Ready(&crypto_X509Name_Type) < 0) {
475 return 0;
476 }
477
478 if (PyModule_AddObject(module, "X509Name", (PyObject *)&crypto_X509Name_Type) != 0) {
479 return 0;
480 }
481
482 if (PyModule_AddObject(module, "X509NameType", (PyObject *)&crypto_X509Name_Type) != 0) {
483 return 0;
484 }
485
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500486 return 1;
487}