Initial import to Tizen
[profile/ivi/python-pyOpenSSL.git] / OpenSSL / crypto / x509req.c
1 /*
2  * x509req.c
3  *
4  * Copyright (C) AB Strakt
5  * Copyright (C) Jean-Paul Calderone
6  * See LICENSE for details.
7  *
8  * X.509 Request handling, mostly thin wrapping.
9  * See the file RATIONALE for a short explanation of why this module was written.
10  */
11 #include <Python.h>
12 #define crypto_MODULE
13 #include "crypto.h"
14
15
16 static char crypto_X509Req_get_subject_doc[] = "\n\
17 Create an X509Name object for the subject of the certificate request\n\
18 \n\
19 @return: An X509Name object\n\
20 ";
21
22 static PyObject *
23 crypto_X509Req_get_subject(crypto_X509ReqObj *self, PyObject *args)
24 {
25     crypto_X509NameObj *crypto_X509Name_New(X509_NAME *, int);
26     X509_NAME *name;
27     crypto_X509NameObj* pyname;
28
29     if (!PyArg_ParseTuple(args, ":get_subject"))
30         return NULL;
31
32     if ((name = X509_REQ_get_subject_name(self->x509_req)) == NULL)
33     {
34         exception_from_error_queue(crypto_Error);
35         return NULL;
36     }
37     if ((pyname = crypto_X509Name_New(name, 0)) != NULL) {
38             pyname->parent_cert = (PyObject *)self;
39             Py_INCREF(self);
40     }
41     return (PyObject *)pyname;
42 }
43
44 static char crypto_X509Req_get_pubkey_doc[] = "\n\
45 Get the public key from the certificate request\n\
46 \n\
47 @return: The public key\n\
48 ";
49
50 static PyObject *
51 crypto_X509Req_get_pubkey(crypto_X509ReqObj *self, PyObject *args)
52 {
53     crypto_PKeyObj *crypto_PKey_New(EVP_PKEY *, int);
54     EVP_PKEY *pkey;
55     crypto_PKeyObj *py_pkey;
56
57     if (!PyArg_ParseTuple(args, ":get_pubkey"))
58         return NULL;
59
60     if ((pkey = X509_REQ_get_pubkey(self->x509_req)) == NULL)
61     {
62         exception_from_error_queue(crypto_Error);
63         return NULL;
64     }
65
66     py_pkey = crypto_PKey_New(pkey, 1);
67     if (py_pkey != NULL) {
68         py_pkey->only_public = 1;
69     }
70     return (PyObject *)py_pkey;
71 }
72
73 static char crypto_X509Req_set_pubkey_doc[] = "\n\
74 Set the public key of the certificate request\n\
75 \n\
76 @param pkey: The public key to use\n\
77 @return: None\n\
78 ";
79
80 static PyObject *
81 crypto_X509Req_set_pubkey(crypto_X509ReqObj *self, PyObject *args)
82 {
83     crypto_PKeyObj *pkey;
84
85     if (!PyArg_ParseTuple(args, "O!:set_pubkey", &crypto_PKey_Type, &pkey))
86         return NULL;
87
88     if (!X509_REQ_set_pubkey(self->x509_req, pkey->pkey))
89     {
90         exception_from_error_queue(crypto_Error);
91         return NULL;
92     }
93
94     Py_INCREF(Py_None);
95     return Py_None;
96 }
97
98 static char crypto_X509Req_sign_doc[] = "\n\
99 Sign the certificate request using the supplied key and digest\n\
100 \n\
101 @param pkey: The key to sign with\n\
102 @param digest: The message digest to use\n\
103 @return: None\n\
104 ";
105
106 static PyObject *
107 crypto_X509Req_sign(crypto_X509ReqObj *self, PyObject *args)
108 {
109     crypto_PKeyObj *pkey;
110     char *digest_name;
111     const EVP_MD *digest;
112
113     if (!PyArg_ParseTuple(args, "O!s:sign", &crypto_PKey_Type, &pkey,
114                           &digest_name))
115         return NULL;
116
117     if (pkey->only_public) {
118         PyErr_SetString(PyExc_ValueError, "Key has only public part");
119         return NULL;
120     }
121
122     if (!pkey->initialized) {
123         PyErr_SetString(PyExc_ValueError, "Key is uninitialized");
124         return NULL;
125     }
126
127     if ((digest = EVP_get_digestbyname(digest_name)) == NULL)
128     {
129         PyErr_SetString(PyExc_ValueError, "No such digest method");
130         return NULL;
131     }
132
133     if (!X509_REQ_sign(self->x509_req, pkey->pkey, digest))
134     {
135         exception_from_error_queue(crypto_Error);
136         return NULL;
137     }
138
139     Py_INCREF(Py_None);
140     return Py_None;
141 }
142
143 static char crypto_X509Req_verify_doc[] = "\n\
144 Verifies a certificate request using the supplied public key\n\
145 \n\
146 @param key: a public key\n\
147 @return: True if the signature is correct.\n\
148 @raise OpenSSL.crypto.Error: If the signature is invalid or there is a\n\
149     problem verifying the signature.\n\
150 ";
151
152 PyObject *
153 crypto_X509Req_verify(crypto_X509ReqObj *self, PyObject *args)
154 {
155     PyObject *obj;
156     crypto_PKeyObj *key;
157     int answer;
158
159     if (!PyArg_ParseTuple(args, "O!:verify", &crypto_PKey_Type, &obj)) {
160         return NULL;
161     }
162
163     key = (crypto_PKeyObj *)obj;
164
165     if ((answer = X509_REQ_verify(self->x509_req, key->pkey)) <= 0) {
166         exception_from_error_queue(crypto_Error);
167         return NULL;
168     }
169
170     return PyLong_FromLong(answer);
171 }
172
173 static char crypto_X509Req_add_extensions_doc[] = "\n\
174 Add extensions to the request.\n\
175 \n\
176 @param extensions: a sequence of X509Extension objects\n\
177 @return: None\n\
178 ";
179
180 static PyObject *
181 crypto_X509Req_add_extensions(crypto_X509ReqObj *self, PyObject *args)
182 {
183     PyObject *extensions;
184     crypto_X509ExtensionObj *ext;
185     STACK_OF(X509_EXTENSION) *exts;
186     int nr_of_extensions, i;
187
188     if (!PyArg_ParseTuple(args, "O:add_extensions", &extensions))
189         return NULL;
190
191     if (!PySequence_Check(extensions))
192     {
193         PyErr_SetString(PyExc_TypeError, "Expected a sequence");
194         return NULL;
195     }
196
197     /* Make a STACK_OF(X509_EXTENSION) from sequence */
198     if ((exts = sk_X509_EXTENSION_new_null()) == NULL)
199     {
200         exception_from_error_queue(crypto_Error);
201         return NULL;
202     }
203
204     /* Put the extensions in a stack */
205     nr_of_extensions = PySequence_Length(extensions);
206
207     for (i = 0; i < nr_of_extensions; i++)
208     {
209         ext = (crypto_X509ExtensionObj *)PySequence_GetItem(extensions, i);
210         if (!(crypto_X509Extension_Check(ext)))
211         {
212             PyErr_SetString(PyExc_ValueError,
213                             "One of the elements is not an X509Extension");
214             sk_X509_EXTENSION_free(exts);
215             return NULL;
216         }
217         sk_X509_EXTENSION_push(exts, ext->x509_extension);
218     }
219
220     if (!X509_REQ_add_extensions(self->x509_req, exts))
221     {
222         sk_X509_EXTENSION_free(exts);
223         exception_from_error_queue(crypto_Error);
224         return NULL;
225     }
226
227     sk_X509_EXTENSION_free(exts);
228
229     Py_INCREF(Py_None);
230     return Py_None;
231 }
232
233 static char crypto_X509Req_set_version_doc[] = "\n\
234 Set the version subfield (RFC 2459, section 4.1.2.1) of the certificate\n\
235 request.\n\
236 \n\
237 @param version: The version number\n\
238 @return: None\n\
239 ";
240
241 static PyObject *
242 crypto_X509Req_set_version(crypto_X509ReqObj *self, PyObject *args)
243 {
244     long version;
245
246     if (!PyArg_ParseTuple(args, "l:set_version", &version)) {
247         return NULL;
248     }
249
250     if (!X509_REQ_set_version(self->x509_req, version)) {
251         return NULL;
252     }
253
254     Py_INCREF(Py_None);
255     return Py_None;
256 }
257
258 static char crypto_X509Req_get_version_doc[] = "\n\
259 Get the version subfield (RFC 2459, section 4.1.2.1) of the certificate\n\
260 request.\n\
261 \n\
262 @return: an integer giving the value of the version subfield\n\
263 ";
264
265 static PyObject *
266 crypto_X509Req_get_version(crypto_X509ReqObj *self, PyObject *args)
267 {
268     long version;
269
270     if (!PyArg_ParseTuple(args, ":get_version")) {
271         return NULL;
272     }
273
274     version = X509_REQ_get_version(self->x509_req);
275
276     return PyLong_FromLong(version);
277 }
278
279 /*
280  * ADD_METHOD(name) expands to a correct PyMethodDef declaration
281  *   {  'name', (PyCFunction)crypto_X509Req_name, METH_VARARGS }
282  * for convenience
283  */
284 #define ADD_METHOD(name)        \
285     { #name, (PyCFunction)crypto_X509Req_##name, METH_VARARGS, crypto_X509Req_##name##_doc }
286 static PyMethodDef crypto_X509Req_methods[] =
287 {
288     ADD_METHOD(get_subject),
289     ADD_METHOD(get_pubkey),
290     ADD_METHOD(set_pubkey),
291     ADD_METHOD(sign),
292     ADD_METHOD(verify),
293     ADD_METHOD(add_extensions),
294     ADD_METHOD(set_version),
295     ADD_METHOD(get_version),
296     { NULL, NULL }
297 };
298 #undef ADD_METHOD
299
300
301 /*
302  * Constructor for X509Req, never called by Python code directly
303  *
304  * Arguments: name    - A "real" X509_REQ object
305  *            dealloc - Boolean value to specify whether the destructor should
306  *                      free the "real" X509_REQ object
307  * Returns:   The newly created X509Req object
308  */
309 crypto_X509ReqObj *
310 crypto_X509Req_New(X509_REQ *req, int dealloc)
311 {
312     crypto_X509ReqObj *self;
313
314     self = PyObject_New(crypto_X509ReqObj, &crypto_X509Req_Type);
315
316     if (self == NULL)
317         return NULL;
318
319     self->x509_req = req;
320     self->dealloc = dealloc;
321
322     return self;
323 }
324
325
326 static char crypto_X509Req_doc[] = "\n\
327 X509Req() -> X509Req instance\n\
328 \n\
329 Create a new X509Req object.\n\
330 \n\
331 @return: The X509Req object\n\
332 ";
333
334 static PyObject *
335 crypto_X509Req_new(PyTypeObject *subtype, PyObject *args, PyObject *kwargs) {
336     if (!PyArg_ParseTuple(args, ":X509Req")) {
337         return NULL;
338     }
339
340     return (PyObject *)crypto_X509Req_New(X509_REQ_new(), 1);
341 }
342
343
344 /*
345  * Deallocate the memory used by the X509Req object
346  *
347  * Arguments: self - The X509Req object
348  * Returns:   None
349  */
350 static void
351 crypto_X509Req_dealloc(crypto_X509ReqObj *self)
352 {
353     /* Sometimes we don't have to dealloc this */
354     if (self->dealloc)
355         X509_REQ_free(self->x509_req);
356
357     PyObject_Del(self);
358 }
359
360
361 PyTypeObject crypto_X509Req_Type = {
362     PyOpenSSL_HEAD_INIT(&PyType_Type, 0)
363     "X509Req",
364     sizeof(crypto_X509ReqObj),
365     0,
366     (destructor)crypto_X509Req_dealloc,
367     NULL, /* print */
368     NULL, /* getattr */
369     NULL, /* setattr */
370     NULL, /* compare */
371     NULL, /* repr */
372     NULL, /* as_number */
373     NULL, /* as_sequence */
374     NULL, /* as_mapping */
375     NULL, /* hash */
376     NULL, /* call */
377     NULL, /* str */
378     NULL, /* getattro */
379     NULL, /* setattro */
380     NULL, /* as_buffer */
381     Py_TPFLAGS_DEFAULT,
382     crypto_X509Req_doc, /* doc */
383     NULL, /* traverse */
384     NULL, /* clear */
385     NULL, /* tp_richcompare */
386     0, /* tp_weaklistoffset */
387     NULL, /* tp_iter */
388     NULL, /* tp_iternext */
389     crypto_X509Req_methods, /* tp_methods */
390     NULL, /* tp_members */
391     NULL, /* tp_getset */
392     NULL, /* tp_base */
393     NULL, /* tp_dict */
394     NULL, /* tp_descr_get */
395     NULL, /* tp_descr_set */
396     0, /* tp_dictoffset */
397     NULL, /* tp_init */
398     NULL, /* tp_alloc */
399     crypto_X509Req_new, /* tp_new */
400 };
401
402
403 /*
404  * Initialize the X509Req part of the crypto module
405  *
406  * Arguments: module - The crypto module
407  * Returns:   None
408  */
409 int
410 init_crypto_x509req(PyObject *module)
411 {
412     if (PyType_Ready(&crypto_X509Req_Type) < 0) {
413         return 0;
414     }
415
416     if (PyModule_AddObject(module, "X509Req", (PyObject *)&crypto_X509Req_Type) != 0) {
417         return 0;
418     }
419
420     if (PyModule_AddObject(module, "X509ReqType", (PyObject *)&crypto_X509Req_Type) != 0) {
421         return 0;
422     }
423
424     return 1;
425 }