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