Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 1 | /* |
| 2 | * crypto.c |
| 3 | * |
| 4 | * Copyright (C) AB Strakt 2001, All rights reserved |
| 5 | * |
| 6 | * Main file of crypto sub module. |
| 7 | * See the file RATIONALE for a short explanation of why this module was written. |
| 8 | * |
| 9 | * Reviewed 2001-07-23 |
| 10 | */ |
| 11 | #include <Python.h> |
| 12 | #define crypto_MODULE |
| 13 | #include "crypto.h" |
| 14 | |
| 15 | static char crypto_doc[] = "\n\ |
| 16 | Main file of crypto sub module.\n\ |
| 17 | See the file RATIONALE for a short explanation of why this module was written.\n\ |
| 18 | "; |
| 19 | |
| 20 | static char *CVSid = "@(#) $Id: crypto.c,v 1.28 2004/08/09 14:56:05 martin Exp $"; |
| 21 | |
| 22 | void **ssl_API; |
| 23 | |
| 24 | PyObject *crypto_Error; |
| 25 | |
| 26 | static int |
| 27 | global_passphrase_callback(char *buf, int len, int rwflag, void *cb_arg) |
| 28 | { |
| 29 | PyObject *func, *argv, *ret; |
| 30 | int nchars; |
| 31 | |
| 32 | func = (PyObject *)cb_arg; |
| 33 | argv = Py_BuildValue("(i)", rwflag); |
| 34 | ret = PyEval_CallObject(func, argv); |
| 35 | Py_DECREF(argv); |
| 36 | if (ret == NULL) |
| 37 | return 0; |
| 38 | if (!PyString_Check(ret)) |
| 39 | { |
| 40 | PyErr_SetString(PyExc_ValueError, "String expected"); |
| 41 | return 0; |
| 42 | } |
| 43 | nchars = PyString_Size(ret); |
| 44 | if (nchars > len) |
| 45 | nchars = len; |
| 46 | strncpy(buf, PyString_AsString(ret), nchars); |
| 47 | return nchars; |
| 48 | } |
| 49 | |
| 50 | static char crypto_load_privatekey_doc[] = "\n\ |
| 51 | Load a private key from a buffer\n\ |
| 52 | \n\ |
| 53 | Arguments: spam - Always NULL\n\ |
| 54 | args - The Python argument tuple, should be:\n\ |
| 55 | type - The file type (one of FILETYPE_PEM, FILETYPE_ASN1)\n\ |
| 56 | buffer - The buffer the key is stored in\n\ |
| 57 | passphrase - (optional) if encrypted PEM format, this can be\n\ |
| 58 | either the passphrase to use, or a callback for\n\ |
| 59 | providing the passphrase.\n\ |
| 60 | Returns: The PKey object\n\ |
| 61 | "; |
| 62 | |
| 63 | static PyObject * |
| 64 | crypto_load_privatekey(PyObject *spam, PyObject *args) |
| 65 | { |
| 66 | crypto_PKeyObj *crypto_PKey_New(EVP_PKEY *, int); |
| 67 | int type, len; |
| 68 | char *buffer; |
| 69 | PyObject *pw = NULL; |
| 70 | pem_password_cb *cb = NULL; |
| 71 | void *cb_arg = NULL; |
| 72 | BIO *bio; |
| 73 | EVP_PKEY *pkey; |
| 74 | |
| 75 | if (!PyArg_ParseTuple(args, "is#|O:load_privatekey", &type, &buffer, &len, &pw)) |
| 76 | return NULL; |
| 77 | |
| 78 | if (pw != NULL) |
| 79 | { |
| 80 | if (PyString_Check(pw)) |
| 81 | { |
| 82 | cb = NULL; |
| 83 | cb_arg = PyString_AsString(pw); |
| 84 | } |
| 85 | else if (PyCallable_Check(pw)) |
| 86 | { |
| 87 | cb = global_passphrase_callback; |
| 88 | cb_arg = pw; |
| 89 | } |
| 90 | else |
| 91 | { |
| 92 | PyErr_SetString(PyExc_TypeError, "Last argument must be string or callable"); |
| 93 | return NULL; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | bio = BIO_new_mem_buf(buffer, len); |
| 98 | switch (type) |
| 99 | { |
| 100 | case X509_FILETYPE_PEM: |
| 101 | pkey = PEM_read_bio_PrivateKey(bio, NULL, cb, cb_arg); |
| 102 | break; |
| 103 | |
| 104 | case X509_FILETYPE_ASN1: |
| 105 | pkey = d2i_PrivateKey_bio(bio, NULL); |
| 106 | break; |
| 107 | |
| 108 | default: |
| 109 | PyErr_SetString(PyExc_ValueError, "type argument must be FILETYPE_PEM or FILETYPE_ASN1"); |
| 110 | BIO_free(bio); |
| 111 | return NULL; |
| 112 | } |
| 113 | BIO_free(bio); |
| 114 | |
| 115 | if (pkey == NULL) |
| 116 | { |
| 117 | exception_from_error_queue(); |
| 118 | return NULL; |
| 119 | } |
| 120 | |
| 121 | return (PyObject *)crypto_PKey_New(pkey, 1); |
| 122 | } |
| 123 | |
| 124 | static char crypto_dump_privatekey_doc[] = "\n\ |
| 125 | Dump a private key to a buffer\n\ |
| 126 | \n\ |
| 127 | Arguments: spam - Always NULL\n\ |
| 128 | args - The Python argument tuple, should be:\n\ |
| 129 | type - The file type (one of FILETYPE_PEM, FILETYPE_ASN1)\n\ |
| 130 | pkey - The PKey to dump\n\ |
| 131 | cipher - (optional) if encrypted PEM format, the cipher to\n\ |
| 132 | use\n\ |
| 133 | passphrase - (optional) if encrypted PEM format, this can be either\n\ |
| 134 | the passphrase to use, or a callback for providing the\n\ |
| 135 | passphrase.\n\ |
| 136 | Returns: The buffer with the dumped key in\n\ |
| 137 | "; |
| 138 | |
| 139 | static PyObject * |
| 140 | crypto_dump_privatekey(PyObject *spam, PyObject *args) |
| 141 | { |
| 142 | int type, ret, buf_len; |
| 143 | char *temp; |
| 144 | PyObject *buffer; |
| 145 | char *cipher_name = NULL; |
| 146 | const EVP_CIPHER *cipher = NULL; |
| 147 | PyObject *pw = NULL; |
| 148 | pem_password_cb *cb = NULL; |
| 149 | void *cb_arg = NULL; |
| 150 | BIO *bio; |
| 151 | crypto_PKeyObj *pkey; |
| 152 | |
| 153 | if (!PyArg_ParseTuple(args, "iO!|sO:dump_privatekey", &type, |
| 154 | &crypto_PKey_Type, &pkey, &cipher_name, &pw)) |
| 155 | return NULL; |
| 156 | |
| 157 | if (cipher_name != NULL && pw == NULL) |
| 158 | { |
| 159 | PyErr_SetString(PyExc_ValueError, "Illegal number of arguments"); |
| 160 | return NULL; |
| 161 | } |
| 162 | if (cipher_name != NULL) |
| 163 | { |
| 164 | cipher = EVP_get_cipherbyname(cipher_name); |
| 165 | if (cipher == NULL) |
| 166 | { |
| 167 | PyErr_SetString(PyExc_ValueError, "Invalid cipher name"); |
| 168 | return NULL; |
| 169 | } |
| 170 | if (PyString_Check(pw)) |
| 171 | { |
| 172 | cb = NULL; |
| 173 | cb_arg = PyString_AsString(pw); |
| 174 | } |
| 175 | else if (PyCallable_Check(pw)) |
| 176 | { |
| 177 | cb = global_passphrase_callback; |
| 178 | cb_arg = pw; |
| 179 | } |
| 180 | else |
| 181 | { |
| 182 | PyErr_SetString(PyExc_TypeError, "Last argument must be string or callable"); |
| 183 | return NULL; |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | bio = BIO_new(BIO_s_mem()); |
| 188 | switch (type) |
| 189 | { |
| 190 | case X509_FILETYPE_PEM: |
| 191 | ret = PEM_write_bio_PrivateKey(bio, pkey->pkey, cipher, NULL, 0, cb, cb_arg); |
| 192 | if (PyErr_Occurred()) |
| 193 | { |
| 194 | BIO_free(bio); |
| 195 | return NULL; |
| 196 | } |
| 197 | break; |
| 198 | |
| 199 | case X509_FILETYPE_ASN1: |
| 200 | ret = i2d_PrivateKey_bio(bio, pkey->pkey); |
| 201 | break; |
| 202 | |
| 203 | default: |
| 204 | PyErr_SetString(PyExc_ValueError, "type argument must be FILETYPE_PEM or FILETYPE_ASN1"); |
| 205 | BIO_free(bio); |
| 206 | return NULL; |
| 207 | } |
| 208 | |
| 209 | if (ret == 0) |
| 210 | { |
| 211 | BIO_free(bio); |
| 212 | exception_from_error_queue(); |
| 213 | return NULL; |
| 214 | } |
| 215 | |
| 216 | buf_len = BIO_get_mem_data(bio, &temp); |
| 217 | buffer = PyString_FromStringAndSize(temp, buf_len); |
| 218 | BIO_free(bio); |
| 219 | |
| 220 | return buffer; |
| 221 | } |
| 222 | |
| 223 | static char crypto_load_certificate_doc[] = "\n\ |
| 224 | Load a certificate from a buffer\n\ |
| 225 | \n\ |
| 226 | Arguments: spam - Always NULL\n\ |
| 227 | args - The Python argument tuple, should be:\n\ |
| 228 | type - The file type (one of FILETYPE_PEM, FILETYPE_ASN1)\n\ |
| 229 | buffer - The buffer the certificate is stored in\n\ |
| 230 | Returns: The X509 object\n\ |
| 231 | "; |
| 232 | |
| 233 | static PyObject * |
| 234 | crypto_load_certificate(PyObject *spam, PyObject *args) |
| 235 | { |
| 236 | crypto_X509Obj *crypto_X509_New(X509 *, int); |
| 237 | int type, len; |
| 238 | char *buffer; |
| 239 | BIO *bio; |
| 240 | X509 *cert; |
| 241 | |
| 242 | if (!PyArg_ParseTuple(args, "is#:load_certificate", &type, &buffer, &len)) |
| 243 | return NULL; |
| 244 | |
| 245 | bio = BIO_new_mem_buf(buffer, len); |
| 246 | switch (type) |
| 247 | { |
| 248 | case X509_FILETYPE_PEM: |
| 249 | cert = PEM_read_bio_X509(bio, NULL, NULL, NULL); |
| 250 | break; |
| 251 | |
| 252 | case X509_FILETYPE_ASN1: |
| 253 | cert = d2i_X509_bio(bio, NULL); |
| 254 | break; |
| 255 | |
| 256 | default: |
| 257 | PyErr_SetString(PyExc_ValueError, "type argument must be FILETYPE_PEM or FILETYPE_ASN1"); |
| 258 | BIO_free(bio); |
| 259 | return NULL; |
| 260 | } |
| 261 | BIO_free(bio); |
| 262 | |
| 263 | if (cert == NULL) |
| 264 | { |
| 265 | exception_from_error_queue(); |
| 266 | return NULL; |
| 267 | } |
| 268 | |
| 269 | return (PyObject *)crypto_X509_New(cert, 1); |
| 270 | } |
| 271 | |
| 272 | static char crypto_dump_certificate_doc[] = "\n\ |
| 273 | Dump a certificate to a buffer\n\ |
| 274 | \n\ |
| 275 | Arguments: spam - Always NULL\n\ |
| 276 | args - The Python argument tuple, should be:\n\ |
| 277 | type - The file type (one of FILETYPE_PEM, FILETYPE_ASN1)\n\ |
| 278 | cert - The certificate to dump\n\ |
| 279 | Returns: The buffer with the dumped certificate in\n\ |
| 280 | "; |
| 281 | |
| 282 | static PyObject * |
| 283 | crypto_dump_certificate(PyObject *spam, PyObject *args) |
| 284 | { |
| 285 | int type, ret, buf_len; |
| 286 | char *temp; |
| 287 | PyObject *buffer; |
| 288 | BIO *bio; |
| 289 | crypto_X509Obj *cert; |
| 290 | |
| 291 | if (!PyArg_ParseTuple(args, "iO!:dump_certificate", &type, |
| 292 | &crypto_X509_Type, &cert)) |
| 293 | return NULL; |
| 294 | |
| 295 | bio = BIO_new(BIO_s_mem()); |
| 296 | switch (type) |
| 297 | { |
| 298 | case X509_FILETYPE_PEM: |
| 299 | ret = PEM_write_bio_X509(bio, cert->x509); |
| 300 | break; |
| 301 | |
| 302 | case X509_FILETYPE_ASN1: |
| 303 | ret = i2d_X509_bio(bio, cert->x509); |
| 304 | break; |
| 305 | |
| 306 | default: |
| 307 | PyErr_SetString(PyExc_ValueError, "type argument must be FILETYPE_PEM or FILETYPE_ASN1"); |
| 308 | BIO_free(bio); |
| 309 | return NULL; |
| 310 | } |
| 311 | |
| 312 | if (ret == 0) |
| 313 | { |
| 314 | BIO_free(bio); |
| 315 | exception_from_error_queue(); |
| 316 | return NULL; |
| 317 | } |
| 318 | |
| 319 | buf_len = BIO_get_mem_data(bio, &temp); |
| 320 | buffer = PyString_FromStringAndSize(temp, buf_len); |
| 321 | BIO_free(bio); |
| 322 | |
| 323 | return buffer; |
| 324 | } |
| 325 | |
| 326 | static char crypto_load_certificate_request_doc[] = "\n\ |
| 327 | Load a certificate request from a buffer\n\ |
| 328 | \n\ |
| 329 | Arguments: spam - Always NULL\n\ |
| 330 | args - The Python argument tuple, should be:\n\ |
| 331 | type - The file type (one of FILETYPE_PEM, FILETYPE_ASN1)\n\ |
| 332 | buffer - The buffer the certificate request is stored in\n\ |
| 333 | Returns: The X509Req object\n\ |
| 334 | "; |
| 335 | |
| 336 | static PyObject * |
| 337 | crypto_load_certificate_request(PyObject *spam, PyObject *args) |
| 338 | { |
| 339 | crypto_X509ReqObj *crypto_X509Req_New(X509_REQ *, int); |
| 340 | int type, len; |
| 341 | char *buffer; |
| 342 | BIO *bio; |
| 343 | X509_REQ *req; |
| 344 | |
| 345 | if (!PyArg_ParseTuple(args, "is#:load_certificate_request", &type, &buffer, &len)) |
| 346 | return NULL; |
| 347 | |
| 348 | bio = BIO_new_mem_buf(buffer, len); |
| 349 | switch (type) |
| 350 | { |
| 351 | case X509_FILETYPE_PEM: |
| 352 | req = PEM_read_bio_X509_REQ(bio, NULL, NULL, NULL); |
| 353 | break; |
| 354 | |
| 355 | case X509_FILETYPE_ASN1: |
| 356 | req = d2i_X509_REQ_bio(bio, NULL); |
| 357 | break; |
| 358 | |
| 359 | default: |
| 360 | PyErr_SetString(PyExc_ValueError, "type argument must be FILETYPE_PEM or FILETYPE_ASN1"); |
| 361 | BIO_free(bio); |
| 362 | return NULL; |
| 363 | } |
| 364 | BIO_free(bio); |
| 365 | |
| 366 | if (req == NULL) |
| 367 | { |
| 368 | exception_from_error_queue(); |
| 369 | return NULL; |
| 370 | } |
| 371 | |
| 372 | return (PyObject *)crypto_X509Req_New(req, 1); |
| 373 | } |
| 374 | |
| 375 | static char crypto_dump_certificate_request_doc[] = "\n\ |
| 376 | Dump a certificate request to a buffer\n\ |
| 377 | \n\ |
| 378 | Arguments: spam - Always NULL\n\ |
| 379 | args - The Python argument tuple, should be:\n\ |
| 380 | type - The file type (one of FILETYPE_PEM, FILETYPE_ASN1)\n\ |
| 381 | req - The certificate request to dump\n\ |
| 382 | Returns: The buffer with the dumped certificate request in\n\ |
| 383 | "; |
| 384 | |
| 385 | static PyObject * |
| 386 | crypto_dump_certificate_request(PyObject *spam, PyObject *args) |
| 387 | { |
| 388 | int type, ret, buf_len; |
| 389 | char *temp; |
| 390 | PyObject *buffer; |
| 391 | BIO *bio; |
| 392 | crypto_X509ReqObj *req; |
| 393 | |
| 394 | if (!PyArg_ParseTuple(args, "iO!:dump_certificate_request", &type, |
| 395 | &crypto_X509Req_Type, &req)) |
| 396 | return NULL; |
| 397 | |
| 398 | bio = BIO_new(BIO_s_mem()); |
| 399 | switch (type) |
| 400 | { |
| 401 | case X509_FILETYPE_PEM: |
| 402 | ret = PEM_write_bio_X509_REQ(bio, req->x509_req); |
| 403 | break; |
| 404 | |
| 405 | case X509_FILETYPE_ASN1: |
| 406 | ret = i2d_X509_REQ_bio(bio, req->x509_req); |
| 407 | break; |
| 408 | |
| 409 | default: |
| 410 | PyErr_SetString(PyExc_ValueError, "type argument must be FILETYPE_PEM or FILETYPE_ASN1"); |
| 411 | BIO_free(bio); |
| 412 | return NULL; |
| 413 | } |
| 414 | |
| 415 | if (ret == 0) |
| 416 | { |
| 417 | BIO_free(bio); |
| 418 | exception_from_error_queue(); |
| 419 | return NULL; |
| 420 | } |
| 421 | |
| 422 | buf_len = BIO_get_mem_data(bio, &temp); |
| 423 | buffer = PyString_FromStringAndSize(temp, buf_len); |
| 424 | BIO_free(bio); |
| 425 | |
| 426 | return buffer; |
| 427 | } |
| 428 | |
| 429 | static char crypto_load_pkcs7_data_doc[] = "\n\ |
| 430 | Load pkcs7 data from a buffer\n\ |
| 431 | \n\ |
| 432 | Arguments: spam - Always NULL\n\ |
| 433 | args - The argument tuple, should be:\n\ |
| 434 | type - The file type (one of FILETYPE_PEM or FILETYPE_ASN1)\n\ |
| 435 | buffer - The buffer with the pkcs7 data.\n\ |
| 436 | Returns: The PKCS7 object\n\ |
| 437 | "; |
| 438 | |
| 439 | static PyObject * |
| 440 | crypto_load_pkcs7_data(PyObject *spam, PyObject *args) |
| 441 | { |
| 442 | int type, len; |
| 443 | char *buffer; |
| 444 | BIO *bio; |
| 445 | PKCS7 *pkcs7 = NULL; |
| 446 | |
| 447 | if (!PyArg_ParseTuple(args, "is#:load_pkcs7_data", &type, &buffer, &len)) |
| 448 | return NULL; |
| 449 | |
| 450 | /* |
| 451 | * Try to read the pkcs7 data from the bio |
| 452 | */ |
| 453 | bio = BIO_new_mem_buf(buffer, len); |
| 454 | switch (type) |
| 455 | { |
| 456 | case X509_FILETYPE_PEM: |
| 457 | pkcs7 = PEM_read_bio_PKCS7(bio, NULL, NULL, NULL); |
| 458 | break; |
| 459 | |
| 460 | case X509_FILETYPE_ASN1: |
| 461 | pkcs7 = d2i_PKCS7_bio(bio, NULL); |
| 462 | break; |
| 463 | |
| 464 | default: |
| 465 | PyErr_SetString(PyExc_ValueError, |
| 466 | "type argument must be FILETYPE_PEM or FILETYPE_ASN1"); |
| 467 | return NULL; |
| 468 | } |
| 469 | BIO_free(bio); |
| 470 | |
| 471 | /* |
| 472 | * Check if we got a PKCS7 structure |
| 473 | */ |
| 474 | if (pkcs7 == NULL) |
| 475 | { |
| 476 | exception_from_error_queue(); |
| 477 | return NULL; |
| 478 | } |
| 479 | |
| 480 | return (PyObject *)crypto_PKCS7_New(pkcs7, 1); |
| 481 | } |
| 482 | |
| 483 | static char crypto_load_pkcs12_doc[] = "\n\ |
| 484 | Load a PKCS12 object from a buffer\n\ |
| 485 | \n\ |
| 486 | Arguments: spam - Always NULL\n\ |
| 487 | args - The Python argument tuple, should be:\n\ |
| 488 | buffer - The buffer the certificate is stored in\n\ |
| 489 | passphrase (Optional) - The password to decrypt the PKCS12 lump\n\ |
| 490 | Returns: The PKCS12 object\n\ |
| 491 | "; |
| 492 | |
| 493 | static PyObject * |
| 494 | crypto_load_pkcs12(PyObject *spam, PyObject *args) |
| 495 | { |
| 496 | crypto_PKCS12Obj *crypto_PKCS12_New(PKCS12 *, char *); |
| 497 | int len; |
| 498 | char *buffer, *passphrase = NULL; |
| 499 | BIO *bio; |
| 500 | PKCS12 *p12; |
| 501 | |
| 502 | if (!PyArg_ParseTuple(args, "s#|s:load_pkcs12", &buffer, &len, &passphrase)) |
| 503 | return NULL; |
| 504 | |
| 505 | bio = BIO_new_mem_buf(buffer, len); |
| 506 | if ((p12 = d2i_PKCS12_bio(bio, NULL)) == NULL) |
| 507 | { |
| 508 | BIO_free(bio); |
| 509 | exception_from_error_queue(); |
| 510 | return NULL; |
| 511 | } |
| 512 | BIO_free(bio); |
| 513 | |
| 514 | return (PyObject *)crypto_PKCS12_New(p12, passphrase); |
| 515 | } |
| 516 | |
| 517 | |
| 518 | static char crypto_X509_doc[] = "\n\ |
| 519 | The factory function inserted in the module dictionary to create X509\n\ |
| 520 | objects\n\ |
| 521 | \n\ |
| 522 | Arguments: spam - Always NULL\n\ |
| 523 | args - The Python argument tuple, should be empty\n\ |
| 524 | Returns: The X509 object\n\ |
| 525 | "; |
| 526 | |
| 527 | static PyObject * |
| 528 | crypto_X509(PyObject *spam, PyObject *args) |
| 529 | { |
| 530 | if (!PyArg_ParseTuple(args, ":X509")) |
| 531 | return NULL; |
| 532 | |
| 533 | return (PyObject *)crypto_X509_New(X509_new(), 1); |
| 534 | } |
| 535 | |
| 536 | static char crypto_X509Name_doc[] = "\n\ |
| 537 | The factory function inserted in the module dictionary as a copy\n\ |
| 538 | constructor for X509Name objects.\n\ |
| 539 | \n\ |
| 540 | Arguments: spam - Always NULL\n\ |
| 541 | args - The Python argument tuple, should be:\n\ |
| 542 | name - An X509Name object to copy\n\ |
| 543 | Returns: The X509Name object\n\ |
| 544 | "; |
| 545 | |
| 546 | static PyObject * |
| 547 | crypto_X509Name(PyObject *spam, PyObject *args) |
| 548 | { |
| 549 | crypto_X509NameObj *name; |
| 550 | |
| 551 | if (!PyArg_ParseTuple(args, "O!:X509Name", &crypto_X509Name_Type, &name)) |
| 552 | return NULL; |
| 553 | |
| 554 | return (PyObject *)crypto_X509Name_New(X509_NAME_dup(name->x509_name), 1); |
| 555 | } |
| 556 | |
| 557 | static char crypto_X509Req_doc[] = "\n\ |
| 558 | The factory function inserted in the module dictionary to create X509Req\n\ |
| 559 | objects\n\ |
| 560 | \n\ |
| 561 | Arguments: spam - Always NULL\n\ |
| 562 | args - The Python argument tuple, should be empty\n\ |
| 563 | Returns: The X509Req object\n\ |
| 564 | "; |
| 565 | |
| 566 | static PyObject * |
| 567 | crypto_X509Req(PyObject *spam, PyObject *args) |
| 568 | { |
| 569 | if (!PyArg_ParseTuple(args, ":X509Req")) |
| 570 | return NULL; |
| 571 | |
| 572 | return (PyObject *)crypto_X509Req_New(X509_REQ_new(), 1); |
| 573 | } |
| 574 | |
| 575 | static char crypto_PKey_doc[] = "\n\ |
| 576 | The factory function inserted in the module dictionary to create PKey\n\ |
| 577 | objects\n\ |
| 578 | \n\ |
| 579 | Arguments: spam - Always NULL\n\ |
| 580 | args - The Python argument tuple, should be empty\n\ |
| 581 | Returns: The PKey object\n\ |
| 582 | "; |
| 583 | |
| 584 | static PyObject * |
| 585 | crypto_PKey(PyObject *spam, PyObject *args) |
| 586 | { |
| 587 | if (!PyArg_ParseTuple(args, ":PKey")) |
| 588 | return NULL; |
| 589 | |
| 590 | return (PyObject *)crypto_PKey_New(EVP_PKEY_new(), 1); |
| 591 | } |
| 592 | |
| 593 | static char crypto_X509Extension_doc[] = "\n\ |
| 594 | The factory function inserted in the module dictionary to create\n\ |
| 595 | X509Extension objects.\n\ |
| 596 | \n\ |
| 597 | Arguments: spam - Always NULL\n\ |
| 598 | args - The Python argument tuple, should be\n\ |
| 599 | typename - ???\n\ |
| 600 | critical - ???\n\ |
| 601 | value - ???\n\ |
| 602 | Returns: The X509Extension object\n\ |
| 603 | "; |
| 604 | |
| 605 | static PyObject * |
| 606 | crypto_X509Extension(PyObject *spam, PyObject *args) |
| 607 | { |
| 608 | char *type_name, *value; |
| 609 | int critical; |
| 610 | |
| 611 | if (!PyArg_ParseTuple(args, "sis:X509Extension", &type_name, &critical, |
| 612 | &value)) |
| 613 | return NULL; |
| 614 | |
| 615 | return (PyObject *)crypto_X509Extension_New(type_name, critical, value); |
| 616 | } |
| 617 | |
| 618 | static char crypto_NetscapeSPKI_doc[] = "\n\ |
| 619 | The factory function inserted in the module dictionary to create NetscapeSPKI\n\ |
| 620 | objects\n\ |
| 621 | \n\ |
| 622 | Arguments: spam - Always NULL\n\ |
| 623 | args - The Python argument tuple, should be empty or, optionally\n\ |
| 624 | enc - Base64 encoded NetscapeSPKI object.\n\ |
| 625 | Returns: The NetscapeSPKI object\n\ |
| 626 | "; |
| 627 | |
| 628 | static PyObject * |
| 629 | crypto_NetscapeSPKI(PyObject *spam, PyObject *args) |
| 630 | { |
| 631 | char *enc = NULL; |
| 632 | int enc_len = -1; |
| 633 | NETSCAPE_SPKI *spki; |
| 634 | |
| 635 | if (!PyArg_ParseTuple(args, "|s#:NetscapeSPKI", &enc, &enc_len)) |
| 636 | return NULL; |
| 637 | |
| 638 | if (enc_len >= 0) |
| 639 | spki = NETSCAPE_SPKI_b64_decode(enc, enc_len); |
| 640 | else |
| 641 | spki = NETSCAPE_SPKI_new(); |
| 642 | if (spki == NULL) |
| 643 | { |
| 644 | exception_from_error_queue(); |
| 645 | return NULL; |
| 646 | } |
| 647 | return (PyObject *)crypto_NetscapeSPKI_New(spki, 1); |
| 648 | } |
| 649 | |
Jean-Paul Calderone | 7df40db | 2008-03-03 15:12:42 -0500 | [diff] [blame] | 650 | static char crypto_X509_verify_cert_error_string_doc[] = "\n\ |
| 651 | Get X509 verify certificate error string.\n\ |
| 652 | \n\ |
| 653 | Arguments: errnum - Error number\n\ |
| 654 | Returns: Error string as a Python string\n\ |
| 655 | "; |
| 656 | |
| 657 | static PyObject * |
| 658 | crypto_X509_verify_cert_error_string(PyObject *spam, PyObject *args) |
| 659 | { |
| 660 | int errnum; |
| 661 | const char *str; |
| 662 | |
| 663 | if (!PyArg_ParseTuple(args, "i", &errnum)) |
| 664 | return NULL; |
| 665 | |
| 666 | str = X509_verify_cert_error_string(errnum); |
| 667 | return PyString_FromString(str); |
| 668 | } |
| 669 | |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 670 | /* Methods in the OpenSSL.crypto module (i.e. none) */ |
| 671 | static PyMethodDef crypto_methods[] = { |
| 672 | /* Module functions */ |
| 673 | { "load_privatekey", (PyCFunction)crypto_load_privatekey, METH_VARARGS, crypto_load_privatekey_doc }, |
| 674 | { "dump_privatekey", (PyCFunction)crypto_dump_privatekey, METH_VARARGS, crypto_dump_privatekey_doc }, |
| 675 | { "load_certificate", (PyCFunction)crypto_load_certificate, METH_VARARGS, crypto_load_certificate_doc }, |
| 676 | { "dump_certificate", (PyCFunction)crypto_dump_certificate, METH_VARARGS, crypto_dump_certificate_doc }, |
| 677 | { "load_certificate_request", (PyCFunction)crypto_load_certificate_request, METH_VARARGS, crypto_load_certificate_request_doc }, |
| 678 | { "dump_certificate_request", (PyCFunction)crypto_dump_certificate_request, METH_VARARGS, crypto_dump_certificate_request_doc }, |
| 679 | { "load_pkcs7_data", (PyCFunction)crypto_load_pkcs7_data, METH_VARARGS, crypto_load_pkcs7_data_doc }, |
| 680 | { "load_pkcs12", (PyCFunction)crypto_load_pkcs12, METH_VARARGS, crypto_load_pkcs12_doc }, |
| 681 | /* Factory functions */ |
| 682 | { "X509", (PyCFunction)crypto_X509, METH_VARARGS, crypto_X509_doc }, |
| 683 | { "X509Name",(PyCFunction)crypto_X509Name,METH_VARARGS, crypto_X509Name_doc }, |
| 684 | { "X509Req", (PyCFunction)crypto_X509Req, METH_VARARGS, crypto_X509Req_doc }, |
| 685 | { "PKey", (PyCFunction)crypto_PKey, METH_VARARGS, crypto_PKey_doc }, |
| 686 | { "X509Extension", (PyCFunction)crypto_X509Extension, METH_VARARGS, crypto_X509Extension_doc }, |
| 687 | { "NetscapeSPKI", (PyCFunction)crypto_NetscapeSPKI, METH_VARARGS, crypto_NetscapeSPKI_doc }, |
Jean-Paul Calderone | 7df40db | 2008-03-03 15:12:42 -0500 | [diff] [blame] | 688 | { "X509_verify_cert_error_string", (PyCFunction)crypto_X509_verify_cert_error_string, METH_VARARGS, crypto_X509_verify_cert_error_string_doc }, |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 689 | { NULL, NULL } |
| 690 | }; |
| 691 | |
| 692 | /* |
| 693 | * Initialize crypto sub module |
| 694 | * |
| 695 | * Arguments: None |
| 696 | * Returns: None |
| 697 | */ |
| 698 | void |
| 699 | initcrypto(void) |
| 700 | { |
| 701 | static void *crypto_API[crypto_API_pointers]; |
| 702 | PyObject *c_api_object; |
| 703 | PyObject *module, *dict; |
| 704 | |
| 705 | ERR_load_crypto_strings(); |
| 706 | OpenSSL_add_all_algorithms(); |
| 707 | |
| 708 | if ((module = Py_InitModule3("crypto", crypto_methods, crypto_doc)) == NULL) |
| 709 | return; |
| 710 | |
| 711 | /* Initialize the C API pointer array */ |
| 712 | crypto_API[crypto_X509_New_NUM] = (void *)crypto_X509_New; |
| 713 | crypto_API[crypto_X509Name_New_NUM] = (void *)crypto_X509Name_New; |
| 714 | crypto_API[crypto_X509Req_New_NUM] = (void *)crypto_X509Req_New; |
| 715 | crypto_API[crypto_X509Store_New_NUM] = (void *)crypto_X509Store_New; |
| 716 | crypto_API[crypto_PKey_New_NUM] = (void *)crypto_PKey_New; |
| 717 | crypto_API[crypto_X509Extension_New_NUM] = (void *)crypto_X509Extension_New; |
| 718 | crypto_API[crypto_PKCS7_New_NUM] = (void *)crypto_PKCS7_New; |
| 719 | crypto_API[crypto_NetscapeSPKI_New_NUM] = (void *)crypto_NetscapeSPKI_New; |
| 720 | c_api_object = PyCObject_FromVoidPtr((void *)crypto_API, NULL); |
| 721 | if (c_api_object != NULL) |
| 722 | PyModule_AddObject(module, "_C_API", c_api_object); |
| 723 | |
| 724 | crypto_Error = PyErr_NewException("OpenSSL.crypto.Error", NULL, NULL); |
| 725 | if (crypto_Error == NULL) |
| 726 | goto error; |
| 727 | if (PyModule_AddObject(module, "Error", crypto_Error) != 0) |
| 728 | goto error; |
| 729 | |
| 730 | PyModule_AddIntConstant(module, "FILETYPE_PEM", X509_FILETYPE_PEM); |
| 731 | PyModule_AddIntConstant(module, "FILETYPE_ASN1", X509_FILETYPE_ASN1); |
| 732 | |
| 733 | PyModule_AddIntConstant(module, "TYPE_RSA", crypto_TYPE_RSA); |
| 734 | PyModule_AddIntConstant(module, "TYPE_DSA", crypto_TYPE_DSA); |
| 735 | |
| 736 | dict = PyModule_GetDict(module); |
| 737 | if (!init_crypto_x509(dict)) |
| 738 | goto error; |
| 739 | if (!init_crypto_x509name(dict)) |
| 740 | goto error; |
| 741 | if (!init_crypto_x509store(dict)) |
| 742 | goto error; |
| 743 | if (!init_crypto_x509req(dict)) |
| 744 | goto error; |
| 745 | if (!init_crypto_pkey(dict)) |
| 746 | goto error; |
| 747 | if (!init_crypto_x509extension(dict)) |
| 748 | goto error; |
| 749 | if (!init_crypto_pkcs7(dict)) |
| 750 | goto error; |
| 751 | if (!init_crypto_pkcs12(dict)) |
| 752 | goto error; |
| 753 | if (!init_crypto_netscape_spki(dict)) |
| 754 | goto error; |
| 755 | |
| 756 | error: |
| 757 | ; |
| 758 | } |
| 759 | |