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