blob: 3d67cfc5103fab88595b20ca66a699d128f9ddfd [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
159/*
160 * Deallocate the memory used by the PKey object
161 *
162 * Arguments: self - The PKey object
163 * Returns: None
164 */
165static void
166crypto_PKey_dealloc(crypto_PKeyObj *self)
167{
168 /* Sometimes we don't have to dealloc the "real" EVP_PKEY pointer ourselves */
169 if (self->dealloc)
170 EVP_PKEY_free(self->pkey);
171
172 PyObject_Del(self);
173}
174
175/*
176 * Find attribute
177 *
178 * Arguments: self - The PKey object
179 * name - The attribute name
180 * Returns: A Python object for the attribute, or NULL if something went
181 * wrong
182 */
183static PyObject *
184crypto_PKey_getattr(crypto_PKeyObj *self, char *name)
185{
186 return Py_FindMethod(crypto_PKey_methods, (PyObject *)self, name);
187}
188
189PyTypeObject crypto_PKey_Type = {
190 PyObject_HEAD_INIT(NULL)
191 0,
192 "PKey",
193 sizeof(crypto_PKeyObj),
194 0,
195 (destructor)crypto_PKey_dealloc,
196 NULL, /* print */
197 (getattrfunc)crypto_PKey_getattr,
198 NULL, /* setattr */
199 NULL, /* compare */
200 NULL, /* repr */
201 NULL, /* as_number */
202 NULL, /* as_sequence */
203 NULL, /* as_mapping */
204 NULL, /* hash */
205};
206
207
208/*
209 * Initialize the PKey part of the crypto sub module
210 *
211 * Arguments: dict - The crypto module dictionary
212 * Returns: None
213 */
214int
215init_crypto_pkey(PyObject *dict)
216{
217 crypto_PKey_Type.ob_type = &PyType_Type;
218 Py_INCREF(&crypto_PKey_Type);
219 PyDict_SetItemString(dict, "PKeyType", (PyObject *)&crypto_PKey_Type);
220 return 1;
221}
222