Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 1 | /* |
| 2 | * pkcs12.c |
| 3 | * |
| 4 | * Copyright (C) AB Strakt 2001, All rights reserved |
| 5 | * |
| 6 | * Certificate transport (PKCS12) handling code, |
| 7 | * mostly thin wrappers around OpenSSL. |
| 8 | * See the file RATIONALE for a short explanation of why |
| 9 | * this module was written. |
| 10 | * |
| 11 | * Reviewed 2001-07-23 |
| 12 | */ |
| 13 | #include <Python.h> |
| 14 | #define crypto_MODULE |
| 15 | #include "crypto.h" |
| 16 | |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 17 | /* |
| 18 | * PKCS12 is a standard exchange format for digital certificates. |
| 19 | * See e.g. the OpenSSL homepage http://www.openssl.org/ for more information |
| 20 | */ |
| 21 | |
| 22 | static void crypto_PKCS12_dealloc(crypto_PKCS12Obj *self); |
| 23 | |
| 24 | static char crypto_PKCS12_get_certificate_doc[] = "\n\ |
| 25 | Return certificate portion of the PKCS12 structure\n\ |
| 26 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 27 | @return: X509 object containing the certificate\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 28 | "; |
| 29 | static PyObject * |
| 30 | crypto_PKCS12_get_certificate(crypto_PKCS12Obj *self, PyObject *args) |
| 31 | { |
| 32 | if (!PyArg_ParseTuple(args, ":get_certificate")) |
| 33 | return NULL; |
| 34 | |
| 35 | Py_INCREF(self->cert); |
| 36 | return self->cert; |
| 37 | } |
| 38 | |
Rick Dean | 623ee36 | 2009-07-17 12:22:16 -0500 | [diff] [blame] | 39 | static char crypto_PKCS12_set_certificate_doc[] = "\n\ |
Rick Dean | e182f48 | 2009-07-17 14:49:48 -0500 | [diff] [blame] | 40 | Replace the certificate portion of the PKCS12 structure\n\ |
Rick Dean | 623ee36 | 2009-07-17 12:22:16 -0500 | [diff] [blame] | 41 | \n\ |
Rick Dean | e182f48 | 2009-07-17 14:49:48 -0500 | [diff] [blame] | 42 | @param cert: The new certificate.\n\ |
| 43 | @type cert: L{X509}\n\ |
| 44 | @return: X509 object containing the certificate\n\ |
Rick Dean | 623ee36 | 2009-07-17 12:22:16 -0500 | [diff] [blame] | 45 | "; |
Rick Dean | 38a05c8 | 2009-07-18 01:41:30 -0500 | [diff] [blame] | 46 | static PyObject * |
Rick Dean | 623ee36 | 2009-07-17 12:22:16 -0500 | [diff] [blame] | 47 | crypto_PKCS12_set_certificate(crypto_PKCS12Obj *self, PyObject *args, PyObject *keywds) |
| 48 | { |
| 49 | PyObject *cert = NULL; |
| 50 | static char *kwlist[] = {"cert", NULL}; |
| 51 | |
| 52 | if (!PyArg_ParseTupleAndKeywords(args, keywds, "O:set_certificate", |
| 53 | kwlist, &cert)) |
| 54 | return NULL; |
| 55 | |
| 56 | if (cert != Py_None && ! PyObject_IsInstance(cert, (PyObject *) &crypto_X509_Type)) { |
| 57 | PyErr_SetString(PyExc_TypeError, "cert must be type X509 or None"); |
| 58 | return NULL; |
| 59 | } |
| 60 | |
| 61 | Py_INCREF(cert); /* Make consistent before calling Py_DECREF() */ |
Rick Dean | 585cd9e | 2009-07-18 14:58:11 -0500 | [diff] [blame^] | 62 | Py_DECREF(self->cert); |
Rick Dean | 623ee36 | 2009-07-17 12:22:16 -0500 | [diff] [blame] | 63 | self->cert = cert; |
| 64 | |
Rick Dean | 38a05c8 | 2009-07-18 01:41:30 -0500 | [diff] [blame] | 65 | Py_INCREF(Py_None); |
| 66 | return Py_None; |
Rick Dean | 623ee36 | 2009-07-17 12:22:16 -0500 | [diff] [blame] | 67 | } |
| 68 | |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 69 | static char crypto_PKCS12_get_privatekey_doc[] = "\n\ |
| 70 | Return private key portion of the PKCS12 structure\n\ |
| 71 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 72 | @returns: PKey object containing the private key\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 73 | "; |
Rick Dean | 623ee36 | 2009-07-17 12:22:16 -0500 | [diff] [blame] | 74 | static crypto_PKeyObj * |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 75 | crypto_PKCS12_get_privatekey(crypto_PKCS12Obj *self, PyObject *args) |
| 76 | { |
| 77 | if (!PyArg_ParseTuple(args, ":get_privatekey")) |
| 78 | return NULL; |
| 79 | |
| 80 | Py_INCREF(self->key); |
Rick Dean | 864e15e | 2009-07-17 15:21:23 -0500 | [diff] [blame] | 81 | return (crypto_PKeyObj *) self->key; |
Rick Dean | 623ee36 | 2009-07-17 12:22:16 -0500 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | static char crypto_PKCS12_set_privatekey_doc[] = "\n\ |
Rick Dean | e182f48 | 2009-07-17 14:49:48 -0500 | [diff] [blame] | 85 | Replace or set the certificate portion of the PKCS12 structure\n\ |
Rick Dean | 623ee36 | 2009-07-17 12:22:16 -0500 | [diff] [blame] | 86 | \n\ |
Rick Dean | e182f48 | 2009-07-17 14:49:48 -0500 | [diff] [blame] | 87 | @param pkey: The new private key.\n\ |
| 88 | @type pkey: L{PKey}\n\ |
| 89 | @return: None\n\ |
Rick Dean | 623ee36 | 2009-07-17 12:22:16 -0500 | [diff] [blame] | 90 | "; |
Rick Dean | 38a05c8 | 2009-07-18 01:41:30 -0500 | [diff] [blame] | 91 | static PyObject * |
Rick Dean | 623ee36 | 2009-07-17 12:22:16 -0500 | [diff] [blame] | 92 | crypto_PKCS12_set_privatekey(crypto_PKCS12Obj *self, PyObject *args, PyObject *keywds) |
| 93 | { |
| 94 | PyObject *pkey = NULL; |
| 95 | static char *kwlist[] = {"pkey", NULL}; |
| 96 | |
| 97 | if (!PyArg_ParseTupleAndKeywords(args, keywds, "O:set_privatekey", |
| 98 | kwlist, &pkey)) |
| 99 | return NULL; |
| 100 | |
| 101 | if (pkey != Py_None && ! PyObject_IsInstance(pkey, (PyObject *) &crypto_PKey_Type)) { |
| 102 | PyErr_SetString(PyExc_TypeError, "pkey must be type X509 or None"); |
| 103 | return NULL; |
| 104 | } |
| 105 | |
| 106 | Py_INCREF(pkey); /* Make consistent before calling Py_DECREF() */ |
Rick Dean | 585cd9e | 2009-07-18 14:58:11 -0500 | [diff] [blame^] | 107 | Py_DECREF(self->key); |
Rick Dean | 623ee36 | 2009-07-17 12:22:16 -0500 | [diff] [blame] | 108 | self->key = pkey; |
| 109 | |
Rick Dean | 38a05c8 | 2009-07-18 01:41:30 -0500 | [diff] [blame] | 110 | Py_INCREF(Py_None); |
| 111 | return Py_None; |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | static char crypto_PKCS12_get_ca_certificates_doc[] = "\n\ |
| 115 | Return CA certificates within of the PKCS12 object\n\ |
| 116 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 117 | @return: A newly created tuple containing the CA certificates in the chain,\n\ |
| 118 | if any are present, or None if no CA certificates are present.\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 119 | "; |
| 120 | static PyObject * |
| 121 | crypto_PKCS12_get_ca_certificates(crypto_PKCS12Obj *self, PyObject *args) |
| 122 | { |
| 123 | if (!PyArg_ParseTuple(args, ":get_ca_certificates")) |
| 124 | return NULL; |
| 125 | |
| 126 | Py_INCREF(self->cacerts); |
| 127 | return self->cacerts; |
| 128 | } |
| 129 | |
Rick Dean | 623ee36 | 2009-07-17 12:22:16 -0500 | [diff] [blame] | 130 | static char crypto_PKCS12_set_ca_certificates_doc[] = "\n\ |
Rick Dean | e182f48 | 2009-07-17 14:49:48 -0500 | [diff] [blame] | 131 | Replace or set the CA certificates withing the PKCS12 object.\n\ |
Rick Dean | 623ee36 | 2009-07-17 12:22:16 -0500 | [diff] [blame] | 132 | \n\ |
Rick Dean | e182f48 | 2009-07-17 14:49:48 -0500 | [diff] [blame] | 133 | @param cacerts: The new CA certificates.\n\ |
| 134 | @type cacerts: Sequence of L{X509}\n\ |
| 135 | @return: None\n\ |
Rick Dean | 623ee36 | 2009-07-17 12:22:16 -0500 | [diff] [blame] | 136 | "; |
Rick Dean | 38a05c8 | 2009-07-18 01:41:30 -0500 | [diff] [blame] | 137 | static PyObject * |
Rick Dean | 623ee36 | 2009-07-17 12:22:16 -0500 | [diff] [blame] | 138 | crypto_PKCS12_set_ca_certificates(crypto_PKCS12Obj *self, PyObject *args, PyObject *keywds) |
| 139 | { |
| 140 | PyObject *cacerts; |
| 141 | static char *kwlist[] = {"cacerts", NULL}; |
Rick Dean | 864e15e | 2009-07-17 15:21:23 -0500 | [diff] [blame] | 142 | int i; /* Py_ssize_t for Python 2.5+ */ |
Rick Dean | 623ee36 | 2009-07-17 12:22:16 -0500 | [diff] [blame] | 143 | |
| 144 | if (!PyArg_ParseTupleAndKeywords(args, keywds, "O:set_ca_certificates", |
| 145 | kwlist, &cacerts)) |
| 146 | return NULL; |
| 147 | if (cacerts == Py_None) { |
| 148 | /* We are good. */ |
| 149 | } else if (PySequence_Check(cacerts)) { /* is iterable */ |
| 150 | for(i = 0;i < PySequence_Length(cacerts);i++) { /* For each CA cert */ |
| 151 | PyObject *obj; |
| 152 | obj = PySequence_GetItem(cacerts, i); |
| 153 | if (PyObject_Type(obj) != (PyObject *) &crypto_X509_Type) { |
| 154 | Py_DECREF(obj); |
| 155 | PyErr_SetString(PyExc_TypeError, "cacerts iterable must only contain X509Type"); |
| 156 | return NULL; |
| 157 | } |
| 158 | Py_DECREF(obj); |
| 159 | } |
| 160 | } else { |
| 161 | PyErr_SetString(PyExc_TypeError, "cacerts must be an iterable or None"); |
| 162 | return NULL; |
| 163 | } |
| 164 | |
| 165 | Py_INCREF(cacerts); /* Make consistent before calling Py_DECREF() */ |
Rick Dean | 585cd9e | 2009-07-18 14:58:11 -0500 | [diff] [blame^] | 166 | Py_DECREF(self->cacerts); |
Rick Dean | 623ee36 | 2009-07-17 12:22:16 -0500 | [diff] [blame] | 167 | self->cacerts = cacerts; |
| 168 | |
Rick Dean | 38a05c8 | 2009-07-18 01:41:30 -0500 | [diff] [blame] | 169 | Py_INCREF(Py_None); |
| 170 | return Py_None; |
Rick Dean | 623ee36 | 2009-07-17 12:22:16 -0500 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | static char crypto_PKCS12_export_doc[] = "\n\ |
Rick Dean | e182f48 | 2009-07-17 14:49:48 -0500 | [diff] [blame] | 174 | export([passphrase=None][, friendly_name=None][, iter=2048][, maciter=1]\n\ |
| 175 | Dump a PKCS12 object as a string. See also \"man PKCS12_create\".\n\ |
Rick Dean | 623ee36 | 2009-07-17 12:22:16 -0500 | [diff] [blame] | 176 | \n\ |
Rick Dean | e182f48 | 2009-07-17 14:49:48 -0500 | [diff] [blame] | 177 | @param passphrase: used to encrypt the PKCS12\n\ |
| 178 | @type passphrase: L{str}\n\ |
| 179 | @param friendly_name: A descriptive comment\n\ |
| 180 | @type friendly_name: L{str}\n\ |
| 181 | @param iter: How many times to repeat the encryption\n\ |
| 182 | @type iter: L{int}\n\ |
| 183 | @param maciter: How many times to repeat the MAC\n\ |
| 184 | @type maciter: L{int}\n\ |
| 185 | @return: The string containing the PKCS12\n\ |
Rick Dean | 623ee36 | 2009-07-17 12:22:16 -0500 | [diff] [blame] | 186 | "; |
Rick Dean | 623ee36 | 2009-07-17 12:22:16 -0500 | [diff] [blame] | 187 | static PyObject * |
| 188 | crypto_PKCS12_export(crypto_PKCS12Obj *self, PyObject *args, PyObject *keywds) |
| 189 | { |
| 190 | int buf_len; |
| 191 | PyObject *buffer; |
| 192 | char *temp, *passphrase = NULL, *friendly_name = NULL; |
| 193 | BIO *bio; |
| 194 | PKCS12 *p12; |
| 195 | EVP_PKEY *pkey = NULL; |
| 196 | STACK_OF(X509) *cacerts = NULL; |
| 197 | X509 *x509 = NULL; |
Rick Dean | e182f48 | 2009-07-17 14:49:48 -0500 | [diff] [blame] | 198 | int iter = 0; /* defaults to PKCS12_DEFAULT_ITER */ |
Rick Dean | 623ee36 | 2009-07-17 12:22:16 -0500 | [diff] [blame] | 199 | int maciter = 0; |
| 200 | static char *kwlist[] = {"passphrase", "friendly_name", "iter", "maciter", NULL}; |
| 201 | |
| 202 | if (!PyArg_ParseTupleAndKeywords(args, keywds, "|zzii:export", |
| 203 | kwlist, &passphrase, &friendly_name, &iter, &maciter)) |
| 204 | return NULL; |
| 205 | |
Rick Dean | 585cd9e | 2009-07-18 14:58:11 -0500 | [diff] [blame^] | 206 | if (self->key != Py_None) { |
Rick Dean | 623ee36 | 2009-07-17 12:22:16 -0500 | [diff] [blame] | 207 | pkey = ((crypto_PKeyObj*) self->key)->pkey; |
| 208 | } |
Rick Dean | 585cd9e | 2009-07-18 14:58:11 -0500 | [diff] [blame^] | 209 | if (self->cert != Py_None) { |
Rick Dean | 623ee36 | 2009-07-17 12:22:16 -0500 | [diff] [blame] | 210 | x509 = ((crypto_X509Obj*) self->cert)->x509; |
| 211 | } |
| 212 | cacerts = sk_X509_new_null(); |
Rick Dean | 585cd9e | 2009-07-18 14:58:11 -0500 | [diff] [blame^] | 213 | if (self->cacerts != Py_None) { |
Rick Dean | 864e15e | 2009-07-17 15:21:23 -0500 | [diff] [blame] | 214 | int i; /* Py_ssize_t for Python 2.5+ */ |
Rick Dean | 623ee36 | 2009-07-17 12:22:16 -0500 | [diff] [blame] | 215 | PyObject *obj; |
| 216 | for(i = 0;i < PySequence_Length(self->cacerts);i++) { /* For each CA cert */ |
| 217 | obj = PySequence_GetItem(self->cacerts, i); |
| 218 | /* assert(PyObject_IsInstance(obj, (PyObject *) &crypto_X509_Type )); */ |
| 219 | sk_X509_push(cacerts, (( crypto_X509Obj* ) obj)->x509); |
| 220 | Py_DECREF(obj); |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | p12 = PKCS12_create(passphrase, friendly_name, pkey, x509, cacerts, |
| 225 | NID_pbe_WithSHA1And3_Key_TripleDES_CBC, |
| 226 | NID_pbe_WithSHA1And3_Key_TripleDES_CBC, |
| 227 | iter, maciter, 0); |
| 228 | if( p12 == NULL ) { |
| 229 | exception_from_error_queue(crypto_Error); |
| 230 | return NULL; |
| 231 | } |
| 232 | bio = BIO_new(BIO_s_mem()); |
| 233 | i2d_PKCS12_bio(bio, p12); |
| 234 | buf_len = BIO_get_mem_data(bio, &temp); |
| 235 | buffer = PyString_FromStringAndSize(temp, buf_len); |
| 236 | BIO_free(bio); |
| 237 | return buffer; |
| 238 | } |
| 239 | |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 240 | /* |
| 241 | * ADD_METHOD(name) expands to a correct PyMethodDef declaration |
| 242 | * { 'name', (PyCFunction)crypto_PKCS12_name, METH_VARARGS, crypto_PKCS12_name_doc } |
| 243 | * for convenience |
| 244 | */ |
| 245 | #define ADD_METHOD(name) \ |
| 246 | { #name, (PyCFunction)crypto_PKCS12_##name, METH_VARARGS, crypto_PKCS12_##name##_doc } |
Rick Dean | 623ee36 | 2009-07-17 12:22:16 -0500 | [diff] [blame] | 247 | #define ADD_KW_METHOD(name) \ |
| 248 | { #name, (PyCFunction)crypto_PKCS12_##name, METH_VARARGS | METH_KEYWORDS, crypto_PKCS12_##name##_doc } |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 249 | static PyMethodDef crypto_PKCS12_methods[] = |
| 250 | { |
| 251 | ADD_METHOD(get_certificate), |
Rick Dean | 623ee36 | 2009-07-17 12:22:16 -0500 | [diff] [blame] | 252 | ADD_KW_METHOD(set_certificate), |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 253 | ADD_METHOD(get_privatekey), |
Rick Dean | 623ee36 | 2009-07-17 12:22:16 -0500 | [diff] [blame] | 254 | ADD_KW_METHOD(set_privatekey), |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 255 | ADD_METHOD(get_ca_certificates), |
Rick Dean | 623ee36 | 2009-07-17 12:22:16 -0500 | [diff] [blame] | 256 | ADD_KW_METHOD(set_ca_certificates), |
| 257 | ADD_KW_METHOD(export), |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 258 | { NULL, NULL } |
| 259 | }; |
| 260 | #undef ADD_METHOD |
| 261 | |
| 262 | /* |
| 263 | * Constructor for PKCS12 objects, never called by Python code directly. |
| 264 | * The strategy for this object is to create all the Python objects |
| 265 | * corresponding to the cert/key/CA certs right away |
| 266 | * |
| 267 | * Arguments: p12 - A "real" PKCS12 object |
| 268 | * passphrase - Passphrase to use when decrypting the PKCS12 object |
| 269 | * Returns: The newly created PKCS12 object |
| 270 | */ |
| 271 | crypto_PKCS12Obj * |
| 272 | crypto_PKCS12_New(PKCS12 *p12, char *passphrase) |
| 273 | { |
| 274 | crypto_PKCS12Obj *self; |
| 275 | PyObject *cacertobj = NULL; |
| 276 | |
| 277 | X509 *cert = NULL; |
| 278 | EVP_PKEY *pkey = NULL; |
| 279 | STACK_OF(X509) *cacerts = NULL; |
| 280 | |
| 281 | int i, cacert_count = 0; |
| 282 | |
| 283 | /* allocate space for the CA cert stack */ |
| 284 | cacerts = sk_X509_new_null(); |
| 285 | |
| 286 | /* parse the PKCS12 lump */ |
Rick Dean | 623ee36 | 2009-07-17 12:22:16 -0500 | [diff] [blame] | 287 | if (p12 && !(cacerts && PKCS12_parse(p12, passphrase, &pkey, &cert, &cacerts))) |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 288 | { |
Rick Dean | d369c93 | 2009-07-08 11:48:33 -0500 | [diff] [blame] | 289 | exception_from_error_queue(crypto_Error); |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 290 | return NULL; |
| 291 | } |
| 292 | |
| 293 | if (!(self = PyObject_GC_New(crypto_PKCS12Obj, &crypto_PKCS12_Type))) |
| 294 | return NULL; |
| 295 | |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 296 | Py_INCREF(Py_None); |
| 297 | self->cacerts = Py_None; |
| 298 | |
Rick Dean | 623ee36 | 2009-07-17 12:22:16 -0500 | [diff] [blame] | 299 | if (cert == NULL) { |
| 300 | Py_INCREF(Py_None); |
| 301 | self->cert = Py_None; |
| 302 | } else { |
| 303 | if ((self->cert = (PyObject *)crypto_X509_New(cert, 1)) == NULL) |
| 304 | goto error; |
| 305 | } |
| 306 | if (pkey == NULL) { |
| 307 | Py_INCREF(Py_None); |
| 308 | self->key = Py_None; |
| 309 | } else { |
| 310 | if ((self->key = (PyObject *)crypto_PKey_New(pkey, 1)) == NULL) |
| 311 | goto error; |
| 312 | } |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 313 | |
| 314 | /* Make a tuple for the CA certs */ |
| 315 | cacert_count = sk_X509_num(cacerts); |
| 316 | if (cacert_count > 0) |
| 317 | { |
| 318 | Py_DECREF(self->cacerts); |
| 319 | if ((self->cacerts = PyTuple_New(cacert_count)) == NULL) |
| 320 | goto error; |
| 321 | |
| 322 | for (i = 0; i < cacert_count; i++) |
| 323 | { |
| 324 | cert = sk_X509_value(cacerts, i); |
| 325 | if ((cacertobj = (PyObject *)crypto_X509_New(cert, 1)) == NULL) |
| 326 | goto error; |
| 327 | PyTuple_SET_ITEM(self->cacerts, i, cacertobj); |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | sk_X509_free(cacerts); /* don't free the certs, just the stack */ |
| 332 | PyObject_GC_Track(self); |
| 333 | |
| 334 | return self; |
| 335 | error: |
| 336 | crypto_PKCS12_dealloc(self); |
| 337 | return NULL; |
| 338 | } |
| 339 | |
Rick Dean | cbeaca0 | 2009-07-17 12:50:12 -0500 | [diff] [blame] | 340 | static char crypto_PKCS12_doc[] = "\n\ |
| 341 | PKCS12() -> PKCS12 instance\n\ |
| 342 | \n\ |
Rick Dean | e182f48 | 2009-07-17 14:49:48 -0500 | [diff] [blame] | 343 | Create a new empty PKCS12 object.\n\ |
Rick Dean | cbeaca0 | 2009-07-17 12:50:12 -0500 | [diff] [blame] | 344 | \n\ |
| 345 | @returns: The PKCS12 object\n\ |
| 346 | "; |
| 347 | static PyObject * |
| 348 | crypto_PKCS12_new(PyTypeObject *subtype, PyObject *args, PyObject *kwargs) |
| 349 | { |
| 350 | if (!PyArg_ParseTuple(args, ":PKCS12")) { |
| 351 | return NULL; |
| 352 | } |
| 353 | |
| 354 | return (PyObject *)crypto_PKCS12_New(NULL, NULL); |
| 355 | } |
| 356 | |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 357 | /* |
| 358 | * Find attribute |
| 359 | * |
| 360 | * Arguments: self - The PKCS12 object |
| 361 | * name - The attribute name |
| 362 | * Returns: A Python object for the attribute, or NULL if something went |
| 363 | * wrong |
| 364 | */ |
| 365 | static PyObject * |
| 366 | crypto_PKCS12_getattr(crypto_PKCS12Obj *self, char *name) |
| 367 | { |
| 368 | return Py_FindMethod(crypto_PKCS12_methods, (PyObject *)self, name); |
| 369 | } |
| 370 | |
| 371 | /* |
| 372 | * Call the visitproc on all contained objects. |
| 373 | * |
| 374 | * Arguments: self - The PKCS12 object |
| 375 | * visit - Function to call |
| 376 | * arg - Extra argument to visit |
| 377 | * Returns: 0 if all goes well, otherwise the return code from the first |
| 378 | * call that gave non-zero result. |
| 379 | */ |
| 380 | static int |
| 381 | crypto_PKCS12_traverse(crypto_PKCS12Obj *self, visitproc visit, void *arg) |
| 382 | { |
| 383 | int ret = 0; |
| 384 | |
| 385 | if (ret == 0 && self->cert != NULL) |
| 386 | ret = visit(self->cert, arg); |
| 387 | if (ret == 0 && self->key != NULL) |
| 388 | ret = visit(self->key, arg); |
| 389 | if (ret == 0 && self->cacerts != NULL) |
| 390 | ret = visit(self->cacerts, arg); |
| 391 | return ret; |
| 392 | } |
| 393 | |
| 394 | /* |
| 395 | * Decref all contained objects and zero the pointers. |
| 396 | * |
| 397 | * Arguments: self - The PKCS12 object |
| 398 | * Returns: Always 0. |
| 399 | */ |
| 400 | static int |
| 401 | crypto_PKCS12_clear(crypto_PKCS12Obj *self) |
| 402 | { |
| 403 | Py_XDECREF(self->cert); |
| 404 | self->cert = NULL; |
| 405 | Py_XDECREF(self->key); |
| 406 | self->key = NULL; |
| 407 | Py_XDECREF(self->cacerts); |
| 408 | self->cacerts = NULL; |
| 409 | return 0; |
| 410 | } |
| 411 | |
| 412 | /* |
| 413 | * Deallocate the memory used by the PKCS12 object |
| 414 | * |
| 415 | * Arguments: self - The PKCS12 object |
| 416 | * Returns: None |
| 417 | */ |
| 418 | static void |
| 419 | crypto_PKCS12_dealloc(crypto_PKCS12Obj *self) |
| 420 | { |
| 421 | PyObject_GC_UnTrack(self); |
| 422 | crypto_PKCS12_clear(self); |
| 423 | PyObject_GC_Del(self); |
| 424 | } |
| 425 | |
| 426 | PyTypeObject crypto_PKCS12_Type = { |
| 427 | PyObject_HEAD_INIT(NULL) |
| 428 | 0, |
| 429 | "PKCS12", |
| 430 | sizeof(crypto_PKCS12Obj), |
| 431 | 0, |
| 432 | (destructor)crypto_PKCS12_dealloc, |
| 433 | NULL, /* print */ |
| 434 | (getattrfunc)crypto_PKCS12_getattr, |
| 435 | NULL, /* setattr */ |
| 436 | NULL, /* compare */ |
| 437 | NULL, /* repr */ |
| 438 | NULL, /* as_number */ |
| 439 | NULL, /* as_sequence */ |
| 440 | NULL, /* as_mapping */ |
| 441 | NULL, /* hash */ |
| 442 | NULL, /* call */ |
| 443 | NULL, /* str */ |
| 444 | NULL, /* getattro */ |
| 445 | NULL, /* setattro */ |
| 446 | NULL, /* as_buffer */ |
| 447 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, |
Rick Dean | cbeaca0 | 2009-07-17 12:50:12 -0500 | [diff] [blame] | 448 | crypto_PKCS12_doc, |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 449 | (traverseproc)crypto_PKCS12_traverse, |
| 450 | (inquiry)crypto_PKCS12_clear, |
Rick Dean | cbeaca0 | 2009-07-17 12:50:12 -0500 | [diff] [blame] | 451 | NULL, /* tp_richcompare */ |
| 452 | 0, /* tp_weaklistoffset */ |
| 453 | NULL, /* tp_iter */ |
| 454 | NULL, /* tp_iternext */ |
| 455 | crypto_PKCS12_methods, /* tp_methods */ |
| 456 | NULL, /* tp_members */ |
| 457 | NULL, /* tp_getset */ |
| 458 | NULL, /* tp_base */ |
| 459 | NULL, /* tp_dict */ |
| 460 | NULL, /* tp_descr_get */ |
| 461 | NULL, /* tp_descr_set */ |
| 462 | 0, /* tp_dictoffset */ |
| 463 | NULL, /* tp_init */ |
| 464 | NULL, /* tp_alloc */ |
| 465 | crypto_PKCS12_new, /* tp_new */ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 466 | }; |
| 467 | |
| 468 | /* |
| 469 | * Initialize the PKCS12 part of the crypto sub module |
| 470 | * |
Jean-Paul Calderone | dc138fa | 2009-06-27 14:32:07 -0400 | [diff] [blame] | 471 | * Arguments: module - The crypto module |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 472 | * Returns: None |
| 473 | */ |
| 474 | int |
Jean-Paul Calderone | dc138fa | 2009-06-27 14:32:07 -0400 | [diff] [blame] | 475 | init_crypto_pkcs12(PyObject *module) { |
| 476 | if (PyType_Ready(&crypto_PKCS12_Type) < 0) { |
| 477 | return 0; |
| 478 | } |
| 479 | |
Rick Dean | cbeaca0 | 2009-07-17 12:50:12 -0500 | [diff] [blame] | 480 | if (PyModule_AddObject(module, "PKCS12", (PyObject *)&crypto_PKCS12_Type) != 0) { |
| 481 | return 0; |
| 482 | } |
| 483 | |
Jean-Paul Calderone | dc138fa | 2009-06-27 14:32:07 -0400 | [diff] [blame] | 484 | if (PyModule_AddObject(module, "PKCS12Type", (PyObject *)&crypto_PKCS12_Type) != 0) { |
| 485 | return 0; |
| 486 | } |
| 487 | |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 488 | return 1; |
| 489 | } |
| 490 | |