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 |
| 5 | * |
| 6 | * X.509 Name handling, mostly thin wrapping. |
| 7 | * See the file RATIONALE for a short explanation of why this module was written. |
| 8 | * |
| 9 | * Reviewed 2001-07-23 |
| 10 | */ |
| 11 | #include <Python.h> |
| 12 | #define crypto_MODULE |
| 13 | #include "crypto.h" |
| 14 | |
| 15 | static char *CVSid = "@(#) $Id: x509name.c,v 1.16 2003/01/09 17:08:32 martin Exp $"; |
| 16 | |
| 17 | |
| 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 */ |
| 105 | if (!X509_NAME_add_entry_by_NID(name, nid, MBSTRING_UTF8, utf8string, |
| 106 | -1, -1, 0)) |
| 107 | { |
| 108 | exception_from_error_queue(); |
| 109 | return -1; |
| 110 | } |
| 111 | return 0; |
| 112 | } |
| 113 | |
| 114 | |
| 115 | /* |
| 116 | * Find attribute. An X509Name object has the following attributes: |
| 117 | * countryName (alias C), stateOrProvince (alias ST), locality (alias L), |
| 118 | * organization (alias O), organizationalUnit (alias OU), commonName (alias |
| 119 | * CN) and more... |
| 120 | * |
| 121 | * Arguments: self - The X509Name object |
| 122 | * name - The attribute name |
| 123 | * Returns: A Python object for the attribute, or NULL if something went |
| 124 | * wrong |
| 125 | */ |
| 126 | static PyObject * |
| 127 | crypto_X509Name_getattr(crypto_X509NameObj *self, char *name) |
| 128 | { |
| 129 | int nid, len; |
| 130 | char *utf8string; |
| 131 | |
| 132 | if ((nid = OBJ_txt2nid(name)) == NID_undef) |
| 133 | { |
| 134 | PyErr_SetString(PyExc_AttributeError, "No such attribute"); |
| 135 | return NULL; |
| 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 | } |
| 146 | else |
| 147 | return PyUnicode_Decode(utf8string, len, "utf-8", NULL); |
| 148 | } |
| 149 | |
| 150 | /* |
| 151 | * Set attribute |
| 152 | * |
| 153 | * Arguments: self - The X509Name object |
| 154 | * name - The attribute name |
| 155 | * value - The value to set |
| 156 | */ |
| 157 | static int |
| 158 | crypto_X509Name_setattr(crypto_X509NameObj *self, char *name, PyObject *value) |
| 159 | { |
| 160 | int nid; |
Jean-Paul Calderone | 7b0443a | 2008-02-19 00:25:30 -0500 | [diff] [blame^] | 161 | int result; |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 162 | char *buffer; |
| 163 | |
| 164 | if ((nid = OBJ_txt2nid(name)) == NID_undef) |
| 165 | { |
| 166 | PyErr_SetString(PyExc_AttributeError, "No such attribute"); |
| 167 | return -1; |
| 168 | } |
| 169 | |
| 170 | /* Something of a hack to get nice unicode behaviour */ |
| 171 | if (!PyArg_Parse(value, "es:setattr", "utf-8", &buffer)) |
| 172 | return -1; |
| 173 | |
Jean-Paul Calderone | 7b0443a | 2008-02-19 00:25:30 -0500 | [diff] [blame^] | 174 | result = set_name_by_nid(self->x509_name, nid, buffer); |
| 175 | PyMem_Free(buffer); |
| 176 | return result; |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | /* |
| 180 | * Compare two X509Name structures. |
| 181 | * |
| 182 | * Arguments: n - The first X509Name |
| 183 | * m - The second X509Name |
| 184 | * Returns: <0 if n < m, 0 if n == m and >0 if n > m |
| 185 | */ |
| 186 | static int |
| 187 | crypto_X509Name_compare(crypto_X509NameObj *n, crypto_X509NameObj *m) |
| 188 | { |
| 189 | return X509_NAME_cmp(n->x509_name, m->x509_name); |
| 190 | } |
| 191 | |
| 192 | /* |
| 193 | * String representation of an X509Name |
| 194 | * |
| 195 | * Arguments: self - The X509Name object |
| 196 | * Returns: A string representation of the object |
| 197 | */ |
| 198 | static PyObject * |
| 199 | crypto_X509Name_repr(crypto_X509NameObj *self) |
| 200 | { |
| 201 | char tmpbuf[512] = ""; |
| 202 | char realbuf[512+64]; |
| 203 | |
| 204 | if (X509_NAME_oneline(self->x509_name, tmpbuf, 512) == NULL) |
| 205 | { |
| 206 | exception_from_error_queue(); |
| 207 | return NULL; |
| 208 | } |
| 209 | else |
| 210 | { |
| 211 | /* This is safe because tmpbuf is max 512 characters */ |
| 212 | sprintf(realbuf, "<X509Name object '%s'>", tmpbuf); |
| 213 | return PyString_FromString(realbuf); |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | /* |
| 218 | * Call the visitproc on all contained objects. |
| 219 | * |
| 220 | * Arguments: self - The Connection object |
| 221 | * visit - Function to call |
| 222 | * arg - Extra argument to visit |
| 223 | * Returns: 0 if all goes well, otherwise the return code from the first |
| 224 | * call that gave non-zero result. |
| 225 | */ |
| 226 | static int |
| 227 | crypto_X509Name_traverse(crypto_X509NameObj *self, visitproc visit, void *arg) |
| 228 | { |
| 229 | int ret = 0; |
| 230 | |
| 231 | if (ret == 0 && self->parent_cert != NULL) |
| 232 | ret = visit(self->parent_cert, arg); |
| 233 | return ret; |
| 234 | } |
| 235 | |
| 236 | /* |
| 237 | * Decref all contained objects and zero the pointers. |
| 238 | * |
| 239 | * Arguments: self - The Connection object |
| 240 | * Returns: Always 0. |
| 241 | */ |
| 242 | static int |
| 243 | crypto_X509Name_clear(crypto_X509NameObj *self) |
| 244 | { |
| 245 | Py_XDECREF(self->parent_cert); |
| 246 | self->parent_cert = NULL; |
| 247 | return 0; |
| 248 | } |
| 249 | |
| 250 | /* |
| 251 | * Deallocate the memory used by the X509Name object |
| 252 | * |
| 253 | * Arguments: self - The X509Name object |
| 254 | * Returns: None |
| 255 | */ |
| 256 | static void |
| 257 | crypto_X509Name_dealloc(crypto_X509NameObj *self) |
| 258 | { |
| 259 | PyObject_GC_UnTrack(self); |
| 260 | /* Sometimes we don't have to dealloc this */ |
| 261 | if (self->dealloc) |
| 262 | X509_NAME_free(self->x509_name); |
| 263 | |
| 264 | crypto_X509Name_clear(self); |
| 265 | |
| 266 | PyObject_GC_Del(self); |
| 267 | } |
| 268 | |
| 269 | PyTypeObject crypto_X509Name_Type = { |
| 270 | PyObject_HEAD_INIT(NULL) |
| 271 | 0, |
| 272 | "X509Name", |
| 273 | sizeof(crypto_X509NameObj), |
| 274 | 0, |
| 275 | (destructor)crypto_X509Name_dealloc, |
| 276 | NULL, /* print */ |
| 277 | (getattrfunc)crypto_X509Name_getattr, |
| 278 | (setattrfunc)crypto_X509Name_setattr, |
| 279 | (cmpfunc)crypto_X509Name_compare, |
| 280 | (reprfunc)crypto_X509Name_repr, |
| 281 | NULL, /* as_number */ |
| 282 | NULL, /* as_sequence */ |
| 283 | NULL, /* as_mapping */ |
| 284 | NULL, /* hash */ |
| 285 | NULL, /* call */ |
| 286 | NULL, /* str */ |
| 287 | NULL, /* getattro */ |
| 288 | NULL, /* setattro */ |
| 289 | NULL, /* as_buffer */ |
| 290 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, |
| 291 | NULL, /* doc */ |
| 292 | (traverseproc)crypto_X509Name_traverse, |
| 293 | (inquiry)crypto_X509Name_clear, |
| 294 | }; |
| 295 | |
| 296 | |
| 297 | /* |
| 298 | * Initialize the X509Name part of the crypto module |
| 299 | * |
| 300 | * Arguments: dict - The crypto module dictionary |
| 301 | * Returns: None |
| 302 | */ |
| 303 | int |
| 304 | init_crypto_x509name(PyObject *dict) |
| 305 | { |
| 306 | crypto_X509Name_Type.ob_type = &PyType_Type; |
| 307 | Py_INCREF(&crypto_X509Name_Type); |
| 308 | PyDict_SetItemString(dict, "X509NameType", (PyObject *)&crypto_X509Name_Type); |
| 309 | return 1; |
| 310 | } |