blob: 7cb7fedf43bce86b81250d9aa338193abd2c1960 [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 Calderone3fe7f672010-08-11 23:55:10 -0400155 char *name;
156#ifdef PY3
157 nameobj = PyUnicode_AsASCIIString(nameobj);
158#endif
159 name = PyBytes_AsString(nameobj);
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500160
Jean-Paul Calderoneef414d52009-07-16 16:21:04 -0400161 if ((nid = OBJ_txt2nid(name)) == NID_undef) {
162 /*
163 * This is a bit weird. OBJ_txt2nid indicated failure, but it seems
164 * a lower level function, a2d_ASN1_OBJECT, also feels the need to
165 * push something onto the error queue. If we don't clean that up
166 * now, someone else will bump into it later and be quite confused.
167 * See lp#314814.
168 */
169 flush_error_queue();
Jean-Paul Calderone0f31ec52010-08-11 22:56:42 -0400170 return PyObject_GenericGetAttr((PyObject*)self, nameobj);
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500171 }
172
173 len = get_name_by_nid(self->x509_name, nid, &utf8string);
174 if (len < 0)
175 return NULL;
176 else if (len == 0)
177 {
178 Py_INCREF(Py_None);
179 return Py_None;
180 }
Jean-Paul Calderone5b8c5ee2008-02-19 00:43:02 -0500181 else {
182 PyObject* result = PyUnicode_Decode(utf8string, len, "utf-8", NULL);
183 OPENSSL_free(utf8string);
184 return result;
185 }
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500186}
187
188/*
189 * Set attribute
190 *
191 * Arguments: self - The X509Name object
192 * name - The attribute name
193 * value - The value to set
194 */
195static int
196crypto_X509Name_setattr(crypto_X509NameObj *self, char *name, PyObject *value)
197{
198 int nid;
Jean-Paul Calderone7b0443a2008-02-19 00:25:30 -0500199 int result;
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500200 char *buffer;
201
202 if ((nid = OBJ_txt2nid(name)) == NID_undef)
203 {
204 PyErr_SetString(PyExc_AttributeError, "No such attribute");
205 return -1;
206 }
207
208 /* Something of a hack to get nice unicode behaviour */
209 if (!PyArg_Parse(value, "es:setattr", "utf-8", &buffer))
210 return -1;
Jean-Paul Calderone5b8c5ee2008-02-19 00:43:02 -0500211
Jean-Paul Calderone7b0443a2008-02-19 00:25:30 -0500212 result = set_name_by_nid(self->x509_name, nid, buffer);
213 PyMem_Free(buffer);
214 return result;
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500215}
216
217/*
218 * Compare two X509Name structures.
219 *
220 * Arguments: n - The first X509Name
221 * m - The second X509Name
222 * Returns: <0 if n < m, 0 if n == m and >0 if n > m
223 */
Jean-Paul Calderone4d0c3212010-08-10 22:57:42 -0400224static PyObject *
225crypto_X509Name_richcompare(PyObject *n, PyObject *m, int op) {
226 int result;
227
228 if (!crypto_X509Name_Check(n) || !crypto_X509Name_Check(m)) {
229 Py_INCREF(Py_NotImplemented);
230 return Py_NotImplemented;
231 }
232
233 result = X509_NAME_cmp(
234 ((crypto_X509NameObj*)n)->x509_name,
235 ((crypto_X509NameObj*)m)->x509_name);
236
237 switch (op) {
238 case Py_EQ:
239 result = (result == 0);
240
241 case Py_NE:
242 result = (result != 0);
243
244 case Py_LT:
245 result = (result < 0);
246
247 case Py_LE:
248 result = (result <= 0);
249
250 case Py_GT:
251 result = (result > 0);
252
253 case Py_GE:
254 result = (result >= 0);
255
256 default:
257 /* Should be impossible */
258 Py_INCREF(Py_NotImplemented);
259 return Py_NotImplemented;
260 }
261
262 if (result) {
263 Py_INCREF(Py_True);
264 return Py_True;
Jean-Paul Calderone138a3122008-12-30 15:05:38 -0500265 } else {
Jean-Paul Calderone4d0c3212010-08-10 22:57:42 -0400266 Py_INCREF(Py_False);
267 return Py_False;
Jean-Paul Calderone138a3122008-12-30 15:05:38 -0500268 }
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500269}
270
271/*
272 * String representation of an X509Name
273 *
274 * Arguments: self - The X509Name object
275 * Returns: A string representation of the object
276 */
277static PyObject *
278crypto_X509Name_repr(crypto_X509NameObj *self)
279{
280 char tmpbuf[512] = "";
281 char realbuf[512+64];
282
283 if (X509_NAME_oneline(self->x509_name, tmpbuf, 512) == NULL)
284 {
Rick Deand369c932009-07-08 11:48:33 -0500285 exception_from_error_queue(crypto_Error);
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500286 return NULL;
287 }
288 else
289 {
290 /* This is safe because tmpbuf is max 512 characters */
291 sprintf(realbuf, "<X509Name object '%s'>", tmpbuf);
Jean-Paul Calderone4d0c3212010-08-10 22:57:42 -0400292 return PyText_FromString(realbuf);
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500293 }
294}
295
Jean-Paul Calderone110cd092008-03-24 17:27:42 -0400296static char crypto_X509Name_hash_doc[] = "\n\
Jean-Paul Calderone54bcc832009-05-27 14:06:48 -0400297Return the hash value of this name\n\
Jean-Paul Calderone110cd092008-03-24 17:27:42 -0400298\n\
Jean-Paul Calderone54bcc832009-05-27 14:06:48 -0400299@return: None\n\
Jean-Paul Calderone110cd092008-03-24 17:27:42 -0400300";
301
302/*
303 * First four bytes of the MD5 digest of the DER form of an X509Name.
304 *
305 * Arguments: self - The X509Name object
306 * Returns: An integer giving the hash.
307 */
308static PyObject *
309crypto_X509Name_hash(crypto_X509NameObj *self, PyObject* args)
310{
311 unsigned long hash;
312
313 if (!PyArg_ParseTuple(args, ":hash")) {
314 return NULL;
315 }
316 hash = X509_NAME_hash(self->x509_name);
Jean-Paul Calderone4d0c3212010-08-10 22:57:42 -0400317 return PyLong_FromLong(hash);
Jean-Paul Calderone110cd092008-03-24 17:27:42 -0400318}
319
Jean-Paul Calderonee957a002008-03-25 15:16:51 -0400320static char crypto_X509Name_der_doc[] = "\n\
Jean-Paul Calderone54bcc832009-05-27 14:06:48 -0400321Return the DER encoding of this name\n\
Jean-Paul Calderonee957a002008-03-25 15:16:51 -0400322\n\
Jean-Paul Calderone54bcc832009-05-27 14:06:48 -0400323@return: None\n\
Jean-Paul Calderonee957a002008-03-25 15:16:51 -0400324";
325
326/*
327 * Arguments: self - The X509Name object
328 * Returns: The DER form of an X509Name.
329 */
330static PyObject *
331crypto_X509Name_der(crypto_X509NameObj *self, PyObject *args)
332{
333 if (!PyArg_ParseTuple(args, ":der")) {
334 return NULL;
335 }
336
337 i2d_X509_NAME(self->x509_name, 0);
Jean-Paul Calderone4d0c3212010-08-10 22:57:42 -0400338 return PyBytes_FromStringAndSize(self->x509_name->bytes->data,
339 self->x509_name->bytes->length);
Jean-Paul Calderonee957a002008-03-25 15:16:51 -0400340}
341
Jean-Paul Calderone110cd092008-03-24 17:27:42 -0400342
Jean-Paul Calderonec54cc182008-03-26 21:11:07 -0400343static char crypto_X509Name_get_components_doc[] = "\n\
344Returns the split-up components of this name.\n\
345\n\
Jean-Paul Calderone54bcc832009-05-27 14:06:48 -0400346@return: List of tuples (name, value).\n\
Jean-Paul Calderonec54cc182008-03-26 21:11:07 -0400347";
348
349static PyObject *
350crypto_X509Name_get_components(crypto_X509NameObj *self, PyObject *args)
351{
352 int n, i;
353 X509_NAME *name = self->x509_name;
354 PyObject *list;
355
356 if (!PyArg_ParseTuple(args, ":get_components"))
357 return NULL;
358
359 n = X509_NAME_entry_count(name);
360 list = PyList_New(n);
361 for (i = 0; i < n; i++)
362 {
363 X509_NAME_ENTRY *ent;
364 ASN1_OBJECT *fname;
365 ASN1_STRING *fval;
366 int nid;
367 int l;
Jean-Paul Calderonec54cc182008-03-26 21:11:07 -0400368 unsigned char *str;
369 PyObject *tuple;
370
371 ent = X509_NAME_get_entry(name, i);
372
373 fname = X509_NAME_ENTRY_get_object(ent);
374 fval = X509_NAME_ENTRY_get_data(ent);
375
376 l = ASN1_STRING_length(fval);
377 str = ASN1_STRING_data(fval);
378
379 nid = OBJ_obj2nid(fname);
380
381 /* printf("fname is %s len=%d str=%s\n", OBJ_nid2sn(nid), l, str); */
382
383 tuple = PyTuple_New(2);
Jean-Paul Calderone4d0c3212010-08-10 22:57:42 -0400384 PyTuple_SetItem(tuple, 0, PyBytes_FromString(OBJ_nid2sn(nid)));
385 PyTuple_SetItem(tuple, 1, PyBytes_FromStringAndSize((char *)str, l));
Jean-Paul Calderonec54cc182008-03-26 21:11:07 -0400386
387 PyList_SetItem(list, i, tuple);
388 }
389
390 return list;
391}
392
393
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500394/*
395 * Call the visitproc on all contained objects.
396 *
397 * Arguments: self - The Connection object
398 * visit - Function to call
399 * arg - Extra argument to visit
400 * Returns: 0 if all goes well, otherwise the return code from the first
401 * call that gave non-zero result.
402 */
403static int
404crypto_X509Name_traverse(crypto_X509NameObj *self, visitproc visit, void *arg)
405{
406 int ret = 0;
407
408 if (ret == 0 && self->parent_cert != NULL)
409 ret = visit(self->parent_cert, arg);
410 return ret;
411}
412
413/*
414 * Decref all contained objects and zero the pointers.
415 *
416 * Arguments: self - The Connection object
417 * Returns: Always 0.
418 */
419static int
420crypto_X509Name_clear(crypto_X509NameObj *self)
421{
422 Py_XDECREF(self->parent_cert);
423 self->parent_cert = NULL;
424 return 0;
425}
426
427/*
428 * Deallocate the memory used by the X509Name object
429 *
430 * Arguments: self - The X509Name object
431 * Returns: None
432 */
433static void
434crypto_X509Name_dealloc(crypto_X509NameObj *self)
435{
436 PyObject_GC_UnTrack(self);
437 /* Sometimes we don't have to dealloc this */
438 if (self->dealloc)
439 X509_NAME_free(self->x509_name);
440
441 crypto_X509Name_clear(self);
442
443 PyObject_GC_Del(self);
444}
445
Jean-Paul Calderone110cd092008-03-24 17:27:42 -0400446/*
447 * ADD_METHOD(name) expands to a correct PyMethodDef declaration
448 * { 'name', (PyCFunction)crypto_X509_name, METH_VARARGS }
449 * for convenience
450 */
451#define ADD_METHOD(name) \
452 { #name, (PyCFunction)crypto_X509Name_##name, METH_VARARGS, crypto_X509Name_##name##_doc }
453static PyMethodDef crypto_X509Name_methods[] =
454{
455 ADD_METHOD(hash),
Jean-Paul Calderonee957a002008-03-25 15:16:51 -0400456 ADD_METHOD(der),
Jean-Paul Calderonec54cc182008-03-26 21:11:07 -0400457 ADD_METHOD(get_components),
Jean-Paul Calderone110cd092008-03-24 17:27:42 -0400458 { NULL, NULL }
459};
460#undef ADD_METHOD
461
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500462PyTypeObject crypto_X509Name_Type = {
Jean-Paul Calderone3fe7f672010-08-11 23:55:10 -0400463 PyOpenSSL_HEAD_INIT(&PyType_Type, 0)
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500464 "X509Name",
465 sizeof(crypto_X509NameObj),
466 0,
467 (destructor)crypto_X509Name_dealloc,
468 NULL, /* print */
Jean-Paul Calderone0f31ec52010-08-11 22:56:42 -0400469 NULL, /* getattr */
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500470 (setattrfunc)crypto_X509Name_setattr,
Jean-Paul Calderone4d0c3212010-08-10 22:57:42 -0400471 NULL, /* reserved */
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500472 (reprfunc)crypto_X509Name_repr,
473 NULL, /* as_number */
474 NULL, /* as_sequence */
475 NULL, /* as_mapping */
476 NULL, /* hash */
477 NULL, /* call */
478 NULL, /* str */
Jean-Paul Calderone0f31ec52010-08-11 22:56:42 -0400479 (getattrofunc)crypto_X509Name_getattro, /* getattro */
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500480 NULL, /* setattro */
481 NULL, /* as_buffer */
Jean-Paul Calderone110cd092008-03-24 17:27:42 -0400482 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
Jean-Paul Calderone2cd7a922009-06-27 11:02:46 -0400483 crypto_X509Name_doc, /* tp_doc */
Jean-Paul Calderone110cd092008-03-24 17:27:42 -0400484 (traverseproc)crypto_X509Name_traverse, /* tp_traverse */
485 (inquiry)crypto_X509Name_clear, /* tp_clear */
Jean-Paul Calderone4d0c3212010-08-10 22:57:42 -0400486 crypto_X509Name_richcompare, /* tp_richcompare */
Jean-Paul Calderone2cd7a922009-06-27 11:02:46 -0400487 0, /* tp_weaklistoffset */
488 NULL, /* tp_iter */
489 NULL, /* tp_iternext */
490 crypto_X509Name_methods, /* tp_methods */
491 NULL, /* tp_members */
492 NULL, /* tp_getset */
493 NULL, /* tp_base */
494 NULL, /* tp_dict */
495 NULL, /* tp_descr_get */
496 NULL, /* tp_descr_set */
497 0, /* tp_dictoffset */
498 NULL, /* tp_init */
499 NULL, /* tp_alloc */
500 crypto_X509Name_new, /* tp_new */
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500501};
502
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500503/*
504 * Initialize the X509Name part of the crypto module
505 *
Jean-Paul Calderone2cd7a922009-06-27 11:02:46 -0400506 * Arguments: module - The crypto module
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500507 * Returns: None
508 */
509int
Jean-Paul Calderone2cd7a922009-06-27 11:02:46 -0400510init_crypto_x509name(PyObject *module)
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500511{
Jean-Paul Calderone2cd7a922009-06-27 11:02:46 -0400512 if (PyType_Ready(&crypto_X509Name_Type) < 0) {
513 return 0;
514 }
515
516 if (PyModule_AddObject(module, "X509Name", (PyObject *)&crypto_X509Name_Type) != 0) {
517 return 0;
518 }
519
520 if (PyModule_AddObject(module, "X509NameType", (PyObject *)&crypto_X509Name_Type) != 0) {
521 return 0;
522 }
523
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500524 return 1;
525}