Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 1 | /* |
| 2 | * x509.c |
| 3 | * |
| 4 | * Copyright (C) AB Strakt 2001, All rights reserved |
Jean-Paul Calderone | 8b63d45 | 2008-03-21 18:31:12 -0400 | [diff] [blame] | 5 | * Copyright (C) Jean-Paul Calderone 2008, All rights reserved |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 6 | * |
| 7 | * Certificate (X.509) handling code, mostly thin wrappers around OpenSSL. |
| 8 | * See the file RATIONALE for a short explanation of why this module was written. |
| 9 | * |
| 10 | * Reviewed 2001-07-23 |
| 11 | */ |
| 12 | #include <Python.h> |
| 13 | #define crypto_MODULE |
| 14 | #include "crypto.h" |
| 15 | |
Jean-Paul Calderone | 525ef80 | 2008-03-09 20:39:42 -0400 | [diff] [blame] | 16 | /* |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 17 | * X.509 is a standard for digital certificates. See e.g. the OpenSSL homepage |
| 18 | * http://www.openssl.org/ for more information |
| 19 | */ |
| 20 | |
| 21 | static char crypto_X509_get_version_doc[] = "\n\ |
| 22 | Return version number of the certificate\n\ |
| 23 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 24 | @return: Version number as a Python integer\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 25 | "; |
| 26 | |
| 27 | static PyObject * |
| 28 | crypto_X509_get_version(crypto_X509Obj *self, PyObject *args) |
| 29 | { |
| 30 | if (!PyArg_ParseTuple(args, ":get_version")) |
| 31 | return NULL; |
| 32 | |
Jean-Paul Calderone | ae479ad | 2010-08-10 20:04:28 -0400 | [diff] [blame] | 33 | return PyLong_FromLong((long)X509_get_version(self->x509)); |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | static char crypto_X509_set_version_doc[] = "\n\ |
| 37 | Set version number of the certificate\n\ |
| 38 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 39 | @param version: The version number\n\ |
| 40 | @return: None\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 41 | "; |
| 42 | |
| 43 | static PyObject * |
| 44 | crypto_X509_set_version(crypto_X509Obj *self, PyObject *args) |
| 45 | { |
| 46 | int version; |
| 47 | |
| 48 | if (!PyArg_ParseTuple(args, "i:set_version", &version)) |
| 49 | return NULL; |
| 50 | |
| 51 | X509_set_version(self->x509, version); |
| 52 | |
| 53 | Py_INCREF(Py_None); |
| 54 | return Py_None; |
| 55 | } |
| 56 | |
| 57 | static char crypto_X509_get_serial_number_doc[] = "\n\ |
| 58 | Return serial number of the certificate\n\ |
| 59 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 60 | @return: Serial number as a Python integer\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 61 | "; |
| 62 | |
| 63 | static PyObject * |
| 64 | crypto_X509_get_serial_number(crypto_X509Obj *self, PyObject *args) |
| 65 | { |
| 66 | ASN1_INTEGER *asn1_i; |
Jean-Paul Calderone | 78381d2 | 2008-03-06 23:35:22 -0500 | [diff] [blame] | 67 | BIGNUM *bignum; |
| 68 | char *hex; |
| 69 | PyObject *res; |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 70 | |
| 71 | if (!PyArg_ParseTuple(args, ":get_serial_number")) |
| 72 | return NULL; |
| 73 | |
| 74 | asn1_i = X509_get_serialNumber(self->x509); |
Jean-Paul Calderone | 78381d2 | 2008-03-06 23:35:22 -0500 | [diff] [blame] | 75 | bignum = ASN1_INTEGER_to_BN(asn1_i, NULL); |
| 76 | hex = BN_bn2hex(bignum); |
| 77 | res = PyLong_FromString(hex, NULL, 16); |
| 78 | BN_free(bignum); |
| 79 | free(hex); |
| 80 | return res; |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | static char crypto_X509_set_serial_number_doc[] = "\n\ |
| 84 | Set serial number of the certificate\n\ |
| 85 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 86 | @param serial: The serial number\n\ |
| 87 | @return: None\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 88 | "; |
| 89 | |
| 90 | static PyObject * |
| 91 | crypto_X509_set_serial_number(crypto_X509Obj *self, PyObject *args) |
| 92 | { |
Jean-Paul Calderone | 78381d2 | 2008-03-06 23:35:22 -0500 | [diff] [blame] | 93 | long small_serial; |
| 94 | PyObject *serial = NULL; |
| 95 | PyObject *hex = NULL; |
Jean-Paul Calderone | 78381d2 | 2008-03-06 23:35:22 -0500 | [diff] [blame] | 96 | ASN1_INTEGER *asn1_i = NULL; |
| 97 | BIGNUM *bignum = NULL; |
Jean-Paul Calderone | dd467fb | 2010-08-12 20:44:32 -0400 | [diff] [blame] | 98 | char *hexstr; |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 99 | |
Jean-Paul Calderone | 78381d2 | 2008-03-06 23:35:22 -0500 | [diff] [blame] | 100 | if (!PyArg_ParseTuple(args, "O:set_serial_number", &serial)) { |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 101 | return NULL; |
Jean-Paul Calderone | 78381d2 | 2008-03-06 23:35:22 -0500 | [diff] [blame] | 102 | } |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 103 | |
Jean-Paul Calderone | c18669e | 2010-08-12 00:03:05 -0400 | [diff] [blame] | 104 | if (!PyOpenSSL_Integer_Check(serial)) { |
Jean-Paul Calderone | 78381d2 | 2008-03-06 23:35:22 -0500 | [diff] [blame] | 105 | PyErr_SetString( |
| 106 | PyExc_TypeError, "serial number must be integer"); |
| 107 | goto err; |
| 108 | } |
| 109 | |
Jean-Paul Calderone | a5a032f | 2010-08-12 20:03:32 -0400 | [diff] [blame] | 110 | if ((hex = PyOpenSSL_LongToHex(serial)) == NULL) { |
Jean-Paul Calderone | 78381d2 | 2008-03-06 23:35:22 -0500 | [diff] [blame] | 111 | goto err; |
| 112 | } |
| 113 | |
Jean-Paul Calderone | dd467fb | 2010-08-12 20:44:32 -0400 | [diff] [blame] | 114 | #ifdef PY3 |
| 115 | { |
| 116 | PyObject *hexbytes = PyUnicode_AsASCIIString(hex); |
| 117 | Py_DECREF(hex); |
| 118 | hex = hexbytes; |
| 119 | } |
| 120 | #endif |
| 121 | |
Jean-Paul Calderone | 78381d2 | 2008-03-06 23:35:22 -0500 | [diff] [blame] | 122 | /** |
| 123 | * BN_hex2bn stores the result in &bignum. Unless it doesn't feel like |
| 124 | * it. If bignum is still NULL after this call, then the return value |
| 125 | * is actually the result. I hope. -exarkun |
| 126 | */ |
Jean-Paul Calderone | dd467fb | 2010-08-12 20:44:32 -0400 | [diff] [blame] | 127 | hexstr = PyBytes_AsString(hex); |
Jean-Paul Calderone | 8eb46c7 | 2010-08-22 17:05:37 -0400 | [diff] [blame^] | 128 | if (hexstr[1] == 'x') { |
Jean-Paul Calderone | 728eba9 | 2010-08-12 20:17:59 -0400 | [diff] [blame] | 129 | /* +2 to skip the "0x" */ |
Jean-Paul Calderone | dd467fb | 2010-08-12 20:44:32 -0400 | [diff] [blame] | 130 | hexstr += 2; |
Jean-Paul Calderone | 728eba9 | 2010-08-12 20:17:59 -0400 | [diff] [blame] | 131 | } |
Jean-Paul Calderone | dd467fb | 2010-08-12 20:44:32 -0400 | [diff] [blame] | 132 | small_serial = BN_hex2bn(&bignum, hexstr); |
Jean-Paul Calderone | 78381d2 | 2008-03-06 23:35:22 -0500 | [diff] [blame] | 133 | |
Jean-Paul Calderone | 78381d2 | 2008-03-06 23:35:22 -0500 | [diff] [blame] | 134 | Py_DECREF(hex); |
| 135 | hex = NULL; |
| 136 | |
| 137 | if (bignum == NULL) { |
| 138 | if (ASN1_INTEGER_set(X509_get_serialNumber(self->x509), small_serial)) { |
Rick Dean | d369c93 | 2009-07-08 11:48:33 -0500 | [diff] [blame] | 139 | exception_from_error_queue(crypto_Error); |
Jean-Paul Calderone | 78381d2 | 2008-03-06 23:35:22 -0500 | [diff] [blame] | 140 | goto err; |
| 141 | } |
| 142 | } else { |
| 143 | asn1_i = BN_to_ASN1_INTEGER(bignum, NULL); |
| 144 | BN_free(bignum); |
| 145 | bignum = NULL; |
| 146 | if (asn1_i == NULL) { |
Rick Dean | d369c93 | 2009-07-08 11:48:33 -0500 | [diff] [blame] | 147 | exception_from_error_queue(crypto_Error); |
Jean-Paul Calderone | 78381d2 | 2008-03-06 23:35:22 -0500 | [diff] [blame] | 148 | goto err; |
| 149 | } |
| 150 | if (!X509_set_serialNumber(self->x509, asn1_i)) { |
Rick Dean | d369c93 | 2009-07-08 11:48:33 -0500 | [diff] [blame] | 151 | exception_from_error_queue(crypto_Error); |
Jean-Paul Calderone | 78381d2 | 2008-03-06 23:35:22 -0500 | [diff] [blame] | 152 | goto err; |
| 153 | } |
| 154 | ASN1_INTEGER_free(asn1_i); |
| 155 | asn1_i = NULL; |
| 156 | } |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 157 | |
| 158 | Py_INCREF(Py_None); |
| 159 | return Py_None; |
Jean-Paul Calderone | 78381d2 | 2008-03-06 23:35:22 -0500 | [diff] [blame] | 160 | |
| 161 | err: |
Jean-Paul Calderone | 78381d2 | 2008-03-06 23:35:22 -0500 | [diff] [blame] | 162 | if (hex) { |
| 163 | Py_DECREF(hex); |
| 164 | } |
| 165 | if (bignum) { |
| 166 | BN_free(bignum); |
| 167 | } |
| 168 | if (asn1_i) { |
| 169 | ASN1_INTEGER_free(asn1_i); |
| 170 | } |
| 171 | return NULL; |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | static char crypto_X509_get_issuer_doc[] = "\n\ |
| 175 | Create an X509Name object for the issuer of the certificate\n\ |
| 176 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 177 | @return: An X509Name object\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 178 | "; |
| 179 | |
| 180 | static PyObject * |
| 181 | crypto_X509_get_issuer(crypto_X509Obj *self, PyObject *args) |
| 182 | { |
| 183 | crypto_X509NameObj *pyname; |
| 184 | X509_NAME *name; |
| 185 | |
| 186 | if (!PyArg_ParseTuple(args, ":get_issuer")) |
| 187 | return NULL; |
| 188 | |
| 189 | name = X509_get_issuer_name(self->x509); |
| 190 | pyname = crypto_X509Name_New(name, 0); |
| 191 | if (pyname != NULL) |
| 192 | { |
| 193 | pyname->parent_cert = (PyObject *)self; |
| 194 | Py_INCREF(self); |
| 195 | } |
| 196 | return (PyObject *)pyname; |
| 197 | } |
| 198 | |
| 199 | static char crypto_X509_set_issuer_doc[] = "\n\ |
| 200 | Set the issuer of the certificate\n\ |
| 201 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 202 | @param issuer: The issuer name\n\ |
| 203 | @type issuer: L{X509Name}\n\ |
| 204 | @return: None\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 205 | "; |
| 206 | |
| 207 | static PyObject * |
| 208 | crypto_X509_set_issuer(crypto_X509Obj *self, PyObject *args) |
| 209 | { |
| 210 | crypto_X509NameObj *issuer; |
| 211 | |
| 212 | if (!PyArg_ParseTuple(args, "O!:set_issuer", &crypto_X509Name_Type, |
| 213 | &issuer)) |
| 214 | return NULL; |
| 215 | |
| 216 | if (!X509_set_issuer_name(self->x509, issuer->x509_name)) |
| 217 | { |
Rick Dean | d369c93 | 2009-07-08 11:48:33 -0500 | [diff] [blame] | 218 | exception_from_error_queue(crypto_Error); |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 219 | return NULL; |
| 220 | } |
| 221 | |
| 222 | Py_INCREF(Py_None); |
| 223 | return Py_None; |
| 224 | } |
| 225 | |
| 226 | static char crypto_X509_get_subject_doc[] = "\n\ |
| 227 | Create an X509Name object for the subject of the certificate\n\ |
| 228 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 229 | @return: An X509Name object\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 230 | "; |
| 231 | |
| 232 | static PyObject * |
| 233 | crypto_X509_get_subject(crypto_X509Obj *self, PyObject *args) |
| 234 | { |
| 235 | crypto_X509NameObj *pyname; |
| 236 | X509_NAME *name; |
| 237 | |
| 238 | if (!PyArg_ParseTuple(args, ":get_subject")) |
| 239 | return NULL; |
| 240 | |
| 241 | name = X509_get_subject_name(self->x509); |
| 242 | pyname = crypto_X509Name_New(name, 0); |
| 243 | if (pyname != NULL) |
| 244 | { |
| 245 | pyname->parent_cert = (PyObject *)self; |
| 246 | Py_INCREF(self); |
| 247 | } |
| 248 | return (PyObject *)pyname; |
| 249 | } |
| 250 | |
| 251 | static char crypto_X509_set_subject_doc[] = "\n\ |
| 252 | Set the subject of the certificate\n\ |
| 253 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 254 | @param subject: The subject name\n\ |
| 255 | @type subject: L{X509Name}\n\ |
| 256 | @return: None\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 257 | "; |
| 258 | |
| 259 | static PyObject * |
| 260 | crypto_X509_set_subject(crypto_X509Obj *self, PyObject *args) |
| 261 | { |
| 262 | crypto_X509NameObj *subject; |
| 263 | |
| 264 | if (!PyArg_ParseTuple(args, "O!:set_subject", &crypto_X509Name_Type, |
| 265 | &subject)) |
| 266 | return NULL; |
| 267 | |
| 268 | if (!X509_set_subject_name(self->x509, subject->x509_name)) |
| 269 | { |
Rick Dean | d369c93 | 2009-07-08 11:48:33 -0500 | [diff] [blame] | 270 | exception_from_error_queue(crypto_Error); |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 271 | return NULL; |
| 272 | } |
| 273 | |
| 274 | Py_INCREF(Py_None); |
| 275 | return Py_None; |
| 276 | } |
| 277 | |
| 278 | static char crypto_X509_get_pubkey_doc[] = "\n\ |
| 279 | Get the public key of the certificate\n\ |
| 280 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 281 | @return: The public key\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 282 | "; |
| 283 | |
| 284 | static PyObject * |
| 285 | crypto_X509_get_pubkey(crypto_X509Obj *self, PyObject *args) |
| 286 | { |
| 287 | crypto_PKeyObj *crypto_PKey_New(EVP_PKEY *, int); |
| 288 | EVP_PKEY *pkey; |
Jean-Paul Calderone | ac0d95f | 2008-03-10 00:00:42 -0400 | [diff] [blame] | 289 | crypto_PKeyObj *py_pkey; |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 290 | |
| 291 | if (!PyArg_ParseTuple(args, ":get_pubkey")) |
| 292 | return NULL; |
| 293 | |
| 294 | if ((pkey = X509_get_pubkey(self->x509)) == NULL) |
| 295 | { |
Rick Dean | d369c93 | 2009-07-08 11:48:33 -0500 | [diff] [blame] | 296 | exception_from_error_queue(crypto_Error); |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 297 | return NULL; |
| 298 | } |
| 299 | |
Jean-Paul Calderone | ac0d95f | 2008-03-10 00:00:42 -0400 | [diff] [blame] | 300 | py_pkey = crypto_PKey_New(pkey, 1); |
| 301 | if (py_pkey != NULL) { |
| 302 | py_pkey->only_public = 1; |
| 303 | } |
Jean-Paul Calderone | f60beec | 2008-07-22 21:19:02 -0400 | [diff] [blame] | 304 | return (PyObject *)py_pkey; |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | static char crypto_X509_set_pubkey_doc[] = "\n\ |
| 308 | Set the public key of the certificate\n\ |
| 309 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 310 | @param pkey: The public key\n\ |
| 311 | @return: None\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 312 | "; |
| 313 | |
| 314 | static PyObject * |
| 315 | crypto_X509_set_pubkey(crypto_X509Obj *self, PyObject *args) |
| 316 | { |
| 317 | crypto_PKeyObj *pkey; |
| 318 | |
| 319 | if (!PyArg_ParseTuple(args, "O!:set_pubkey", &crypto_PKey_Type, &pkey)) |
| 320 | return NULL; |
| 321 | |
| 322 | if (!X509_set_pubkey(self->x509, pkey->pkey)) |
| 323 | { |
Rick Dean | d369c93 | 2009-07-08 11:48:33 -0500 | [diff] [blame] | 324 | exception_from_error_queue(crypto_Error); |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 325 | return NULL; |
| 326 | } |
| 327 | |
| 328 | Py_INCREF(Py_None); |
| 329 | return Py_None; |
| 330 | } |
| 331 | |
Rick Dean | 536ba02 | 2009-07-24 23:57:27 -0500 | [diff] [blame] | 332 | PyObject* |
| 333 | _set_asn1_time(char *format, ASN1_TIME* timestamp, PyObject *args) |
Jean-Paul Calderone | 525ef80 | 2008-03-09 20:39:42 -0400 | [diff] [blame] | 334 | { |
| 335 | char *when; |
| 336 | |
| 337 | if (!PyArg_ParseTuple(args, format, &when)) |
| 338 | return NULL; |
| 339 | |
| 340 | if (ASN1_GENERALIZEDTIME_set_string(timestamp, when) == 0) { |
| 341 | ASN1_GENERALIZEDTIME dummy; |
| 342 | dummy.type = V_ASN1_GENERALIZEDTIME; |
| 343 | dummy.length = strlen(when); |
Jean-Paul Calderone | 28ebb30 | 2008-12-29 16:25:30 -0500 | [diff] [blame] | 344 | dummy.data = (unsigned char *)when; |
Jean-Paul Calderone | 525ef80 | 2008-03-09 20:39:42 -0400 | [diff] [blame] | 345 | if (!ASN1_GENERALIZEDTIME_check(&dummy)) { |
| 346 | PyErr_SetString(PyExc_ValueError, "Invalid string"); |
| 347 | } else { |
| 348 | PyErr_SetString(PyExc_RuntimeError, "Unknown ASN1_GENERALIZEDTIME_set_string failure"); |
| 349 | } |
| 350 | return NULL; |
| 351 | } |
| 352 | Py_INCREF(Py_None); |
| 353 | return Py_None; |
| 354 | } |
| 355 | |
| 356 | static char crypto_X509_set_notBefore_doc[] = "\n\ |
| 357 | Set the time stamp for when the certificate starts being valid\n\ |
| 358 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 359 | @param when: A string giving the timestamp, in the format:\n\ |
Jean-Paul Calderone | 525ef80 | 2008-03-09 20:39:42 -0400 | [diff] [blame] | 360 | \n\ |
| 361 | YYYYMMDDhhmmssZ\n\ |
| 362 | YYYYMMDDhhmmss+hhmm\n\ |
| 363 | YYYYMMDDhhmmss-hhmm\n\ |
| 364 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 365 | @return: None\n\ |
Jean-Paul Calderone | 525ef80 | 2008-03-09 20:39:42 -0400 | [diff] [blame] | 366 | "; |
| 367 | |
Jean-Paul Calderone | e890db3 | 2010-08-22 16:55:15 -0400 | [diff] [blame] | 368 | #ifdef PY3 |
| 369 | #define FMT(name) ("y" name) |
| 370 | #else |
| 371 | #define FMT(name) ("s" name) |
| 372 | #endif |
| 373 | |
Jean-Paul Calderone | 525ef80 | 2008-03-09 20:39:42 -0400 | [diff] [blame] | 374 | static PyObject* |
| 375 | crypto_X509_set_notBefore(crypto_X509Obj *self, PyObject *args) |
| 376 | { |
| 377 | return _set_asn1_time( |
Jean-Paul Calderone | e890db3 | 2010-08-22 16:55:15 -0400 | [diff] [blame] | 378 | FMT(":set_notBefore"), X509_get_notBefore(self->x509), args); |
Jean-Paul Calderone | 525ef80 | 2008-03-09 20:39:42 -0400 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | static char crypto_X509_set_notAfter_doc[] = "\n\ |
| 382 | Set the time stamp for when the certificate stops being valid\n\ |
| 383 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 384 | @param when: A string giving the timestamp, in the format:\n\ |
Jean-Paul Calderone | 525ef80 | 2008-03-09 20:39:42 -0400 | [diff] [blame] | 385 | \n\ |
| 386 | YYYYMMDDhhmmssZ\n\ |
| 387 | YYYYMMDDhhmmss+hhmm\n\ |
| 388 | YYYYMMDDhhmmss-hhmm\n\ |
| 389 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 390 | @return: None\n\ |
Jean-Paul Calderone | 525ef80 | 2008-03-09 20:39:42 -0400 | [diff] [blame] | 391 | "; |
| 392 | |
| 393 | static PyObject* |
| 394 | crypto_X509_set_notAfter(crypto_X509Obj *self, PyObject *args) |
| 395 | { |
| 396 | return _set_asn1_time( |
Jean-Paul Calderone | e890db3 | 2010-08-22 16:55:15 -0400 | [diff] [blame] | 397 | FMT(":set_notAfter"), X509_get_notAfter(self->x509), args); |
Jean-Paul Calderone | 525ef80 | 2008-03-09 20:39:42 -0400 | [diff] [blame] | 398 | } |
| 399 | |
Jean-Paul Calderone | e890db3 | 2010-08-22 16:55:15 -0400 | [diff] [blame] | 400 | #undef FMT |
| 401 | |
Rick Dean | 536ba02 | 2009-07-24 23:57:27 -0500 | [diff] [blame] | 402 | PyObject* |
| 403 | _get_asn1_time(char *format, ASN1_TIME* timestamp, PyObject *args) |
Jean-Paul Calderone | 525ef80 | 2008-03-09 20:39:42 -0400 | [diff] [blame] | 404 | { |
| 405 | ASN1_GENERALIZEDTIME *gt_timestamp = NULL; |
| 406 | PyObject *py_timestamp = NULL; |
| 407 | |
| 408 | if (!PyArg_ParseTuple(args, format)) { |
| 409 | return NULL; |
| 410 | } |
| 411 | |
| 412 | /* |
| 413 | * http://www.columbia.edu/~ariel/ssleay/asn1-time.html |
| 414 | */ |
| 415 | /* |
| 416 | * There must be a way to do this without touching timestamp->data |
| 417 | * directly. -exarkun |
| 418 | */ |
Jean-Paul Calderone | e0615b5 | 2008-03-09 21:44:46 -0400 | [diff] [blame] | 419 | if (timestamp->length == 0) { |
| 420 | Py_INCREF(Py_None); |
| 421 | return Py_None; |
| 422 | } else if (timestamp->type == V_ASN1_GENERALIZEDTIME) { |
Jean-Paul Calderone | ae479ad | 2010-08-10 20:04:28 -0400 | [diff] [blame] | 423 | return PyBytes_FromString((char *)timestamp->data); |
Jean-Paul Calderone | 525ef80 | 2008-03-09 20:39:42 -0400 | [diff] [blame] | 424 | } else { |
| 425 | ASN1_TIME_to_generalizedtime(timestamp, >_timestamp); |
Jean-Paul Calderone | 38a646d | 2008-03-25 15:16:18 -0400 | [diff] [blame] | 426 | if (gt_timestamp == NULL) { |
Rick Dean | d369c93 | 2009-07-08 11:48:33 -0500 | [diff] [blame] | 427 | exception_from_error_queue(crypto_Error); |
Jean-Paul Calderone | 525ef80 | 2008-03-09 20:39:42 -0400 | [diff] [blame] | 428 | return NULL; |
| 429 | } else { |
Jean-Paul Calderone | ae479ad | 2010-08-10 20:04:28 -0400 | [diff] [blame] | 430 | py_timestamp = PyBytes_FromString((char *)gt_timestamp->data); |
Jean-Paul Calderone | 525ef80 | 2008-03-09 20:39:42 -0400 | [diff] [blame] | 431 | ASN1_GENERALIZEDTIME_free(gt_timestamp); |
| 432 | return py_timestamp; |
| 433 | } |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | static char crypto_X509_get_notBefore_doc[] = "\n\ |
| 438 | Retrieve the time stamp for when the certificate starts being valid\n\ |
| 439 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 440 | @return: A string giving the timestamp, in the format:\n\ |
Jean-Paul Calderone | 525ef80 | 2008-03-09 20:39:42 -0400 | [diff] [blame] | 441 | \n\ |
| 442 | YYYYMMDDhhmmssZ\n\ |
| 443 | YYYYMMDDhhmmss+hhmm\n\ |
| 444 | YYYYMMDDhhmmss-hhmm\n\ |
| 445 | or None if there is no value set.\n\ |
| 446 | "; |
| 447 | |
| 448 | static PyObject* |
| 449 | crypto_X509_get_notBefore(crypto_X509Obj *self, PyObject *args) |
| 450 | { |
| 451 | /* |
| 452 | * X509_get_notBefore returns a borrowed reference. |
| 453 | */ |
| 454 | return _get_asn1_time( |
Rick Dean | 536ba02 | 2009-07-24 23:57:27 -0500 | [diff] [blame] | 455 | ":get_notBefore", X509_get_notBefore(self->x509), args); |
Jean-Paul Calderone | 525ef80 | 2008-03-09 20:39:42 -0400 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | |
| 459 | static char crypto_X509_get_notAfter_doc[] = "\n\ |
| 460 | Retrieve the time stamp for when the certificate stops being valid\n\ |
| 461 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 462 | @return: A string giving the timestamp, in the format:\n\ |
Jean-Paul Calderone | 525ef80 | 2008-03-09 20:39:42 -0400 | [diff] [blame] | 463 | \n\ |
| 464 | YYYYMMDDhhmmssZ\n\ |
| 465 | YYYYMMDDhhmmss+hhmm\n\ |
| 466 | YYYYMMDDhhmmss-hhmm\n\ |
| 467 | or None if there is no value set.\n\ |
| 468 | "; |
| 469 | |
| 470 | static PyObject* |
| 471 | crypto_X509_get_notAfter(crypto_X509Obj *self, PyObject *args) |
| 472 | { |
| 473 | /* |
| 474 | * X509_get_notAfter returns a borrowed reference. |
| 475 | */ |
| 476 | return _get_asn1_time( |
Rick Dean | 536ba02 | 2009-07-24 23:57:27 -0500 | [diff] [blame] | 477 | ":get_notAfter", X509_get_notAfter(self->x509), args); |
Jean-Paul Calderone | 525ef80 | 2008-03-09 20:39:42 -0400 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 481 | static char crypto_X509_gmtime_adj_notBefore_doc[] = "\n\ |
Jean-Paul Calderone | 525ef80 | 2008-03-09 20:39:42 -0400 | [diff] [blame] | 482 | Change the timestamp for when the certificate starts being valid to the current\n\ |
| 483 | time plus an offset.\n \ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 484 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 485 | @param amount: The number of seconds by which to adjust the starting validity\n\ |
| 486 | time.\n\ |
| 487 | @return: None\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 488 | "; |
| 489 | |
| 490 | static PyObject * |
| 491 | crypto_X509_gmtime_adj_notBefore(crypto_X509Obj *self, PyObject *args) |
| 492 | { |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 493 | long amount; |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 494 | |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 495 | if (!PyArg_ParseTuple(args, "l:gmtime_adj_notBefore", &amount)) |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 496 | return NULL; |
| 497 | |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 498 | X509_gmtime_adj(X509_get_notBefore(self->x509), amount); |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 499 | |
| 500 | Py_INCREF(Py_None); |
| 501 | return Py_None; |
| 502 | } |
| 503 | |
| 504 | static char crypto_X509_gmtime_adj_notAfter_doc[] = "\n\ |
| 505 | Adjust the time stamp for when the certificate stops being valid\n\ |
| 506 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 507 | @param amount: The number of seconds by which to adjust the ending validity\n\ |
| 508 | time.\n\ |
| 509 | @return: None\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 510 | "; |
| 511 | |
| 512 | static PyObject * |
| 513 | crypto_X509_gmtime_adj_notAfter(crypto_X509Obj *self, PyObject *args) |
| 514 | { |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 515 | long amount; |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 516 | |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 517 | if (!PyArg_ParseTuple(args, "l:gmtime_adj_notAfter", &amount)) |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 518 | return NULL; |
| 519 | |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 520 | X509_gmtime_adj(X509_get_notAfter(self->x509), amount); |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 521 | |
| 522 | Py_INCREF(Py_None); |
| 523 | return Py_None; |
| 524 | } |
| 525 | |
| 526 | static char crypto_X509_sign_doc[] = "\n\ |
| 527 | Sign the certificate using the supplied key and digest\n\ |
| 528 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 529 | @param pkey: The key to sign with\n\ |
| 530 | @param digest: The message digest to use\n\ |
| 531 | @return: None\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 532 | "; |
| 533 | |
| 534 | static PyObject * |
| 535 | crypto_X509_sign(crypto_X509Obj *self, PyObject *args) |
| 536 | { |
| 537 | crypto_PKeyObj *pkey; |
| 538 | char *digest_name; |
| 539 | const EVP_MD *digest; |
| 540 | |
| 541 | if (!PyArg_ParseTuple(args, "O!s:sign", &crypto_PKey_Type, &pkey, |
| 542 | &digest_name)) |
| 543 | return NULL; |
| 544 | |
Jean-Paul Calderone | ac0d95f | 2008-03-10 00:00:42 -0400 | [diff] [blame] | 545 | if (pkey->only_public) { |
| 546 | PyErr_SetString(PyExc_ValueError, "Key has only public part"); |
| 547 | return NULL; |
| 548 | } |
| 549 | |
| 550 | if (!pkey->initialized) { |
| 551 | PyErr_SetString(PyExc_ValueError, "Key is uninitialized"); |
| 552 | return NULL; |
| 553 | } |
| 554 | |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 555 | if ((digest = EVP_get_digestbyname(digest_name)) == NULL) |
| 556 | { |
| 557 | PyErr_SetString(PyExc_ValueError, "No such digest method"); |
| 558 | return NULL; |
| 559 | } |
| 560 | |
| 561 | if (!X509_sign(self->x509, pkey->pkey, digest)) |
| 562 | { |
Rick Dean | d369c93 | 2009-07-08 11:48:33 -0500 | [diff] [blame] | 563 | exception_from_error_queue(crypto_Error); |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 564 | return NULL; |
| 565 | } |
| 566 | |
| 567 | Py_INCREF(Py_None); |
| 568 | return Py_None; |
| 569 | } |
| 570 | |
| 571 | static char crypto_X509_has_expired_doc[] = "\n\ |
| 572 | Check whether the certificate has expired.\n\ |
| 573 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 574 | @return: True if the certificate has expired, false otherwise\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 575 | "; |
| 576 | |
| 577 | static PyObject * |
| 578 | crypto_X509_has_expired(crypto_X509Obj *self, PyObject *args) |
| 579 | { |
| 580 | time_t tnow; |
| 581 | |
| 582 | if (!PyArg_ParseTuple(args, ":has_expired")) |
| 583 | return NULL; |
| 584 | |
| 585 | tnow = time(NULL); |
| 586 | if (ASN1_UTCTIME_cmp_time_t(X509_get_notAfter(self->x509), tnow) < 0) |
Jean-Paul Calderone | ae479ad | 2010-08-10 20:04:28 -0400 | [diff] [blame] | 587 | return PyLong_FromLong(1L); |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 588 | else |
Jean-Paul Calderone | ae479ad | 2010-08-10 20:04:28 -0400 | [diff] [blame] | 589 | return PyLong_FromLong(0L); |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 590 | } |
| 591 | |
| 592 | static char crypto_X509_subject_name_hash_doc[] = "\n\ |
| 593 | Return the hash of the X509 subject.\n\ |
| 594 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 595 | @return: The hash of the subject\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 596 | "; |
| 597 | |
| 598 | static PyObject * |
| 599 | crypto_X509_subject_name_hash(crypto_X509Obj *self, PyObject *args) |
| 600 | { |
| 601 | if (!PyArg_ParseTuple(args, ":subject_name_hash")) |
| 602 | return NULL; |
| 603 | |
| 604 | return PyLong_FromLong(X509_subject_name_hash(self->x509)); |
| 605 | } |
| 606 | |
| 607 | static char crypto_X509_digest_doc[] = "\n\ |
| 608 | Return the digest of the X509 object.\n\ |
| 609 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 610 | @return: The digest of the object\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 611 | "; |
| 612 | |
| 613 | static PyObject * |
| 614 | crypto_X509_digest(crypto_X509Obj *self, PyObject *args) |
| 615 | { |
| 616 | unsigned char fp[EVP_MAX_MD_SIZE]; |
| 617 | char *tmp; |
| 618 | char *digest_name; |
Jean-Paul Calderone | 28ebb30 | 2008-12-29 16:25:30 -0500 | [diff] [blame] | 619 | unsigned int len,i; |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 620 | PyObject *ret; |
| 621 | const EVP_MD *digest; |
| 622 | |
| 623 | if (!PyArg_ParseTuple(args, "s:digest", &digest_name)) |
| 624 | return NULL; |
| 625 | |
| 626 | if ((digest = EVP_get_digestbyname(digest_name)) == NULL) |
| 627 | { |
| 628 | PyErr_SetString(PyExc_ValueError, "No such digest method"); |
| 629 | return NULL; |
| 630 | } |
| 631 | |
| 632 | if (!X509_digest(self->x509,digest,fp,&len)) |
| 633 | { |
Rick Dean | d369c93 | 2009-07-08 11:48:33 -0500 | [diff] [blame] | 634 | exception_from_error_queue(crypto_Error); |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 635 | } |
| 636 | tmp = malloc(3*len+1); |
| 637 | memset(tmp, 0, 3*len+1); |
| 638 | for (i = 0; i < len; i++) { |
| 639 | sprintf(tmp+i*3,"%02X:",fp[i]); |
| 640 | } |
| 641 | tmp[3*len-1] = 0; |
Jean-Paul Calderone | ae479ad | 2010-08-10 20:04:28 -0400 | [diff] [blame] | 642 | ret = PyBytes_FromStringAndSize(tmp,3*len-1); |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 643 | free(tmp); |
| 644 | return ret; |
| 645 | } |
| 646 | |
| 647 | |
| 648 | static char crypto_X509_add_extensions_doc[] = "\n\ |
| 649 | Add extensions to the certificate.\n\ |
| 650 | \n\ |
Jean-Paul Calderone | 54bcc83 | 2009-05-27 14:06:48 -0400 | [diff] [blame] | 651 | @param extensions: a sequence of X509Extension objects\n\ |
| 652 | @return: None\n\ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 653 | "; |
| 654 | |
| 655 | static PyObject * |
| 656 | crypto_X509_add_extensions(crypto_X509Obj *self, PyObject *args) |
| 657 | { |
| 658 | PyObject *extensions, *seq; |
| 659 | crypto_X509ExtensionObj *ext; |
| 660 | int nr_of_extensions, i; |
| 661 | |
| 662 | if (!PyArg_ParseTuple(args, "O:add_extensions", &extensions)) |
| 663 | return NULL; |
| 664 | |
| 665 | seq = PySequence_Fast(extensions, "Expected a sequence"); |
| 666 | if (seq == NULL) |
| 667 | return NULL; |
| 668 | |
| 669 | nr_of_extensions = PySequence_Fast_GET_SIZE(seq); |
| 670 | |
| 671 | for (i = 0; i < nr_of_extensions; i++) |
| 672 | { |
| 673 | ext = (crypto_X509ExtensionObj *)PySequence_Fast_GET_ITEM(seq, i); |
| 674 | if (!crypto_X509Extension_Check(ext)) |
| 675 | { |
| 676 | Py_DECREF(seq); |
| 677 | PyErr_SetString(PyExc_ValueError, |
| 678 | "One of the elements is not an X509Extension"); |
| 679 | return NULL; |
| 680 | } |
| 681 | if (!X509_add_ext(self->x509, ext->x509_extension, -1)) |
| 682 | { |
| 683 | Py_DECREF(seq); |
Rick Dean | d369c93 | 2009-07-08 11:48:33 -0500 | [diff] [blame] | 684 | exception_from_error_queue(crypto_Error); |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 685 | return NULL; |
| 686 | } |
| 687 | } |
| 688 | |
| 689 | Py_INCREF(Py_None); |
| 690 | return Py_None; |
| 691 | } |
| 692 | |
| 693 | /* |
| 694 | * ADD_METHOD(name) expands to a correct PyMethodDef declaration |
| 695 | * { 'name', (PyCFunction)crypto_X509_name, METH_VARARGS } |
| 696 | * for convenience |
| 697 | */ |
| 698 | #define ADD_METHOD(name) \ |
| 699 | { #name, (PyCFunction)crypto_X509_##name, METH_VARARGS, crypto_X509_##name##_doc } |
| 700 | static PyMethodDef crypto_X509_methods[] = |
| 701 | { |
| 702 | ADD_METHOD(get_version), |
| 703 | ADD_METHOD(set_version), |
| 704 | ADD_METHOD(get_serial_number), |
| 705 | ADD_METHOD(set_serial_number), |
| 706 | ADD_METHOD(get_issuer), |
| 707 | ADD_METHOD(set_issuer), |
| 708 | ADD_METHOD(get_subject), |
| 709 | ADD_METHOD(set_subject), |
| 710 | ADD_METHOD(get_pubkey), |
| 711 | ADD_METHOD(set_pubkey), |
Jean-Paul Calderone | 525ef80 | 2008-03-09 20:39:42 -0400 | [diff] [blame] | 712 | ADD_METHOD(get_notBefore), |
| 713 | ADD_METHOD(set_notBefore), |
| 714 | ADD_METHOD(get_notAfter), |
| 715 | ADD_METHOD(set_notAfter), |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 716 | ADD_METHOD(gmtime_adj_notBefore), |
| 717 | ADD_METHOD(gmtime_adj_notAfter), |
| 718 | ADD_METHOD(sign), |
| 719 | ADD_METHOD(has_expired), |
| 720 | ADD_METHOD(subject_name_hash), |
| 721 | ADD_METHOD(digest), |
| 722 | ADD_METHOD(add_extensions), |
| 723 | { NULL, NULL } |
| 724 | }; |
| 725 | #undef ADD_METHOD |
| 726 | |
| 727 | |
| 728 | /* |
| 729 | * Constructor for X509 objects, never called by Python code directly |
| 730 | * |
| 731 | * Arguments: cert - A "real" X509 certificate object |
| 732 | * dealloc - Boolean value to specify whether the destructor should |
| 733 | * free the "real" X509 object |
| 734 | * Returns: The newly created X509 object |
| 735 | */ |
| 736 | crypto_X509Obj * |
| 737 | crypto_X509_New(X509 *cert, int dealloc) |
| 738 | { |
| 739 | crypto_X509Obj *self; |
| 740 | |
| 741 | self = PyObject_New(crypto_X509Obj, &crypto_X509_Type); |
| 742 | |
| 743 | if (self == NULL) |
| 744 | return NULL; |
| 745 | |
| 746 | self->x509 = cert; |
| 747 | self->dealloc = dealloc; |
| 748 | |
| 749 | return self; |
| 750 | } |
| 751 | |
Jean-Paul Calderone | a4230d9 | 2009-06-27 10:54:04 -0400 | [diff] [blame] | 752 | |
| 753 | static char crypto_X509_doc[] = "\n\ |
| 754 | X509() -> X509 instance\n\ |
| 755 | \n\ |
| 756 | Create a new X509 object.\n\ |
| 757 | \n\ |
| 758 | @returns: The X509 object\n\ |
| 759 | "; |
| 760 | |
| 761 | static PyObject * |
| 762 | crypto_X509_new(PyTypeObject *subtype, PyObject *args, PyObject *kwargs) |
| 763 | { |
| 764 | if (!PyArg_ParseTuple(args, ":X509")) { |
| 765 | return NULL; |
| 766 | } |
| 767 | |
| 768 | return (PyObject *)crypto_X509_New(X509_new(), 1); |
| 769 | } |
| 770 | |
| 771 | |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 772 | /* |
| 773 | * Deallocate the memory used by the X509 object |
| 774 | * |
| 775 | * Arguments: self - The X509 object |
| 776 | * Returns: None |
| 777 | */ |
| 778 | static void |
| 779 | crypto_X509_dealloc(crypto_X509Obj *self) |
| 780 | { |
| 781 | /* Sometimes we don't have to dealloc the "real" X509 pointer ourselves */ |
| 782 | if (self->dealloc) |
| 783 | X509_free(self->x509); |
| 784 | |
| 785 | PyObject_Del(self); |
| 786 | } |
| 787 | |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 788 | PyTypeObject crypto_X509_Type = { |
Jean-Paul Calderone | 3fe7f67 | 2010-08-11 23:55:10 -0400 | [diff] [blame] | 789 | PyOpenSSL_HEAD_INIT(&PyType_Type, 0) |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 790 | "X509", |
| 791 | sizeof(crypto_X509Obj), |
| 792 | 0, |
| 793 | (destructor)crypto_X509_dealloc, |
| 794 | NULL, /* print */ |
Jean-Paul Calderone | ae479ad | 2010-08-10 20:04:28 -0400 | [diff] [blame] | 795 | NULL, /* getattr */ |
Jean-Paul Calderone | a4230d9 | 2009-06-27 10:54:04 -0400 | [diff] [blame] | 796 | NULL, /* setattr */ |
| 797 | NULL, /* compare */ |
| 798 | NULL, /* repr */ |
| 799 | NULL, /* as_number */ |
| 800 | NULL, /* as_sequence */ |
| 801 | NULL, /* as_mapping */ |
| 802 | NULL, /* hash */ |
| 803 | NULL, /* call */ |
| 804 | NULL, /* str */ |
| 805 | NULL, /* getattro */ |
| 806 | NULL, /* setattro */ |
| 807 | NULL, /* as_buffer */ |
| 808 | Py_TPFLAGS_DEFAULT, |
| 809 | crypto_X509_doc, /* doc */ |
| 810 | NULL, /* traverse */ |
| 811 | NULL, /* clear */ |
| 812 | NULL, /* tp_richcompare */ |
| 813 | 0, /* tp_weaklistoffset */ |
| 814 | NULL, /* tp_iter */ |
| 815 | NULL, /* tp_iternext */ |
| 816 | crypto_X509_methods, /* tp_methods */ |
| 817 | NULL, /* tp_members */ |
| 818 | NULL, /* tp_getset */ |
| 819 | NULL, /* tp_base */ |
| 820 | NULL, /* tp_dict */ |
| 821 | NULL, /* tp_descr_get */ |
| 822 | NULL, /* tp_descr_set */ |
| 823 | 0, /* tp_dictoffset */ |
| 824 | NULL, /* tp_init */ |
| 825 | NULL, /* tp_alloc */ |
| 826 | crypto_X509_new, /* tp_new */ |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 827 | }; |
| 828 | |
| 829 | /* |
| 830 | * Initialize the X509 part of the crypto sub module |
| 831 | * |
Jean-Paul Calderone | a4230d9 | 2009-06-27 10:54:04 -0400 | [diff] [blame] | 832 | * Arguments: module - The crypto module |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 833 | * Returns: None |
| 834 | */ |
| 835 | int |
Jean-Paul Calderone | a4230d9 | 2009-06-27 10:54:04 -0400 | [diff] [blame] | 836 | init_crypto_x509(PyObject *module) |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 837 | { |
Jean-Paul Calderone | a4230d9 | 2009-06-27 10:54:04 -0400 | [diff] [blame] | 838 | if (PyType_Ready(&crypto_X509_Type) < 0) { |
| 839 | return 0; |
| 840 | } |
| 841 | |
| 842 | if (PyModule_AddObject(module, "X509", (PyObject *)&crypto_X509_Type) != 0) { |
| 843 | return 0; |
| 844 | } |
| 845 | |
| 846 | if (PyModule_AddObject(module, "X509Type", (PyObject *)&crypto_X509_Type) != 0) { |
| 847 | return 0; |
| 848 | } |
| 849 | |
Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 850 | return 1; |
| 851 | } |
| 852 | |