blob: 82894081b5b3a2ca87a125bbd58189dac774f0ac [file] [log] [blame]
Rick Dean536ba022009-07-24 23:57:27 -05001#include <Python.h>
2#define crypto_MODULE
3#include "crypto.h"
4
5
6static char crypto_Revoked_get_rev_date_doc[] = "\n\
7Retrieve the revocation date\n\
8\n\
9@return: A string giving the timestamp, in the format:\n\
10\n\
11 YYYYMMDDhhmmssZ\n\
12 YYYYMMDDhhmmss+hhmm\n\
13 YYYYMMDDhhmmss-hhmm\n\
14";
15
16static PyObject*
17crypto_Revoked_get_rev_date(crypto_RevokedObj *self, PyObject *args)
18{
19 /* returns a borrowed reference. */
20 return _get_asn1_time(
21 ":get_rev_date", self->revoked->revocationDate, args);
22}
23
24static char crypto_Revoked_set_rev_date_doc[] = "\n\
25Set the revocation timestamp\n\
26\n\
27@param when: A string giving the timestamp, in the format:\n\
28\n\
29 YYYYMMDDhhmmssZ\n\
30 YYYYMMDDhhmmss+hhmm\n\
31 YYYYMMDDhhmmss-hhmm\n\
32\n\
33@return: None\n\
34";
35
36static PyObject*
37crypto_Revoked_set_rev_date(crypto_RevokedObj *self, PyObject *args)
38{
39 return _set_asn1_time(
40 "s:set_rev_date", self->revoked->revocationDate, args);
41}
42
43
44static PyObject *
45ASN1_INTEGER_to_PyString(ASN1_INTEGER *asn1_int)
46{
47 BIO *bio = NULL;
48 PyObject *buf = NULL;
49 int ret, pending;
50
51 /* Create a openssl BIO buffer */
52 bio = BIO_new(BIO_s_mem());
53 if (bio == NULL)
54 goto err;
55
56 /* Write the integer to the BIO as a hex string. */
57 i2a_ASN1_INTEGER(bio, asn1_int);
58
59 /* Allocate a Python string. */
60 pending = BIO_pending(bio);
61 buf = PyString_FromStringAndSize(NULL, pending);
62 if (buf == NULL) {
63 goto err;
64 }
65
66 /* Copy the BIO contents to a Python string. */
67 ret = BIO_read(bio, PyString_AsString(buf), pending);
68 if (ret <= 0) { /* problem with BIO_read */
69 goto err;
70 }
71
72 /* Cleanup */
73 BIO_free(bio);
74 bio = NULL;
75 return buf;
76
77 err:
78 if(bio) {
79 BIO_free(bio);
80 }
81 if(buf) {
82 Py_DECREF(buf);
83 }
84 return NULL;
85}
86
87
88static char crypto_Revoked_get_serial_doc[] = "\n\
89Return the serial number of a Revoked structure\n\
90\n\
91@return: The serial number as a string\n\
92";
93static PyObject *
94crypto_Revoked_get_serial(crypto_RevokedObj *self, PyObject *args)
95{
96 if (!PyArg_ParseTuple(args, ":get_serial"))
97 return NULL;
98
99 if(self->revoked->serialNumber == NULL) {
100 /* never happens */
101 Py_INCREF(Py_None);
102 return Py_None;
103 } else {
104 return ASN1_INTEGER_to_PyString(self->revoked->serialNumber);
105 }
106}
107
108static char crypto_Revoked_set_serial_doc[] = "\n\
109Set the serial number of a revoked Revoked structure\n\
110\n\
111@param hex_str: The new serial number.\n\
112@type hex_str: L{str}\n\
113@return: None\n\
114";
115static PyObject *
116crypto_Revoked_set_serial(crypto_RevokedObj *self, PyObject *args, PyObject *keywds)
117{
118 static char *kwlist[] = {"hex_str", NULL};
119 const char *hex_str = NULL;
120 BIGNUM *serial = NULL;
121 ASN1_INTEGER *tmpser = NULL;
122
123 if (!PyArg_ParseTupleAndKeywords(args, keywds, "s:set_serial",
124 kwlist, &hex_str))
125 return NULL;
126
127 if( ! BN_hex2bn(&serial, hex_str) ) {
128 PyErr_SetString(PyExc_TypeError, "bad hex string");
129 return NULL;
130 }
131
132 tmpser = BN_to_ASN1_INTEGER(serial, NULL);
133 BN_free(serial);
134 serial = NULL;
135 X509_REVOKED_set_serialNumber(self->revoked, tmpser);
136 ASN1_INTEGER_free(tmpser);
137
138 Py_INCREF(Py_None);
139 return Py_None;
140}
141
142
143crypto_RevokedObj *
144crypto_Revoked_New(X509_REVOKED *revoked)
145{
146 crypto_RevokedObj *self;
147
148 self = PyObject_New(crypto_RevokedObj, &crypto_Revoked_Type);
149 if (self==NULL)
150 return NULL;
151 self->revoked = revoked;
152 return self;
153}
154
155/*
156 * ADD_METHOD(name) expands to a correct PyMethodDef declaration
157 * { 'name', (PyCFunction)crypto_Revoked_name, METH_VARARGS, crypto_Revoked_name_doc }
158 * for convenience
159 */
160#define ADD_METHOD(name) \
161 { #name, (PyCFunction)crypto_Revoked_##name, METH_VARARGS, crypto_Revoked_##name##_doc }
162#define ADD_KW_METHOD(name) \
163 { #name, (PyCFunction)crypto_Revoked_##name, METH_VARARGS | METH_KEYWORDS, crypto_Revoked_##name##_doc }
164static PyMethodDef crypto_Revoked_methods[] =
165{
166 ADD_METHOD(get_rev_date),
167 ADD_METHOD(set_rev_date),
168 ADD_METHOD(get_serial),
169 ADD_KW_METHOD(set_serial),
170 { NULL, NULL }
171};
172#undef ADD_METHOD
173
174
175static PyObject *
176crypto_Revoked_getattr(crypto_RevokedObj *self, char *name)
177{
178 return Py_FindMethod(crypto_Revoked_methods, (PyObject *)self, name);
179}
180
181static void
182crypto_Revoked_dealloc(crypto_RevokedObj *self)
183{
184 X509_REVOKED_free(self->revoked);
185 self->revoked = NULL;
186
187 PyObject_Del(self);
188}
189
190static char crypto_Revoked_doc[] = "\n\
191Revoked() -> Revoked instance\n\
192\n\
193Create a new empty Revoked object.\n\
194\n\
195@returns: The Revoked object\n\
196";
197
198static PyObject* crypto_Revoked_new(PyTypeObject *subtype, PyObject *args, PyObject *kwargs) {
199 if (!PyArg_ParseTuple(args, ":Revoked")) {
200 return NULL;
201 }
202
203 return (PyObject *)crypto_Revoked_New(X509_REVOKED_new());
204}
205
206PyTypeObject crypto_Revoked_Type = {
207 PyObject_HEAD_INIT(NULL)
208 0,
209 "Revoked",
210 sizeof(crypto_RevokedObj),
211 0,
212 (destructor)crypto_Revoked_dealloc,
213 NULL, /* print */
214 (getattrfunc)crypto_Revoked_getattr,
215 NULL, /* setattr */
216 NULL, /* compare */
217 NULL, /* repr */
218 NULL, /* as_number */
219 NULL, /* as_sequence */
220 NULL, /* as_mapping */
221 NULL, /* hash */
222 NULL, /* call */
223 NULL, /* str */
224 NULL, /* getattro */
225 NULL, /* setattro */
226 NULL, /* as_buffer */
227 Py_TPFLAGS_DEFAULT,
228 crypto_Revoked_doc, /* doc */
229 NULL, /* traverse */
230 NULL, /* clear */
231 NULL, /* tp_richcompare */
232 0, /* tp_weaklistoffset */
233 NULL, /* tp_iter */
234 NULL, /* tp_iternext */
235 crypto_Revoked_methods, /* tp_methods */
236 NULL, /* tp_members */
237 NULL, /* tp_getset */
238 NULL, /* tp_base */
239 NULL, /* tp_dict */
240 NULL, /* tp_descr_get */
241 NULL, /* tp_descr_set */
242 0, /* tp_dictoffset */
243 NULL, /* tp_init */
244 NULL, /* tp_alloc */
245 crypto_Revoked_new, /* tp_new */
246};
247
248int init_crypto_revoked(PyObject *module) {
249 if(PyType_Ready(&crypto_Revoked_Type) < 0) {
250 return 0;
251 }
252
253 if (PyModule_AddObject(module, "Revoked", (PyObject *)&crypto_Revoked_Type) != 0) {
254 return 0;
255 }
256 return 1;
257}
258