Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 1 | /* |
| 2 | * x509name.c |
| 3 | * |
| 4 | * Copyright (C) AB Strakt 2001, All rights reserved |
Jean-Paul Calderone | 8b63d45 | 2008-03-21 18:31:12 -0400 | [diff] [blame] | 5 | * Copyright (C) Jean-Paul Calderone 2008, All rights reserved |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 6 | * |
| 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 Calderone | 28ebb30 | 2008-12-29 16:25:30 -0500 | [diff] [blame] | 16 | static PyMethodDef crypto_X509Name_methods[4]; |
Jean-Paul Calderone | 110cd09 | 2008-03-24 17:27:42 -0400 | [diff] [blame] | 17 | |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 18 | /* |
| 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 | */ |
| 26 | crypto_X509NameObj * |
| 27 | crypto_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 | |
| 44 | /* |
| 45 | * Return a name string given a X509_NAME object and a name identifier. Used |
| 46 | * by the getattr function. |
| 47 | * |
| 48 | * Arguments: name - The X509_NAME object |
| 49 | * nid - The name identifier |
| 50 | * Returns: The name as a Python string object |
| 51 | */ |
| 52 | static int |
| 53 | get_name_by_nid(X509_NAME *name, int nid, char **utf8string) |
| 54 | { |
| 55 | int entry_idx; |
| 56 | X509_NAME_ENTRY *entry; |
| 57 | ASN1_STRING *data; |
| 58 | int len; |
| 59 | |
| 60 | if ((entry_idx = X509_NAME_get_index_by_NID(name, nid, -1)) == -1) |
| 61 | { |
| 62 | return 0; |
| 63 | } |
| 64 | entry = X509_NAME_get_entry(name, entry_idx); |
| 65 | data = X509_NAME_ENTRY_get_data(entry); |
| 66 | if ((len = ASN1_STRING_to_UTF8((unsigned char **)utf8string, data)) < 0) |
| 67 | { |
| 68 | exception_from_error_queue(); |
| 69 | return -1; |
| 70 | } |
| 71 | |
| 72 | return len; |
| 73 | } |
| 74 | |
| 75 | /* |
| 76 | * Given a X509_NAME object and a name identifier, set the corresponding |
| 77 | * attribute to the given string. Used by the setattr function. |
| 78 | * |
| 79 | * Arguments: name - The X509_NAME object |
| 80 | * nid - The name identifier |
| 81 | * value - The string to set |
| 82 | * Returns: 0 for success, -1 on failure |
| 83 | */ |
| 84 | static int |
| 85 | set_name_by_nid(X509_NAME *name, int nid, char *utf8string) |
| 86 | { |
| 87 | X509_NAME_ENTRY *ne; |
| 88 | int i, entry_count, temp_nid; |
| 89 | |
| 90 | /* If there's an old entry for this NID, remove it */ |
| 91 | entry_count = X509_NAME_entry_count(name); |
| 92 | for (i = 0; i < entry_count; i++) |
| 93 | { |
| 94 | ne = X509_NAME_get_entry(name, i); |
| 95 | temp_nid = OBJ_obj2nid(X509_NAME_ENTRY_get_object(ne)); |
| 96 | if (temp_nid == nid) |
| 97 | { |
| 98 | ne = X509_NAME_delete_entry(name, i); |
| 99 | X509_NAME_ENTRY_free(ne); |
| 100 | break; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | /* Add the new entry */ |
Jean-Paul Calderone | 28ebb30 | 2008-12-29 16:25:30 -0500 | [diff] [blame] | 105 | if (!X509_NAME_add_entry_by_NID(name, nid, MBSTRING_UTF8, |
| 106 | (unsigned char *)utf8string, |
| 107 | -1, -1, 0)) |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 108 | { |
| 109 | exception_from_error_queue(); |
| 110 | return -1; |
| 111 | } |
| 112 | return 0; |
| 113 | } |
| 114 | |
| 115 | |
| 116 | /* |
| 117 | * Find attribute. An X509Name object has the following attributes: |
| 118 | * countryName (alias C), stateOrProvince (alias ST), locality (alias L), |
| 119 | * organization (alias O), organizationalUnit (alias OU), commonName (alias |
| 120 | * CN) and more... |
| 121 | * |
| 122 | * Arguments: self - The X509Name object |
| 123 | * name - The attribute name |
| 124 | * Returns: A Python object for the attribute, or NULL if something went |
| 125 | * wrong |
| 126 | */ |
| 127 | static PyObject * |
| 128 | crypto_X509Name_getattr(crypto_X509NameObj *self, char *name) |
| 129 | { |
| 130 | int nid, len; |
| 131 | char *utf8string; |
| 132 | |
| 133 | if ((nid = OBJ_txt2nid(name)) == NID_undef) |
| 134 | { |
Jean-Paul Calderone | 110cd09 | 2008-03-24 17:27:42 -0400 | [diff] [blame] | 135 | return Py_FindMethod(crypto_X509Name_methods, (PyObject *)self, name); |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | len = get_name_by_nid(self->x509_name, nid, &utf8string); |
| 139 | if (len < 0) |
| 140 | return NULL; |
| 141 | else if (len == 0) |
| 142 | { |
| 143 | Py_INCREF(Py_None); |
| 144 | return Py_None; |
| 145 | } |
Jean-Paul Calderone | 5b8c5ee | 2008-02-19 00:43:02 -0500 | [diff] [blame] | 146 | else { |
| 147 | PyObject* result = PyUnicode_Decode(utf8string, len, "utf-8", NULL); |
| 148 | OPENSSL_free(utf8string); |
| 149 | return result; |
| 150 | } |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | /* |
| 154 | * Set attribute |
| 155 | * |
| 156 | * Arguments: self - The X509Name object |
| 157 | * name - The attribute name |
| 158 | * value - The value to set |
| 159 | */ |
| 160 | static int |
| 161 | crypto_X509Name_setattr(crypto_X509NameObj *self, char *name, PyObject *value) |
| 162 | { |
| 163 | int nid; |
Jean-Paul Calderone | 7b0443a | 2008-02-19 00:25:30 -0500 | [diff] [blame] | 164 | int result; |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 165 | char *buffer; |
| 166 | |
| 167 | if ((nid = OBJ_txt2nid(name)) == NID_undef) |
| 168 | { |
| 169 | PyErr_SetString(PyExc_AttributeError, "No such attribute"); |
| 170 | return -1; |
| 171 | } |
| 172 | |
| 173 | /* Something of a hack to get nice unicode behaviour */ |
| 174 | if (!PyArg_Parse(value, "es:setattr", "utf-8", &buffer)) |
| 175 | return -1; |
Jean-Paul Calderone | 5b8c5ee | 2008-02-19 00:43:02 -0500 | [diff] [blame] | 176 | |
Jean-Paul Calderone | 7b0443a | 2008-02-19 00:25:30 -0500 | [diff] [blame] | 177 | result = set_name_by_nid(self->x509_name, nid, buffer); |
| 178 | PyMem_Free(buffer); |
| 179 | return result; |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | /* |
| 183 | * Compare two X509Name structures. |
| 184 | * |
| 185 | * Arguments: n - The first X509Name |
| 186 | * m - The second X509Name |
| 187 | * Returns: <0 if n < m, 0 if n == m and >0 if n > m |
| 188 | */ |
| 189 | static int |
| 190 | crypto_X509Name_compare(crypto_X509NameObj *n, crypto_X509NameObj *m) |
| 191 | { |
Jean-Paul Calderone | e098dc7 | 2008-03-06 18:36:19 -0500 | [diff] [blame] | 192 | int result = X509_NAME_cmp(n->x509_name, m->x509_name); |
| 193 | if (result < 0) { |
| 194 | return -1; |
| 195 | } else if (result > 0) { |
| 196 | return 1; |
Jean-Paul Calderone | 138a312 | 2008-12-30 15:05:38 -0500 | [diff] [blame] | 197 | } else { |
Jean-Paul Calderone | e098dc7 | 2008-03-06 18:36:19 -0500 | [diff] [blame] | 198 | return 0; |
Jean-Paul Calderone | 138a312 | 2008-12-30 15:05:38 -0500 | [diff] [blame] | 199 | } |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | /* |
| 203 | * String representation of an X509Name |
| 204 | * |
| 205 | * Arguments: self - The X509Name object |
| 206 | * Returns: A string representation of the object |
| 207 | */ |
| 208 | static PyObject * |
| 209 | crypto_X509Name_repr(crypto_X509NameObj *self) |
| 210 | { |
| 211 | char tmpbuf[512] = ""; |
| 212 | char realbuf[512+64]; |
| 213 | |
| 214 | if (X509_NAME_oneline(self->x509_name, tmpbuf, 512) == NULL) |
| 215 | { |
| 216 | exception_from_error_queue(); |
| 217 | return NULL; |
| 218 | } |
| 219 | else |
| 220 | { |
| 221 | /* This is safe because tmpbuf is max 512 characters */ |
| 222 | sprintf(realbuf, "<X509Name object '%s'>", tmpbuf); |
| 223 | return PyString_FromString(realbuf); |
| 224 | } |
| 225 | } |
| 226 | |
Jean-Paul Calderone | 110cd09 | 2008-03-24 17:27:42 -0400 | [diff] [blame] | 227 | static char crypto_X509Name_hash_doc[] = "\n\ |
| 228 | Return the has value of this name\n\ |
| 229 | \n\ |
| 230 | Arguments: self - The X509 object\n\ |
| 231 | args - The Python argument tuple, should be empty\n\ |
| 232 | Returns: None\n\ |
| 233 | "; |
| 234 | |
| 235 | /* |
| 236 | * First four bytes of the MD5 digest of the DER form of an X509Name. |
| 237 | * |
| 238 | * Arguments: self - The X509Name object |
| 239 | * Returns: An integer giving the hash. |
| 240 | */ |
| 241 | static PyObject * |
| 242 | crypto_X509Name_hash(crypto_X509NameObj *self, PyObject* args) |
| 243 | { |
| 244 | unsigned long hash; |
| 245 | |
| 246 | if (!PyArg_ParseTuple(args, ":hash")) { |
| 247 | return NULL; |
| 248 | } |
| 249 | hash = X509_NAME_hash(self->x509_name); |
| 250 | return PyInt_FromLong(hash); |
| 251 | } |
| 252 | |
Jean-Paul Calderone | e957a00 | 2008-03-25 15:16:51 -0400 | [diff] [blame] | 253 | static char crypto_X509Name_der_doc[] = "\n\ |
| 254 | Return the DER encodeing of this name\n\ |
| 255 | \n\ |
| 256 | Arguments: self - The X509 object\n\ |
| 257 | args - The Python argument tuple, should be empty\n\ |
| 258 | Returns: None\n\ |
| 259 | "; |
| 260 | |
| 261 | /* |
| 262 | * Arguments: self - The X509Name object |
| 263 | * Returns: The DER form of an X509Name. |
| 264 | */ |
| 265 | static PyObject * |
| 266 | crypto_X509Name_der(crypto_X509NameObj *self, PyObject *args) |
| 267 | { |
| 268 | if (!PyArg_ParseTuple(args, ":der")) { |
| 269 | return NULL; |
| 270 | } |
| 271 | |
| 272 | i2d_X509_NAME(self->x509_name, 0); |
| 273 | return PyString_FromStringAndSize(self->x509_name->bytes->data, |
| 274 | self->x509_name->bytes->length); |
| 275 | } |
| 276 | |
Jean-Paul Calderone | 110cd09 | 2008-03-24 17:27:42 -0400 | [diff] [blame] | 277 | |
Jean-Paul Calderone | c54cc18 | 2008-03-26 21:11:07 -0400 | [diff] [blame] | 278 | static char crypto_X509Name_get_components_doc[] = "\n\ |
| 279 | Returns the split-up components of this name.\n\ |
| 280 | \n\ |
| 281 | Arguments: self - The X509 object\n\ |
| 282 | args - The Python argument tuple, should be empty\n\ |
| 283 | Returns: List of tuples (name, value).\n\ |
| 284 | "; |
| 285 | |
| 286 | static PyObject * |
| 287 | crypto_X509Name_get_components(crypto_X509NameObj *self, PyObject *args) |
| 288 | { |
| 289 | int n, i; |
| 290 | X509_NAME *name = self->x509_name; |
| 291 | PyObject *list; |
| 292 | |
| 293 | if (!PyArg_ParseTuple(args, ":get_components")) |
| 294 | return NULL; |
| 295 | |
| 296 | n = X509_NAME_entry_count(name); |
| 297 | list = PyList_New(n); |
| 298 | for (i = 0; i < n; i++) |
| 299 | { |
| 300 | X509_NAME_ENTRY *ent; |
| 301 | ASN1_OBJECT *fname; |
| 302 | ASN1_STRING *fval; |
| 303 | int nid; |
| 304 | int l; |
Jean-Paul Calderone | c54cc18 | 2008-03-26 21:11:07 -0400 | [diff] [blame] | 305 | unsigned char *str; |
| 306 | PyObject *tuple; |
| 307 | |
| 308 | ent = X509_NAME_get_entry(name, i); |
| 309 | |
| 310 | fname = X509_NAME_ENTRY_get_object(ent); |
| 311 | fval = X509_NAME_ENTRY_get_data(ent); |
| 312 | |
| 313 | l = ASN1_STRING_length(fval); |
| 314 | str = ASN1_STRING_data(fval); |
| 315 | |
| 316 | nid = OBJ_obj2nid(fname); |
| 317 | |
| 318 | /* printf("fname is %s len=%d str=%s\n", OBJ_nid2sn(nid), l, str); */ |
| 319 | |
| 320 | tuple = PyTuple_New(2); |
| 321 | PyTuple_SetItem(tuple, 0, PyString_FromString(OBJ_nid2sn(nid))); |
Jean-Paul Calderone | 28ebb30 | 2008-12-29 16:25:30 -0500 | [diff] [blame] | 322 | PyTuple_SetItem(tuple, 1, PyString_FromStringAndSize((char *)str, l)); |
Jean-Paul Calderone | c54cc18 | 2008-03-26 21:11:07 -0400 | [diff] [blame] | 323 | |
| 324 | PyList_SetItem(list, i, tuple); |
| 325 | } |
| 326 | |
| 327 | return list; |
| 328 | } |
| 329 | |
| 330 | |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 331 | /* |
| 332 | * Call the visitproc on all contained objects. |
| 333 | * |
| 334 | * Arguments: self - The Connection object |
| 335 | * visit - Function to call |
| 336 | * arg - Extra argument to visit |
| 337 | * Returns: 0 if all goes well, otherwise the return code from the first |
| 338 | * call that gave non-zero result. |
| 339 | */ |
| 340 | static int |
| 341 | crypto_X509Name_traverse(crypto_X509NameObj *self, visitproc visit, void *arg) |
| 342 | { |
| 343 | int ret = 0; |
| 344 | |
| 345 | if (ret == 0 && self->parent_cert != NULL) |
| 346 | ret = visit(self->parent_cert, arg); |
| 347 | return ret; |
| 348 | } |
| 349 | |
| 350 | /* |
| 351 | * Decref all contained objects and zero the pointers. |
| 352 | * |
| 353 | * Arguments: self - The Connection object |
| 354 | * Returns: Always 0. |
| 355 | */ |
| 356 | static int |
| 357 | crypto_X509Name_clear(crypto_X509NameObj *self) |
| 358 | { |
| 359 | Py_XDECREF(self->parent_cert); |
| 360 | self->parent_cert = NULL; |
| 361 | return 0; |
| 362 | } |
| 363 | |
| 364 | /* |
| 365 | * Deallocate the memory used by the X509Name object |
| 366 | * |
| 367 | * Arguments: self - The X509Name object |
| 368 | * Returns: None |
| 369 | */ |
| 370 | static void |
| 371 | crypto_X509Name_dealloc(crypto_X509NameObj *self) |
| 372 | { |
| 373 | PyObject_GC_UnTrack(self); |
| 374 | /* Sometimes we don't have to dealloc this */ |
| 375 | if (self->dealloc) |
| 376 | X509_NAME_free(self->x509_name); |
| 377 | |
| 378 | crypto_X509Name_clear(self); |
| 379 | |
| 380 | PyObject_GC_Del(self); |
| 381 | } |
| 382 | |
Jean-Paul Calderone | 110cd09 | 2008-03-24 17:27:42 -0400 | [diff] [blame] | 383 | /* |
| 384 | * ADD_METHOD(name) expands to a correct PyMethodDef declaration |
| 385 | * { 'name', (PyCFunction)crypto_X509_name, METH_VARARGS } |
| 386 | * for convenience |
| 387 | */ |
| 388 | #define ADD_METHOD(name) \ |
| 389 | { #name, (PyCFunction)crypto_X509Name_##name, METH_VARARGS, crypto_X509Name_##name##_doc } |
| 390 | static PyMethodDef crypto_X509Name_methods[] = |
| 391 | { |
| 392 | ADD_METHOD(hash), |
Jean-Paul Calderone | e957a00 | 2008-03-25 15:16:51 -0400 | [diff] [blame] | 393 | ADD_METHOD(der), |
Jean-Paul Calderone | c54cc18 | 2008-03-26 21:11:07 -0400 | [diff] [blame] | 394 | ADD_METHOD(get_components), |
Jean-Paul Calderone | 110cd09 | 2008-03-24 17:27:42 -0400 | [diff] [blame] | 395 | { NULL, NULL } |
| 396 | }; |
| 397 | #undef ADD_METHOD |
| 398 | |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 399 | PyTypeObject crypto_X509Name_Type = { |
| 400 | PyObject_HEAD_INIT(NULL) |
| 401 | 0, |
| 402 | "X509Name", |
| 403 | sizeof(crypto_X509NameObj), |
| 404 | 0, |
| 405 | (destructor)crypto_X509Name_dealloc, |
| 406 | NULL, /* print */ |
| 407 | (getattrfunc)crypto_X509Name_getattr, |
| 408 | (setattrfunc)crypto_X509Name_setattr, |
| 409 | (cmpfunc)crypto_X509Name_compare, |
| 410 | (reprfunc)crypto_X509Name_repr, |
| 411 | NULL, /* as_number */ |
| 412 | NULL, /* as_sequence */ |
| 413 | NULL, /* as_mapping */ |
| 414 | NULL, /* hash */ |
| 415 | NULL, /* call */ |
| 416 | NULL, /* str */ |
| 417 | NULL, /* getattro */ |
| 418 | NULL, /* setattro */ |
| 419 | NULL, /* as_buffer */ |
Jean-Paul Calderone | 110cd09 | 2008-03-24 17:27:42 -0400 | [diff] [blame] | 420 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
| 421 | NULL, /* tp_doc */ |
| 422 | (traverseproc)crypto_X509Name_traverse, /* tp_traverse */ |
| 423 | (inquiry)crypto_X509Name_clear, /* tp_clear */ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 424 | }; |
| 425 | |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 426 | /* |
| 427 | * Initialize the X509Name part of the crypto module |
| 428 | * |
| 429 | * Arguments: dict - The crypto module dictionary |
| 430 | * Returns: None |
| 431 | */ |
| 432 | int |
| 433 | init_crypto_x509name(PyObject *dict) |
| 434 | { |
| 435 | crypto_X509Name_Type.ob_type = &PyType_Type; |
| 436 | Py_INCREF(&crypto_X509Name_Type); |
| 437 | PyDict_SetItemString(dict, "X509NameType", (PyObject *)&crypto_X509Name_Type); |
| 438 | return 1; |
| 439 | } |