blob: d06ff968c46eba3eccb3d21187fa43728ece33cd [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
5 *
6 * Public/rivate key handling code, mostly thin wrappers around OpenSSL.
7 * See the file RATIONALE for a short explanation of why this module was written.
8 *
9 */
10#include <Python.h>
11#define crypto_MODULE
12#include "crypto.h"
13
14static char *CVSid = "@(#) $Id: pkey.c,v 1.9 2002/07/09 13:47:21 martin Exp $";
15
16/*
17 * This is done every time something fails, so turning it into a macro is
18 * really nice.
19 *
20 * Arguments: None
21 * Returns: Doesn't return
22 */
23#define FAIL() \
24do { \
25 exception_from_error_queue(); \
26 return NULL; \
27} while (0)
28
29
30static char crypto_PKey_generate_key_doc[] = "\n\
31Generate a key of a given type, with a given number of a bits\n\
32\n\
33Arguments: self - The PKey object\n\
34 args - The Python argument tuple, should be:\n\
35 type - The key type (TYPE_RSA or TYPE_DSA)\n\
36 bits - The number of bits\n\
37Returns: None\n\
38";
39
40static PyObject *
41crypto_PKey_generate_key(crypto_PKeyObj *self, PyObject *args)
42{
43 int type, bits;
44 RSA *rsa;
45 DSA *dsa;
46
47 if (!PyArg_ParseTuple(args, "ii:generate_key", &type, &bits))
48 return NULL;
49
50 switch (type)
51 {
52 case crypto_TYPE_RSA:
Jean-Paul Calderoneab82db72008-03-06 00:09:31 -050053 if (bits <= 0) {
54 PyErr_SetString(PyExc_ValueError, "Invalid number of bits");
55 return NULL;
56 }
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050057 if ((rsa = RSA_generate_key(bits, 0x10001, NULL, NULL)) == NULL)
58 FAIL();
59 if (!EVP_PKEY_assign_RSA(self->pkey, rsa))
60 FAIL();
Jean-Paul Calderoneac0d95f2008-03-10 00:00:42 -040061 break;
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050062
63 case crypto_TYPE_DSA:
64 if ((dsa = DSA_generate_parameters(bits, NULL, 0, NULL, NULL, NULL, NULL)) == NULL)
65 FAIL();
66 if (!DSA_generate_key(dsa))
67 FAIL();
68 if (!EVP_PKEY_assign_DSA(self->pkey, dsa))
69 FAIL();
Jean-Paul Calderoneac0d95f2008-03-10 00:00:42 -040070 break;
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050071
Jean-Paul Calderoneac0d95f2008-03-10 00:00:42 -040072 default:
73 PyErr_SetString(crypto_Error, "No such key type");
74 return NULL;
75
76 }
77 self->initialized = 1;
78 Py_INCREF(Py_None);
79 return Py_None;
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050080}
81
82static char crypto_PKey_bits_doc[] = "\n\
83Returns the number of bits of the key\n\
84\n\
85Arguments: self - The PKey object\n\
86 args - The Python argument tuple, should be empty\n\
87Returns: The number of bits of the key.\n\
88";
89
90static PyObject *
91crypto_PKey_bits(crypto_PKeyObj *self, PyObject *args)
92{
93 if (!PyArg_ParseTuple(args, ":bits"))
94 return NULL;
95
96 return PyInt_FromLong(EVP_PKEY_bits(self->pkey));
97}
98
99static char crypto_PKey_type_doc[] = "\n\
100Returns the type of the key\n\
101\n\
102Arguments: self - The PKey object\n\
103 args - The Python argument tuple, should be empty\n\
104Returns: The type of the key.\n\
105";
106
107static PyObject *
108crypto_PKey_type(crypto_PKeyObj *self, PyObject *args)
109{
110 if (!PyArg_ParseTuple(args, ":type"))
111 return NULL;
112
113 return PyInt_FromLong(self->pkey->type);
114}
115
116
117/*
118 * ADD_METHOD(name) expands to a correct PyMethodDef declaration
119 * { 'name', (PyCFunction)crypto_PKey_name, METH_VARARGS }
120 * for convenience
121 */
122#define ADD_METHOD(name) \
123 { #name, (PyCFunction)crypto_PKey_##name, METH_VARARGS, crypto_PKey_##name##_doc }
124static PyMethodDef crypto_PKey_methods[] =
125{
126 ADD_METHOD(generate_key),
127 ADD_METHOD(bits),
128 ADD_METHOD(type),
129 { NULL, NULL }
130};
131#undef ADD_METHOD
132
133
134/*
135 * Constructor for PKey objects, never called by Python code directly
136 *
137 * Arguments: pkey - A "real" EVP_PKEY object
138 * dealloc - Boolean value to specify whether the destructor should
139 * free the "real" EVP_PKEY object
140 * Returns: The newly created PKey object
141 */
142crypto_PKeyObj *
143crypto_PKey_New(EVP_PKEY *pkey, int dealloc)
144{
145 crypto_PKeyObj *self;
146
147 self = PyObject_New(crypto_PKeyObj, &crypto_PKey_Type);
148
149 if (self == NULL)
150 return NULL;
151
152 self->pkey = pkey;
153 self->dealloc = dealloc;
Jean-Paul Calderoneac0d95f2008-03-10 00:00:42 -0400154 self->only_public = 0;
155
156 /*
157 * Heuristic. Most call-sites pass an initialized EVP_PKEY. Not
158 * necessarily the case that they will, though. That's part of why this is
159 * a hack. -exarkun
160 */
161 self->initialized = 1;
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500162
163 return self;
164}
165
166/*
167 * Deallocate the memory used by the PKey object
168 *
169 * Arguments: self - The PKey object
170 * Returns: None
171 */
172static void
173crypto_PKey_dealloc(crypto_PKeyObj *self)
174{
175 /* Sometimes we don't have to dealloc the "real" EVP_PKEY pointer ourselves */
176 if (self->dealloc)
177 EVP_PKEY_free(self->pkey);
178
179 PyObject_Del(self);
180}
181
182/*
183 * Find attribute
184 *
185 * Arguments: self - The PKey object
186 * name - The attribute name
187 * Returns: A Python object for the attribute, or NULL if something went
188 * wrong
189 */
190static PyObject *
191crypto_PKey_getattr(crypto_PKeyObj *self, char *name)
192{
193 return Py_FindMethod(crypto_PKey_methods, (PyObject *)self, name);
194}
195
196PyTypeObject crypto_PKey_Type = {
197 PyObject_HEAD_INIT(NULL)
198 0,
199 "PKey",
200 sizeof(crypto_PKeyObj),
201 0,
202 (destructor)crypto_PKey_dealloc,
203 NULL, /* print */
204 (getattrfunc)crypto_PKey_getattr,
205 NULL, /* setattr */
206 NULL, /* compare */
207 NULL, /* repr */
208 NULL, /* as_number */
209 NULL, /* as_sequence */
210 NULL, /* as_mapping */
211 NULL, /* hash */
212};
213
214
215/*
216 * Initialize the PKey part of the crypto sub module
217 *
218 * Arguments: dict - The crypto module dictionary
219 * Returns: None
220 */
221int
222init_crypto_pkey(PyObject *dict)
223{
224 crypto_PKey_Type.ob_type = &PyType_Type;
225 Py_INCREF(&crypto_PKey_Type);
226 PyDict_SetItemString(dict, "PKeyType", (PyObject *)&crypto_PKey_Type);
227 return 1;
228}
229