blob: 0c2bea6cff3fc23b7d7a2e55ecc81fff3dc10148 [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();
61 Py_INCREF(Py_None);
62 return Py_None;
63
64 case crypto_TYPE_DSA:
65 if ((dsa = DSA_generate_parameters(bits, NULL, 0, NULL, NULL, NULL, NULL)) == NULL)
66 FAIL();
67 if (!DSA_generate_key(dsa))
68 FAIL();
69 if (!EVP_PKEY_assign_DSA(self->pkey, dsa))
70 FAIL();
71 Py_INCREF(Py_None);
72 return Py_None;
73 }
74
75 PyErr_SetString(crypto_Error, "No such key type");
Jean-Paul Calderoned8782ad2008-03-04 23:39:59 -050076 return NULL;
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\
82Arguments: self - The PKey object\n\
83 args - The Python argument tuple, should be empty\n\
84Returns: The number of bits of the key.\n\
85";
86
87static PyObject *
88crypto_PKey_bits(crypto_PKeyObj *self, PyObject *args)
89{
90 if (!PyArg_ParseTuple(args, ":bits"))
91 return NULL;
92
93 return PyInt_FromLong(EVP_PKEY_bits(self->pkey));
94}
95
96static char crypto_PKey_type_doc[] = "\n\
97Returns the type of the key\n\
98\n\
99Arguments: self - The PKey object\n\
100 args - The Python argument tuple, should be empty\n\
101Returns: The type of the key.\n\
102";
103
104static PyObject *
105crypto_PKey_type(crypto_PKeyObj *self, PyObject *args)
106{
107 if (!PyArg_ParseTuple(args, ":type"))
108 return NULL;
109
110 return PyInt_FromLong(self->pkey->type);
111}
112
113
114/*
115 * ADD_METHOD(name) expands to a correct PyMethodDef declaration
116 * { 'name', (PyCFunction)crypto_PKey_name, METH_VARARGS }
117 * for convenience
118 */
119#define ADD_METHOD(name) \
120 { #name, (PyCFunction)crypto_PKey_##name, METH_VARARGS, crypto_PKey_##name##_doc }
121static PyMethodDef crypto_PKey_methods[] =
122{
123 ADD_METHOD(generate_key),
124 ADD_METHOD(bits),
125 ADD_METHOD(type),
126 { NULL, NULL }
127};
128#undef ADD_METHOD
129
130
131/*
132 * Constructor for PKey objects, never called by Python code directly
133 *
134 * Arguments: pkey - A "real" EVP_PKEY object
135 * dealloc - Boolean value to specify whether the destructor should
136 * free the "real" EVP_PKEY object
137 * Returns: The newly created PKey object
138 */
139crypto_PKeyObj *
140crypto_PKey_New(EVP_PKEY *pkey, int dealloc)
141{
142 crypto_PKeyObj *self;
143
144 self = PyObject_New(crypto_PKeyObj, &crypto_PKey_Type);
145
146 if (self == NULL)
147 return NULL;
148
149 self->pkey = pkey;
150 self->dealloc = dealloc;
151
152 return self;
153}
154
155/*
156 * Deallocate the memory used by the PKey object
157 *
158 * Arguments: self - The PKey object
159 * Returns: None
160 */
161static void
162crypto_PKey_dealloc(crypto_PKeyObj *self)
163{
164 /* Sometimes we don't have to dealloc the "real" EVP_PKEY pointer ourselves */
165 if (self->dealloc)
166 EVP_PKEY_free(self->pkey);
167
168 PyObject_Del(self);
169}
170
171/*
172 * Find attribute
173 *
174 * Arguments: self - The PKey object
175 * name - The attribute name
176 * Returns: A Python object for the attribute, or NULL if something went
177 * wrong
178 */
179static PyObject *
180crypto_PKey_getattr(crypto_PKeyObj *self, char *name)
181{
182 return Py_FindMethod(crypto_PKey_methods, (PyObject *)self, name);
183}
184
185PyTypeObject crypto_PKey_Type = {
186 PyObject_HEAD_INIT(NULL)
187 0,
188 "PKey",
189 sizeof(crypto_PKeyObj),
190 0,
191 (destructor)crypto_PKey_dealloc,
192 NULL, /* print */
193 (getattrfunc)crypto_PKey_getattr,
194 NULL, /* setattr */
195 NULL, /* compare */
196 NULL, /* repr */
197 NULL, /* as_number */
198 NULL, /* as_sequence */
199 NULL, /* as_mapping */
200 NULL, /* hash */
201};
202
203
204/*
205 * Initialize the PKey part of the crypto sub module
206 *
207 * Arguments: dict - The crypto module dictionary
208 * Returns: None
209 */
210int
211init_crypto_pkey(PyObject *dict)
212{
213 crypto_PKey_Type.ob_type = &PyType_Type;
214 Py_INCREF(&crypto_PKey_Type);
215 PyDict_SetItemString(dict, "PKeyType", (PyObject *)&crypto_PKey_Type);
216 return 1;
217}
218