4 * Copyright (C) AB Strakt
5 * See LICENSE for details.
7 * PKCS7 handling code, mostly thin wrappers around OpenSSL.
8 * See the file RATIONALE for a short explanation of why this module was written.
15 static char crypto_PKCS7_type_is_signed_doc[] = "\n\
16 Check if this NID_pkcs7_signed object\n\
18 @return: True if the PKCS7 is of type signed\n\
22 crypto_PKCS7_type_is_signed(crypto_PKCS7Obj *self, PyObject *args)
24 if (!PyArg_ParseTuple(args, ":type_is_signed"))
27 if (PKCS7_type_is_signed(self->pkcs7))
28 return PyLong_FromLong(1L);
30 return PyLong_FromLong(0L);
33 static char crypto_PKCS7_type_is_enveloped_doc[] = "\n\
34 Check if this NID_pkcs7_enveloped object\n\
36 @returns: True if the PKCS7 is of type enveloped\n\
40 crypto_PKCS7_type_is_enveloped(crypto_PKCS7Obj *self, PyObject *args)
42 if (!PyArg_ParseTuple(args, ":type_is_enveloped"))
45 if (PKCS7_type_is_enveloped(self->pkcs7))
46 return PyLong_FromLong(1L);
48 return PyLong_FromLong(0L);
51 static char crypto_PKCS7_type_is_signedAndEnveloped_doc[] = "\n\
52 Check if this NID_pkcs7_signedAndEnveloped object\n\
54 @returns: True if the PKCS7 is of type signedAndEnveloped\n\
58 crypto_PKCS7_type_is_signedAndEnveloped(crypto_PKCS7Obj *self, PyObject *args)
60 if (!PyArg_ParseTuple(args, ":type_is_signedAndEnveloped"))
63 if (PKCS7_type_is_signedAndEnveloped(self->pkcs7))
64 return PyLong_FromLong(1L);
66 return PyLong_FromLong(0L);
69 static char crypto_PKCS7_type_is_data_doc[] = "\n\
70 Check if this NID_pkcs7_data object\n\
72 @return: True if the PKCS7 is of type data\n\
76 crypto_PKCS7_type_is_data(crypto_PKCS7Obj *self, PyObject *args)
78 if (!PyArg_ParseTuple(args, ":type_is_data"))
81 if (PKCS7_type_is_data(self->pkcs7))
82 return PyLong_FromLong(1L);
84 return PyLong_FromLong(0L);
87 static char crypto_PKCS7_get_type_name_doc[] = "\n\
88 Returns the type name of the PKCS7 structure\n\
90 @return: A string with the typename\n\
94 crypto_PKCS7_get_type_name(crypto_PKCS7Obj *self, PyObject *args)
96 if (!PyArg_ParseTuple(args, ":get_type_name"))
100 * return a string with the typename
102 return PyBytes_FromString(OBJ_nid2sn(OBJ_obj2nid(self->pkcs7->type)));
106 * ADD_METHOD(name) expands to a correct PyMethodDef declaration
107 * { 'name', (PyCFunction)crypto_PKCS7_name, METH_VARARGS }
110 #define ADD_METHOD(name) \
111 { #name, (PyCFunction)crypto_PKCS7_##name, METH_VARARGS, crypto_PKCS7_##name##_doc }
112 static PyMethodDef crypto_PKCS7_methods[] =
114 ADD_METHOD(type_is_signed),
115 ADD_METHOD(type_is_enveloped),
116 ADD_METHOD(type_is_signedAndEnveloped),
117 ADD_METHOD(type_is_data),
118 ADD_METHOD(get_type_name),
125 * Constructor for PKCS7 objects, never called by Python code directly
127 * Arguments: pkcs7 - A "real" pkcs7 certificate object
128 * dealloc - Boolean value to specify whether the destructor should
129 * free the "real" pkcs7 object
130 * Returns: The newly created pkcs7 object
133 crypto_PKCS7_New(PKCS7 *pkcs7, int dealloc)
135 crypto_PKCS7Obj *self;
137 self = PyObject_New(crypto_PKCS7Obj, &crypto_PKCS7_Type);
143 self->dealloc = dealloc;
149 * Deallocate the memory used by the PKCS7 object
151 * Arguments: self - The PKCS7 object
155 crypto_PKCS7_dealloc(crypto_PKCS7Obj *self)
157 /* Sometimes we don't have to dealloc the "real" PKCS7 pointer ourselves */
159 PKCS7_free(self->pkcs7);
164 PyTypeObject crypto_PKCS7_Type = {
165 PyOpenSSL_HEAD_INIT(&PyType_Type, 0)
167 sizeof(crypto_PKCS7Obj),
169 (destructor)crypto_PKCS7_dealloc,
175 NULL, /* as_number */
176 NULL, /* as_sequence */
177 NULL, /* as_mapping */
183 NULL, /* as_buffer */
188 NULL, /* tp_richcompare */
189 0, /* tp_weaklistoffset */
191 NULL, /* tp_iternext */
192 crypto_PKCS7_methods, /* tp_methods */
196 * Initialize the PKCS7 part of the crypto sub module
198 * Arguments: module - The crypto module
202 init_crypto_pkcs7(PyObject *module) {
203 if (PyType_Ready(&crypto_PKCS7_Type) < 0) {
207 if (PyModule_AddObject(module, "PKCS7Type", (PyObject *)&crypto_PKCS7_Type) != 0) {