blob: a9750edfa794fdc7b06392db60b2915261c37940 [file] [log] [blame]
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001/*
2 * x509store.c
3 *
Jean-Paul Calderone8671c852011-03-02 19:26:20 -05004 * Copyright (C) AB Strakt
5 * See LICENSE for details.
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05006 *
7 * X.509 Store handling, mostly thin wrapping.
8 * See the file RATIONALE for a short explanation of why this module was written.
9 */
10#include <Python.h>
11#define crypto_MODULE
12#include "crypto.h"
13
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050014static char crypto_X509Store_add_cert_doc[] = "\n\
15Add a certificate\n\
16\n\
Jonathan Ballet78b92a22011-07-16 08:07:26 +090017:param cert: The certificate to add\n\
18:return: None\n\
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050019";
20
21static PyObject *
22crypto_X509Store_add_cert(crypto_X509StoreObj *self, PyObject *args)
23{
24 crypto_X509Obj *cert;
25
26 if (!PyArg_ParseTuple(args, "O!:add_cert", &crypto_X509_Type, &cert))
27 return NULL;
28
29 if (!X509_STORE_add_cert(self->x509_store, cert->x509))
30 {
Rick Deand369c932009-07-08 11:48:33 -050031 exception_from_error_queue(crypto_Error);
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050032 return NULL;
33 }
34
35 Py_INCREF(Py_None);
36 return Py_None;
37}
38
39
40/*
41 * ADD_METHOD(name) expands to a correct PyMethodDef declaration
42 * { 'name', (PyCFunction)crypto_X509Store_name, METH_VARARGS }
43 * for convenience
44 */
45#define ADD_METHOD(name) \
46 { #name, (PyCFunction)crypto_X509Store_##name, METH_VARARGS, crypto_X509Store_##name##_doc }
47static PyMethodDef crypto_X509Store_methods[] =
48{
49 ADD_METHOD(add_cert),
50 { NULL, NULL }
51};
52#undef ADD_METHOD
53
54
55/*
56 * Constructor for X509Store, never called by Python code directly
57 *
58 * Arguments: name - A "real" X509_STORE object
59 * dealloc - Boolean value to specify whether the destructor should
60 * free the "real" X509_STORE object
61 * Returns: The newly created X509Store object
62 */
63crypto_X509StoreObj *
64crypto_X509Store_New(X509_STORE *store, int dealloc)
65{
66 crypto_X509StoreObj *self;
67
68 self = PyObject_New(crypto_X509StoreObj, &crypto_X509Store_Type);
69
70 if (self == NULL)
71 return NULL;
72
73 self->x509_store = store;
74 self->dealloc = dealloc;
75
76 return self;
77}
78
79/*
80 * Deallocate the memory used by the X509Store object
81 *
82 * Arguments: self - The X509Store object
83 * Returns: None
84 */
85static void
86crypto_X509Store_dealloc(crypto_X509StoreObj *self)
87{
88 /* Sometimes we don't have to dealloc this */
89 if (self->dealloc)
90 X509_STORE_free(self->x509_store);
91
92 PyObject_Del(self);
93}
94
95
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050096PyTypeObject crypto_X509Store_Type = {
Jean-Paul Calderone3fe7f672010-08-11 23:55:10 -040097 PyOpenSSL_HEAD_INIT(&PyType_Type, 0)
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050098 "X509Store",
99 sizeof(crypto_X509StoreObj),
100 0,
101 (destructor)crypto_X509Store_dealloc,
102 NULL, /* print */
Jean-Paul Calderone2f6c66f2010-08-11 19:53:43 -0400103 NULL, /* getattr */
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500104 NULL, /* setattr */
105 NULL, /* compare */
106 NULL, /* repr */
107 NULL, /* as_number */
108 NULL, /* as_sequence */
109 NULL, /* as_mapping */
Jean-Paul Calderone2f6c66f2010-08-11 19:53:43 -0400110 NULL, /* hash */
111 NULL, /* call */
112 NULL, /* str */
113 NULL, /* getattro */
114 NULL, /* setattro */
115 NULL, /* as_buffer */
116 Py_TPFLAGS_DEFAULT,
117 NULL, /* doc */
118 NULL, /* traverse */
119 NULL, /* clear */
120 NULL, /* tp_richcompare */
121 0, /* tp_weaklistoffset */
122 NULL, /* tp_iter */
123 NULL, /* tp_iternext */
124 crypto_X509Store_methods, /* tp_methods */
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500125};
126
127
128/*
129 * Initialize the X509Store part of the crypto module
130 *
Jean-Paul Calderone0cdb7bd2009-07-04 10:21:07 -0400131 * Arguments: module - The crypto module
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500132 * Returns: None
133 */
134int
Jean-Paul Calderone0cdb7bd2009-07-04 10:21:07 -0400135init_crypto_x509store(PyObject *module)
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500136{
Jean-Paul Calderone0cdb7bd2009-07-04 10:21:07 -0400137 if (PyType_Ready(&crypto_X509Store_Type) < 0) {
138 return 0;
139 }
140
Jean-Paul Calderoneaed23582011-03-12 22:45:02 -0500141 /* PyModule_AddObject steals a reference.
142 */
143 Py_INCREF((PyObject *)&crypto_X509Store_Type);
Jean-Paul Calderone0cdb7bd2009-07-04 10:21:07 -0400144 if (PyModule_AddObject(module, "X509StoreType", (PyObject *)&crypto_X509Store_Type) != 0) {
145 return 0;
146 }
147
Jean-Paul Calderone897bc252008-02-18 20:50:23 -0500148 return 1;
149}