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\ |
| 19 | Arguments: self - The X509Extension object\n\ |
| 20 | args - The argument tuple, should be empty\n\ |
| 21 | Returns: The critical field.\n\ |
| 22 | "; |
| 23 | |
| 24 | static PyObject * |
| 25 | crypto_X509Extension_get_critical(crypto_X509ExtensionObj *self, PyObject *args) |
| 26 | { |
| 27 | if (!PyArg_ParseTuple(args, ":get_critical")) |
| 28 | return NULL; |
| 29 | |
| 30 | return PyInt_FromLong(X509_EXTENSION_get_critical(self->x509_extension)); |
| 31 | } |
| 32 | |
| 33 | /* |
| 34 | * ADD_METHOD(name) expands to a correct PyMethodDef declaration |
| 35 | * { 'name', (PyCFunction)crypto_X509Extension_name, METH_VARARGS } |
| 36 | * for convenience |
| 37 | */ |
| 38 | #define ADD_METHOD(name) \ |
| 39 | { #name, (PyCFunction)crypto_X509Extension_##name, METH_VARARGS, crypto_X509Extension_##name##_doc } |
| 40 | static PyMethodDef crypto_X509Extension_methods[] = |
| 41 | { |
| 42 | ADD_METHOD(get_critical), |
| 43 | { NULL, NULL } |
| 44 | }; |
| 45 | #undef ADD_METHOD |
| 46 | |
| 47 | /* |
| 48 | * Constructor for X509Extension, never called by Python code directly |
| 49 | * |
| 50 | * Arguments: type_name - ??? |
| 51 | * critical - ??? |
| 52 | * value - ??? |
| 53 | * Returns: The newly created X509Extension object |
| 54 | */ |
| 55 | crypto_X509ExtensionObj * |
| 56 | crypto_X509Extension_New(char *type_name, int critical, char *value) |
| 57 | { |
| 58 | crypto_X509ExtensionObj *self; |
Jean-Paul Calderone | e7db4b4 | 2008-12-31 13:39:24 -0500 | [diff] [blame^] | 59 | char* value_with_critical = NULL; |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 60 | |
| 61 | self = PyObject_New(crypto_X509ExtensionObj, &crypto_X509Extension_Type); |
| 62 | |
Jean-Paul Calderone | e7db4b4 | 2008-12-31 13:39:24 -0500 | [diff] [blame^] | 63 | if (self == NULL) { |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 64 | return NULL; |
| 65 | } |
| 66 | |
Jean-Paul Calderone | e7db4b4 | 2008-12-31 13:39:24 -0500 | [diff] [blame^] | 67 | /* There are other OpenSSL APIs which would let us pass in critical |
| 68 | * separately, but they're harder to use, and since value is already a pile |
| 69 | * of crappy junk smuggling a ton of utterly important structured data, |
| 70 | * what's the point of trying to avoid nasty stuff with strings? (However, |
| 71 | * X509V3_EXT_i2d in particular seems like it would be a better API to |
| 72 | * invoke. I do not know where to get the ext_struc it desires for its |
| 73 | * last parameter, though.) */ |
| 74 | value_with_critical = malloc(strlen("critical,") + strlen(value) + 1); |
| 75 | if (critical) { |
| 76 | strcpy(value_with_critical, "critical,"); |
| 77 | strcpy(value_with_critical + strlen("critical,"), value); |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 78 | } else { |
Jean-Paul Calderone | e7db4b4 | 2008-12-31 13:39:24 -0500 | [diff] [blame^] | 79 | strcpy(value_with_critical, value); |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 80 | } |
| 81 | |
Jean-Paul Calderone | e7db4b4 | 2008-12-31 13:39:24 -0500 | [diff] [blame^] | 82 | self->x509_extension = X509V3_EXT_nconf( |
| 83 | NULL, NULL, type_name, value_with_critical); |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 84 | self->dealloc = 1; |
| 85 | |
Jean-Paul Calderone | e7db4b4 | 2008-12-31 13:39:24 -0500 | [diff] [blame^] | 86 | free(value_with_critical); |
| 87 | |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 88 | return self; |
| 89 | } |
| 90 | |
| 91 | /* |
| 92 | * Deallocate the memory used by the X509Extension object |
| 93 | * |
| 94 | * Arguments: self - The X509Extension object |
| 95 | * Returns: None |
| 96 | */ |
| 97 | static void |
| 98 | crypto_X509Extension_dealloc(crypto_X509ExtensionObj *self) |
| 99 | { |
| 100 | /* Sometimes we don't have to dealloc this */ |
| 101 | if (self->dealloc) |
| 102 | X509_EXTENSION_free(self->x509_extension); |
| 103 | |
| 104 | PyObject_Del(self); |
| 105 | } |
| 106 | |
| 107 | /* |
| 108 | * Find attribute |
| 109 | * |
| 110 | * Arguments: self - The X509Extension object |
| 111 | * name - The attribute name |
| 112 | * Returns: A Python object for the attribute, or NULL if something |
| 113 | * went wrong. |
| 114 | */ |
| 115 | static PyObject * |
| 116 | crypto_X509Extension_getattr(crypto_X509ExtensionObj *self, char *name) |
| 117 | { |
| 118 | return Py_FindMethod(crypto_X509Extension_methods, (PyObject *)self, name); |
| 119 | } |
| 120 | |
| 121 | /* |
| 122 | * Print a nice text representation of the certificate request. |
| 123 | */ |
| 124 | static PyObject * |
| 125 | crypto_X509Extension_str(crypto_X509ExtensionObj *self) |
| 126 | { |
| 127 | int str_len; |
| 128 | char *tmp_str; |
| 129 | PyObject *str; |
| 130 | BIO *bio = BIO_new(BIO_s_mem()); |
| 131 | |
| 132 | if (!X509V3_EXT_print(bio, self->x509_extension, 0, 0)) |
| 133 | { |
| 134 | BIO_free(bio); |
| 135 | exception_from_error_queue(); |
| 136 | return NULL; |
| 137 | } |
| 138 | |
| 139 | str_len = BIO_get_mem_data(bio, &tmp_str); |
| 140 | str = PyString_FromStringAndSize(tmp_str, str_len); |
| 141 | |
| 142 | BIO_free(bio); |
| 143 | |
| 144 | return str; |
| 145 | } |
| 146 | |
| 147 | PyTypeObject crypto_X509Extension_Type = { |
| 148 | PyObject_HEAD_INIT(NULL) |
| 149 | 0, |
| 150 | "X509Extension", |
| 151 | sizeof(crypto_X509ExtensionObj), |
| 152 | 0, |
| 153 | (destructor)crypto_X509Extension_dealloc, |
| 154 | NULL, /* print */ |
| 155 | (getattrfunc)crypto_X509Extension_getattr, |
| 156 | NULL, /* setattr (setattrfunc)crypto_X509Name_setattr, */ |
| 157 | NULL, /* compare */ |
| 158 | NULL, /* repr */ |
| 159 | NULL, /* as_number */ |
| 160 | NULL, /* as_sequence */ |
| 161 | NULL, /* as_mapping */ |
| 162 | NULL, /* hash */ |
| 163 | NULL, /* call */ |
| 164 | (reprfunc)crypto_X509Extension_str /* str */ |
| 165 | }; |
| 166 | |
| 167 | /* |
| 168 | * Initialize the X509Extension part of the crypto module |
| 169 | * |
| 170 | * Arguments: dict - The crypto module dictionary |
| 171 | * Returns: None |
| 172 | */ |
| 173 | int |
| 174 | init_crypto_x509extension(PyObject *dict) |
| 175 | { |
| 176 | crypto_X509Extension_Type.ob_type = &PyType_Type; |
| 177 | Py_INCREF(&crypto_X509Extension_Type); |
| 178 | PyDict_SetItemString(dict, "X509ExtensionType", |
| 179 | (PyObject *)&crypto_X509Extension_Type); |
| 180 | return 1; |
| 181 | } |
| 182 | |