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