Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 1 | /* |
| 2 | * x509ext.c |
| 3 | * |
Jean-Paul Calderone | 8b63d45 | 2008-03-21 18:31:12 -0400 | [diff] [blame] | 4 | * Copyright (C) Jean-Paul Calderone 2008, All rights reserved |
| 5 | * |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 6 | * Export X.509 extension functions and data structures. |
| 7 | * See the file RATIONALE for a short explanation of why this module was written. |
| 8 | * |
| 9 | * @(#) $Id: x509ext.c,v 1.1 2002/07/09 13:34:46 martin Exp $ |
| 10 | */ |
| 11 | |
| 12 | #include <Python.h> |
| 13 | #define crypto_MODULE |
| 14 | #include "crypto.h" |
| 15 | |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 16 | static char crypto_X509Extension_get_critical_doc[] = "\n\ |
| 17 | Returns the critical field of the X509Extension\n\ |
| 18 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 19 | @return: The critical field.\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 20 | "; |
| 21 | |
| 22 | static PyObject * |
| 23 | crypto_X509Extension_get_critical(crypto_X509ExtensionObj *self, PyObject *args) |
| 24 | { |
| 25 | if (!PyArg_ParseTuple(args, ":get_critical")) |
| 26 | return NULL; |
| 27 | |
| 28 | return PyInt_FromLong(X509_EXTENSION_get_critical(self->x509_extension)); |
| 29 | } |
| 30 | |
Jean-Paul Calderone | f8c5fab | 2008-12-31 15:53:48 -0500 | [diff] [blame] | 31 | static char crypto_X509Extension_get_short_name_doc[] = "\n\ |
| 32 | Returns the short version of the type name of the X509Extension\n\ |
| 33 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 34 | @return: The short type name.\n\ |
Jean-Paul Calderone | f8c5fab | 2008-12-31 15:53:48 -0500 | [diff] [blame] | 35 | "; |
| 36 | |
| 37 | static PyObject * |
| 38 | crypto_X509Extension_get_short_name(crypto_X509ExtensionObj *self, PyObject *args) { |
| 39 | ASN1_OBJECT *obj; |
| 40 | const char *extname; |
| 41 | |
| 42 | if (!PyArg_ParseTuple(args, ":get_short_name")) { |
| 43 | return NULL; |
| 44 | } |
| 45 | |
| 46 | /* Returns an internal pointer to x509_extension, not a copy */ |
| 47 | obj = X509_EXTENSION_get_object(self->x509_extension); |
| 48 | |
| 49 | extname = OBJ_nid2sn(OBJ_obj2nid(obj)); |
| 50 | return PyString_FromString(extname); |
| 51 | } |
| 52 | |
| 53 | |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 54 | /* |
| 55 | * ADD_METHOD(name) expands to a correct PyMethodDef declaration |
| 56 | * { 'name', (PyCFunction)crypto_X509Extension_name, METH_VARARGS } |
| 57 | * for convenience |
| 58 | */ |
| 59 | #define ADD_METHOD(name) \ |
| 60 | { #name, (PyCFunction)crypto_X509Extension_##name, METH_VARARGS, crypto_X509Extension_##name##_doc } |
| 61 | static PyMethodDef crypto_X509Extension_methods[] = |
| 62 | { |
| 63 | ADD_METHOD(get_critical), |
Jean-Paul Calderone | f8c5fab | 2008-12-31 15:53:48 -0500 | [diff] [blame] | 64 | ADD_METHOD(get_short_name), |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 65 | { NULL, NULL } |
| 66 | }; |
| 67 | #undef ADD_METHOD |
| 68 | |
| 69 | /* |
| 70 | * Constructor for X509Extension, never called by Python code directly |
| 71 | * |
| 72 | * Arguments: type_name - ??? |
| 73 | * critical - ??? |
| 74 | * value - ??? |
Jean-Paul Calderone | e04be5f | 2009-07-17 15:09:48 -0400 | [diff] [blame^] | 75 | * subject - An x509v3 certificate which is the subject for this extension. |
| 76 | * issuer - An x509v3 certificate which is the issuer for this extension. |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 77 | * Returns: The newly created X509Extension object |
| 78 | */ |
| 79 | crypto_X509ExtensionObj * |
Jean-Paul Calderone | 5ae32fb | 2009-07-17 15:03:31 -0400 | [diff] [blame] | 80 | crypto_X509Extension_New(char *type_name, int critical, char *value, |
Jean-Paul Calderone | e04be5f | 2009-07-17 15:09:48 -0400 | [diff] [blame^] | 81 | crypto_X509Obj *subject, crypto_X509Obj *issuer) { |
Jean-Paul Calderone | 2ee1e7c | 2008-12-31 14:58:38 -0500 | [diff] [blame] | 82 | X509V3_CTX ctx; |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 83 | crypto_X509ExtensionObj *self; |
Jean-Paul Calderone | e7db4b4 | 2008-12-31 13:39:24 -0500 | [diff] [blame] | 84 | char* value_with_critical = NULL; |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 85 | |
Jean-Paul Calderone | e04be5f | 2009-07-17 15:09:48 -0400 | [diff] [blame^] | 86 | |
Jean-Paul Calderone | 5ae32fb | 2009-07-17 15:03:31 -0400 | [diff] [blame] | 87 | /* |
Jean-Paul Calderone | e04be5f | 2009-07-17 15:09:48 -0400 | [diff] [blame^] | 88 | * A context is necessary for any extension which uses the r2i conversion |
| 89 | * method. That is, X509V3_EXT_nconf may segfault if passed a NULL ctx. |
| 90 | * Start off by initializing most of the fields to NULL. |
Jean-Paul Calderone | 5ae32fb | 2009-07-17 15:03:31 -0400 | [diff] [blame] | 91 | */ |
| 92 | X509V3_set_ctx(&ctx, NULL, NULL, NULL, NULL, 0); |
| 93 | |
Jean-Paul Calderone | e04be5f | 2009-07-17 15:09:48 -0400 | [diff] [blame^] | 94 | /* |
| 95 | * We have no configuration database - but perhaps we should (some |
| 96 | * extensions may require it). |
Jean-Paul Calderone | 5ae32fb | 2009-07-17 15:03:31 -0400 | [diff] [blame] | 97 | */ |
Jean-Paul Calderone | 2ee1e7c | 2008-12-31 14:58:38 -0500 | [diff] [blame] | 98 | X509V3_set_ctx_nodb(&ctx); |
Jean-Paul Calderone | 5ae32fb | 2009-07-17 15:03:31 -0400 | [diff] [blame] | 99 | |
| 100 | /* |
| 101 | * Initialize the subject and issuer, if appropriate. ctx is a local, and |
| 102 | * as far as I can tell none of the X509V3_* APIs invoked here steal any |
| 103 | * references, so no need to incref subject or issuer. |
| 104 | */ |
| 105 | if (subject) { |
Rick Dean | 47262da | 2009-07-08 16:17:17 -0500 | [diff] [blame] | 106 | ctx.subject_cert = subject->x509; |
Jean-Paul Calderone | 5ae32fb | 2009-07-17 15:03:31 -0400 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | if (issuer) { |
Rick Dean | 47262da | 2009-07-08 16:17:17 -0500 | [diff] [blame] | 110 | ctx.issuer_cert = issuer->x509; |
Jean-Paul Calderone | 5ae32fb | 2009-07-17 15:03:31 -0400 | [diff] [blame] | 111 | } |
Jean-Paul Calderone | 2ee1e7c | 2008-12-31 14:58:38 -0500 | [diff] [blame] | 112 | |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 113 | self = PyObject_New(crypto_X509ExtensionObj, &crypto_X509Extension_Type); |
| 114 | |
Jean-Paul Calderone | e7db4b4 | 2008-12-31 13:39:24 -0500 | [diff] [blame] | 115 | if (self == NULL) { |
Jean-Paul Calderone | 2ee1e7c | 2008-12-31 14:58:38 -0500 | [diff] [blame] | 116 | goto error; |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 117 | } |
| 118 | |
Jean-Paul Calderone | 391585f | 2008-12-31 14:36:31 -0500 | [diff] [blame] | 119 | self->dealloc = 0; |
| 120 | |
Jean-Paul Calderone | e7db4b4 | 2008-12-31 13:39:24 -0500 | [diff] [blame] | 121 | /* There are other OpenSSL APIs which would let us pass in critical |
| 122 | * separately, but they're harder to use, and since value is already a pile |
| 123 | * of crappy junk smuggling a ton of utterly important structured data, |
| 124 | * what's the point of trying to avoid nasty stuff with strings? (However, |
| 125 | * X509V3_EXT_i2d in particular seems like it would be a better API to |
| 126 | * invoke. I do not know where to get the ext_struc it desires for its |
| 127 | * last parameter, though.) */ |
| 128 | value_with_critical = malloc(strlen("critical,") + strlen(value) + 1); |
Jean-Paul Calderone | 2ee1e7c | 2008-12-31 14:58:38 -0500 | [diff] [blame] | 129 | if (!value_with_critical) { |
| 130 | goto critical_malloc_error; |
| 131 | } |
| 132 | |
Jean-Paul Calderone | e7db4b4 | 2008-12-31 13:39:24 -0500 | [diff] [blame] | 133 | if (critical) { |
| 134 | strcpy(value_with_critical, "critical,"); |
| 135 | strcpy(value_with_critical + strlen("critical,"), value); |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 136 | } else { |
Jean-Paul Calderone | e7db4b4 | 2008-12-31 13:39:24 -0500 | [diff] [blame] | 137 | strcpy(value_with_critical, value); |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 138 | } |
| 139 | |
Jean-Paul Calderone | e7db4b4 | 2008-12-31 13:39:24 -0500 | [diff] [blame] | 140 | self->x509_extension = X509V3_EXT_nconf( |
Jean-Paul Calderone | 2ee1e7c | 2008-12-31 14:58:38 -0500 | [diff] [blame] | 141 | NULL, &ctx, type_name, value_with_critical); |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 142 | |
Jean-Paul Calderone | e7db4b4 | 2008-12-31 13:39:24 -0500 | [diff] [blame] | 143 | free(value_with_critical); |
| 144 | |
Jean-Paul Calderone | 391585f | 2008-12-31 14:36:31 -0500 | [diff] [blame] | 145 | if (!self->x509_extension) { |
Jean-Paul Calderone | 2ee1e7c | 2008-12-31 14:58:38 -0500 | [diff] [blame] | 146 | goto nconf_error; |
Jean-Paul Calderone | 391585f | 2008-12-31 14:36:31 -0500 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | self->dealloc = 1; |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 150 | return self; |
Jean-Paul Calderone | 2ee1e7c | 2008-12-31 14:58:38 -0500 | [diff] [blame] | 151 | |
| 152 | nconf_error: |
| 153 | exception_from_error_queue(); |
| 154 | |
| 155 | critical_malloc_error: |
Jean-Paul Calderone | a96bfed | 2009-05-27 08:47:34 -0400 | [diff] [blame] | 156 | Py_XDECREF(self); |
Jean-Paul Calderone | 2ee1e7c | 2008-12-31 14:58:38 -0500 | [diff] [blame] | 157 | |
| 158 | error: |
| 159 | return NULL; |
| 160 | |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 161 | } |
| 162 | |
Jean-Paul Calderone | 51e9066 | 2009-06-27 11:17:28 -0400 | [diff] [blame] | 163 | static char crypto_X509Extension_doc[] = "\n\ |
Rick Dean | 47262da | 2009-07-08 16:17:17 -0500 | [diff] [blame] | 164 | X509Extension(typename, critical, value[, subject][, issuer]) -> \n\ |
| 165 | X509Extension instance\n\ |
Jean-Paul Calderone | 51e9066 | 2009-06-27 11:17:28 -0400 | [diff] [blame] | 166 | \n\ |
| 167 | @param typename: The name of the extension to create.\n\ |
| 168 | @type typename: C{str}\n\ |
| 169 | @param critical: A flag indicating whether this is a critical extension.\n\ |
| 170 | @param value: The value of the extension.\n\ |
| 171 | @type value: C{str}\n\ |
Rick Dean | 47262da | 2009-07-08 16:17:17 -0500 | [diff] [blame] | 172 | @param subject: Optional X509 cert to use as subject.\n\ |
| 173 | @type subject: C{X509}\n\ |
| 174 | @param issuer: Optional X509 cert to use as issuer.\n\ |
| 175 | @type issuer: C{X509}\n\ |
Jean-Paul Calderone | 51e9066 | 2009-06-27 11:17:28 -0400 | [diff] [blame] | 176 | @return: The X509Extension object\n\ |
| 177 | "; |
| 178 | |
| 179 | static PyObject * |
Jean-Paul Calderone | e04be5f | 2009-07-17 15:09:48 -0400 | [diff] [blame^] | 180 | crypto_X509Extension_new(PyTypeObject *subtype, PyObject *args, |
Rick Dean | 47262da | 2009-07-08 16:17:17 -0500 | [diff] [blame] | 181 | PyObject *kwargs) { |
Jean-Paul Calderone | 51e9066 | 2009-06-27 11:17:28 -0400 | [diff] [blame] | 182 | char *type_name, *value; |
Rick Dean | 47262da | 2009-07-08 16:17:17 -0500 | [diff] [blame] | 183 | int critical = 0; |
| 184 | crypto_X509Obj * subject = NULL; |
| 185 | crypto_X509Obj * issuer = NULL; |
Jean-Paul Calderone | e04be5f | 2009-07-17 15:09:48 -0400 | [diff] [blame^] | 186 | static char *kwlist[] = {"type_name", "critical", "value", "subject", |
Rick Dean | 47262da | 2009-07-08 16:17:17 -0500 | [diff] [blame] | 187 | "issuer", NULL}; |
Jean-Paul Calderone | 51e9066 | 2009-06-27 11:17:28 -0400 | [diff] [blame] | 188 | |
Jean-Paul Calderone | e04be5f | 2009-07-17 15:09:48 -0400 | [diff] [blame^] | 189 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "sis|O!O!:X509Extension", |
| 190 | kwlist, &type_name, &critical, &value, |
| 191 | &crypto_X509_Type, &subject, |
| 192 | &crypto_X509_Type, &issuer )) { |
Jean-Paul Calderone | 51e9066 | 2009-06-27 11:17:28 -0400 | [diff] [blame] | 193 | return NULL; |
| 194 | } |
| 195 | |
Rick Dean | 47262da | 2009-07-08 16:17:17 -0500 | [diff] [blame] | 196 | return (PyObject *)crypto_X509Extension_New(type_name, critical, value, |
| 197 | subject, issuer); |
Jean-Paul Calderone | 51e9066 | 2009-06-27 11:17:28 -0400 | [diff] [blame] | 198 | } |
| 199 | |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 200 | /* |
| 201 | * Deallocate the memory used by the X509Extension object |
| 202 | * |
| 203 | * Arguments: self - The X509Extension object |
| 204 | * Returns: None |
| 205 | */ |
| 206 | static void |
| 207 | crypto_X509Extension_dealloc(crypto_X509ExtensionObj *self) |
| 208 | { |
| 209 | /* Sometimes we don't have to dealloc this */ |
| 210 | if (self->dealloc) |
| 211 | X509_EXTENSION_free(self->x509_extension); |
| 212 | |
| 213 | PyObject_Del(self); |
| 214 | } |
| 215 | |
| 216 | /* |
| 217 | * Find attribute |
| 218 | * |
| 219 | * Arguments: self - The X509Extension object |
| 220 | * name - The attribute name |
| 221 | * Returns: A Python object for the attribute, or NULL if something |
| 222 | * went wrong. |
| 223 | */ |
| 224 | static PyObject * |
| 225 | crypto_X509Extension_getattr(crypto_X509ExtensionObj *self, char *name) |
| 226 | { |
| 227 | return Py_FindMethod(crypto_X509Extension_methods, (PyObject *)self, name); |
| 228 | } |
| 229 | |
| 230 | /* |
| 231 | * Print a nice text representation of the certificate request. |
| 232 | */ |
| 233 | static PyObject * |
| 234 | crypto_X509Extension_str(crypto_X509ExtensionObj *self) |
| 235 | { |
| 236 | int str_len; |
| 237 | char *tmp_str; |
| 238 | PyObject *str; |
| 239 | BIO *bio = BIO_new(BIO_s_mem()); |
| 240 | |
| 241 | if (!X509V3_EXT_print(bio, self->x509_extension, 0, 0)) |
| 242 | { |
| 243 | BIO_free(bio); |
| 244 | exception_from_error_queue(); |
| 245 | return NULL; |
| 246 | } |
| 247 | |
| 248 | str_len = BIO_get_mem_data(bio, &tmp_str); |
| 249 | str = PyString_FromStringAndSize(tmp_str, str_len); |
| 250 | |
| 251 | BIO_free(bio); |
| 252 | |
| 253 | return str; |
| 254 | } |
| 255 | |
| 256 | PyTypeObject crypto_X509Extension_Type = { |
| 257 | PyObject_HEAD_INIT(NULL) |
| 258 | 0, |
| 259 | "X509Extension", |
| 260 | sizeof(crypto_X509ExtensionObj), |
| 261 | 0, |
| 262 | (destructor)crypto_X509Extension_dealloc, |
| 263 | NULL, /* print */ |
| 264 | (getattrfunc)crypto_X509Extension_getattr, |
| 265 | NULL, /* setattr (setattrfunc)crypto_X509Name_setattr, */ |
| 266 | NULL, /* compare */ |
| 267 | NULL, /* repr */ |
| 268 | NULL, /* as_number */ |
| 269 | NULL, /* as_sequence */ |
| 270 | NULL, /* as_mapping */ |
| 271 | NULL, /* hash */ |
| 272 | NULL, /* call */ |
Jean-Paul Calderone | 51e9066 | 2009-06-27 11:17:28 -0400 | [diff] [blame] | 273 | (reprfunc)crypto_X509Extension_str, /* str */ |
| 274 | NULL, /* getattro */ |
| 275 | NULL, /* setattro */ |
| 276 | NULL, /* as_buffer */ |
| 277 | Py_TPFLAGS_DEFAULT, |
| 278 | crypto_X509Extension_doc, /* doc */ |
| 279 | NULL, /* traverse */ |
| 280 | NULL, /* clear */ |
| 281 | NULL, /* tp_richcompare */ |
| 282 | 0, /* tp_weaklistoffset */ |
| 283 | NULL, /* tp_iter */ |
| 284 | NULL, /* tp_iternext */ |
| 285 | crypto_X509Extension_methods, /* tp_methods */ |
| 286 | NULL, /* tp_members */ |
| 287 | NULL, /* tp_getset */ |
| 288 | NULL, /* tp_base */ |
| 289 | NULL, /* tp_dict */ |
| 290 | NULL, /* tp_descr_get */ |
| 291 | NULL, /* tp_descr_set */ |
| 292 | 0, /* tp_dictoffset */ |
| 293 | NULL, /* tp_init */ |
| 294 | NULL, /* tp_alloc */ |
| 295 | crypto_X509Extension_new, /* tp_new */ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 296 | }; |
| 297 | |
| 298 | /* |
| 299 | * Initialize the X509Extension part of the crypto module |
| 300 | * |
Jean-Paul Calderone | 51e9066 | 2009-06-27 11:17:28 -0400 | [diff] [blame] | 301 | * Arguments: dict - The crypto module |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 302 | * Returns: None |
| 303 | */ |
| 304 | int |
Jean-Paul Calderone | 51e9066 | 2009-06-27 11:17:28 -0400 | [diff] [blame] | 305 | init_crypto_x509extension(PyObject *module) |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 306 | { |
Jean-Paul Calderone | 51e9066 | 2009-06-27 11:17:28 -0400 | [diff] [blame] | 307 | if (PyType_Ready(&crypto_X509Extension_Type) < 0) { |
| 308 | return 0; |
| 309 | } |
| 310 | |
| 311 | if (PyModule_AddObject(module, "X509Extension", |
| 312 | (PyObject *)&crypto_X509Extension_Type) != 0) { |
| 313 | return 0; |
| 314 | } |
| 315 | |
| 316 | if (PyModule_AddObject(module, "X509ExtensionType", |
| 317 | (PyObject *)&crypto_X509Extension_Type) != 0) { |
| 318 | return 0; |
| 319 | } |
| 320 | |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 321 | return 1; |
| 322 | } |