blob: fee3e814ee6d0b2fc255a0b6799437f9443952c3 [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\
32Arguments: self - The PKey object\n\
33 args - The Python argument tuple, should be:\n\
34 type - The key type (TYPE_RSA or TYPE_DSA)\n\
35 bits - The number of bits\n\
36Returns: None\n\
37";
38
39static PyObject *
40crypto_PKey_generate_key(crypto_PKeyObj *self, PyObject *args)
41{
42 int type, bits;
43 RSA *rsa;
44 DSA *dsa;
45
46 if (!PyArg_ParseTuple(args, "ii:generate_key", &type, &bits))
47 return NULL;
48
49 switch (type)
50 {
51 case crypto_TYPE_RSA:
Jean-Paul Calderoneab82db72008-03-06 00:09:31 -050052 if (bits <= 0) {
53 PyErr_SetString(PyExc_ValueError, "Invalid number of bits");
54 return NULL;
55 }
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050056 if ((rsa = RSA_generate_key(bits, 0x10001, NULL, NULL)) == NULL)
57 FAIL();
58 if (!EVP_PKEY_assign_RSA(self->pkey, rsa))
59 FAIL();
Jean-Paul Calderoneac0d95f2008-03-10 00:00:42 -040060 break;
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050061
62 case crypto_TYPE_DSA:
63 if ((dsa = DSA_generate_parameters(bits, NULL, 0, NULL, NULL, NULL, NULL)) == NULL)
64 FAIL();
65 if (!DSA_generate_key(dsa))
66 FAIL();
67 if (!EVP_PKEY_assign_DSA(self->pkey, dsa))
68 FAIL();
Jean-Paul Calderoneac0d95f2008-03-10 00:00:42 -040069 break;
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050070
Jean-Paul Calderoneac0d95f2008-03-10 00:00:42 -040071 default:
72 PyErr_SetString(crypto_Error, "No such key type");
73 return NULL;
74
75 }
76 self->initialized = 1;
77 Py_INCREF(Py_None);
78 return Py_None;
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050079}
80
81static char crypto_PKey_bits_doc[] = "\n\
82Returns the number of bits of the key\n\
83\n\
84Arguments: self - The PKey object\n\
85 args - The Python argument tuple, should be empty\n\
86Returns: The number of bits of the key.\n\
87";
88
89static PyObject *
90crypto_PKey_bits(crypto_PKeyObj *self, PyObject *args)
91{
92 if (!PyArg_ParseTuple(args, ":bits"))
93 return NULL;
94
95 return PyInt_FromLong(EVP_PKEY_bits(self->pkey));
96}
97
98static char crypto_PKey_type_doc[] = "\n\
99Returns the type of the key\n\
100\n\
101Arguments: self - The PKey object\n\
102 args - The Python argument tuple, should be empty\n\
103Returns: The type of the key.\n\
104";
105
106static PyObject *
107crypto_PKey_type(crypto_PKeyObj *self, PyObject *args)
108{
109 if (!PyArg_ParseTuple(args, ":type"))
110 return NULL;
111
112 return PyInt_FromLong(self->pkey->type);
113}
114
115
116/*
117 * ADD_METHOD(name) expands to a correct PyMethodDef declaration
118 * { 'name', (PyCFunction)crypto_PKey_name, METH_VARARGS }
119 * for convenience
120 */
121#define ADD_METHOD(name) \
122 { #name, (PyCFunction)crypto_PKey_##name, METH_VARARGS, crypto_PKey_##name##_doc }
123static PyMethodDef crypto_PKey_methods[] =
124{
125 ADD_METHOD(generate_key),
126 ADD_METHOD(bits),
127 ADD_METHOD(type),
128 { NULL, NULL }
129};
130#undef ADD_METHOD
131
132
133/*
134 * Constructor for PKey objects, never called by Python code directly
135 *
136 * Arguments: pkey - A "real" EVP_PKEY object
137 * dealloc - Boolean value to specify whether the destructor should
138 * free the "real" EVP_PKEY object
139 * Returns: The newly created PKey object
140 */
141crypto_PKeyObj *
142crypto_PKey_New(EVP_PKEY *pkey, int dealloc)
143{
144 crypto_PKeyObj *self;
145
146 self = PyObject_New(crypto_PKeyObj, &crypto_PKey_Type);
147
148 if (self == NULL)
149 return NULL;
150
151 self->pkey = pkey;
152 self->dealloc = dealloc;
Jean-Paul Calderoneac0d95f2008-03-10 00:00:42 -0400153 self->only_public = 0;
154
155 /*
156 * Heuristic. Most call-sites pass an initialized EVP_PKEY. Not
157 * necessarily the case that they will, though. That's part of why this is
158 * a hack. -exarkun
159 */
160 self->initialized = 1;
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500161
162 return self;
163}
164
165/*
166 * Deallocate the memory used by the PKey object
167 *
168 * Arguments: self - The PKey object
169 * Returns: None
170 */
171static void
172crypto_PKey_dealloc(crypto_PKeyObj *self)
173{
174 /* Sometimes we don't have to dealloc the "real" EVP_PKEY pointer ourselves */
175 if (self->dealloc)
176 EVP_PKEY_free(self->pkey);
177
178 PyObject_Del(self);
179}
180
181/*
182 * Find attribute
183 *
184 * Arguments: self - The PKey object
185 * name - The attribute name
186 * Returns: A Python object for the attribute, or NULL if something went
187 * wrong
188 */
189static PyObject *
190crypto_PKey_getattr(crypto_PKeyObj *self, char *name)
191{
192 return Py_FindMethod(crypto_PKey_methods, (PyObject *)self, name);
193}
194
195PyTypeObject crypto_PKey_Type = {
196 PyObject_HEAD_INIT(NULL)
197 0,
198 "PKey",
199 sizeof(crypto_PKeyObj),
200 0,
201 (destructor)crypto_PKey_dealloc,
202 NULL, /* print */
203 (getattrfunc)crypto_PKey_getattr,
204 NULL, /* setattr */
205 NULL, /* compare */
206 NULL, /* repr */
207 NULL, /* as_number */
208 NULL, /* as_sequence */
209 NULL, /* as_mapping */
210 NULL, /* hash */
211};
212
213
214/*
215 * Initialize the PKey part of the crypto sub module
216 *
217 * Arguments: dict - The crypto module dictionary
218 * Returns: None
219 */
220int
221init_crypto_pkey(PyObject *dict)
222{
223 crypto_PKey_Type.ob_type = &PyType_Type;
224 Py_INCREF(&crypto_PKey_Type);
225 PyDict_SetItemString(dict, "PKeyType", (PyObject *)&crypto_PKey_Type);
226 return 1;
227}
228