blob: 5d494305d4becc977545ff5cd870bf9ad4790b8d [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 Calderone8b63d452008-03-21 18:31:12 -04005 * Copyright (C) Jean-Paul Calderone 2008, 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 {
91 exception_from_error_queue();
92 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 {
132 exception_from_error_queue();
133 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
156 if ((nid = OBJ_txt2nid(name)) == NID_undef)
157 {
Jean-Paul Calderone110cd092008-03-24 17:27:42 -0400158 return Py_FindMethod(crypto_X509Name_methods, (PyObject *)self, name);
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500159 }
160
161 len = get_name_by_nid(self->x509_name, nid, &utf8string);
162 if (len < 0)
163 return NULL;
164 else if (len == 0)
165 {
166 Py_INCREF(Py_None);
167 return Py_None;
168 }
Jean-Paul Calderone5b8c5ee2008-02-19 00:43:02 -0500169 else {
170 PyObject* result = PyUnicode_Decode(utf8string, len, "utf-8", NULL);
171 OPENSSL_free(utf8string);
172 return result;
173 }
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500174}
175
176/*
177 * Set attribute
178 *
179 * Arguments: self - The X509Name object
180 * name - The attribute name
181 * value - The value to set
182 */
183static int
184crypto_X509Name_setattr(crypto_X509NameObj *self, char *name, PyObject *value)
185{
186 int nid;
Jean-Paul Calderone7b0443a2008-02-19 00:25:30 -0500187 int result;
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500188 char *buffer;
189
190 if ((nid = OBJ_txt2nid(name)) == NID_undef)
191 {
192 PyErr_SetString(PyExc_AttributeError, "No such attribute");
193 return -1;
194 }
195
196 /* Something of a hack to get nice unicode behaviour */
197 if (!PyArg_Parse(value, "es:setattr", "utf-8", &buffer))
198 return -1;
Jean-Paul Calderone5b8c5ee2008-02-19 00:43:02 -0500199
Jean-Paul Calderone7b0443a2008-02-19 00:25:30 -0500200 result = set_name_by_nid(self->x509_name, nid, buffer);
201 PyMem_Free(buffer);
202 return result;
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500203}
204
205/*
206 * Compare two X509Name structures.
207 *
208 * Arguments: n - The first X509Name
209 * m - The second X509Name
210 * Returns: <0 if n < m, 0 if n == m and >0 if n > m
211 */
212static int
213crypto_X509Name_compare(crypto_X509NameObj *n, crypto_X509NameObj *m)
214{
Jean-Paul Calderonee098dc72008-03-06 18:36:19 -0500215 int result = X509_NAME_cmp(n->x509_name, m->x509_name);
216 if (result < 0) {
217 return -1;
218 } else if (result > 0) {
219 return 1;
Jean-Paul Calderone138a3122008-12-30 15:05:38 -0500220 } else {
Jean-Paul Calderonee098dc72008-03-06 18:36:19 -0500221 return 0;
Jean-Paul Calderone138a3122008-12-30 15:05:38 -0500222 }
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500223}
224
225/*
226 * String representation of an X509Name
227 *
228 * Arguments: self - The X509Name object
229 * Returns: A string representation of the object
230 */
231static PyObject *
232crypto_X509Name_repr(crypto_X509NameObj *self)
233{
234 char tmpbuf[512] = "";
235 char realbuf[512+64];
236
237 if (X509_NAME_oneline(self->x509_name, tmpbuf, 512) == NULL)
238 {
239 exception_from_error_queue();
240 return NULL;
241 }
242 else
243 {
244 /* This is safe because tmpbuf is max 512 characters */
245 sprintf(realbuf, "<X509Name object '%s'>", tmpbuf);
246 return PyString_FromString(realbuf);
247 }
248}
249
Jean-Paul Calderone110cd092008-03-24 17:27:42 -0400250static char crypto_X509Name_hash_doc[] = "\n\
Jean-Paul Calderone54bcc832009-05-27 14:06:48 -0400251Return the hash value of this name\n\
Jean-Paul Calderone110cd092008-03-24 17:27:42 -0400252\n\
Jean-Paul Calderone54bcc832009-05-27 14:06:48 -0400253@return: None\n\
Jean-Paul Calderone110cd092008-03-24 17:27:42 -0400254";
255
256/*
257 * First four bytes of the MD5 digest of the DER form of an X509Name.
258 *
259 * Arguments: self - The X509Name object
260 * Returns: An integer giving the hash.
261 */
262static PyObject *
263crypto_X509Name_hash(crypto_X509NameObj *self, PyObject* args)
264{
265 unsigned long hash;
266
267 if (!PyArg_ParseTuple(args, ":hash")) {
268 return NULL;
269 }
270 hash = X509_NAME_hash(self->x509_name);
271 return PyInt_FromLong(hash);
272}
273
Jean-Paul Calderonee957a002008-03-25 15:16:51 -0400274static char crypto_X509Name_der_doc[] = "\n\
Jean-Paul Calderone54bcc832009-05-27 14:06:48 -0400275Return the DER encoding of this name\n\
Jean-Paul Calderonee957a002008-03-25 15:16:51 -0400276\n\
Jean-Paul Calderone54bcc832009-05-27 14:06:48 -0400277@return: None\n\
Jean-Paul Calderonee957a002008-03-25 15:16:51 -0400278";
279
280/*
281 * Arguments: self - The X509Name object
282 * Returns: The DER form of an X509Name.
283 */
284static PyObject *
285crypto_X509Name_der(crypto_X509NameObj *self, PyObject *args)
286{
287 if (!PyArg_ParseTuple(args, ":der")) {
288 return NULL;
289 }
290
291 i2d_X509_NAME(self->x509_name, 0);
292 return PyString_FromStringAndSize(self->x509_name->bytes->data,
293 self->x509_name->bytes->length);
294}
295
Jean-Paul Calderone110cd092008-03-24 17:27:42 -0400296
Jean-Paul Calderonec54cc182008-03-26 21:11:07 -0400297static char crypto_X509Name_get_components_doc[] = "\n\
298Returns the split-up components of this name.\n\
299\n\
Jean-Paul Calderone54bcc832009-05-27 14:06:48 -0400300@return: List of tuples (name, value).\n\
Jean-Paul Calderonec54cc182008-03-26 21:11:07 -0400301";
302
303static PyObject *
304crypto_X509Name_get_components(crypto_X509NameObj *self, PyObject *args)
305{
306 int n, i;
307 X509_NAME *name = self->x509_name;
308 PyObject *list;
309
310 if (!PyArg_ParseTuple(args, ":get_components"))
311 return NULL;
312
313 n = X509_NAME_entry_count(name);
314 list = PyList_New(n);
315 for (i = 0; i < n; i++)
316 {
317 X509_NAME_ENTRY *ent;
318 ASN1_OBJECT *fname;
319 ASN1_STRING *fval;
320 int nid;
321 int l;
Jean-Paul Calderonec54cc182008-03-26 21:11:07 -0400322 unsigned char *str;
323 PyObject *tuple;
324
325 ent = X509_NAME_get_entry(name, i);
326
327 fname = X509_NAME_ENTRY_get_object(ent);
328 fval = X509_NAME_ENTRY_get_data(ent);
329
330 l = ASN1_STRING_length(fval);
331 str = ASN1_STRING_data(fval);
332
333 nid = OBJ_obj2nid(fname);
334
335 /* printf("fname is %s len=%d str=%s\n", OBJ_nid2sn(nid), l, str); */
336
337 tuple = PyTuple_New(2);
338 PyTuple_SetItem(tuple, 0, PyString_FromString(OBJ_nid2sn(nid)));
Jean-Paul Calderone28ebb302008-12-29 16:25:30 -0500339 PyTuple_SetItem(tuple, 1, PyString_FromStringAndSize((char *)str, l));
Jean-Paul Calderonec54cc182008-03-26 21:11:07 -0400340
341 PyList_SetItem(list, i, tuple);
342 }
343
344 return list;
345}
346
347
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500348/*
349 * Call the visitproc on all contained objects.
350 *
351 * Arguments: self - The Connection object
352 * visit - Function to call
353 * arg - Extra argument to visit
354 * Returns: 0 if all goes well, otherwise the return code from the first
355 * call that gave non-zero result.
356 */
357static int
358crypto_X509Name_traverse(crypto_X509NameObj *self, visitproc visit, void *arg)
359{
360 int ret = 0;
361
362 if (ret == 0 && self->parent_cert != NULL)
363 ret = visit(self->parent_cert, arg);
364 return ret;
365}
366
367/*
368 * Decref all contained objects and zero the pointers.
369 *
370 * Arguments: self - The Connection object
371 * Returns: Always 0.
372 */
373static int
374crypto_X509Name_clear(crypto_X509NameObj *self)
375{
376 Py_XDECREF(self->parent_cert);
377 self->parent_cert = NULL;
378 return 0;
379}
380
381/*
382 * Deallocate the memory used by the X509Name object
383 *
384 * Arguments: self - The X509Name object
385 * Returns: None
386 */
387static void
388crypto_X509Name_dealloc(crypto_X509NameObj *self)
389{
390 PyObject_GC_UnTrack(self);
391 /* Sometimes we don't have to dealloc this */
392 if (self->dealloc)
393 X509_NAME_free(self->x509_name);
394
395 crypto_X509Name_clear(self);
396
397 PyObject_GC_Del(self);
398}
399
Jean-Paul Calderone110cd092008-03-24 17:27:42 -0400400/*
401 * ADD_METHOD(name) expands to a correct PyMethodDef declaration
402 * { 'name', (PyCFunction)crypto_X509_name, METH_VARARGS }
403 * for convenience
404 */
405#define ADD_METHOD(name) \
406 { #name, (PyCFunction)crypto_X509Name_##name, METH_VARARGS, crypto_X509Name_##name##_doc }
407static PyMethodDef crypto_X509Name_methods[] =
408{
409 ADD_METHOD(hash),
Jean-Paul Calderonee957a002008-03-25 15:16:51 -0400410 ADD_METHOD(der),
Jean-Paul Calderonec54cc182008-03-26 21:11:07 -0400411 ADD_METHOD(get_components),
Jean-Paul Calderone110cd092008-03-24 17:27:42 -0400412 { NULL, NULL }
413};
414#undef ADD_METHOD
415
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500416PyTypeObject crypto_X509Name_Type = {
417 PyObject_HEAD_INIT(NULL)
418 0,
419 "X509Name",
420 sizeof(crypto_X509NameObj),
421 0,
422 (destructor)crypto_X509Name_dealloc,
423 NULL, /* print */
424 (getattrfunc)crypto_X509Name_getattr,
425 (setattrfunc)crypto_X509Name_setattr,
426 (cmpfunc)crypto_X509Name_compare,
427 (reprfunc)crypto_X509Name_repr,
428 NULL, /* as_number */
429 NULL, /* as_sequence */
430 NULL, /* as_mapping */
431 NULL, /* hash */
432 NULL, /* call */
433 NULL, /* str */
434 NULL, /* getattro */
435 NULL, /* setattro */
436 NULL, /* as_buffer */
Jean-Paul Calderone110cd092008-03-24 17:27:42 -0400437 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
Jean-Paul Calderone2cd7a922009-06-27 11:02:46 -0400438 crypto_X509Name_doc, /* tp_doc */
Jean-Paul Calderone110cd092008-03-24 17:27:42 -0400439 (traverseproc)crypto_X509Name_traverse, /* tp_traverse */
440 (inquiry)crypto_X509Name_clear, /* tp_clear */
Jean-Paul Calderone2cd7a922009-06-27 11:02:46 -0400441 NULL, /* tp_richcompare */
442 0, /* tp_weaklistoffset */
443 NULL, /* tp_iter */
444 NULL, /* tp_iternext */
445 crypto_X509Name_methods, /* tp_methods */
446 NULL, /* tp_members */
447 NULL, /* tp_getset */
448 NULL, /* tp_base */
449 NULL, /* tp_dict */
450 NULL, /* tp_descr_get */
451 NULL, /* tp_descr_set */
452 0, /* tp_dictoffset */
453 NULL, /* tp_init */
454 NULL, /* tp_alloc */
455 crypto_X509Name_new, /* tp_new */
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500456};
457
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500458/*
459 * Initialize the X509Name part of the crypto module
460 *
Jean-Paul Calderone2cd7a922009-06-27 11:02:46 -0400461 * Arguments: module - The crypto module
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500462 * Returns: None
463 */
464int
Jean-Paul Calderone2cd7a922009-06-27 11:02:46 -0400465init_crypto_x509name(PyObject *module)
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500466{
Jean-Paul Calderone2cd7a922009-06-27 11:02:46 -0400467 if (PyType_Ready(&crypto_X509Name_Type) < 0) {
468 return 0;
469 }
470
471 if (PyModule_AddObject(module, "X509Name", (PyObject *)&crypto_X509Name_Type) != 0) {
472 return 0;
473 }
474
475 if (PyModule_AddObject(module, "X509NameType", (PyObject *)&crypto_X509Name_Type) != 0) {
476 return 0;
477 }
478
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500479 return 1;
480}