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