blob: c2881805aaa66cd7a301340a4f92e9d8ce9e26cf [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 *
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 Calderone4d0c3212010-08-10 22:57:42 -0400165 return crypto_X509Name_Type.tp_getattr((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 */
Jean-Paul Calderone4d0c3212010-08-10 22:57:42 -0400219static PyObject *
220crypto_X509Name_richcompare(PyObject *n, PyObject *m, int op) {
221 int result;
222
223 if (!crypto_X509Name_Check(n) || !crypto_X509Name_Check(m)) {
224 Py_INCREF(Py_NotImplemented);
225 return Py_NotImplemented;
226 }
227
228 result = X509_NAME_cmp(
229 ((crypto_X509NameObj*)n)->x509_name,
230 ((crypto_X509NameObj*)m)->x509_name);
231
232 switch (op) {
233 case Py_EQ:
234 result = (result == 0);
235
236 case Py_NE:
237 result = (result != 0);
238
239 case Py_LT:
240 result = (result < 0);
241
242 case Py_LE:
243 result = (result <= 0);
244
245 case Py_GT:
246 result = (result > 0);
247
248 case Py_GE:
249 result = (result >= 0);
250
251 default:
252 /* Should be impossible */
253 Py_INCREF(Py_NotImplemented);
254 return Py_NotImplemented;
255 }
256
257 if (result) {
258 Py_INCREF(Py_True);
259 return Py_True;
Jean-Paul Calderone138a3122008-12-30 15:05:38 -0500260 } else {
Jean-Paul Calderone4d0c3212010-08-10 22:57:42 -0400261 Py_INCREF(Py_False);
262 return Py_False;
Jean-Paul Calderone138a3122008-12-30 15:05:38 -0500263 }
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500264}
265
266/*
267 * String representation of an X509Name
268 *
269 * Arguments: self - The X509Name object
270 * Returns: A string representation of the object
271 */
272static PyObject *
273crypto_X509Name_repr(crypto_X509NameObj *self)
274{
275 char tmpbuf[512] = "";
276 char realbuf[512+64];
277
278 if (X509_NAME_oneline(self->x509_name, tmpbuf, 512) == NULL)
279 {
Rick Deand369c932009-07-08 11:48:33 -0500280 exception_from_error_queue(crypto_Error);
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500281 return NULL;
282 }
283 else
284 {
285 /* This is safe because tmpbuf is max 512 characters */
286 sprintf(realbuf, "<X509Name object '%s'>", tmpbuf);
Jean-Paul Calderone4d0c3212010-08-10 22:57:42 -0400287 return PyText_FromString(realbuf);
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500288 }
289}
290
Jean-Paul Calderone110cd092008-03-24 17:27:42 -0400291static char crypto_X509Name_hash_doc[] = "\n\
Jean-Paul Calderone54bcc832009-05-27 14:06:48 -0400292Return the hash value of this name\n\
Jean-Paul Calderone110cd092008-03-24 17:27:42 -0400293\n\
Jean-Paul Calderone54bcc832009-05-27 14:06:48 -0400294@return: None\n\
Jean-Paul Calderone110cd092008-03-24 17:27:42 -0400295";
296
297/*
298 * First four bytes of the MD5 digest of the DER form of an X509Name.
299 *
300 * Arguments: self - The X509Name object
301 * Returns: An integer giving the hash.
302 */
303static PyObject *
304crypto_X509Name_hash(crypto_X509NameObj *self, PyObject* args)
305{
306 unsigned long hash;
307
308 if (!PyArg_ParseTuple(args, ":hash")) {
309 return NULL;
310 }
311 hash = X509_NAME_hash(self->x509_name);
Jean-Paul Calderone4d0c3212010-08-10 22:57:42 -0400312 return PyLong_FromLong(hash);
Jean-Paul Calderone110cd092008-03-24 17:27:42 -0400313}
314
Jean-Paul Calderonee957a002008-03-25 15:16:51 -0400315static char crypto_X509Name_der_doc[] = "\n\
Jean-Paul Calderone54bcc832009-05-27 14:06:48 -0400316Return the DER encoding of this name\n\
Jean-Paul Calderonee957a002008-03-25 15:16:51 -0400317\n\
Jean-Paul Calderone54bcc832009-05-27 14:06:48 -0400318@return: None\n\
Jean-Paul Calderonee957a002008-03-25 15:16:51 -0400319";
320
321/*
322 * Arguments: self - The X509Name object
323 * Returns: The DER form of an X509Name.
324 */
325static PyObject *
326crypto_X509Name_der(crypto_X509NameObj *self, PyObject *args)
327{
328 if (!PyArg_ParseTuple(args, ":der")) {
329 return NULL;
330 }
331
332 i2d_X509_NAME(self->x509_name, 0);
Jean-Paul Calderone4d0c3212010-08-10 22:57:42 -0400333 return PyBytes_FromStringAndSize(self->x509_name->bytes->data,
334 self->x509_name->bytes->length);
Jean-Paul Calderonee957a002008-03-25 15:16:51 -0400335}
336
Jean-Paul Calderone110cd092008-03-24 17:27:42 -0400337
Jean-Paul Calderonec54cc182008-03-26 21:11:07 -0400338static char crypto_X509Name_get_components_doc[] = "\n\
339Returns the split-up components of this name.\n\
340\n\
Jean-Paul Calderone54bcc832009-05-27 14:06:48 -0400341@return: List of tuples (name, value).\n\
Jean-Paul Calderonec54cc182008-03-26 21:11:07 -0400342";
343
344static PyObject *
345crypto_X509Name_get_components(crypto_X509NameObj *self, PyObject *args)
346{
347 int n, i;
348 X509_NAME *name = self->x509_name;
349 PyObject *list;
350
351 if (!PyArg_ParseTuple(args, ":get_components"))
352 return NULL;
353
354 n = X509_NAME_entry_count(name);
355 list = PyList_New(n);
356 for (i = 0; i < n; i++)
357 {
358 X509_NAME_ENTRY *ent;
359 ASN1_OBJECT *fname;
360 ASN1_STRING *fval;
361 int nid;
362 int l;
Jean-Paul Calderonec54cc182008-03-26 21:11:07 -0400363 unsigned char *str;
364 PyObject *tuple;
365
366 ent = X509_NAME_get_entry(name, i);
367
368 fname = X509_NAME_ENTRY_get_object(ent);
369 fval = X509_NAME_ENTRY_get_data(ent);
370
371 l = ASN1_STRING_length(fval);
372 str = ASN1_STRING_data(fval);
373
374 nid = OBJ_obj2nid(fname);
375
376 /* printf("fname is %s len=%d str=%s\n", OBJ_nid2sn(nid), l, str); */
377
378 tuple = PyTuple_New(2);
Jean-Paul Calderone4d0c3212010-08-10 22:57:42 -0400379 PyTuple_SetItem(tuple, 0, PyBytes_FromString(OBJ_nid2sn(nid)));
380 PyTuple_SetItem(tuple, 1, PyBytes_FromStringAndSize((char *)str, l));
Jean-Paul Calderonec54cc182008-03-26 21:11:07 -0400381
382 PyList_SetItem(list, i, tuple);
383 }
384
385 return list;
386}
387
388
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500389/*
390 * Call the visitproc on all contained objects.
391 *
392 * Arguments: self - The Connection object
393 * visit - Function to call
394 * arg - Extra argument to visit
395 * Returns: 0 if all goes well, otherwise the return code from the first
396 * call that gave non-zero result.
397 */
398static int
399crypto_X509Name_traverse(crypto_X509NameObj *self, visitproc visit, void *arg)
400{
401 int ret = 0;
402
403 if (ret == 0 && self->parent_cert != NULL)
404 ret = visit(self->parent_cert, arg);
405 return ret;
406}
407
408/*
409 * Decref all contained objects and zero the pointers.
410 *
411 * Arguments: self - The Connection object
412 * Returns: Always 0.
413 */
414static int
415crypto_X509Name_clear(crypto_X509NameObj *self)
416{
417 Py_XDECREF(self->parent_cert);
418 self->parent_cert = NULL;
419 return 0;
420}
421
422/*
423 * Deallocate the memory used by the X509Name object
424 *
425 * Arguments: self - The X509Name object
426 * Returns: None
427 */
428static void
429crypto_X509Name_dealloc(crypto_X509NameObj *self)
430{
431 PyObject_GC_UnTrack(self);
432 /* Sometimes we don't have to dealloc this */
433 if (self->dealloc)
434 X509_NAME_free(self->x509_name);
435
436 crypto_X509Name_clear(self);
437
438 PyObject_GC_Del(self);
439}
440
Jean-Paul Calderone110cd092008-03-24 17:27:42 -0400441/*
442 * ADD_METHOD(name) expands to a correct PyMethodDef declaration
443 * { 'name', (PyCFunction)crypto_X509_name, METH_VARARGS }
444 * for convenience
445 */
446#define ADD_METHOD(name) \
447 { #name, (PyCFunction)crypto_X509Name_##name, METH_VARARGS, crypto_X509Name_##name##_doc }
448static PyMethodDef crypto_X509Name_methods[] =
449{
450 ADD_METHOD(hash),
Jean-Paul Calderonee957a002008-03-25 15:16:51 -0400451 ADD_METHOD(der),
Jean-Paul Calderonec54cc182008-03-26 21:11:07 -0400452 ADD_METHOD(get_components),
Jean-Paul Calderone110cd092008-03-24 17:27:42 -0400453 { NULL, NULL }
454};
455#undef ADD_METHOD
456
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500457PyTypeObject crypto_X509Name_Type = {
Jean-Paul Calderone4d0c3212010-08-10 22:57:42 -0400458 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500459 "X509Name",
460 sizeof(crypto_X509NameObj),
461 0,
462 (destructor)crypto_X509Name_dealloc,
463 NULL, /* print */
464 (getattrfunc)crypto_X509Name_getattr,
465 (setattrfunc)crypto_X509Name_setattr,
Jean-Paul Calderone4d0c3212010-08-10 22:57:42 -0400466 NULL, /* reserved */
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500467 (reprfunc)crypto_X509Name_repr,
468 NULL, /* as_number */
469 NULL, /* as_sequence */
470 NULL, /* as_mapping */
471 NULL, /* hash */
472 NULL, /* call */
473 NULL, /* str */
474 NULL, /* getattro */
475 NULL, /* setattro */
476 NULL, /* as_buffer */
Jean-Paul Calderone110cd092008-03-24 17:27:42 -0400477 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
Jean-Paul Calderone2cd7a922009-06-27 11:02:46 -0400478 crypto_X509Name_doc, /* tp_doc */
Jean-Paul Calderone110cd092008-03-24 17:27:42 -0400479 (traverseproc)crypto_X509Name_traverse, /* tp_traverse */
480 (inquiry)crypto_X509Name_clear, /* tp_clear */
Jean-Paul Calderone4d0c3212010-08-10 22:57:42 -0400481 crypto_X509Name_richcompare, /* tp_richcompare */
Jean-Paul Calderone2cd7a922009-06-27 11:02:46 -0400482 0, /* tp_weaklistoffset */
483 NULL, /* tp_iter */
484 NULL, /* tp_iternext */
485 crypto_X509Name_methods, /* tp_methods */
486 NULL, /* tp_members */
487 NULL, /* tp_getset */
488 NULL, /* tp_base */
489 NULL, /* tp_dict */
490 NULL, /* tp_descr_get */
491 NULL, /* tp_descr_set */
492 0, /* tp_dictoffset */
493 NULL, /* tp_init */
494 NULL, /* tp_alloc */
495 crypto_X509Name_new, /* tp_new */
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500496};
497
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500498/*
499 * Initialize the X509Name part of the crypto module
500 *
Jean-Paul Calderone2cd7a922009-06-27 11:02:46 -0400501 * Arguments: module - The crypto module
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500502 * Returns: None
503 */
504int
Jean-Paul Calderone2cd7a922009-06-27 11:02:46 -0400505init_crypto_x509name(PyObject *module)
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500506{
Jean-Paul Calderone2cd7a922009-06-27 11:02:46 -0400507 if (PyType_Ready(&crypto_X509Name_Type) < 0) {
508 return 0;
509 }
510
511 if (PyModule_AddObject(module, "X509Name", (PyObject *)&crypto_X509Name_Type) != 0) {
512 return 0;
513 }
514
515 if (PyModule_AddObject(module, "X509NameType", (PyObject *)&crypto_X509Name_Type) != 0) {
516 return 0;
517 }
518
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500519 return 1;
520}