blob: 82b9e06ee27a5a4aea8955045ed79061c5824ae7 [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 { \
Rick Deand369c932009-07-08 11:48:33 -050024 exception_from_error_queue(crypto_Error); \
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050025 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
Jean-Paul Calderone2f6c66f2010-08-11 19:53:43 -040091 return PyLong_FromLong(EVP_PKEY_bits(self->pkey));
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050092}
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
Jean-Paul Calderone2f6c66f2010-08-11 19:53:43 -0400106 return PyLong_FromLong(self->pkey->type);
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500107}
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
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500199PyTypeObject crypto_PKey_Type = {
Jean-Paul Calderone2f6c66f2010-08-11 19:53:43 -0400200 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Jean-Paul Calderone2e1da572009-06-27 10:44:00 -0400201 "OpenSSL.crypto.PKey",
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500202 sizeof(crypto_PKeyObj),
203 0,
204 (destructor)crypto_PKey_dealloc,
205 NULL, /* print */
Jean-Paul Calderone2f6c66f2010-08-11 19:53:43 -0400206 NULL, /* getattr */
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500207 NULL, /* setattr */
208 NULL, /* compare */
209 NULL, /* repr */
210 NULL, /* as_number */
211 NULL, /* as_sequence */
212 NULL, /* as_mapping */
213 NULL, /* hash */
Jean-Paul Calderone2e1da572009-06-27 10:44:00 -0400214 NULL, /* call */
215 NULL, /* str */
216 NULL, /* getattro */
217 NULL, /* setattro */
218 NULL, /* as_buffer */
Jean-Paul Calderone09654fe2009-06-27 10:54:48 -0400219 Py_TPFLAGS_DEFAULT,
Jean-Paul Calderone2e1da572009-06-27 10:44:00 -0400220 crypto_PKey_doc, /* doc */
221 NULL, /* traverse */
222 NULL, /* clear */
223 NULL, /* tp_richcompare */
224 0, /* tp_weaklistoffset */
225 NULL, /* tp_iter */
226 NULL, /* tp_iternext */
227 crypto_PKey_methods, /* tp_methods */
228 NULL, /* tp_members */
229 NULL, /* tp_getset */
230 NULL, /* tp_base */
231 NULL, /* tp_dict */
232 NULL, /* tp_descr_get */
233 NULL, /* tp_descr_set */
234 0, /* tp_dictoffset */
235 NULL, /* tp_init */
236 NULL, /* tp_alloc */
237 crypto_PKey_new, /* tp_new */
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500238};
239
240
241/*
242 * Initialize the PKey part of the crypto sub module
243 *
Jean-Paul Calderone2e1da572009-06-27 10:44:00 -0400244 * Arguments: module - The crypto module
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500245 * Returns: None
246 */
247int
Jean-Paul Calderone2e1da572009-06-27 10:44:00 -0400248init_crypto_pkey(PyObject *module)
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500249{
Jean-Paul Calderone2e1da572009-06-27 10:44:00 -0400250 if (PyType_Ready(&crypto_PKey_Type) < 0) {
251 return 0;
252 }
253
254 if (PyModule_AddObject(module, "PKey", (PyObject *)&crypto_PKey_Type) != 0) {
255 return 0;
256 }
257
258 if (PyModule_AddObject(module, "PKeyType", (PyObject *)&crypto_PKey_Type) != 0) {
259 return 0;
260 }
261
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500262 return 1;
263}
264