blob: d551de4e54d5905207459929e8bc0576ca76179a [file] [log] [blame]
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001/*
2 * x509req.c
3 *
4 * Copyright (C) AB Strakt 2001, All rights reserved
5 *
6 * X.509 Request handling, mostly thin wrapping.
7 * See the file RATIONALE for a short explanation of why this module was written.
8 */
9#include <Python.h>
10#define crypto_MODULE
11#include "crypto.h"
12
13static char *CVSid = "@(#) $Id: x509req.c,v 1.15 2002/09/04 22:24:59 iko Exp $";
14
15
16static char crypto_X509Req_get_subject_doc[] = "\n\
17Create an X509Name object for the subject of the certificate request\n\
18\n\
19Arguments: self - The X509Req object\n\
20 args - The Python argument tuple, should be empty\n\
21Returns: An X509Name object\n\
22";
23
24static PyObject *
25crypto_X509Req_get_subject(crypto_X509ReqObj *self, PyObject *args)
26{
27 crypto_X509NameObj *crypto_X509Name_New(X509_NAME *, int);
28 X509_NAME *name;
29
30 if (!PyArg_ParseTuple(args, ":get_subject"))
31 return NULL;
32
33 if ((name = X509_REQ_get_subject_name(self->x509_req)) == NULL)
34 {
35 exception_from_error_queue();
36 return NULL;
37 }
38
39 return (PyObject *)crypto_X509Name_New(name, 0);
40}
41
42static char crypto_X509Req_get_pubkey_doc[] = "\n\
43Get the public key from the certificate request\n\
44\n\
45Arguments: self - The X509Req object\n\
46 args - The Python argument tuple, should be empty\n\
47Returns: The public key\n\
48";
49
50static PyObject *
51crypto_X509Req_get_pubkey(crypto_X509ReqObj *self, PyObject *args)
52{
53 crypto_PKeyObj *crypto_PKey_New(EVP_PKEY *, int);
54 EVP_PKEY *pkey;
55
56 if (!PyArg_ParseTuple(args, ":get_pubkey"))
57 return NULL;
58
59 if ((pkey = X509_REQ_get_pubkey(self->x509_req)) == NULL)
60 {
61 exception_from_error_queue();
62 return NULL;
63 }
64
65 return (PyObject *)crypto_PKey_New(pkey, 1);
66}
67
68static char crypto_X509Req_set_pubkey_doc[] = "\n\
69Set the public key of the certificate request\n\
70\n\
71Arguments: self - The X509Req object\n\
72 args - The Python argument tuple, should be:\n\
73 pkey - The public key to use\n\
74Returns: None\n\
75";
76
77static PyObject *
78crypto_X509Req_set_pubkey(crypto_X509ReqObj *self, PyObject *args)
79{
80 crypto_PKeyObj *pkey;
81
82 if (!PyArg_ParseTuple(args, "O!:set_pubkey", &crypto_PKey_Type, &pkey))
83 return NULL;
84
85 if (!X509_REQ_set_pubkey(self->x509_req, pkey->pkey))
86 {
87 exception_from_error_queue();
88 return NULL;
89 }
90
91 Py_INCREF(Py_None);
92 return Py_None;
93}
94
95static char crypto_X509Req_sign_doc[] = "\n\
96Sign the certificate request using the supplied key and digest\n\
97\n\
98Arguments: self - The X509Req object\n\
99 args - The Python argument tuple, should be:\n\
100 pkey - The key to sign with\n\
101 digest - The message digest to use\n\
102Returns: None\n\
103";
104
105static PyObject *
106crypto_X509Req_sign(crypto_X509ReqObj *self, PyObject *args)
107{
108 crypto_PKeyObj *pkey;
109 char *digest_name;
110 const EVP_MD *digest;
111
112 if (!PyArg_ParseTuple(args, "O!s:sign", &crypto_PKey_Type, &pkey,
113 &digest_name))
114 return NULL;
115
116 if ((digest = EVP_get_digestbyname(digest_name)) == NULL)
117 {
118 PyErr_SetString(PyExc_ValueError, "No such digest method");
119 return NULL;
120 }
121
122 if (!X509_REQ_sign(self->x509_req, pkey->pkey, digest))
123 {
124 exception_from_error_queue();
125 return NULL;
126 }
127
128 Py_INCREF(Py_None);
129 return Py_None;
130}
131
132static char crypto_X509Req_verify_doc[] = "\n\
133Verifies a certificate request using the supplied public key\n\
134 \n\
135Arguments: self - X509Req object\n\
136 args - The Python argument tuple, should be:\n\
137 key - a public key\n\
138Returns: True, if the signature is correct, 0 otherwise.\n\
139";
140
141PyObject *
142crypto_X509Req_verify(crypto_X509ReqObj *self, PyObject *args)
143{
144 PyObject *obj;
145 crypto_PKeyObj *key;
146 int answer;
147
148 if (!PyArg_ParseTuple(args, "O!:verify", &crypto_PKey_Type, &obj))
149 return NULL;
150
151 key = (crypto_PKeyObj *)obj;
152
153 if ((answer = X509_REQ_verify(self->x509_req, key->pkey)) < 0)
154 {
155 exception_from_error_queue();
156 return NULL;
157 }
158
159 return PyInt_FromLong(answer);
160}
161
162static char crypto_X509Req_add_extensions_doc[] = "\n\
163Add extensions to the request.\n\
164\n\
165Arguments: self - X509Req object\n\
166 args - The Python argument tuple, should be:\n\
167 extensions - a sequence of X509Extension objects\n\
168Returns: None\n\
169";
170
171static PyObject *
172crypto_X509Req_add_extensions(crypto_X509ReqObj *self, PyObject *args)
173{
174 PyObject *extensions;
175 crypto_X509ExtensionObj *ext;
176 STACK_OF(X509_EXTENSION) *exts;
177 int nr_of_extensions, i;
178
179 if (!PyArg_ParseTuple(args, "O:add_extensions", &extensions))
180 return NULL;
181
182 if (!PySequence_Check(extensions))
183 {
184 PyErr_SetString(PyExc_TypeError, "Expected a sequence");
185 return NULL;
186 }
187
188 /* Make a STACK_OF(X509_EXTENSION) from sequence */
189 if ((exts = sk_X509_EXTENSION_new_null()) == NULL)
190 {
191 exception_from_error_queue();
192 return NULL;
193 }
194
195 /* Put the extensions in a stack */
196 nr_of_extensions = PySequence_Length(extensions);
197
198 for (i = 0; i < nr_of_extensions; i++)
199 {
200 ext = (crypto_X509ExtensionObj *)PySequence_GetItem(extensions, i);
201 if (!(crypto_X509Extension_Check(ext)))
202 {
203 PyErr_SetString(PyExc_ValueError,
204 "One of the elements is not an X509Extension");
205 sk_X509_EXTENSION_free(exts);
206 return NULL;
207 }
208 sk_X509_EXTENSION_push(exts, ext->x509_extension);
209 }
210
211 if (!X509_REQ_add_extensions(self->x509_req, exts))
212 {
213 sk_X509_EXTENSION_free(exts);
214 exception_from_error_queue();
215 return NULL;
216 }
217
218 sk_X509_EXTENSION_free(exts);
219
220 Py_INCREF(Py_None);
221 return Py_None;
222}
223
224/*
225 * ADD_METHOD(name) expands to a correct PyMethodDef declaration
226 * { 'name', (PyCFunction)crypto_X509Req_name, METH_VARARGS }
227 * for convenience
228 */
229#define ADD_METHOD(name) \
230 { #name, (PyCFunction)crypto_X509Req_##name, METH_VARARGS, crypto_X509Req_##name##_doc }
231static PyMethodDef crypto_X509Req_methods[] =
232{
233 ADD_METHOD(get_subject),
234 ADD_METHOD(get_pubkey),
235 ADD_METHOD(set_pubkey),
236 ADD_METHOD(sign),
237 ADD_METHOD(verify),
238 ADD_METHOD(add_extensions),
239 { NULL, NULL }
240};
241#undef ADD_METHOD
242
243
244/*
245 * Constructor for X509Req, never called by Python code directly
246 *
247 * Arguments: name - A "real" X509_REQ object
248 * dealloc - Boolean value to specify whether the destructor should
249 * free the "real" X509_REQ object
250 * Returns: The newly created X509Req object
251 */
252crypto_X509ReqObj *
253crypto_X509Req_New(X509_REQ *req, int dealloc)
254{
255 crypto_X509ReqObj *self;
256
257 self = PyObject_New(crypto_X509ReqObj, &crypto_X509Req_Type);
258
259 if (self == NULL)
260 return NULL;
261
262 self->x509_req = req;
263 self->dealloc = dealloc;
264
265 return self;
266}
267
268/*
269 * Deallocate the memory used by the X509Req object
270 *
271 * Arguments: self - The X509Req object
272 * Returns: None
273 */
274static void
275crypto_X509Req_dealloc(crypto_X509ReqObj *self)
276{
277 /* Sometimes we don't have to dealloc this */
278 if (self->dealloc)
279 X509_REQ_free(self->x509_req);
280
281 PyObject_Del(self);
282}
283
284
285/*
286 * Find attribute.
287 *
288 * Arguments: self - The X509Req object
289 * name - The attribute name
290 * Returns: A Python object for the attribute, or NULL if something went
291 * wrong
292 */
293static PyObject *
294crypto_X509Req_getattr(crypto_X509ReqObj *self, char *name)
295{
296 return Py_FindMethod(crypto_X509Req_methods, (PyObject *)self, name);
297}
298
299PyTypeObject crypto_X509Req_Type = {
300 PyObject_HEAD_INIT(NULL)
301 0,
302 "X509Req",
303 sizeof(crypto_X509ReqObj),
304 0,
305 (destructor)crypto_X509Req_dealloc,
306 NULL, /* print */
307 (getattrfunc)crypto_X509Req_getattr,
308};
309
310
311/*
312 * Initialize the X509Req part of the crypto module
313 *
314 * Arguments: dict - The crypto module dictionary
315 * Returns: None
316 */
317int
318init_crypto_x509req(PyObject *dict)
319{
320 crypto_X509Req_Type.ob_type = &PyType_Type;
321 Py_INCREF(&crypto_X509Req_Type);
322 PyDict_SetItemString(dict, "X509ReqType", (PyObject *)&crypto_X509Req_Type);
323 return 1;
324}