Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame^] | 1 | /* |
| 2 | * x509req.c |
| 3 | * |
| 4 | * Copyright (C) AB Strakt 2001, All rights reserved |
| 5 | * |
| 6 | * X.509 Request handling, mostly thin wrapping. |
| 7 | * See the file RATIONALE for a short explanation of why this module was written. |
| 8 | */ |
| 9 | #include <Python.h> |
| 10 | #define crypto_MODULE |
| 11 | #include "crypto.h" |
| 12 | |
| 13 | static char *CVSid = "@(#) $Id: x509req.c,v 1.15 2002/09/04 22:24:59 iko Exp $"; |
| 14 | |
| 15 | |
| 16 | static char crypto_X509Req_get_subject_doc[] = "\n\ |
| 17 | Create an X509Name object for the subject of the certificate request\n\ |
| 18 | \n\ |
| 19 | Arguments: self - The X509Req object\n\ |
| 20 | args - The Python argument tuple, should be empty\n\ |
| 21 | Returns: An X509Name object\n\ |
| 22 | "; |
| 23 | |
| 24 | static PyObject * |
| 25 | crypto_X509Req_get_subject(crypto_X509ReqObj *self, PyObject *args) |
| 26 | { |
| 27 | crypto_X509NameObj *crypto_X509Name_New(X509_NAME *, int); |
| 28 | X509_NAME *name; |
| 29 | |
| 30 | if (!PyArg_ParseTuple(args, ":get_subject")) |
| 31 | return NULL; |
| 32 | |
| 33 | if ((name = X509_REQ_get_subject_name(self->x509_req)) == NULL) |
| 34 | { |
| 35 | exception_from_error_queue(); |
| 36 | return NULL; |
| 37 | } |
| 38 | |
| 39 | return (PyObject *)crypto_X509Name_New(name, 0); |
| 40 | } |
| 41 | |
| 42 | static char crypto_X509Req_get_pubkey_doc[] = "\n\ |
| 43 | Get the public key from the certificate request\n\ |
| 44 | \n\ |
| 45 | Arguments: self - The X509Req object\n\ |
| 46 | args - The Python argument tuple, should be empty\n\ |
| 47 | Returns: The public key\n\ |
| 48 | "; |
| 49 | |
| 50 | static PyObject * |
| 51 | crypto_X509Req_get_pubkey(crypto_X509ReqObj *self, PyObject *args) |
| 52 | { |
| 53 | crypto_PKeyObj *crypto_PKey_New(EVP_PKEY *, int); |
| 54 | EVP_PKEY *pkey; |
| 55 | |
| 56 | if (!PyArg_ParseTuple(args, ":get_pubkey")) |
| 57 | return NULL; |
| 58 | |
| 59 | if ((pkey = X509_REQ_get_pubkey(self->x509_req)) == NULL) |
| 60 | { |
| 61 | exception_from_error_queue(); |
| 62 | return NULL; |
| 63 | } |
| 64 | |
| 65 | return (PyObject *)crypto_PKey_New(pkey, 1); |
| 66 | } |
| 67 | |
| 68 | static char crypto_X509Req_set_pubkey_doc[] = "\n\ |
| 69 | Set the public key of the certificate request\n\ |
| 70 | \n\ |
| 71 | Arguments: self - The X509Req object\n\ |
| 72 | args - The Python argument tuple, should be:\n\ |
| 73 | pkey - The public key to use\n\ |
| 74 | Returns: None\n\ |
| 75 | "; |
| 76 | |
| 77 | static PyObject * |
| 78 | crypto_X509Req_set_pubkey(crypto_X509ReqObj *self, PyObject *args) |
| 79 | { |
| 80 | crypto_PKeyObj *pkey; |
| 81 | |
| 82 | if (!PyArg_ParseTuple(args, "O!:set_pubkey", &crypto_PKey_Type, &pkey)) |
| 83 | return NULL; |
| 84 | |
| 85 | if (!X509_REQ_set_pubkey(self->x509_req, pkey->pkey)) |
| 86 | { |
| 87 | exception_from_error_queue(); |
| 88 | return NULL; |
| 89 | } |
| 90 | |
| 91 | Py_INCREF(Py_None); |
| 92 | return Py_None; |
| 93 | } |
| 94 | |
| 95 | static char crypto_X509Req_sign_doc[] = "\n\ |
| 96 | Sign the certificate request using the supplied key and digest\n\ |
| 97 | \n\ |
| 98 | Arguments: self - The X509Req object\n\ |
| 99 | args - The Python argument tuple, should be:\n\ |
| 100 | pkey - The key to sign with\n\ |
| 101 | digest - The message digest to use\n\ |
| 102 | Returns: None\n\ |
| 103 | "; |
| 104 | |
| 105 | static PyObject * |
| 106 | crypto_X509Req_sign(crypto_X509ReqObj *self, PyObject *args) |
| 107 | { |
| 108 | crypto_PKeyObj *pkey; |
| 109 | char *digest_name; |
| 110 | const EVP_MD *digest; |
| 111 | |
| 112 | if (!PyArg_ParseTuple(args, "O!s:sign", &crypto_PKey_Type, &pkey, |
| 113 | &digest_name)) |
| 114 | return NULL; |
| 115 | |
| 116 | if ((digest = EVP_get_digestbyname(digest_name)) == NULL) |
| 117 | { |
| 118 | PyErr_SetString(PyExc_ValueError, "No such digest method"); |
| 119 | return NULL; |
| 120 | } |
| 121 | |
| 122 | if (!X509_REQ_sign(self->x509_req, pkey->pkey, digest)) |
| 123 | { |
| 124 | exception_from_error_queue(); |
| 125 | return NULL; |
| 126 | } |
| 127 | |
| 128 | Py_INCREF(Py_None); |
| 129 | return Py_None; |
| 130 | } |
| 131 | |
| 132 | static char crypto_X509Req_verify_doc[] = "\n\ |
| 133 | Verifies a certificate request using the supplied public key\n\ |
| 134 | \n\ |
| 135 | Arguments: self - X509Req object\n\ |
| 136 | args - The Python argument tuple, should be:\n\ |
| 137 | key - a public key\n\ |
| 138 | Returns: True, if the signature is correct, 0 otherwise.\n\ |
| 139 | "; |
| 140 | |
| 141 | PyObject * |
| 142 | crypto_X509Req_verify(crypto_X509ReqObj *self, PyObject *args) |
| 143 | { |
| 144 | PyObject *obj; |
| 145 | crypto_PKeyObj *key; |
| 146 | int answer; |
| 147 | |
| 148 | if (!PyArg_ParseTuple(args, "O!:verify", &crypto_PKey_Type, &obj)) |
| 149 | return NULL; |
| 150 | |
| 151 | key = (crypto_PKeyObj *)obj; |
| 152 | |
| 153 | if ((answer = X509_REQ_verify(self->x509_req, key->pkey)) < 0) |
| 154 | { |
| 155 | exception_from_error_queue(); |
| 156 | return NULL; |
| 157 | } |
| 158 | |
| 159 | return PyInt_FromLong(answer); |
| 160 | } |
| 161 | |
| 162 | static char crypto_X509Req_add_extensions_doc[] = "\n\ |
| 163 | Add extensions to the request.\n\ |
| 164 | \n\ |
| 165 | Arguments: self - X509Req object\n\ |
| 166 | args - The Python argument tuple, should be:\n\ |
| 167 | extensions - a sequence of X509Extension objects\n\ |
| 168 | Returns: None\n\ |
| 169 | "; |
| 170 | |
| 171 | static PyObject * |
| 172 | crypto_X509Req_add_extensions(crypto_X509ReqObj *self, PyObject *args) |
| 173 | { |
| 174 | PyObject *extensions; |
| 175 | crypto_X509ExtensionObj *ext; |
| 176 | STACK_OF(X509_EXTENSION) *exts; |
| 177 | int nr_of_extensions, i; |
| 178 | |
| 179 | if (!PyArg_ParseTuple(args, "O:add_extensions", &extensions)) |
| 180 | return NULL; |
| 181 | |
| 182 | if (!PySequence_Check(extensions)) |
| 183 | { |
| 184 | PyErr_SetString(PyExc_TypeError, "Expected a sequence"); |
| 185 | return NULL; |
| 186 | } |
| 187 | |
| 188 | /* Make a STACK_OF(X509_EXTENSION) from sequence */ |
| 189 | if ((exts = sk_X509_EXTENSION_new_null()) == NULL) |
| 190 | { |
| 191 | exception_from_error_queue(); |
| 192 | return NULL; |
| 193 | } |
| 194 | |
| 195 | /* Put the extensions in a stack */ |
| 196 | nr_of_extensions = PySequence_Length(extensions); |
| 197 | |
| 198 | for (i = 0; i < nr_of_extensions; i++) |
| 199 | { |
| 200 | ext = (crypto_X509ExtensionObj *)PySequence_GetItem(extensions, i); |
| 201 | if (!(crypto_X509Extension_Check(ext))) |
| 202 | { |
| 203 | PyErr_SetString(PyExc_ValueError, |
| 204 | "One of the elements is not an X509Extension"); |
| 205 | sk_X509_EXTENSION_free(exts); |
| 206 | return NULL; |
| 207 | } |
| 208 | sk_X509_EXTENSION_push(exts, ext->x509_extension); |
| 209 | } |
| 210 | |
| 211 | if (!X509_REQ_add_extensions(self->x509_req, exts)) |
| 212 | { |
| 213 | sk_X509_EXTENSION_free(exts); |
| 214 | exception_from_error_queue(); |
| 215 | return NULL; |
| 216 | } |
| 217 | |
| 218 | sk_X509_EXTENSION_free(exts); |
| 219 | |
| 220 | Py_INCREF(Py_None); |
| 221 | return Py_None; |
| 222 | } |
| 223 | |
| 224 | /* |
| 225 | * ADD_METHOD(name) expands to a correct PyMethodDef declaration |
| 226 | * { 'name', (PyCFunction)crypto_X509Req_name, METH_VARARGS } |
| 227 | * for convenience |
| 228 | */ |
| 229 | #define ADD_METHOD(name) \ |
| 230 | { #name, (PyCFunction)crypto_X509Req_##name, METH_VARARGS, crypto_X509Req_##name##_doc } |
| 231 | static PyMethodDef crypto_X509Req_methods[] = |
| 232 | { |
| 233 | ADD_METHOD(get_subject), |
| 234 | ADD_METHOD(get_pubkey), |
| 235 | ADD_METHOD(set_pubkey), |
| 236 | ADD_METHOD(sign), |
| 237 | ADD_METHOD(verify), |
| 238 | ADD_METHOD(add_extensions), |
| 239 | { NULL, NULL } |
| 240 | }; |
| 241 | #undef ADD_METHOD |
| 242 | |
| 243 | |
| 244 | /* |
| 245 | * Constructor for X509Req, never called by Python code directly |
| 246 | * |
| 247 | * Arguments: name - A "real" X509_REQ object |
| 248 | * dealloc - Boolean value to specify whether the destructor should |
| 249 | * free the "real" X509_REQ object |
| 250 | * Returns: The newly created X509Req object |
| 251 | */ |
| 252 | crypto_X509ReqObj * |
| 253 | crypto_X509Req_New(X509_REQ *req, int dealloc) |
| 254 | { |
| 255 | crypto_X509ReqObj *self; |
| 256 | |
| 257 | self = PyObject_New(crypto_X509ReqObj, &crypto_X509Req_Type); |
| 258 | |
| 259 | if (self == NULL) |
| 260 | return NULL; |
| 261 | |
| 262 | self->x509_req = req; |
| 263 | self->dealloc = dealloc; |
| 264 | |
| 265 | return self; |
| 266 | } |
| 267 | |
| 268 | /* |
| 269 | * Deallocate the memory used by the X509Req object |
| 270 | * |
| 271 | * Arguments: self - The X509Req object |
| 272 | * Returns: None |
| 273 | */ |
| 274 | static void |
| 275 | crypto_X509Req_dealloc(crypto_X509ReqObj *self) |
| 276 | { |
| 277 | /* Sometimes we don't have to dealloc this */ |
| 278 | if (self->dealloc) |
| 279 | X509_REQ_free(self->x509_req); |
| 280 | |
| 281 | PyObject_Del(self); |
| 282 | } |
| 283 | |
| 284 | |
| 285 | /* |
| 286 | * Find attribute. |
| 287 | * |
| 288 | * Arguments: self - The X509Req object |
| 289 | * name - The attribute name |
| 290 | * Returns: A Python object for the attribute, or NULL if something went |
| 291 | * wrong |
| 292 | */ |
| 293 | static PyObject * |
| 294 | crypto_X509Req_getattr(crypto_X509ReqObj *self, char *name) |
| 295 | { |
| 296 | return Py_FindMethod(crypto_X509Req_methods, (PyObject *)self, name); |
| 297 | } |
| 298 | |
| 299 | PyTypeObject crypto_X509Req_Type = { |
| 300 | PyObject_HEAD_INIT(NULL) |
| 301 | 0, |
| 302 | "X509Req", |
| 303 | sizeof(crypto_X509ReqObj), |
| 304 | 0, |
| 305 | (destructor)crypto_X509Req_dealloc, |
| 306 | NULL, /* print */ |
| 307 | (getattrfunc)crypto_X509Req_getattr, |
| 308 | }; |
| 309 | |
| 310 | |
| 311 | /* |
| 312 | * Initialize the X509Req part of the crypto module |
| 313 | * |
| 314 | * Arguments: dict - The crypto module dictionary |
| 315 | * Returns: None |
| 316 | */ |
| 317 | int |
| 318 | init_crypto_x509req(PyObject *dict) |
| 319 | { |
| 320 | crypto_X509Req_Type.ob_type = &PyType_Type; |
| 321 | Py_INCREF(&crypto_X509Req_Type); |
| 322 | PyDict_SetItemString(dict, "X509ReqType", (PyObject *)&crypto_X509Req_Type); |
| 323 | return 1; |
| 324 | } |