blob: 1c69be1f77a6820035a6609bc57aad7d7c8645d2 [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);
Jean-Paul Calderone072195d2010-08-12 20:09:57 -0400240 break;
Jean-Paul Calderone4d0c3212010-08-10 22:57:42 -0400241
242 case Py_NE:
243 result = (result != 0);
Jean-Paul Calderone072195d2010-08-12 20:09:57 -0400244 break;
Jean-Paul Calderone4d0c3212010-08-10 22:57:42 -0400245
246 case Py_LT:
247 result = (result < 0);
Jean-Paul Calderone072195d2010-08-12 20:09:57 -0400248 break;
Jean-Paul Calderone4d0c3212010-08-10 22:57:42 -0400249
250 case Py_LE:
251 result = (result <= 0);
Jean-Paul Calderone072195d2010-08-12 20:09:57 -0400252 break;
Jean-Paul Calderone4d0c3212010-08-10 22:57:42 -0400253
254 case Py_GT:
255 result = (result > 0);
Jean-Paul Calderone072195d2010-08-12 20:09:57 -0400256 break;
Jean-Paul Calderone4d0c3212010-08-10 22:57:42 -0400257
258 case Py_GE:
259 result = (result >= 0);
Jean-Paul Calderone072195d2010-08-12 20:09:57 -0400260 break;
Jean-Paul Calderone4d0c3212010-08-10 22:57:42 -0400261
262 default:
263 /* Should be impossible */
264 Py_INCREF(Py_NotImplemented);
265 return Py_NotImplemented;
266 }
267
268 if (result) {
269 Py_INCREF(Py_True);
270 return Py_True;
Jean-Paul Calderone138a3122008-12-30 15:05:38 -0500271 } else {
Jean-Paul Calderone4d0c3212010-08-10 22:57:42 -0400272 Py_INCREF(Py_False);
273 return Py_False;
Jean-Paul Calderone138a3122008-12-30 15:05:38 -0500274 }
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500275}
276
277/*
278 * String representation of an X509Name
279 *
280 * Arguments: self - The X509Name object
281 * Returns: A string representation of the object
282 */
283static PyObject *
284crypto_X509Name_repr(crypto_X509NameObj *self)
285{
286 char tmpbuf[512] = "";
287 char realbuf[512+64];
288
289 if (X509_NAME_oneline(self->x509_name, tmpbuf, 512) == NULL)
290 {
Rick Deand369c932009-07-08 11:48:33 -0500291 exception_from_error_queue(crypto_Error);
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500292 return NULL;
293 }
294 else
295 {
296 /* This is safe because tmpbuf is max 512 characters */
297 sprintf(realbuf, "<X509Name object '%s'>", tmpbuf);
Jean-Paul Calderone4d0c3212010-08-10 22:57:42 -0400298 return PyText_FromString(realbuf);
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500299 }
300}
301
Jean-Paul Calderone110cd092008-03-24 17:27:42 -0400302static char crypto_X509Name_hash_doc[] = "\n\
Jean-Paul Calderone54bcc832009-05-27 14:06:48 -0400303Return the hash value of this name\n\
Jean-Paul Calderone110cd092008-03-24 17:27:42 -0400304\n\
Jean-Paul Calderone54bcc832009-05-27 14:06:48 -0400305@return: None\n\
Jean-Paul Calderone110cd092008-03-24 17:27:42 -0400306";
307
308/*
309 * First four bytes of the MD5 digest of the DER form of an X509Name.
310 *
311 * Arguments: self - The X509Name object
312 * Returns: An integer giving the hash.
313 */
314static PyObject *
315crypto_X509Name_hash(crypto_X509NameObj *self, PyObject* args)
316{
317 unsigned long hash;
318
319 if (!PyArg_ParseTuple(args, ":hash")) {
320 return NULL;
321 }
322 hash = X509_NAME_hash(self->x509_name);
Jean-Paul Calderone4d0c3212010-08-10 22:57:42 -0400323 return PyLong_FromLong(hash);
Jean-Paul Calderone110cd092008-03-24 17:27:42 -0400324}
325
Jean-Paul Calderonee957a002008-03-25 15:16:51 -0400326static char crypto_X509Name_der_doc[] = "\n\
Jean-Paul Calderone54bcc832009-05-27 14:06:48 -0400327Return the DER encoding of this name\n\
Jean-Paul Calderonee957a002008-03-25 15:16:51 -0400328\n\
Jean-Paul Calderone54bcc832009-05-27 14:06:48 -0400329@return: None\n\
Jean-Paul Calderonee957a002008-03-25 15:16:51 -0400330";
331
332/*
333 * Arguments: self - The X509Name object
334 * Returns: The DER form of an X509Name.
335 */
336static PyObject *
337crypto_X509Name_der(crypto_X509NameObj *self, PyObject *args)
338{
339 if (!PyArg_ParseTuple(args, ":der")) {
340 return NULL;
341 }
342
343 i2d_X509_NAME(self->x509_name, 0);
Jean-Paul Calderone4d0c3212010-08-10 22:57:42 -0400344 return PyBytes_FromStringAndSize(self->x509_name->bytes->data,
345 self->x509_name->bytes->length);
Jean-Paul Calderonee957a002008-03-25 15:16:51 -0400346}
347
Jean-Paul Calderone110cd092008-03-24 17:27:42 -0400348
Jean-Paul Calderonec54cc182008-03-26 21:11:07 -0400349static char crypto_X509Name_get_components_doc[] = "\n\
350Returns the split-up components of this name.\n\
351\n\
Jean-Paul Calderone54bcc832009-05-27 14:06:48 -0400352@return: List of tuples (name, value).\n\
Jean-Paul Calderonec54cc182008-03-26 21:11:07 -0400353";
354
355static PyObject *
356crypto_X509Name_get_components(crypto_X509NameObj *self, PyObject *args)
357{
358 int n, i;
359 X509_NAME *name = self->x509_name;
360 PyObject *list;
361
362 if (!PyArg_ParseTuple(args, ":get_components"))
363 return NULL;
364
365 n = X509_NAME_entry_count(name);
366 list = PyList_New(n);
367 for (i = 0; i < n; i++)
368 {
369 X509_NAME_ENTRY *ent;
370 ASN1_OBJECT *fname;
371 ASN1_STRING *fval;
372 int nid;
373 int l;
Jean-Paul Calderonec54cc182008-03-26 21:11:07 -0400374 unsigned char *str;
375 PyObject *tuple;
376
377 ent = X509_NAME_get_entry(name, i);
378
379 fname = X509_NAME_ENTRY_get_object(ent);
380 fval = X509_NAME_ENTRY_get_data(ent);
381
382 l = ASN1_STRING_length(fval);
383 str = ASN1_STRING_data(fval);
384
385 nid = OBJ_obj2nid(fname);
386
387 /* printf("fname is %s len=%d str=%s\n", OBJ_nid2sn(nid), l, str); */
388
389 tuple = PyTuple_New(2);
Jean-Paul Calderone4d0c3212010-08-10 22:57:42 -0400390 PyTuple_SetItem(tuple, 0, PyBytes_FromString(OBJ_nid2sn(nid)));
391 PyTuple_SetItem(tuple, 1, PyBytes_FromStringAndSize((char *)str, l));
Jean-Paul Calderonec54cc182008-03-26 21:11:07 -0400392
393 PyList_SetItem(list, i, tuple);
394 }
395
396 return list;
397}
398
399
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500400/*
401 * Call the visitproc on all contained objects.
402 *
403 * Arguments: self - The Connection object
404 * visit - Function to call
405 * arg - Extra argument to visit
406 * Returns: 0 if all goes well, otherwise the return code from the first
407 * call that gave non-zero result.
408 */
409static int
410crypto_X509Name_traverse(crypto_X509NameObj *self, visitproc visit, void *arg)
411{
412 int ret = 0;
413
414 if (ret == 0 && self->parent_cert != NULL)
415 ret = visit(self->parent_cert, arg);
416 return ret;
417}
418
419/*
420 * Decref all contained objects and zero the pointers.
421 *
422 * Arguments: self - The Connection object
423 * Returns: Always 0.
424 */
425static int
426crypto_X509Name_clear(crypto_X509NameObj *self)
427{
428 Py_XDECREF(self->parent_cert);
429 self->parent_cert = NULL;
430 return 0;
431}
432
433/*
434 * Deallocate the memory used by the X509Name object
435 *
436 * Arguments: self - The X509Name object
437 * Returns: None
438 */
439static void
440crypto_X509Name_dealloc(crypto_X509NameObj *self)
441{
442 PyObject_GC_UnTrack(self);
443 /* Sometimes we don't have to dealloc this */
444 if (self->dealloc)
445 X509_NAME_free(self->x509_name);
446
447 crypto_X509Name_clear(self);
448
449 PyObject_GC_Del(self);
450}
451
Jean-Paul Calderone110cd092008-03-24 17:27:42 -0400452/*
453 * ADD_METHOD(name) expands to a correct PyMethodDef declaration
454 * { 'name', (PyCFunction)crypto_X509_name, METH_VARARGS }
455 * for convenience
456 */
457#define ADD_METHOD(name) \
458 { #name, (PyCFunction)crypto_X509Name_##name, METH_VARARGS, crypto_X509Name_##name##_doc }
459static PyMethodDef crypto_X509Name_methods[] =
460{
461 ADD_METHOD(hash),
Jean-Paul Calderonee957a002008-03-25 15:16:51 -0400462 ADD_METHOD(der),
Jean-Paul Calderonec54cc182008-03-26 21:11:07 -0400463 ADD_METHOD(get_components),
Jean-Paul Calderone110cd092008-03-24 17:27:42 -0400464 { NULL, NULL }
465};
466#undef ADD_METHOD
467
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500468PyTypeObject crypto_X509Name_Type = {
Jean-Paul Calderone3fe7f672010-08-11 23:55:10 -0400469 PyOpenSSL_HEAD_INIT(&PyType_Type, 0)
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500470 "X509Name",
471 sizeof(crypto_X509NameObj),
472 0,
473 (destructor)crypto_X509Name_dealloc,
474 NULL, /* print */
Jean-Paul Calderone0f31ec52010-08-11 22:56:42 -0400475 NULL, /* getattr */
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500476 (setattrfunc)crypto_X509Name_setattr,
Jean-Paul Calderone4d0c3212010-08-10 22:57:42 -0400477 NULL, /* reserved */
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500478 (reprfunc)crypto_X509Name_repr,
479 NULL, /* as_number */
480 NULL, /* as_sequence */
481 NULL, /* as_mapping */
482 NULL, /* hash */
483 NULL, /* call */
484 NULL, /* str */
Jean-Paul Calderone0f31ec52010-08-11 22:56:42 -0400485 (getattrofunc)crypto_X509Name_getattro, /* getattro */
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500486 NULL, /* setattro */
487 NULL, /* as_buffer */
Jean-Paul Calderone110cd092008-03-24 17:27:42 -0400488 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
Jean-Paul Calderone2cd7a922009-06-27 11:02:46 -0400489 crypto_X509Name_doc, /* tp_doc */
Jean-Paul Calderone110cd092008-03-24 17:27:42 -0400490 (traverseproc)crypto_X509Name_traverse, /* tp_traverse */
491 (inquiry)crypto_X509Name_clear, /* tp_clear */
Jean-Paul Calderone4d0c3212010-08-10 22:57:42 -0400492 crypto_X509Name_richcompare, /* tp_richcompare */
Jean-Paul Calderone2cd7a922009-06-27 11:02:46 -0400493 0, /* tp_weaklistoffset */
494 NULL, /* tp_iter */
495 NULL, /* tp_iternext */
496 crypto_X509Name_methods, /* tp_methods */
497 NULL, /* tp_members */
498 NULL, /* tp_getset */
499 NULL, /* tp_base */
500 NULL, /* tp_dict */
501 NULL, /* tp_descr_get */
502 NULL, /* tp_descr_set */
503 0, /* tp_dictoffset */
504 NULL, /* tp_init */
505 NULL, /* tp_alloc */
506 crypto_X509Name_new, /* tp_new */
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500507};
508
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500509/*
510 * Initialize the X509Name part of the crypto module
511 *
Jean-Paul Calderone2cd7a922009-06-27 11:02:46 -0400512 * Arguments: module - The crypto module
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500513 * Returns: None
514 */
515int
Jean-Paul Calderone2cd7a922009-06-27 11:02:46 -0400516init_crypto_x509name(PyObject *module)
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500517{
Jean-Paul Calderone2cd7a922009-06-27 11:02:46 -0400518 if (PyType_Ready(&crypto_X509Name_Type) < 0) {
519 return 0;
520 }
521
522 if (PyModule_AddObject(module, "X509Name", (PyObject *)&crypto_X509Name_Type) != 0) {
523 return 0;
524 }
525
526 if (PyModule_AddObject(module, "X509NameType", (PyObject *)&crypto_X509Name_Type) != 0) {
527 return 0;
528 }
529
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500530 return 1;
531}