blob: 07fe423e1fa575d6a1ef3de6eb1b874048b4c7a0 [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");
Jean-Paul Calderoned8782ad2008-03-04 23:39:59 -050072 return NULL;
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050073}
74
75static char crypto_PKey_bits_doc[] = "\n\
76Returns the number of bits of the key\n\
77\n\
78Arguments: self - The PKey object\n\
79 args - The Python argument tuple, should be empty\n\
80Returns: The number of bits of the key.\n\
81";
82
83static PyObject *
84crypto_PKey_bits(crypto_PKeyObj *self, PyObject *args)
85{
86 if (!PyArg_ParseTuple(args, ":bits"))
87 return NULL;
88
89 return PyInt_FromLong(EVP_PKEY_bits(self->pkey));
90}
91
92static char crypto_PKey_type_doc[] = "\n\
93Returns the type of the key\n\
94\n\
95Arguments: self - The PKey object\n\
96 args - The Python argument tuple, should be empty\n\
97Returns: The type of the key.\n\
98";
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;
147
148 return self;
149}
150
151/*
152 * Deallocate the memory used by the PKey object
153 *
154 * Arguments: self - The PKey object
155 * Returns: None
156 */
157static void
158crypto_PKey_dealloc(crypto_PKeyObj *self)
159{
160 /* Sometimes we don't have to dealloc the "real" EVP_PKEY pointer ourselves */
161 if (self->dealloc)
162 EVP_PKEY_free(self->pkey);
163
164 PyObject_Del(self);
165}
166
167/*
168 * Find attribute
169 *
170 * Arguments: self - The PKey object
171 * name - The attribute name
172 * Returns: A Python object for the attribute, or NULL if something went
173 * wrong
174 */
175static PyObject *
176crypto_PKey_getattr(crypto_PKeyObj *self, char *name)
177{
178 return Py_FindMethod(crypto_PKey_methods, (PyObject *)self, name);
179}
180
181PyTypeObject crypto_PKey_Type = {
182 PyObject_HEAD_INIT(NULL)
183 0,
184 "PKey",
185 sizeof(crypto_PKeyObj),
186 0,
187 (destructor)crypto_PKey_dealloc,
188 NULL, /* print */
189 (getattrfunc)crypto_PKey_getattr,
190 NULL, /* setattr */
191 NULL, /* compare */
192 NULL, /* repr */
193 NULL, /* as_number */
194 NULL, /* as_sequence */
195 NULL, /* as_mapping */
196 NULL, /* hash */
197};
198
199
200/*
201 * Initialize the PKey part of the crypto sub module
202 *
203 * Arguments: dict - The crypto module dictionary
204 * Returns: None
205 */
206int
207init_crypto_pkey(PyObject *dict)
208{
209 crypto_PKey_Type.ob_type = &PyType_Type;
210 Py_INCREF(&crypto_PKey_Type);
211 PyDict_SetItemString(dict, "PKeyType", (PyObject *)&crypto_PKey_Type);
212 return 1;
213}
214