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