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