blob: 442c2d5915dbf92585d7bfc7aa2968298ccf6b72 [file] [log] [blame]
Jean-Paul Calderone897bc252008-02-18 20:50:23 -05001/*
2 * x509store.c
3 *
4 * Copyright (C) AB Strakt 2001, All rights reserved
5 *
6 * X.509 Store handling, mostly thin wrapping.
7 * See the file RATIONALE for a short explanation of why this module was written.
8 */
9#include <Python.h>
10#define crypto_MODULE
11#include "crypto.h"
12
Jean-Paul Calderone897bc252008-02-18 20:50:23 -050013static char crypto_X509Store_add_cert_doc[] = "\n\
14Add a certificate\n\
15\n\
16Arguments: self - The X509Store object\n\
17 args - The Python argument tuple, should be:\n\
18 cert - The certificate to add\n\
19Returns: None\n\
20";
21
22static PyObject *
23crypto_X509Store_add_cert(crypto_X509StoreObj *self, PyObject *args)
24{
25 crypto_X509Obj *cert;
26
27 if (!PyArg_ParseTuple(args, "O!:add_cert", &crypto_X509_Type, &cert))
28 return NULL;
29
30 if (!X509_STORE_add_cert(self->x509_store, cert->x509))
31 {
32 exception_from_error_queue();
33 return NULL;
34 }
35
36 Py_INCREF(Py_None);
37 return Py_None;
38}
39
40
41/*
42 * ADD_METHOD(name) expands to a correct PyMethodDef declaration
43 * { 'name', (PyCFunction)crypto_X509Store_name, METH_VARARGS }
44 * for convenience
45 */
46#define ADD_METHOD(name) \
47 { #name, (PyCFunction)crypto_X509Store_##name, METH_VARARGS, crypto_X509Store_##name##_doc }
48static PyMethodDef crypto_X509Store_methods[] =
49{
50 ADD_METHOD(add_cert),
51 { NULL, NULL }
52};
53#undef ADD_METHOD
54
55
56/*
57 * Constructor for X509Store, never called by Python code directly
58 *
59 * Arguments: name - A "real" X509_STORE object
60 * dealloc - Boolean value to specify whether the destructor should
61 * free the "real" X509_STORE object
62 * Returns: The newly created X509Store object
63 */
64crypto_X509StoreObj *
65crypto_X509Store_New(X509_STORE *store, int dealloc)
66{
67 crypto_X509StoreObj *self;
68
69 self = PyObject_New(crypto_X509StoreObj, &crypto_X509Store_Type);
70
71 if (self == NULL)
72 return NULL;
73
74 self->x509_store = store;
75 self->dealloc = dealloc;
76
77 return self;
78}
79
80/*
81 * Deallocate the memory used by the X509Store object
82 *
83 * Arguments: self - The X509Store object
84 * Returns: None
85 */
86static void
87crypto_X509Store_dealloc(crypto_X509StoreObj *self)
88{
89 /* Sometimes we don't have to dealloc this */
90 if (self->dealloc)
91 X509_STORE_free(self->x509_store);
92
93 PyObject_Del(self);
94}
95
96
97/*
98 * Find attribute.
99 *
100 * Arguments: self - The X509Store object
101 * name - The attribute name
102 * Returns: A Python object for the attribute, or NULL if something went
103 * wrong
104 */
105static PyObject *
106crypto_X509Store_getattr(crypto_X509StoreObj *self, char *name)
107{
108 return Py_FindMethod(crypto_X509Store_methods, (PyObject *)self, name);
109}
110
111PyTypeObject crypto_X509Store_Type = {
112 PyObject_HEAD_INIT(NULL)
113 0,
114 "X509Store",
115 sizeof(crypto_X509StoreObj),
116 0,
117 (destructor)crypto_X509Store_dealloc,
118 NULL, /* print */
119 (getattrfunc)crypto_X509Store_getattr,
120 NULL, /* setattr */
121 NULL, /* compare */
122 NULL, /* repr */
123 NULL, /* as_number */
124 NULL, /* as_sequence */
125 NULL, /* as_mapping */
126 NULL /* hash */
127};
128
129
130/*
131 * Initialize the X509Store part of the crypto module
132 *
133 * Arguments: dict - The crypto module dictionary
134 * Returns: None
135 */
136int
137init_crypto_x509store(PyObject *dict)
138{
139 crypto_X509Store_Type.ob_type = &PyType_Type;
140 Py_INCREF(&crypto_X509Store_Type);
141 PyDict_SetItemString(dict, "X509StoreType", (PyObject *)&crypto_X509Store_Type);
142 return 1;
143}