blob: e10c5a5fccc729f113169f4e4415543ec8472a59 [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
Jean-Paul Calderonedc3275f2010-08-22 17:04:09 -0400157 name = PyBytes_AsString(PyUnicode_AsASCIIString(nameobj));
158#else
Jean-Paul Calderone3fe7f672010-08-11 23:55:10 -0400159 name = PyBytes_AsString(nameobj);
Jean-Paul Calderonedc3275f2010-08-22 17:04:09 -0400160#endif
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500161
Jean-Paul Calderoneef414d52009-07-16 16:21:04 -0400162 if ((nid = OBJ_txt2nid(name)) == NID_undef) {
163 /*
164 * This is a bit weird. OBJ_txt2nid indicated failure, but it seems
165 * a lower level function, a2d_ASN1_OBJECT, also feels the need to
166 * push something onto the error queue. If we don't clean that up
167 * now, someone else will bump into it later and be quite confused.
168 * See lp#314814.
169 */
170 flush_error_queue();
Jean-Paul Calderone0f31ec52010-08-11 22:56:42 -0400171 return PyObject_GenericGetAttr((PyObject*)self, nameobj);
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500172 }
173
174 len = get_name_by_nid(self->x509_name, nid, &utf8string);
175 if (len < 0)
176 return NULL;
177 else if (len == 0)
178 {
179 Py_INCREF(Py_None);
180 return Py_None;
181 }
Jean-Paul Calderone5b8c5ee2008-02-19 00:43:02 -0500182 else {
183 PyObject* result = PyUnicode_Decode(utf8string, len, "utf-8", NULL);
184 OPENSSL_free(utf8string);
185 return result;
186 }
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500187}
188
189/*
190 * Set attribute
191 *
192 * Arguments: self - The X509Name object
193 * name - The attribute name
194 * value - The value to set
195 */
196static int
197crypto_X509Name_setattr(crypto_X509NameObj *self, char *name, PyObject *value)
198{
199 int nid;
Jean-Paul Calderone7b0443a2008-02-19 00:25:30 -0500200 int result;
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500201 char *buffer;
202
203 if ((nid = OBJ_txt2nid(name)) == NID_undef)
204 {
205 PyErr_SetString(PyExc_AttributeError, "No such attribute");
206 return -1;
207 }
208
209 /* Something of a hack to get nice unicode behaviour */
210 if (!PyArg_Parse(value, "es:setattr", "utf-8", &buffer))
211 return -1;
Jean-Paul Calderone5b8c5ee2008-02-19 00:43:02 -0500212
Jean-Paul Calderone7b0443a2008-02-19 00:25:30 -0500213 result = set_name_by_nid(self->x509_name, nid, buffer);
214 PyMem_Free(buffer);
215 return result;
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500216}
217
218/*
219 * Compare two X509Name structures.
220 *
221 * Arguments: n - The first X509Name
222 * m - The second X509Name
223 * Returns: <0 if n < m, 0 if n == m and >0 if n > m
224 */
Jean-Paul Calderone4d0c3212010-08-10 22:57:42 -0400225static PyObject *
226crypto_X509Name_richcompare(PyObject *n, PyObject *m, int op) {
227 int result;
228
229 if (!crypto_X509Name_Check(n) || !crypto_X509Name_Check(m)) {
230 Py_INCREF(Py_NotImplemented);
231 return Py_NotImplemented;
232 }
233
234 result = X509_NAME_cmp(
235 ((crypto_X509NameObj*)n)->x509_name,
236 ((crypto_X509NameObj*)m)->x509_name);
237
238 switch (op) {
239 case Py_EQ:
240 result = (result == 0);
Jean-Paul Calderone072195d2010-08-12 20:09:57 -0400241 break;
Jean-Paul Calderone4d0c3212010-08-10 22:57:42 -0400242
243 case Py_NE:
244 result = (result != 0);
Jean-Paul Calderone072195d2010-08-12 20:09:57 -0400245 break;
Jean-Paul Calderone4d0c3212010-08-10 22:57:42 -0400246
247 case Py_LT:
248 result = (result < 0);
Jean-Paul Calderone072195d2010-08-12 20:09:57 -0400249 break;
Jean-Paul Calderone4d0c3212010-08-10 22:57:42 -0400250
251 case Py_LE:
252 result = (result <= 0);
Jean-Paul Calderone072195d2010-08-12 20:09:57 -0400253 break;
Jean-Paul Calderone4d0c3212010-08-10 22:57:42 -0400254
255 case Py_GT:
256 result = (result > 0);
Jean-Paul Calderone072195d2010-08-12 20:09:57 -0400257 break;
Jean-Paul Calderone4d0c3212010-08-10 22:57:42 -0400258
259 case Py_GE:
260 result = (result >= 0);
Jean-Paul Calderone072195d2010-08-12 20:09:57 -0400261 break;
Jean-Paul Calderone4d0c3212010-08-10 22:57:42 -0400262
263 default:
264 /* Should be impossible */
265 Py_INCREF(Py_NotImplemented);
266 return Py_NotImplemented;
267 }
268
269 if (result) {
270 Py_INCREF(Py_True);
271 return Py_True;
Jean-Paul Calderone138a3122008-12-30 15:05:38 -0500272 } else {
Jean-Paul Calderone4d0c3212010-08-10 22:57:42 -0400273 Py_INCREF(Py_False);
274 return Py_False;
Jean-Paul Calderone138a3122008-12-30 15:05:38 -0500275 }
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500276}
277
278/*
279 * String representation of an X509Name
280 *
281 * Arguments: self - The X509Name object
282 * Returns: A string representation of the object
283 */
284static PyObject *
285crypto_X509Name_repr(crypto_X509NameObj *self)
286{
287 char tmpbuf[512] = "";
288 char realbuf[512+64];
289
290 if (X509_NAME_oneline(self->x509_name, tmpbuf, 512) == NULL)
291 {
Rick Deand369c932009-07-08 11:48:33 -0500292 exception_from_error_queue(crypto_Error);
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500293 return NULL;
294 }
295 else
296 {
297 /* This is safe because tmpbuf is max 512 characters */
298 sprintf(realbuf, "<X509Name object '%s'>", tmpbuf);
Jean-Paul Calderone4d0c3212010-08-10 22:57:42 -0400299 return PyText_FromString(realbuf);
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500300 }
301}
302
Jean-Paul Calderone110cd092008-03-24 17:27:42 -0400303static char crypto_X509Name_hash_doc[] = "\n\
Jean-Paul Calderone54bcc832009-05-27 14:06:48 -0400304Return the hash value of this name\n\
Jean-Paul Calderone110cd092008-03-24 17:27:42 -0400305\n\
Jean-Paul Calderone54bcc832009-05-27 14:06:48 -0400306@return: None\n\
Jean-Paul Calderone110cd092008-03-24 17:27:42 -0400307";
308
309/*
310 * First four bytes of the MD5 digest of the DER form of an X509Name.
311 *
312 * Arguments: self - The X509Name object
313 * Returns: An integer giving the hash.
314 */
315static PyObject *
316crypto_X509Name_hash(crypto_X509NameObj *self, PyObject* args)
317{
318 unsigned long hash;
319
320 if (!PyArg_ParseTuple(args, ":hash")) {
321 return NULL;
322 }
323 hash = X509_NAME_hash(self->x509_name);
Jean-Paul Calderone4d0c3212010-08-10 22:57:42 -0400324 return PyLong_FromLong(hash);
Jean-Paul Calderone110cd092008-03-24 17:27:42 -0400325}
326
Jean-Paul Calderonee957a002008-03-25 15:16:51 -0400327static char crypto_X509Name_der_doc[] = "\n\
Jean-Paul Calderone54bcc832009-05-27 14:06:48 -0400328Return the DER encoding of this name\n\
Jean-Paul Calderonee957a002008-03-25 15:16:51 -0400329\n\
Jean-Paul Calderone54bcc832009-05-27 14:06:48 -0400330@return: None\n\
Jean-Paul Calderonee957a002008-03-25 15:16:51 -0400331";
332
333/*
334 * Arguments: self - The X509Name object
335 * Returns: The DER form of an X509Name.
336 */
337static PyObject *
338crypto_X509Name_der(crypto_X509NameObj *self, PyObject *args)
339{
340 if (!PyArg_ParseTuple(args, ":der")) {
341 return NULL;
342 }
343
344 i2d_X509_NAME(self->x509_name, 0);
Jean-Paul Calderone4d0c3212010-08-10 22:57:42 -0400345 return PyBytes_FromStringAndSize(self->x509_name->bytes->data,
346 self->x509_name->bytes->length);
Jean-Paul Calderonee957a002008-03-25 15:16:51 -0400347}
348
Jean-Paul Calderone110cd092008-03-24 17:27:42 -0400349
Jean-Paul Calderonec54cc182008-03-26 21:11:07 -0400350static char crypto_X509Name_get_components_doc[] = "\n\
351Returns the split-up components of this name.\n\
352\n\
Jean-Paul Calderone54bcc832009-05-27 14:06:48 -0400353@return: List of tuples (name, value).\n\
Jean-Paul Calderonec54cc182008-03-26 21:11:07 -0400354";
355
356static PyObject *
357crypto_X509Name_get_components(crypto_X509NameObj *self, PyObject *args)
358{
359 int n, i;
360 X509_NAME *name = self->x509_name;
361 PyObject *list;
362
363 if (!PyArg_ParseTuple(args, ":get_components"))
364 return NULL;
365
366 n = X509_NAME_entry_count(name);
367 list = PyList_New(n);
368 for (i = 0; i < n; i++)
369 {
370 X509_NAME_ENTRY *ent;
371 ASN1_OBJECT *fname;
372 ASN1_STRING *fval;
373 int nid;
374 int l;
Jean-Paul Calderonec54cc182008-03-26 21:11:07 -0400375 unsigned char *str;
376 PyObject *tuple;
377
378 ent = X509_NAME_get_entry(name, i);
379
380 fname = X509_NAME_ENTRY_get_object(ent);
381 fval = X509_NAME_ENTRY_get_data(ent);
382
383 l = ASN1_STRING_length(fval);
384 str = ASN1_STRING_data(fval);
385
386 nid = OBJ_obj2nid(fname);
387
388 /* printf("fname is %s len=%d str=%s\n", OBJ_nid2sn(nid), l, str); */
389
390 tuple = PyTuple_New(2);
Jean-Paul Calderone4d0c3212010-08-10 22:57:42 -0400391 PyTuple_SetItem(tuple, 0, PyBytes_FromString(OBJ_nid2sn(nid)));
392 PyTuple_SetItem(tuple, 1, PyBytes_FromStringAndSize((char *)str, l));
Jean-Paul Calderonec54cc182008-03-26 21:11:07 -0400393
394 PyList_SetItem(list, i, tuple);
395 }
396
397 return list;
398}
399
400
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500401/*
402 * Call the visitproc on all contained objects.
403 *
404 * Arguments: self - The Connection object
405 * visit - Function to call
406 * arg - Extra argument to visit
407 * Returns: 0 if all goes well, otherwise the return code from the first
408 * call that gave non-zero result.
409 */
410static int
411crypto_X509Name_traverse(crypto_X509NameObj *self, visitproc visit, void *arg)
412{
413 int ret = 0;
414
415 if (ret == 0 && self->parent_cert != NULL)
416 ret = visit(self->parent_cert, arg);
417 return ret;
418}
419
420/*
421 * Decref all contained objects and zero the pointers.
422 *
423 * Arguments: self - The Connection object
424 * Returns: Always 0.
425 */
426static int
427crypto_X509Name_clear(crypto_X509NameObj *self)
428{
429 Py_XDECREF(self->parent_cert);
430 self->parent_cert = NULL;
431 return 0;
432}
433
434/*
435 * Deallocate the memory used by the X509Name object
436 *
437 * Arguments: self - The X509Name object
438 * Returns: None
439 */
440static void
441crypto_X509Name_dealloc(crypto_X509NameObj *self)
442{
443 PyObject_GC_UnTrack(self);
444 /* Sometimes we don't have to dealloc this */
445 if (self->dealloc)
446 X509_NAME_free(self->x509_name);
447
448 crypto_X509Name_clear(self);
449
450 PyObject_GC_Del(self);
451}
452
Jean-Paul Calderone110cd092008-03-24 17:27:42 -0400453/*
454 * ADD_METHOD(name) expands to a correct PyMethodDef declaration
455 * { 'name', (PyCFunction)crypto_X509_name, METH_VARARGS }
456 * for convenience
457 */
458#define ADD_METHOD(name) \
459 { #name, (PyCFunction)crypto_X509Name_##name, METH_VARARGS, crypto_X509Name_##name##_doc }
460static PyMethodDef crypto_X509Name_methods[] =
461{
462 ADD_METHOD(hash),
Jean-Paul Calderonee957a002008-03-25 15:16:51 -0400463 ADD_METHOD(der),
Jean-Paul Calderonec54cc182008-03-26 21:11:07 -0400464 ADD_METHOD(get_components),
Jean-Paul Calderone110cd092008-03-24 17:27:42 -0400465 { NULL, NULL }
466};
467#undef ADD_METHOD
468
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500469PyTypeObject crypto_X509Name_Type = {
Jean-Paul Calderone3fe7f672010-08-11 23:55:10 -0400470 PyOpenSSL_HEAD_INIT(&PyType_Type, 0)
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500471 "X509Name",
472 sizeof(crypto_X509NameObj),
473 0,
474 (destructor)crypto_X509Name_dealloc,
475 NULL, /* print */
Jean-Paul Calderone0f31ec52010-08-11 22:56:42 -0400476 NULL, /* getattr */
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500477 (setattrfunc)crypto_X509Name_setattr,
Jean-Paul Calderone4d0c3212010-08-10 22:57:42 -0400478 NULL, /* reserved */
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500479 (reprfunc)crypto_X509Name_repr,
480 NULL, /* as_number */
481 NULL, /* as_sequence */
482 NULL, /* as_mapping */
483 NULL, /* hash */
484 NULL, /* call */
485 NULL, /* str */
Jean-Paul Calderone0f31ec52010-08-11 22:56:42 -0400486 (getattrofunc)crypto_X509Name_getattro, /* getattro */
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500487 NULL, /* setattro */
488 NULL, /* as_buffer */
Jean-Paul Calderone110cd092008-03-24 17:27:42 -0400489 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
Jean-Paul Calderone2cd7a922009-06-27 11:02:46 -0400490 crypto_X509Name_doc, /* tp_doc */
Jean-Paul Calderone110cd092008-03-24 17:27:42 -0400491 (traverseproc)crypto_X509Name_traverse, /* tp_traverse */
492 (inquiry)crypto_X509Name_clear, /* tp_clear */
Jean-Paul Calderone4d0c3212010-08-10 22:57:42 -0400493 crypto_X509Name_richcompare, /* tp_richcompare */
Jean-Paul Calderone2cd7a922009-06-27 11:02:46 -0400494 0, /* tp_weaklistoffset */
495 NULL, /* tp_iter */
496 NULL, /* tp_iternext */
497 crypto_X509Name_methods, /* tp_methods */
498 NULL, /* tp_members */
499 NULL, /* tp_getset */
500 NULL, /* tp_base */
501 NULL, /* tp_dict */
502 NULL, /* tp_descr_get */
503 NULL, /* tp_descr_set */
504 0, /* tp_dictoffset */
505 NULL, /* tp_init */
506 NULL, /* tp_alloc */
507 crypto_X509Name_new, /* tp_new */
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500508};
509
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500510/*
511 * Initialize the X509Name part of the crypto module
512 *
Jean-Paul Calderone2cd7a922009-06-27 11:02:46 -0400513 * Arguments: module - The crypto module
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500514 * Returns: None
515 */
516int
Jean-Paul Calderone2cd7a922009-06-27 11:02:46 -0400517init_crypto_x509name(PyObject *module)
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500518{
Jean-Paul Calderone2cd7a922009-06-27 11:02:46 -0400519 if (PyType_Ready(&crypto_X509Name_Type) < 0) {
520 return 0;
521 }
522
523 if (PyModule_AddObject(module, "X509Name", (PyObject *)&crypto_X509Name_Type) != 0) {
524 return 0;
525 }
526
527 if (PyModule_AddObject(module, "X509NameType", (PyObject *)&crypto_X509Name_Type) != 0) {
528 return 0;
529 }
530
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500531 return 1;
532}