blob: f78acf7eb13a21df01250a5226e12025610a6a07 [file] [log] [blame]
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001/*
2 * pkey.c
3 *
4 * Copyright (C) AB Strakt 2001, All rights reserved
Jean-Paul Calderone8b63d452008-03-21 18:31:12 -04005 * Copyright (C) Jean-Paul Calderone 2008, All rights reserved
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05006 *
7 * Public/rivate key 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
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050015/*
16 * This is done every time something fails, so turning it into a macro is
17 * really nice.
18 *
19 * Arguments: None
20 * Returns: Doesn't return
21 */
22#define FAIL() \
23do { \
24 exception_from_error_queue(); \
25 return NULL; \
26} while (0)
27
28
29static char crypto_PKey_generate_key_doc[] = "\n\
30Generate a key of a given type, with a given number of a bits\n\
31\n\
Jean-Paul Calderone54bcc832009-05-27 14:06:48 -040032@param type: The key type (TYPE_RSA or TYPE_DSA)\n\
33@param bits: The number of bits\n\
34@return: None\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050035";
36
37static PyObject *
38crypto_PKey_generate_key(crypto_PKeyObj *self, PyObject *args)
39{
40 int type, bits;
41 RSA *rsa;
42 DSA *dsa;
43
44 if (!PyArg_ParseTuple(args, "ii:generate_key", &type, &bits))
45 return NULL;
46
47 switch (type)
48 {
49 case crypto_TYPE_RSA:
Jean-Paul Calderoneab82db72008-03-06 00:09:31 -050050 if (bits <= 0) {
51 PyErr_SetString(PyExc_ValueError, "Invalid number of bits");
52 return NULL;
53 }
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050054 if ((rsa = RSA_generate_key(bits, 0x10001, NULL, NULL)) == NULL)
55 FAIL();
56 if (!EVP_PKEY_assign_RSA(self->pkey, rsa))
57 FAIL();
Jean-Paul Calderoneac0d95f2008-03-10 00:00:42 -040058 break;
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050059
60 case crypto_TYPE_DSA:
61 if ((dsa = DSA_generate_parameters(bits, NULL, 0, NULL, NULL, NULL, NULL)) == NULL)
62 FAIL();
63 if (!DSA_generate_key(dsa))
64 FAIL();
65 if (!EVP_PKEY_assign_DSA(self->pkey, dsa))
66 FAIL();
Jean-Paul Calderoneac0d95f2008-03-10 00:00:42 -040067 break;
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050068
Jean-Paul Calderoneac0d95f2008-03-10 00:00:42 -040069 default:
70 PyErr_SetString(crypto_Error, "No such key type");
71 return NULL;
72
73 }
74 self->initialized = 1;
75 Py_INCREF(Py_None);
76 return Py_None;
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050077}
78
79static char crypto_PKey_bits_doc[] = "\n\
80Returns the number of bits of the key\n\
81\n\
Jean-Paul Calderone54bcc832009-05-27 14:06:48 -040082@return: The number of bits of the key.\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050083";
84
85static PyObject *
86crypto_PKey_bits(crypto_PKeyObj *self, PyObject *args)
87{
88 if (!PyArg_ParseTuple(args, ":bits"))
89 return NULL;
90
91 return PyInt_FromLong(EVP_PKEY_bits(self->pkey));
92}
93
94static char crypto_PKey_type_doc[] = "\n\
95Returns the type of the key\n\
96\n\
Jean-Paul Calderone54bcc832009-05-27 14:06:48 -040097@return: The type of the key.\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050098";
99
100static PyObject *
101crypto_PKey_type(crypto_PKeyObj *self, PyObject *args)
102{
103 if (!PyArg_ParseTuple(args, ":type"))
104 return NULL;
105
106 return PyInt_FromLong(self->pkey->type);
107}
108
109
110/*
111 * ADD_METHOD(name) expands to a correct PyMethodDef declaration
112 * { 'name', (PyCFunction)crypto_PKey_name, METH_VARARGS }
113 * for convenience
114 */
115#define ADD_METHOD(name) \
116 { #name, (PyCFunction)crypto_PKey_##name, METH_VARARGS, crypto_PKey_##name##_doc }
117static PyMethodDef crypto_PKey_methods[] =
118{
119 ADD_METHOD(generate_key),
120 ADD_METHOD(bits),
121 ADD_METHOD(type),
122 { NULL, NULL }
123};
124#undef ADD_METHOD
125
126
127/*
128 * Constructor for PKey objects, never called by Python code directly
129 *
130 * Arguments: pkey - A "real" EVP_PKEY object
131 * dealloc - Boolean value to specify whether the destructor should
132 * free the "real" EVP_PKEY object
133 * Returns: The newly created PKey object
134 */
135crypto_PKeyObj *
136crypto_PKey_New(EVP_PKEY *pkey, int dealloc)
137{
138 crypto_PKeyObj *self;
139
140 self = PyObject_New(crypto_PKeyObj, &crypto_PKey_Type);
141
142 if (self == NULL)
143 return NULL;
144
145 self->pkey = pkey;
146 self->dealloc = dealloc;
Jean-Paul Calderoneac0d95f2008-03-10 00:00:42 -0400147 self->only_public = 0;
148
149 /*
150 * Heuristic. Most call-sites pass an initialized EVP_PKEY. Not
151 * necessarily the case that they will, though. That's part of why this is
152 * a hack. -exarkun
153 */
154 self->initialized = 1;
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500155
156 return self;
157}
158
Jean-Paul Calderone2e1da572009-06-27 10:44:00 -0400159static char crypto_PKey_doc[] = "\n\
160PKey() -> PKey instance\n\
161\n\
162Create a new PKey object.\n\
163\n\
164@return: The PKey object\n\
165";
166static PyObject*
167crypto_PKey_new(PyTypeObject *subtype, PyObject *args, PyObject *kwargs) {
168 crypto_PKeyObj *self;
169
170 if (!PyArg_ParseTuple(args, ":PKey")) {
171 return NULL;
172 }
173
174 self = crypto_PKey_New(EVP_PKEY_new(), 1);
175 if (self) {
176 self->initialized = 0;
177 }
178
179 return (PyObject *)self;
180}
181
182
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500183/*
184 * Deallocate the memory used by the PKey object
185 *
186 * Arguments: self - The PKey object
187 * Returns: None
188 */
189static void
190crypto_PKey_dealloc(crypto_PKeyObj *self)
191{
192 /* Sometimes we don't have to dealloc the "real" EVP_PKEY pointer ourselves */
193 if (self->dealloc)
194 EVP_PKEY_free(self->pkey);
195
196 PyObject_Del(self);
197}
198
199/*
200 * Find attribute
201 *
202 * Arguments: self - The PKey object
203 * name - The attribute name
204 * Returns: A Python object for the attribute, or NULL if something went
205 * wrong
206 */
207static PyObject *
208crypto_PKey_getattr(crypto_PKeyObj *self, char *name)
209{
210 return Py_FindMethod(crypto_PKey_methods, (PyObject *)self, name);
211}
212
213PyTypeObject crypto_PKey_Type = {
214 PyObject_HEAD_INIT(NULL)
215 0,
Jean-Paul Calderone2e1da572009-06-27 10:44:00 -0400216 "OpenSSL.crypto.PKey",
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500217 sizeof(crypto_PKeyObj),
218 0,
219 (destructor)crypto_PKey_dealloc,
220 NULL, /* print */
221 (getattrfunc)crypto_PKey_getattr,
222 NULL, /* setattr */
223 NULL, /* compare */
224 NULL, /* repr */
225 NULL, /* as_number */
226 NULL, /* as_sequence */
227 NULL, /* as_mapping */
228 NULL, /* hash */
Jean-Paul Calderone2e1da572009-06-27 10:44:00 -0400229 NULL, /* call */
230 NULL, /* str */
231 NULL, /* getattro */
232 NULL, /* setattro */
233 NULL, /* as_buffer */
234 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
235 crypto_PKey_doc, /* doc */
236 NULL, /* traverse */
237 NULL, /* clear */
238 NULL, /* tp_richcompare */
239 0, /* tp_weaklistoffset */
240 NULL, /* tp_iter */
241 NULL, /* tp_iternext */
242 crypto_PKey_methods, /* tp_methods */
243 NULL, /* tp_members */
244 NULL, /* tp_getset */
245 NULL, /* tp_base */
246 NULL, /* tp_dict */
247 NULL, /* tp_descr_get */
248 NULL, /* tp_descr_set */
249 0, /* tp_dictoffset */
250 NULL, /* tp_init */
251 NULL, /* tp_alloc */
252 crypto_PKey_new, /* tp_new */
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500253};
254
255
256/*
257 * Initialize the PKey part of the crypto sub module
258 *
Jean-Paul Calderone2e1da572009-06-27 10:44:00 -0400259 * Arguments: module - The crypto module
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500260 * Returns: None
261 */
262int
Jean-Paul Calderone2e1da572009-06-27 10:44:00 -0400263init_crypto_pkey(PyObject *module)
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500264{
Jean-Paul Calderone2e1da572009-06-27 10:44:00 -0400265 if (PyType_Ready(&crypto_PKey_Type) < 0) {
266 return 0;
267 }
268
269 if (PyModule_AddObject(module, "PKey", (PyObject *)&crypto_PKey_Type) != 0) {
270 return 0;
271 }
272
273 if (PyModule_AddObject(module, "PKeyType", (PyObject *)&crypto_PKey_Type) != 0) {
274 return 0;
275 }
276
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500277 return 1;
278}
279