blob: 83f3c21007c22c55dc7324ceeaeb4f17c1e45b0d [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 Calderone88f38b22009-07-16 16:25:19 -04005 * Copyright (C) Jean-Paul Calderone 2008-2009, 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 *
Jean-Paul Calderone0f31ec52010-08-11 22:56:42 -0400151crypto_X509Name_getattro(crypto_X509NameObj *self, PyObject *nameobj)
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500152{
153 int nid, len;
154 char *utf8string;
Jean-Paul Calderone0f31ec52010-08-11 22:56:42 -0400155 char *name = _PyUnicode_AsString(nameobj);
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500156
Jean-Paul Calderoneef414d52009-07-16 16:21:04 -0400157 if ((nid = OBJ_txt2nid(name)) == NID_undef) {
158 /*
159 * This is a bit weird. OBJ_txt2nid indicated failure, but it seems
160 * a lower level function, a2d_ASN1_OBJECT, also feels the need to
161 * push something onto the error queue. If we don't clean that up
162 * now, someone else will bump into it later and be quite confused.
163 * See lp#314814.
164 */
165 flush_error_queue();
Jean-Paul Calderone0f31ec52010-08-11 22:56:42 -0400166 return PyObject_GenericGetAttr((PyObject*)self, nameobj);
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500167 }
168
169 len = get_name_by_nid(self->x509_name, nid, &utf8string);
170 if (len < 0)
171 return NULL;
172 else if (len == 0)
173 {
174 Py_INCREF(Py_None);
175 return Py_None;
176 }
Jean-Paul Calderone5b8c5ee2008-02-19 00:43:02 -0500177 else {
178 PyObject* result = PyUnicode_Decode(utf8string, len, "utf-8", NULL);
179 OPENSSL_free(utf8string);
180 return result;
181 }
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500182}
183
184/*
185 * Set attribute
186 *
187 * Arguments: self - The X509Name object
188 * name - The attribute name
189 * value - The value to set
190 */
191static int
192crypto_X509Name_setattr(crypto_X509NameObj *self, char *name, PyObject *value)
193{
194 int nid;
Jean-Paul Calderone7b0443a2008-02-19 00:25:30 -0500195 int result;
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500196 char *buffer;
197
198 if ((nid = OBJ_txt2nid(name)) == NID_undef)
199 {
200 PyErr_SetString(PyExc_AttributeError, "No such attribute");
201 return -1;
202 }
203
204 /* Something of a hack to get nice unicode behaviour */
205 if (!PyArg_Parse(value, "es:setattr", "utf-8", &buffer))
206 return -1;
Jean-Paul Calderone5b8c5ee2008-02-19 00:43:02 -0500207
Jean-Paul Calderone7b0443a2008-02-19 00:25:30 -0500208 result = set_name_by_nid(self->x509_name, nid, buffer);
209 PyMem_Free(buffer);
210 return result;
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500211}
212
213/*
214 * Compare two X509Name structures.
215 *
216 * Arguments: n - The first X509Name
217 * m - The second X509Name
218 * Returns: <0 if n < m, 0 if n == m and >0 if n > m
219 */
Jean-Paul Calderone4d0c3212010-08-10 22:57:42 -0400220static PyObject *
221crypto_X509Name_richcompare(PyObject *n, PyObject *m, int op) {
222 int result;
223
224 if (!crypto_X509Name_Check(n) || !crypto_X509Name_Check(m)) {
225 Py_INCREF(Py_NotImplemented);
226 return Py_NotImplemented;
227 }
228
229 result = X509_NAME_cmp(
230 ((crypto_X509NameObj*)n)->x509_name,
231 ((crypto_X509NameObj*)m)->x509_name);
232
233 switch (op) {
234 case Py_EQ:
235 result = (result == 0);
236
237 case Py_NE:
238 result = (result != 0);
239
240 case Py_LT:
241 result = (result < 0);
242
243 case Py_LE:
244 result = (result <= 0);
245
246 case Py_GT:
247 result = (result > 0);
248
249 case Py_GE:
250 result = (result >= 0);
251
252 default:
253 /* Should be impossible */
254 Py_INCREF(Py_NotImplemented);
255 return Py_NotImplemented;
256 }
257
258 if (result) {
259 Py_INCREF(Py_True);
260 return Py_True;
Jean-Paul Calderone138a3122008-12-30 15:05:38 -0500261 } else {
Jean-Paul Calderone4d0c3212010-08-10 22:57:42 -0400262 Py_INCREF(Py_False);
263 return Py_False;
Jean-Paul Calderone138a3122008-12-30 15:05:38 -0500264 }
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500265}
266
267/*
268 * String representation of an X509Name
269 *
270 * Arguments: self - The X509Name object
271 * Returns: A string representation of the object
272 */
273static PyObject *
274crypto_X509Name_repr(crypto_X509NameObj *self)
275{
276 char tmpbuf[512] = "";
277 char realbuf[512+64];
278
279 if (X509_NAME_oneline(self->x509_name, tmpbuf, 512) == NULL)
280 {
Rick Deand369c932009-07-08 11:48:33 -0500281 exception_from_error_queue(crypto_Error);
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500282 return NULL;
283 }
284 else
285 {
286 /* This is safe because tmpbuf is max 512 characters */
287 sprintf(realbuf, "<X509Name object '%s'>", tmpbuf);
Jean-Paul Calderone4d0c3212010-08-10 22:57:42 -0400288 return PyText_FromString(realbuf);
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500289 }
290}
291
Jean-Paul Calderone110cd092008-03-24 17:27:42 -0400292static char crypto_X509Name_hash_doc[] = "\n\
Jean-Paul Calderone54bcc832009-05-27 14:06:48 -0400293Return the hash value of this name\n\
Jean-Paul Calderone110cd092008-03-24 17:27:42 -0400294\n\
Jean-Paul Calderone54bcc832009-05-27 14:06:48 -0400295@return: None\n\
Jean-Paul Calderone110cd092008-03-24 17:27:42 -0400296";
297
298/*
299 * First four bytes of the MD5 digest of the DER form of an X509Name.
300 *
301 * Arguments: self - The X509Name object
302 * Returns: An integer giving the hash.
303 */
304static PyObject *
305crypto_X509Name_hash(crypto_X509NameObj *self, PyObject* args)
306{
307 unsigned long hash;
308
309 if (!PyArg_ParseTuple(args, ":hash")) {
310 return NULL;
311 }
312 hash = X509_NAME_hash(self->x509_name);
Jean-Paul Calderone4d0c3212010-08-10 22:57:42 -0400313 return PyLong_FromLong(hash);
Jean-Paul Calderone110cd092008-03-24 17:27:42 -0400314}
315
Jean-Paul Calderonee957a002008-03-25 15:16:51 -0400316static char crypto_X509Name_der_doc[] = "\n\
Jean-Paul Calderone54bcc832009-05-27 14:06:48 -0400317Return the DER encoding of this name\n\
Jean-Paul Calderonee957a002008-03-25 15:16:51 -0400318\n\
Jean-Paul Calderone54bcc832009-05-27 14:06:48 -0400319@return: None\n\
Jean-Paul Calderonee957a002008-03-25 15:16:51 -0400320";
321
322/*
323 * Arguments: self - The X509Name object
324 * Returns: The DER form of an X509Name.
325 */
326static PyObject *
327crypto_X509Name_der(crypto_X509NameObj *self, PyObject *args)
328{
329 if (!PyArg_ParseTuple(args, ":der")) {
330 return NULL;
331 }
332
333 i2d_X509_NAME(self->x509_name, 0);
Jean-Paul Calderone4d0c3212010-08-10 22:57:42 -0400334 return PyBytes_FromStringAndSize(self->x509_name->bytes->data,
335 self->x509_name->bytes->length);
Jean-Paul Calderonee957a002008-03-25 15:16:51 -0400336}
337
Jean-Paul Calderone110cd092008-03-24 17:27:42 -0400338
Jean-Paul Calderonec54cc182008-03-26 21:11:07 -0400339static char crypto_X509Name_get_components_doc[] = "\n\
340Returns the split-up components of this name.\n\
341\n\
Jean-Paul Calderone54bcc832009-05-27 14:06:48 -0400342@return: List of tuples (name, value).\n\
Jean-Paul Calderonec54cc182008-03-26 21:11:07 -0400343";
344
345static PyObject *
346crypto_X509Name_get_components(crypto_X509NameObj *self, PyObject *args)
347{
348 int n, i;
349 X509_NAME *name = self->x509_name;
350 PyObject *list;
351
352 if (!PyArg_ParseTuple(args, ":get_components"))
353 return NULL;
354
355 n = X509_NAME_entry_count(name);
356 list = PyList_New(n);
357 for (i = 0; i < n; i++)
358 {
359 X509_NAME_ENTRY *ent;
360 ASN1_OBJECT *fname;
361 ASN1_STRING *fval;
362 int nid;
363 int l;
Jean-Paul Calderonec54cc182008-03-26 21:11:07 -0400364 unsigned char *str;
365 PyObject *tuple;
366
367 ent = X509_NAME_get_entry(name, i);
368
369 fname = X509_NAME_ENTRY_get_object(ent);
370 fval = X509_NAME_ENTRY_get_data(ent);
371
372 l = ASN1_STRING_length(fval);
373 str = ASN1_STRING_data(fval);
374
375 nid = OBJ_obj2nid(fname);
376
377 /* printf("fname is %s len=%d str=%s\n", OBJ_nid2sn(nid), l, str); */
378
379 tuple = PyTuple_New(2);
Jean-Paul Calderone4d0c3212010-08-10 22:57:42 -0400380 PyTuple_SetItem(tuple, 0, PyBytes_FromString(OBJ_nid2sn(nid)));
381 PyTuple_SetItem(tuple, 1, PyBytes_FromStringAndSize((char *)str, l));
Jean-Paul Calderonec54cc182008-03-26 21:11:07 -0400382
383 PyList_SetItem(list, i, tuple);
384 }
385
386 return list;
387}
388
389
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500390/*
391 * Call the visitproc on all contained objects.
392 *
393 * Arguments: self - The Connection object
394 * visit - Function to call
395 * arg - Extra argument to visit
396 * Returns: 0 if all goes well, otherwise the return code from the first
397 * call that gave non-zero result.
398 */
399static int
400crypto_X509Name_traverse(crypto_X509NameObj *self, visitproc visit, void *arg)
401{
402 int ret = 0;
403
404 if (ret == 0 && self->parent_cert != NULL)
405 ret = visit(self->parent_cert, arg);
406 return ret;
407}
408
409/*
410 * Decref all contained objects and zero the pointers.
411 *
412 * Arguments: self - The Connection object
413 * Returns: Always 0.
414 */
415static int
416crypto_X509Name_clear(crypto_X509NameObj *self)
417{
418 Py_XDECREF(self->parent_cert);
419 self->parent_cert = NULL;
420 return 0;
421}
422
423/*
424 * Deallocate the memory used by the X509Name object
425 *
426 * Arguments: self - The X509Name object
427 * Returns: None
428 */
429static void
430crypto_X509Name_dealloc(crypto_X509NameObj *self)
431{
432 PyObject_GC_UnTrack(self);
433 /* Sometimes we don't have to dealloc this */
434 if (self->dealloc)
435 X509_NAME_free(self->x509_name);
436
437 crypto_X509Name_clear(self);
438
439 PyObject_GC_Del(self);
440}
441
Jean-Paul Calderone110cd092008-03-24 17:27:42 -0400442/*
443 * ADD_METHOD(name) expands to a correct PyMethodDef declaration
444 * { 'name', (PyCFunction)crypto_X509_name, METH_VARARGS }
445 * for convenience
446 */
447#define ADD_METHOD(name) \
448 { #name, (PyCFunction)crypto_X509Name_##name, METH_VARARGS, crypto_X509Name_##name##_doc }
449static PyMethodDef crypto_X509Name_methods[] =
450{
451 ADD_METHOD(hash),
Jean-Paul Calderonee957a002008-03-25 15:16:51 -0400452 ADD_METHOD(der),
Jean-Paul Calderonec54cc182008-03-26 21:11:07 -0400453 ADD_METHOD(get_components),
Jean-Paul Calderone110cd092008-03-24 17:27:42 -0400454 { NULL, NULL }
455};
456#undef ADD_METHOD
457
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500458PyTypeObject crypto_X509Name_Type = {
Jean-Paul Calderone4d0c3212010-08-10 22:57:42 -0400459 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500460 "X509Name",
461 sizeof(crypto_X509NameObj),
462 0,
463 (destructor)crypto_X509Name_dealloc,
464 NULL, /* print */
Jean-Paul Calderone0f31ec52010-08-11 22:56:42 -0400465 NULL, /* getattr */
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500466 (setattrfunc)crypto_X509Name_setattr,
Jean-Paul Calderone4d0c3212010-08-10 22:57:42 -0400467 NULL, /* reserved */
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500468 (reprfunc)crypto_X509Name_repr,
469 NULL, /* as_number */
470 NULL, /* as_sequence */
471 NULL, /* as_mapping */
472 NULL, /* hash */
473 NULL, /* call */
474 NULL, /* str */
Jean-Paul Calderone0f31ec52010-08-11 22:56:42 -0400475 (getattrofunc)crypto_X509Name_getattro, /* getattro */
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500476 NULL, /* setattro */
477 NULL, /* as_buffer */
Jean-Paul Calderone110cd092008-03-24 17:27:42 -0400478 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
Jean-Paul Calderone2cd7a922009-06-27 11:02:46 -0400479 crypto_X509Name_doc, /* tp_doc */
Jean-Paul Calderone110cd092008-03-24 17:27:42 -0400480 (traverseproc)crypto_X509Name_traverse, /* tp_traverse */
481 (inquiry)crypto_X509Name_clear, /* tp_clear */
Jean-Paul Calderone4d0c3212010-08-10 22:57:42 -0400482 crypto_X509Name_richcompare, /* tp_richcompare */
Jean-Paul Calderone2cd7a922009-06-27 11:02:46 -0400483 0, /* tp_weaklistoffset */
484 NULL, /* tp_iter */
485 NULL, /* tp_iternext */
486 crypto_X509Name_methods, /* tp_methods */
487 NULL, /* tp_members */
488 NULL, /* tp_getset */
489 NULL, /* tp_base */
490 NULL, /* tp_dict */
491 NULL, /* tp_descr_get */
492 NULL, /* tp_descr_set */
493 0, /* tp_dictoffset */
494 NULL, /* tp_init */
495 NULL, /* tp_alloc */
496 crypto_X509Name_new, /* tp_new */
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500497};
498
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500499/*
500 * Initialize the X509Name part of the crypto module
501 *
Jean-Paul Calderone2cd7a922009-06-27 11:02:46 -0400502 * Arguments: module - The crypto module
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500503 * Returns: None
504 */
505int
Jean-Paul Calderone2cd7a922009-06-27 11:02:46 -0400506init_crypto_x509name(PyObject *module)
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500507{
Jean-Paul Calderone2cd7a922009-06-27 11:02:46 -0400508 if (PyType_Ready(&crypto_X509Name_Type) < 0) {
509 return 0;
510 }
511
512 if (PyModule_AddObject(module, "X509Name", (PyObject *)&crypto_X509Name_Type) != 0) {
513 return 0;
514 }
515
516 if (PyModule_AddObject(module, "X509NameType", (PyObject *)&crypto_X509Name_Type) != 0) {
517 return 0;
518 }
519
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500520 return 1;
521}