1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
|
/*
* pkcs7.c
*
* Copyright (C) AB Strakt
* See LICENSE for details.
*
* PKCS7 handling code, mostly thin wrappers around OpenSSL.
* See the file RATIONALE for a short explanation of why this module was written.
*
*/
#include <Python.h>
#define crypto_MODULE
#include "crypto.h"
static char crypto_PKCS7_type_is_signed_doc[] = "\n\
Check if this NID_pkcs7_signed object\n\
\n\
@return: True if the PKCS7 is of type signed\n\
";
static PyObject *
crypto_PKCS7_type_is_signed(crypto_PKCS7Obj *self, PyObject *args)
{
if (!PyArg_ParseTuple(args, ":type_is_signed"))
return NULL;
if (PKCS7_type_is_signed(self->pkcs7))
return PyLong_FromLong(1L);
else
return PyLong_FromLong(0L);
}
static char crypto_PKCS7_type_is_enveloped_doc[] = "\n\
Check if this NID_pkcs7_enveloped object\n\
\n\
@returns: True if the PKCS7 is of type enveloped\n\
";
static PyObject *
crypto_PKCS7_type_is_enveloped(crypto_PKCS7Obj *self, PyObject *args)
{
if (!PyArg_ParseTuple(args, ":type_is_enveloped"))
return NULL;
if (PKCS7_type_is_enveloped(self->pkcs7))
return PyLong_FromLong(1L);
else
return PyLong_FromLong(0L);
}
static char crypto_PKCS7_type_is_signedAndEnveloped_doc[] = "\n\
Check if this NID_pkcs7_signedAndEnveloped object\n\
\n\
@returns: True if the PKCS7 is of type signedAndEnveloped\n\
";
static PyObject *
crypto_PKCS7_type_is_signedAndEnveloped(crypto_PKCS7Obj *self, PyObject *args)
{
if (!PyArg_ParseTuple(args, ":type_is_signedAndEnveloped"))
return NULL;
if (PKCS7_type_is_signedAndEnveloped(self->pkcs7))
return PyLong_FromLong(1L);
else
return PyLong_FromLong(0L);
}
static char crypto_PKCS7_type_is_data_doc[] = "\n\
Check if this NID_pkcs7_data object\n\
\n\
@return: True if the PKCS7 is of type data\n\
";
static PyObject *
crypto_PKCS7_type_is_data(crypto_PKCS7Obj *self, PyObject *args)
{
if (!PyArg_ParseTuple(args, ":type_is_data"))
return NULL;
if (PKCS7_type_is_data(self->pkcs7))
return PyLong_FromLong(1L);
else
return PyLong_FromLong(0L);
}
static char crypto_PKCS7_get_type_name_doc[] = "\n\
Returns the type name of the PKCS7 structure\n\
\n\
@return: A string with the typename\n\
";
static PyObject *
crypto_PKCS7_get_type_name(crypto_PKCS7Obj *self, PyObject *args)
{
if (!PyArg_ParseTuple(args, ":get_type_name"))
return NULL;
/*
* return a string with the typename
*/
return PyBytes_FromString(OBJ_nid2sn(OBJ_obj2nid(self->pkcs7->type)));
}
/*
* ADD_METHOD(name) expands to a correct PyMethodDef declaration
* { 'name', (PyCFunction)crypto_PKCS7_name, METH_VARARGS }
* for convenience
*/
#define ADD_METHOD(name) \
{ #name, (PyCFunction)crypto_PKCS7_##name, METH_VARARGS, crypto_PKCS7_##name##_doc }
static PyMethodDef crypto_PKCS7_methods[] =
{
ADD_METHOD(type_is_signed),
ADD_METHOD(type_is_enveloped),
ADD_METHOD(type_is_signedAndEnveloped),
ADD_METHOD(type_is_data),
ADD_METHOD(get_type_name),
{ NULL, NULL }
};
#undef ADD_METHOD
/*
* Constructor for PKCS7 objects, never called by Python code directly
*
* Arguments: pkcs7 - A "real" pkcs7 certificate object
* dealloc - Boolean value to specify whether the destructor should
* free the "real" pkcs7 object
* Returns: The newly created pkcs7 object
*/
crypto_PKCS7Obj *
crypto_PKCS7_New(PKCS7 *pkcs7, int dealloc)
{
crypto_PKCS7Obj *self;
self = PyObject_New(crypto_PKCS7Obj, &crypto_PKCS7_Type);
if (self == NULL)
return NULL;
self->pkcs7 = pkcs7;
self->dealloc = dealloc;
return self;
}
/*
* Deallocate the memory used by the PKCS7 object
*
* Arguments: self - The PKCS7 object
* Returns: None
*/
static void
crypto_PKCS7_dealloc(crypto_PKCS7Obj *self)
{
/* Sometimes we don't have to dealloc the "real" PKCS7 pointer ourselves */
if (self->dealloc)
PKCS7_free(self->pkcs7);
PyObject_Del(self);
}
PyTypeObject crypto_PKCS7_Type = {
PyOpenSSL_HEAD_INIT(&PyType_Type, 0)
"PKCS7",
sizeof(crypto_PKCS7Obj),
0,
(destructor)crypto_PKCS7_dealloc,
NULL, /* print */
NULL, /* getattr */
NULL, /* setattr */
NULL, /* compare */
NULL, /* repr */
NULL, /* as_number */
NULL, /* as_sequence */
NULL, /* as_mapping */
NULL, /* hash */
NULL, /* call */
NULL, /* str */
NULL, /* getattro */
NULL, /* setattro */
NULL, /* as_buffer */
Py_TPFLAGS_DEFAULT,
NULL, /* doc */
NULL, /* traverse */
NULL, /* clear */
NULL, /* tp_richcompare */
0, /* tp_weaklistoffset */
NULL, /* tp_iter */
NULL, /* tp_iternext */
crypto_PKCS7_methods, /* tp_methods */
};
/*
* Initialize the PKCS7 part of the crypto sub module
*
* Arguments: module - The crypto module
* Returns: None
*/
int
init_crypto_pkcs7(PyObject *module) {
if (PyType_Ready(&crypto_PKCS7_Type) < 0) {
return 0;
}
/* PyModule_AddObject steals a reference.
*/
Py_INCREF((PyObject *)&crypto_PKCS7_Type);
if (PyModule_AddObject(module, "PKCS7Type", (PyObject *)&crypto_PKCS7_Type) != 0) {
return 0;
}
return 1;
}
|